-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgradio_test.py
169 lines (150 loc) · 5.02 KB
/
gradio_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import base64
import pathlib
from PIL import Image
from io import BytesIO
import gradio as gr
from fastapi import APIRouter, HTTPException, status, Request, File, UploadFile
from fastapi.staticfiles import StaticFiles
from faceserve.services.v1 import FaceServiceV1
from faceserve.models import HeadFace, GhostFaceNet
from faceserve.db.qdrant import QdrantFaceDatabase
from faceserve.schema.face_request import FaceRequest
"""
Load models and thresh.
"""
# Model
DETECTION = HeadFace(os.getenv("DETECTION_MODEL_PATH", default="weights/yolov7-hf-v1.onnx"))
RECOGNITION = GhostFaceNet(os.getenv("RECOGNITION_MODEL_PATH", default="weights/ghostnetv1.onnx"))
# Threshold
DETECTION_THRESH = os.getenv("DETECTION_THRESH", default=0.5)
RECOGNITION_THRESH = os.getenv("RECOGNITION_THRESH", default=0.37)
# Face db storage.
FACES = QdrantFaceDatabase(
collection_name="faces_collection",
)
FACES_IMG_DIR = pathlib.Path(os.getenv("IMG_DIR", default="face_images"))
FACES_IMG_DIR.mkdir(exist_ok=True)
"""
Initialize Services
"""
service = FaceServiceV1(
detection=DETECTION,
detection_thresh=DETECTION_THRESH,
recognition=RECOGNITION,
recognition_thresh=RECOGNITION_THRESH,
facedb=FACES,
)
def register_upload(files: list[str], id: str, group_id: str):
print("="*50)
print("Register files: ", files)
if not files: return []
images = [Image.open(x) for x in files]
hash_imgs = service.register_face(images=images, id=id, group_id=group_id, face_folder=FACES_IMG_DIR)
return [Image.open(
f"{FACES_IMG_DIR}/{group_id}/{id}/{x}.jpg"
) for x in hash_imgs]
def get_face_image(id: str, group_id: str):
if id == "": # gradio get "" instead of NoneType
id = None
if group_id == "":
group_id = None
retrieve_faces = FACES.list_faces(person_id=id, group_id=group_id)[0]
if not retrieve_faces:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="No image found, database empty"
)
res = [x for x in retrieve_faces if x is not None]
output = []
for x in res:
res_group = x.payload["group_id"]
res_person = x.payload["person_id"]
res_hash = "".join(x.id.split("-"))
output.append(
f"{FACES_IMG_DIR}/{res_group}/{res_person}/{res_hash}.jpg"
)
return [Image.open(x) for x in output]
def delete_face(face_id: str|None = None, id: str|None = None, group_id: str|None = None):
return FACES.delete_face(
face_id=face_id,
person_id=id,
group_id=group_id
)
def check_faces(files: list[str], id: str|None = None, group_id: str = 'default'):
images = [Image.open(x) for x in files]
results_dict = service.check_faces(
images=images,
thresh=RECOGNITION_THRESH,
group_id=group_id
)
print("="*50)
print("status_check: ", results_dict)
result = results_dict.get('check_per_person') or results_dict.get('check_group')
output, file_crop_paths = [], []
if not result:
print("="*50)
print("No faces accepted!")
return []
for x in result:
if not x: continue
res_group = x["group_id"]
res_person = x["person_id"]
res_hash = "".join(x['image_id'].split("-"))
file_crop_paths.append(x['file_crop'])
output.append(
f"{FACES_IMG_DIR}/{res_group}/{res_person}/{res_hash}.jpg"
)
print("="*50)
print("Faces check: ", output)
# image && crop
image_out = [Image.open(x) for x in output]
image_crop = [Image.open(x) for x in file_crop_paths]
merge = []
for i in range(len(image_out)):
merge.append(image_out[i])
merge.append(image_crop[i])
return merge
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
files = gr.File(label="Register face",file_types=['image'], file_count="multiple")
with gr.Row():
group_id = gr.Textbox(value="default")
person_id = gr.Textbox(value="0")
run = gr.Button()
outputs = gr.Gallery(type="filepath")
with gr.Row():
with gr.Column():
check_files = gr.File(label="Check faces", file_count="multiple", file_types=['image'])
check = gr.Button()
checked_faces = gr.Gallery(type='filepath')
with gr.Row():
with gr.Column():
with gr.Row():
group_id_2 = gr.Textbox(label="Group ID")
person_id_2 = gr.Textbox(label="Person ID")
list_button = gr.Button("List faces")
list_faces = gr.Gallery(type="filepath")
# register
# gallery
# faces registers success
event = run.click(
register_upload,
[files, person_id, group_id],
outputs,
)
# checks
# gallary
# faces check succes + id
event_2 = check.click(
check_faces,
check_files,
checked_faces
)
event_3 = list_button.click(
get_face_image,
[person_id_2, group_id_2],
list_faces
)
if __name__ == "__main__":
demo.launch()