-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
340 lines (283 loc) · 11.4 KB
/
api.py
File metadata and controls
340 lines (283 loc) · 11.4 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from fastapi import FastAPI, UploadFile, File, Query
from fastapi.responses import JSONResponse, Response
import cv2
import numpy as np
import os
from datetime import datetime
from deepface import DeepFace
from retinaface import RetinaFace
import io
from typing import Optional
app = FastAPI()
# 创建输出目录
OUTPUT_DIR = "./output"
# 服务器端口
SERVER_PORT = 8008
os.makedirs(OUTPUT_DIR, exist_ok=True)
# 定义颜色和边框设置
OUTER_COLOR = (255, 255, 255) # 外层边框使用白色
INNER_COLOR = (0, 165, 255) # 内层边框使用橙色
OUTER_THICKNESS = 6 # 外层边框粗细
INNER_THICKNESS = 3 # 内层边框粗细
def draw_detections_with_info(img, faces):
"""在图片上绘制检测结果和额外信息"""
img_result = img.copy()
for face in faces:
# 获取人脸区域
facial_area = face['facial_area']
x = facial_area['x']
y = facial_area['y']
w = facial_area['w']
h = facial_area['h']
# 画双层边框
cv2.rectangle(img_result,
(x, y),
(x + w, y + h),
OUTER_COLOR,
OUTER_THICKNESS)
cv2.rectangle(img_result,
(x, y),
(x + w, y + h),
INNER_COLOR,
INNER_THICKNESS)
# 准备显示的信息
info_lines = []
if face.get('score'):
info_lines.append(f"Conf: {face['score']:.2f}")
if face.get('age'):
info_lines.append(f"Age: {face['age']}")
if face.get('gender'):
info_lines.append(f"Gender: {face['gender']}")
if face.get('dominant_emotion'):
info_lines.append(f"Emotion: {face['dominant_emotion']}")
# 文本显示设置
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.7
text_thickness = 2
line_height = 25
padding = 5
# 计算所有文本的总高度
total_height = len(info_lines) * line_height
# 确定文本起始位置(避免超出图像边界)
start_y = max(total_height + padding, y)
# 绘制信息
for i, line in enumerate(info_lines):
# 获取文本大小
(text_width, text_height), _ = cv2.getTextSize(
line, font, font_scale, text_thickness)
# 计算文本位置
text_y = start_y - (i * line_height)
# 确保文本框不会超出图像边界
text_x = min(x, img_result.shape[1] - text_width - padding)
# 绘制文本背景(双层效果)
cv2.rectangle(img_result,
(text_x - padding, text_y - text_height - padding),
(text_x + text_width + padding, text_y + padding),
OUTER_COLOR,
-1)
cv2.rectangle(img_result,
(text_x - padding + 2, text_y -
text_height - padding + 2),
(text_x + text_width + padding - 2, text_y + padding - 2),
INNER_COLOR,
-1)
# 绘制文本
cv2.putText(img_result,
line,
(text_x, text_y),
font,
font_scale,
(255, 255, 255),
text_thickness)
return img_result
def convert_to_native_types(obj):
"""将 numpy 类型转换为 Python 原生类型"""
if isinstance(obj, dict):
return {key: convert_to_native_types(value) for key, value in obj.items()}
elif isinstance(obj, list):
return [convert_to_native_types(item) for item in obj]
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return obj
@app.post("/analyze")
async def detect(
file: UploadFile = File(...),
save_render: Optional[bool] = Query(
default=False,
description="Whether to save the rendered image with face detection markers"
)
):
try:
# 读取图片
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
return JSONResponse(
status_code=400,
content={"error": "Invalid image"}
)
# 使用 RetinaFace 进行人脸检测
faces = RetinaFace.detect_faces(img)
# 转换检测结果格式
face_list = []
if isinstance(faces, dict):
for face_key in faces:
face_data = faces[face_key]
facial_area = {
'x': int(face_data['facial_area'][0]),
'y': int(face_data['facial_area'][1]),
'w': int(face_data['facial_area'][2] - face_data['facial_area'][0]),
'h': int(face_data['facial_area'][3] - face_data['facial_area'][1])
}
face_img = img[
facial_area['y']:facial_area['y']+facial_area['h'],
facial_area['x']:facial_area['x']+facial_area['w']
]
try:
analysis = DeepFace.analyze(
face_img,
actions=['age', 'gender', 'race', 'emotion'],
enforce_detection=False
)
if isinstance(analysis, list):
analysis = analysis[0]
gender = "Woman" if analysis['gender']['Woman'] > 50 else "Man"
face_info = {
'position': facial_area,
'confidence': float(face_data['score']) if 'score' in face_data else None,
'age': int(analysis['age']),
'gender': gender,
'dominant_race': str(analysis['dominant_race']),
'dominant_emotion': str(analysis['dominant_emotion']),
'emotion': {k: float(v) for k, v in analysis['emotion'].items()},
'race': {k: float(v) for k, v in analysis['race'].items()}
}
except Exception as e:
print(f"Face analysis failed: {str(e)}")
face_info = {
'position': facial_area,
'confidence': float(face_data['score']) if 'score' in face_data else None
}
face_list.append(face_info)
output_path = None
if save_render:
# 准备绘制信息
face_info_list = []
for face in face_list:
face_info = {
'facial_area': face['position'],
'score': face['confidence'],
'age': face.get('age'),
'gender': face.get('gender'),
'dominant_emotion': face.get('dominant_emotion')
}
face_info_list.append(face_info)
# 绘制标记
img_result = draw_detections_with_info(img, face_info_list)
# 保存带标记的图片
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"result_{timestamp}.jpg"
output_path = os.path.join(OUTPUT_DIR, output_filename)
cv2.imwrite(output_path, img_result)
# 准备响应数据
response_data = {
"status": "success",
"faces_detected": len(face_list),
"faces": convert_to_native_types(face_list),
}
# 只在保存了渲染图片时添加文件路径
if output_path:
response_data["output_file"] = output_path
return response_data
except Exception as e:
print(f"Error occurred: {str(e)}")
return JSONResponse(
status_code=500,
content={
"status": "error",
"message": str(e)
}
)
@app.post("/detect_and_return")
async def detect_and_return(file: UploadFile = File(...), info_display: Optional[bool] = Query(default=False)):
try:
# 读取图片
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
return JSONResponse(
status_code=400,
content={"error": "Invalid image"}
)
# 使用 RetinaFace 进行人脸检测
faces = RetinaFace.detect_faces(img)
# 转换检测结果格式
face_list = []
if isinstance(faces, dict): # 检测到人脸
for face_key in faces:
face_data = faces[face_key]
facial_area = {
'x': int(face_data['facial_area'][0]),
'y': int(face_data['facial_area'][1]),
'w': int(face_data['facial_area'][2] - face_data['facial_area'][0]),
'h': int(face_data['facial_area'][3] - face_data['facial_area'][1])
}
face_info = {
'facial_area': facial_area,
'score': float(face_data['score']) if 'score' in face_data else None
}
# 如果需要显示详细信息,则进行 DeepFace 分析
if info_display:
try:
face_img = img[
facial_area['y']:facial_area['y']+facial_area['h'],
facial_area['x']:facial_area['x']+facial_area['w']
]
analysis = DeepFace.analyze(
face_img,
actions=['age', 'gender', 'emotion'],
enforce_detection=False
)
if isinstance(analysis, list):
analysis = analysis[0]
gender = "Woman" if analysis['gender']['Woman'] > 50 else "Man"
face_info.update({
'age': int(analysis['age']),
'gender': gender,
'dominant_emotion': str(analysis['dominant_emotion'])
})
except Exception as e:
print(f"Face analysis failed: {str(e)}")
face_list.append(face_info)
# 在图片上绘制检测结果
img_result = draw_detections_with_info(img, face_list)
# 将图片编码为JPEG格式
_, img_encoded = cv2.imencode('.jpg', img_result)
# 返回处理后的图片
return Response(
content=img_encoded.tobytes(),
media_type="image/jpeg",
headers={
"X-Faces-Detected": str(len(face_list)),
"X-Detection-Status": "success" if face_list else "no_face"
}
)
except Exception as e:
print(f"Error occurred: {str(e)}")
return JSONResponse(
status_code=500,
content={
"status": "error",
"message": str(e)
}
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=SERVER_PORT)