Spaces:
Running
Running
Add application file
Browse files- Dockerfile +15 -0
- app.py +94 -0
- det.onnx +3 -0
- rec.onnx +3 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10.16
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
|
14 |
+
ENV NO_ALBUMENTATIONS_UPDATE=1
|
15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import onnxruntime
|
3 |
+
from insightface.model_zoo import SCRFD
|
4 |
+
from insightface.model_zoo import ArcFaceONNX
|
5 |
+
from insightface.app.common import Face
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
from fastapi import FastAPI, UploadFile
|
9 |
+
|
10 |
+
detection_model_file = os.path.abspath(os.path.join(os.getcwd(), "det.onnx"))
|
11 |
+
recognition_model_file = os.path.abspath(os.path.join(os.getcwd(), "rec.onnx"))
|
12 |
+
|
13 |
+
detection = SCRFD(model_file=detection_model_file)
|
14 |
+
detection.prepare(ctx_id=-1, input_size=(640, 640))
|
15 |
+
|
16 |
+
recognition = ArcFaceONNX(model_file=recognition_model_file)
|
17 |
+
recognition.prepare(ctx_id=-1)
|
18 |
+
|
19 |
+
def read_image_from_spooled_temporary_file(temp_file):
|
20 |
+
temp_file.seek(0)
|
21 |
+
return cv2.imdecode(np.frombuffer(temp_file.read(), np.uint8), cv2.IMREAD_COLOR)
|
22 |
+
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
@app.post("/v1/detection")
|
26 |
+
async def v1_detection(photo: UploadFile):
|
27 |
+
if not detection:
|
28 |
+
return {"faces": []}
|
29 |
+
|
30 |
+
image = read_image_from_spooled_temporary_file(photo.file)
|
31 |
+
bboxes, kpss = detection.detect(image)
|
32 |
+
if bboxes.shape[0] == 0:
|
33 |
+
return {"faces": []}
|
34 |
+
|
35 |
+
faces = []
|
36 |
+
for i in range(bboxes.shape[0]):
|
37 |
+
bbox = bboxes[i, :4]
|
38 |
+
det_score = bboxes[i, 4]
|
39 |
+
kps = None
|
40 |
+
if kpss is not None:
|
41 |
+
kps = kpss[i]
|
42 |
+
faces.append({
|
43 |
+
"detectionScore": det_score.item(),
|
44 |
+
"boundingBox": {
|
45 |
+
"topLeft": {
|
46 |
+
"x": bbox[0].item(),
|
47 |
+
"y": bbox[1].item()
|
48 |
+
},
|
49 |
+
"bottomRight": {
|
50 |
+
"x": bbox[2].item(),
|
51 |
+
"y": bbox[3].item()
|
52 |
+
}
|
53 |
+
},
|
54 |
+
"keyPoints": [{"x": pt[0].item(), "y": pt[1].item()} for pt in kps]
|
55 |
+
})
|
56 |
+
|
57 |
+
return {"faces": faces}
|
58 |
+
|
59 |
+
@app.post("/v1/recognition")
|
60 |
+
async def v1_recognition(photo: UploadFile):
|
61 |
+
if not detection or not recognition:
|
62 |
+
return {"faces": []}
|
63 |
+
|
64 |
+
image = read_image_from_spooled_temporary_file(photo.file)
|
65 |
+
bboxes, kpss = detection.detect(image)
|
66 |
+
if bboxes.shape[0] == 0:
|
67 |
+
return {"faces": []}
|
68 |
+
|
69 |
+
faces = []
|
70 |
+
for i in range(bboxes.shape[0]):
|
71 |
+
bbox = bboxes[i, :4]
|
72 |
+
det_score = bboxes[i, 4]
|
73 |
+
kps = None
|
74 |
+
if kpss is not None:
|
75 |
+
kps = kpss[i]
|
76 |
+
face = Face(bbox=bbox, kps=kps, det_score=det_score)
|
77 |
+
recognition.get(image, face)
|
78 |
+
faces.append({
|
79 |
+
"boundingBox": {
|
80 |
+
"topLeft": {
|
81 |
+
"x": face.bbox[0].item(),
|
82 |
+
"y": face.bbox[1].item()
|
83 |
+
},
|
84 |
+
"bottomRight": {
|
85 |
+
"x": face.bbox[2].item(),
|
86 |
+
"y": face.bbox[3].item()
|
87 |
+
}
|
88 |
+
},
|
89 |
+
"detectionScore": face.det_score.item(),
|
90 |
+
"keyPoints": [{"x": pt[0].item(), "y": pt[1].item()} for pt in face.kps],
|
91 |
+
"embedding": [v.item() for v in face.embedding.ravel()]
|
92 |
+
})
|
93 |
+
|
94 |
+
return {"faces": faces}
|
det.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5838f7fe053675b1c7a08b633df49e7af5495cee0493c7dcf6697200b85b5b91
|
3 |
+
size 16923827
|
rec.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4c06341c33c2ca1f86781dab0e829f88ad5b64be9fba56e56bc9ebdefc619e43
|
3 |
+
size 174383860
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
onnxruntime >= 1.17.0
|
3 |
+
insightface >=0.7.3
|
4 |
+
opencv-python-headless >=4.7.0.72
|
5 |
+
fastapi[standard]
|