Spaces:
Runtime error
Runtime error
Update
Browse files- .pre-commit-config.yaml +59 -34
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +22 -27
- pre-requirements.txt +1 -1
- requirements.txt +2 -2
.pre-commit-config.yaml
CHANGED
|
@@ -1,35 +1,60 @@
|
|
| 1 |
repos:
|
| 2 |
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
repos:
|
| 2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 3 |
+
rev: v4.6.0
|
| 4 |
+
hooks:
|
| 5 |
+
- id: check-executables-have-shebangs
|
| 6 |
+
- id: check-json
|
| 7 |
+
- id: check-merge-conflict
|
| 8 |
+
- id: check-shebang-scripts-are-executable
|
| 9 |
+
- id: check-toml
|
| 10 |
+
- id: check-yaml
|
| 11 |
+
- id: end-of-file-fixer
|
| 12 |
+
- id: mixed-line-ending
|
| 13 |
+
args: ["--fix=lf"]
|
| 14 |
+
- id: requirements-txt-fixer
|
| 15 |
+
- id: trailing-whitespace
|
| 16 |
+
- repo: https://github.com/myint/docformatter
|
| 17 |
+
rev: v1.7.5
|
| 18 |
+
hooks:
|
| 19 |
+
- id: docformatter
|
| 20 |
+
args: ["--in-place"]
|
| 21 |
+
- repo: https://github.com/pycqa/isort
|
| 22 |
+
rev: 5.13.2
|
| 23 |
+
hooks:
|
| 24 |
+
- id: isort
|
| 25 |
+
args: ["--profile", "black"]
|
| 26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
| 27 |
+
rev: v1.10.0
|
| 28 |
+
hooks:
|
| 29 |
+
- id: mypy
|
| 30 |
+
args: ["--ignore-missing-imports"]
|
| 31 |
+
additional_dependencies:
|
| 32 |
+
[
|
| 33 |
+
"types-python-slugify",
|
| 34 |
+
"types-requests",
|
| 35 |
+
"types-PyYAML",
|
| 36 |
+
"types-pytz",
|
| 37 |
+
]
|
| 38 |
+
- repo: https://github.com/psf/black
|
| 39 |
+
rev: 24.4.2
|
| 40 |
+
hooks:
|
| 41 |
+
- id: black
|
| 42 |
+
language_version: python3.10
|
| 43 |
+
args: ["--line-length", "119"]
|
| 44 |
+
- repo: https://github.com/kynan/nbstripout
|
| 45 |
+
rev: 0.7.1
|
| 46 |
+
hooks:
|
| 47 |
+
- id: nbstripout
|
| 48 |
+
args:
|
| 49 |
+
[
|
| 50 |
+
"--extra-keys",
|
| 51 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
| 52 |
+
]
|
| 53 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
| 54 |
+
rev: 1.8.5
|
| 55 |
+
hooks:
|
| 56 |
+
- id: nbqa-black
|
| 57 |
+
- id: nbqa-pyupgrade
|
| 58 |
+
args: ["--py37-plus"]
|
| 59 |
+
- id: nbqa-isort
|
| 60 |
+
args: ["--float-to-top"]
|
.style.yapf
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
[style]
|
| 2 |
-
based_on_style = pep8
|
| 3 |
-
blank_line_before_nested_class_or_def = false
|
| 4 |
-
spaces_before_comment = 2
|
| 5 |
-
split_before_logical_operator = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.vscode/settings.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"editor.formatOnSave": true,
|
| 3 |
+
"files.insertFinalNewline": false,
|
| 4 |
+
"[python]": {
|
| 5 |
+
"editor.defaultFormatter": "ms-python.black-formatter",
|
| 6 |
+
"editor.formatOnType": true,
|
| 7 |
+
"editor.codeActionsOnSave": {
|
| 8 |
+
"source.organizeImports": "explicit"
|
| 9 |
+
}
|
| 10 |
+
},
|
| 11 |
+
"[jupyter]": {
|
| 12 |
+
"files.insertFinalNewline": false
|
| 13 |
+
},
|
| 14 |
+
"black-formatter.args": [
|
| 15 |
+
"--line-length=119"
|
| 16 |
+
],
|
| 17 |
+
"isort.args": ["--profile", "black"],
|
| 18 |
+
"flake8.args": [
|
| 19 |
+
"--max-line-length=119"
|
| 20 |
+
],
|
| 21 |
+
"ruff.lint.args": [
|
| 22 |
+
"--line-length=119"
|
| 23 |
+
],
|
| 24 |
+
"notebook.output.scrolling": true,
|
| 25 |
+
"notebook.formatOnCellExecution": true,
|
| 26 |
+
"notebook.formatOnSave.enabled": true,
|
| 27 |
+
"notebook.codeActionsOnSave": {
|
| 28 |
+
"source.organizeImports": "explicit"
|
| 29 |
+
}
|
| 30 |
+
}
|
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 😻
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.33.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import functools
|
| 6 |
import pathlib
|
| 7 |
|
| 8 |
import cv2
|
|
@@ -12,22 +11,19 @@ import insightface
|
|
| 12 |
import numpy as np
|
| 13 |
import onnxruntime as ort
|
| 14 |
|
| 15 |
-
TITLE =
|
| 16 |
-
DESCRIPTION =
|
| 17 |
|
| 18 |
|
| 19 |
def load_model():
|
| 20 |
-
path = huggingface_hub.hf_hub_download(
|
| 21 |
-
'models/scrfd_person_2.5g.onnx')
|
| 22 |
options = ort.SessionOptions()
|
| 23 |
options.intra_op_num_threads = 8
|
| 24 |
options.inter_op_num_threads = 8
|
| 25 |
session = ort.InferenceSession(
|
| 26 |
-
path,
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path,
|
| 30 |
-
session=session)
|
| 31 |
return model
|
| 32 |
|
| 33 |
|
|
@@ -47,8 +43,7 @@ def detect_person(
|
|
| 47 |
return bboxes, vbboxes
|
| 48 |
|
| 49 |
|
| 50 |
-
def visualize(image: np.ndarray, bboxes: np.ndarray,
|
| 51 |
-
vbboxes: np.ndarray) -> np.ndarray:
|
| 52 |
res = image.copy()
|
| 53 |
for i in range(bboxes.shape[0]):
|
| 54 |
bbox = bboxes[i]
|
|
@@ -59,9 +54,7 @@ def visualize(image: np.ndarray, bboxes: np.ndarray,
|
|
| 59 |
alpha = 0.8
|
| 60 |
color = (255, 0, 0)
|
| 61 |
for c in range(3):
|
| 62 |
-
res[vy1:vy2, vx1:vx2,
|
| 63 |
-
c] = res[vy1:vy2, vx1:vx2,
|
| 64 |
-
c] * alpha + color[c] * (1.0 - alpha)
|
| 65 |
cv2.circle(res, (vx1, vy1), 1, color, 2)
|
| 66 |
cv2.circle(res, (vx1, vy2), 1, color, 2)
|
| 67 |
cv2.circle(res, (vx2, vy1), 1, color, 2)
|
|
@@ -69,26 +62,28 @@ def visualize(image: np.ndarray, bboxes: np.ndarray,
|
|
| 69 |
return res
|
| 70 |
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
image = image[:, :, ::-1] # RGB -> BGR
|
| 74 |
bboxes, vbboxes = detect_person(image, detector)
|
| 75 |
res = visualize(image, bboxes, vbboxes)
|
| 76 |
return res[:, :, ::-1] # BGR -> RGB
|
| 77 |
|
| 78 |
|
| 79 |
-
|
| 80 |
-
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
| 81 |
-
fn = functools.partial(detect, detector=detector)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
gr.
|
| 87 |
-
fn=fn,
|
| 88 |
-
inputs=gr.Image(label='Input', type='numpy'),
|
| 89 |
-
outputs=gr.Image(label='Output', height=600),
|
| 90 |
examples=examples,
|
| 91 |
examples_per_page=30,
|
| 92 |
title=TITLE,
|
| 93 |
description=DESCRIPTION,
|
| 94 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import pathlib
|
| 6 |
|
| 7 |
import cv2
|
|
|
|
| 11 |
import numpy as np
|
| 12 |
import onnxruntime as ort
|
| 13 |
|
| 14 |
+
TITLE = "insightface Person Detection"
|
| 15 |
+
DESCRIPTION = "https://github.com/deepinsight/insightface/tree/master/examples/person_detection"
|
| 16 |
|
| 17 |
|
| 18 |
def load_model():
|
| 19 |
+
path = huggingface_hub.hf_hub_download("public-data/insightface", "models/scrfd_person_2.5g.onnx")
|
|
|
|
| 20 |
options = ort.SessionOptions()
|
| 21 |
options.intra_op_num_threads = 8
|
| 22 |
options.inter_op_num_threads = 8
|
| 23 |
session = ort.InferenceSession(
|
| 24 |
+
path, sess_options=options, providers=["CPUExecutionProvider", "CUDAExecutionProvider"]
|
| 25 |
+
)
|
| 26 |
+
model = insightface.model_zoo.retinaface.RetinaFace(model_file=path, session=session)
|
|
|
|
|
|
|
| 27 |
return model
|
| 28 |
|
| 29 |
|
|
|
|
| 43 |
return bboxes, vbboxes
|
| 44 |
|
| 45 |
|
| 46 |
+
def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> np.ndarray:
|
|
|
|
| 47 |
res = image.copy()
|
| 48 |
for i in range(bboxes.shape[0]):
|
| 49 |
bbox = bboxes[i]
|
|
|
|
| 54 |
alpha = 0.8
|
| 55 |
color = (255, 0, 0)
|
| 56 |
for c in range(3):
|
| 57 |
+
res[vy1:vy2, vx1:vx2, c] = res[vy1:vy2, vx1:vx2, c] * alpha + color[c] * (1.0 - alpha)
|
|
|
|
|
|
|
| 58 |
cv2.circle(res, (vx1, vy1), 1, color, 2)
|
| 59 |
cv2.circle(res, (vx1, vy2), 1, color, 2)
|
| 60 |
cv2.circle(res, (vx2, vy1), 1, color, 2)
|
|
|
|
| 62 |
return res
|
| 63 |
|
| 64 |
|
| 65 |
+
detector = load_model()
|
| 66 |
+
detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def detect(image: np.ndarray) -> np.ndarray:
|
| 70 |
image = image[:, :, ::-1] # RGB -> BGR
|
| 71 |
bboxes, vbboxes = detect_person(image, detector)
|
| 72 |
res = visualize(image, bboxes, vbboxes)
|
| 73 |
return res[:, :, ::-1] # BGR -> RGB
|
| 74 |
|
| 75 |
|
| 76 |
+
examples = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
demo = gr.Interface(
|
| 79 |
+
fn=detect,
|
| 80 |
+
inputs=gr.Image(label="Input", type="numpy"),
|
| 81 |
+
outputs=gr.Image(label="Output"),
|
|
|
|
|
|
|
|
|
|
| 82 |
examples=examples,
|
| 83 |
examples_per_page=30,
|
| 84 |
title=TITLE,
|
| 85 |
description=DESCRIPTION,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.queue(max_size=10).launch()
|
pre-requirements.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
Cython==0.
|
|
|
|
| 1 |
+
Cython==3.0.10
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
insightface==0.7.3
|
| 2 |
-
onnxruntime==1.
|
| 3 |
-
opencv-python-headless==4.
|
|
|
|
| 1 |
insightface==0.7.3
|
| 2 |
+
onnxruntime==1.18.0
|
| 3 |
+
opencv-python-headless==4.10.0.82
|