Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed Jun 17, 2024
1 parent ce754fd commit 3323448
Show file tree
Hide file tree
Showing 24 changed files with 5,051 additions and 552 deletions.
82 changes: 82 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# can be used to exclude certain regex patterns or paths
exclude: '^$'
# if set to true, fails on first failure
fail_fast: false
repos:
# The following pre-commit hooks could be very useful.
# They are not part of CI/CD, so use or remove them as you please
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
- id: name-tests-test
args: [
"--pytest-test-first" # test_.*\.py
#"--pytest" # .*_test\.py
#"--unittest" # test.*\.py
]
- id: check-added-large-files
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-merge-conflict
- id: check-yaml
args: ['--unsafe']
- id: check-toml
- id: check-xml
- id: detect-private-key

- repo: https://gitlab.com/bmares/check-json5
rev: v1.0.0
hooks:
- id: check-json5

- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.13.0
hooks:
- id: markdownlint-cli2
args: [ "--fix", "--config", ".markdownlint.yaml" ]

# part of CI/CD
- repo: local
hooks:
- id: poetry-check
name: poetry-check --lock
description: run poetry check to validate config
entry: poetry check --lock
language: python
pass_filenames: false
files: ^(.*/)?pyproject\.toml$
# - id: poetry-export
# name: poetry-export
# description: run poetry export to sync lock file with requirements.txt
# entry: poetry export
# language: python
# pass_filenames: false
# files: ^(.*/)?poetry\.lock$
# args: ["-f", "requirements.txt", "-o", "requirements.txt"]
# - id: poetry-export-dev
# name: poetry-export --with-dev
# description: run poetry export to sync lock file with requirements.txt
# entry: poetry export
# language: python
# pass_filenames: false
# files: ^(.*/)?poetry\.lock$
# args: ["--with", "dev", "-f", "requirements.txt", "-o", "requirements-dev.txt"]

- id: ruff-format
name: ruff-format
description: "Run 'ruff format' for extremely fast Python formatting"
entry: ruff format --force-exclude
language: python
types_or: [python, pyi]
args: []
- id: ruff
name: ruff
description: Run 'ruff' for extremely fast Python linting
entry: ruff check --force-exclude
language: python
types_or: [python, pyi]
args: [--fix]
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Also included in this repo is an efficient pytorch implementation of MTCNN for f
```bash
# With pip:
pip install facenet-pytorch

# or clone this repo, removing the '-' to allow python imports:
git clone https://github.com/timesler/facenet-pytorch.git facenet_pytorch

# or use a docker container (see https://github.com/timesler/docker-jupyter-dl-gpu):
docker run -it --rm timesler/jupyter-dl-gpu pip install facenet-pytorch && ipython
```
Expand All @@ -50,10 +50,10 @@ Also included in this repo is an efficient pytorch implementation of MTCNN for f

```py
from facenet_pytorch import MTCNN, InceptionResnetV1
# If required, create a face detection pipeline using MTCNN:
mtcnn = MTCNN(image_size=<image_size>, margin=<margin>)
# Create an inception resnet (in eval mode):
resnet = InceptionResnetV1(pretrained='vggface2').eval()
```
Expand All @@ -62,7 +62,7 @@ Also included in this repo is an efficient pytorch implementation of MTCNN for f

```py
from PIL import Image
img = Image.open(<image path>)
# Get cropped and prewhitened image tensor
Expand Down Expand Up @@ -176,7 +176,7 @@ The package and any of the example notebooks can be run with docker (or nvidia-d
docker run --rm -p 8888:8888
-v ./facenet-pytorch:/home/jovyan timesler/jupyter-dl-gpu \
-v <path to data>:/home/jovyan/data
pip install facenet-pytorch && jupyter lab
pip install facenet-pytorch && jupyter lab
```
Navigate to the examples/ directory and run any of the ipython notebooks.
Expand Down
8 changes: 4 additions & 4 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Pytorch 模型权重使用从 David Sandberg 的 [tensorflow Facenet repo](https
## 快速启动

1. 安装:

````bash
# 使用pip安装:
pip install facenet-pytorch
Expand All @@ -85,7 +85,7 @@ docker run -it --rm timesler/jupyter-dl-gpu pip install facenet-pytorch && ipyth
````

2. 在python中,导入 facenet-pytorch 并实例化模型:

````python
from facenet_pytorch import MTCNN, InceptionResnetV1

Expand All @@ -97,7 +97,7 @@ resnet = InceptionResnetV1(pretrained='vggface2').eval()
````

3. 处理图像:

````python
from PIL import Image

Expand Down Expand Up @@ -214,7 +214,7 @@ MTCNN 可用于构建人脸跟踪系统(使用 `MTCNN.detect()` 方法)。
docker run --rm -p 8888:8888
-v ./facenet-pytorch:/home/jovyan timesler/jupyter-dl-gpu \
-v <path to data>:/home/jovyan/data
pip install facenet-pytorch && jupyter lab
pip install facenet-pytorch && jupyter lab
````

导航到 example/ 目录并运行任何 ipython 笔记本。
Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ coverage:
status:
project: off
patch: off
codecov:
codecov:
token: 1e4f3aaa-9c74-4888-9408-71cc7fcfb64c
163 changes: 103 additions & 60 deletions examples/face_tracking.ipynb

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions examples/face_tracking_cn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"source": [
"from facenet_pytorch import MTCNN\n",
"import torch\n",
"import numpy as np\n",
"import mmcv, cv2\n",
"import numpy as np\n",
"from PIL import Image, ImageDraw\n",
"from IPython import display"
]
Expand Down Expand Up @@ -158,16 +158,16 @@
"frames_tracked = []\n",
"for i, frame in enumerate(frames):\n",
" print('\\r当前帧: {}'.format(i + 1), end='')\n",
" \n",
"\n",
" # 检测人脸\n",
" boxes, _ = mtcnn.detect(frame)\n",
" \n",
"\n",
" # 绘制人脸框\n",
" frame_draw = frame.copy()\n",
" draw = ImageDraw.Draw(frame_draw)\n",
" for box in boxes:\n",
" for box in boxes[0]\n",
" draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)\n",
" \n",
"\n",
" # 添加到图像列表\n",
" frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))\n",
"print('\\n结束')"
Expand Down Expand Up @@ -232,10 +232,10 @@
"outputs": [],
"source": [
"dim = frames_tracked[0].size\n",
"fourcc = cv2.VideoWriter_fourcc(*'FMP4') \n",
"fourcc = cv2.VideoWriter_fourcc(*'FMP4')\n",
"video_tracked = cv2.VideoWriter('video_tracked.mp4', fourcc, 25.0, dim)\n",
"for frame in frames_tracked:\n",
" video_tracked.write(cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR))\n",
" video_tracked.write(np.asarray(frame)[:, :, ::-1])\n",
"video_tracked.release()"
]
}
Expand Down
10 changes: 3 additions & 7 deletions examples/infer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import os\n",
"\n",
"from facenet_pytorch import MTCNN, InceptionResnetV1\n",
"import torch\n",
"from torch.utils.data import DataLoader\n",
"from torchvision import datasets\n",
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"\n",
"workers = 0 if os.name == 'nt' else 4"
]
Expand Down Expand Up @@ -119,10 +118,7 @@
"def collate_fn(x):\n",
" return x[0]\n",
"\n",
"root_dir = Path(__file__).parent.parent\n",
"data_dir = root_dir / \"tests\" / \"data\" / \"test_images\"\n",
"\n",
"dataset = datasets.ImageFolder(data_dir)\n",
"dataset = datasets.ImageFolder('../data/test_images')\n",
"dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}\n",
"loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)"
]
Expand Down
10 changes: 3 additions & 7 deletions examples/infer_cn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import os\n",
"\n",
"from facenet_pytorch import MTCNN, InceptionResnetV1\n",
"import torch\n",
"from torch.utils.data import DataLoader\n",
"from torchvision import datasets\n",
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"\n",
"workers = 0 if os.name == 'nt' else 4"
]
Expand Down Expand Up @@ -145,10 +144,7 @@
"def collate_fn(x):\n",
" return x[0]\n",
"\n",
"root_dir = Path(__file__).parent.parent\n",
"data_dir = root_dir / \"tests\" / \"data\" / \"test_images\"\n",
"\n",
"dataset = datasets.ImageFolder(data_dir)\n",
"dataset = datasets.ImageFolder('../data/test_images')\n",
"dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}\n",
"loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)"
]
Expand Down
10 changes: 5 additions & 5 deletions examples/lfw_evaluate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"execution_count": 1,
"outputs": [],
"source": [
"from facenet_pytorch import MTCNN, InceptionResnetV1, fixed_image_standardization, training, extract_face\n",
"from facenet_pytorch import MTCNN, InceptionResnetV1, fixed_image_standardization, training\n",
"import torch\n",
"from torch.utils.data import DataLoader, SubsetRandomSampler, SequentialSampler\n",
"from torch.utils.data import DataLoader, SequentialSampler\n",
"from torchvision import datasets, transforms\n",
"import numpy as np\n",
"import os"
"import os\n",
"import math"
],
"metadata": {
"collapsed": false,
Expand Down Expand Up @@ -487,8 +488,7 @@
],
"source": [
"print(accuracy)\n",
"np.mean(accuracy)\n",
"\n"
"np.mean(accuracy)"
],
"metadata": {
"collapsed": false,
Expand Down
7 changes: 4 additions & 3 deletions examples/lfw_evaluate_cn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
},
"outputs": [],
"source": [
"from facenet_pytorch import MTCNN, InceptionResnetV1, fixed_image_standardization, training, extract_face\n",
"from facenet_pytorch import MTCNN, InceptionResnetV1, fixed_image_standardization, training\n",
"import torch\n",
"from torch.utils.data import DataLoader, SubsetRandomSampler, SequentialSampler\n",
"from torch.utils.data import DataLoader, SequentialSampler\n",
"from torchvision import datasets, transforms\n",
"import numpy as np\n",
"import os"
"import os\n",
"import math"
]
},
{
Expand Down
16 changes: 12 additions & 4 deletions facenet_pytorch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
from facenet_pytorch.models.utils import training
from facenet_pytorch.models.utils.detect_face import extract_face

__all__ = ["InceptionResnetV1", "MTCNN", "ONet", "PNet", "RNet", "fixed_image_standardization", "prewhiten", "training", "extract_face"]
__all__ = [
"InceptionResnetV1",
"MTCNN",
"ONet",
"PNet",
"RNet",
"fixed_image_standardization",
"prewhiten",
"training",
"extract_face",
]

warnings.filterwarnings(
action="ignore",
message="This overload of nonzero is deprecated:\n\tnonzero()",
category=UserWarning,
action="ignore", message="This overload of nonzero is deprecated:\n\tnonzero()", category=UserWarning
)
Empty file.
Empty file.
Loading

0 comments on commit 3323448

Please sign in to comment.