Spaces:
Sleeping
Sleeping
thanhson28
commited on
Commit
•
e7fd1a1
1
Parent(s):
1f49d78
Update app.py
Browse files
app.py
CHANGED
@@ -48,21 +48,26 @@ def detect_objects(img, thr):
|
|
48 |
# logger.info(f"Number of objs: {len(objs)}")
|
49 |
|
50 |
# draw img
|
51 |
-
img = PersonDetectorAttrib.visualize(img, objs)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# return img
|
54 |
-
return img
|
55 |
|
56 |
|
57 |
def detect_poses(img, thr):
|
58 |
# infer img
|
59 |
objs = person_detector.infer(img, threshold=thr)
|
60 |
-
objs = [obj for obj in objs if obj['confidence'] >
|
|
|
61 |
for i, obj in enumerate(objs):
|
62 |
bbox = obj['points']
|
63 |
# crop img
|
64 |
img_crop = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
|
65 |
-
objs_pose = pose_detector.infer(img_crop, 0)
|
66 |
# each_point in objs_pose['points'], add bbox[0] and bbox[1] to each_point
|
67 |
for each_point in objs_pose:
|
68 |
each_point['point'][0] += bbox[0]
|
@@ -71,37 +76,48 @@ def detect_poses(img, thr):
|
|
71 |
objs_pose = [each_point['point']
|
72 |
for each_point in objs_pose]
|
73 |
display_frame_pose(img, [bbox], [objs_pose])
|
74 |
-
#
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# return img
|
82 |
-
return img
|
83 |
|
84 |
|
85 |
def detect_ages_gender(img, thr):
|
86 |
# infer img
|
87 |
objs = person_detector.infer(img, threshold=thr)
|
88 |
-
objs = [obj for obj in objs if obj['confidence'] >
|
89 |
|
90 |
width = img.shape[1]
|
91 |
font_size = width/1284
|
92 |
thickness = int((width/1284)*4)
|
93 |
|
|
|
94 |
for i, obj in enumerate(objs):
|
95 |
bbox = obj['points']
|
96 |
# crop img
|
97 |
img_crop = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
# draw img
|
102 |
|
103 |
# show bbox and age_gender to image
|
104 |
-
ages_genders =
|
|
|
105 |
|
106 |
# draw bbox = [x1,y1,x2,y2]
|
107 |
(text_width, text_height) = cv2.getTextSize(
|
@@ -121,9 +137,10 @@ def detect_ages_gender(img, thr):
|
|
121 |
# putText ages_genders to bbox
|
122 |
cv2.putText(img, ages_genders, (bbox[0], bbox[1]-2),
|
123 |
cv2.FONT_HERSHEY_SIMPLEX, font_size, (0, 0, 255), thickness, cv2.LINE_AA)
|
|
|
124 |
|
125 |
# return img
|
126 |
-
return img
|
127 |
|
128 |
|
129 |
# ------------------------------------------------------------------------------
|
@@ -141,6 +158,7 @@ if __name__ == "__main__":
|
|
141 |
outputs=[
|
142 |
gr.Image(type="numpy", label="Processed Image",
|
143 |
width=800, height=480),
|
|
|
144 |
],
|
145 |
title="Model Testing",
|
146 |
description="Drag an image onto the box",
|
@@ -158,6 +176,7 @@ if __name__ == "__main__":
|
|
158 |
outputs=[
|
159 |
gr.Image(type="numpy", label="Processed Image",
|
160 |
width=800, height=480),
|
|
|
161 |
],
|
162 |
title="Model Testing",
|
163 |
description="Drag an image onto the box",
|
@@ -176,6 +195,7 @@ if __name__ == "__main__":
|
|
176 |
outputs=[
|
177 |
gr.Image(type="numpy", label="Processed Image",
|
178 |
width=800, height=480),
|
|
|
179 |
],
|
180 |
title="Model Testing",
|
181 |
description="Drag an image onto the box",
|
|
|
48 |
# logger.info(f"Number of objs: {len(objs)}")
|
49 |
|
50 |
# draw img
|
51 |
+
img, class_names = PersonDetectorAttrib.visualize(img, objs)
|
52 |
+
output_text = ""
|
53 |
+
for i, obj in enumerate(objs):
|
54 |
+
bbox = obj['points']
|
55 |
+
output_text += class_names[i] + "\n"
|
56 |
|
57 |
# return img
|
58 |
+
return img, output_text
|
59 |
|
60 |
|
61 |
def detect_poses(img, thr):
|
62 |
# infer img
|
63 |
objs = person_detector.infer(img, threshold=thr)
|
64 |
+
objs = [obj for obj in objs if obj['confidence'] > 0.5]
|
65 |
+
output_text = ""
|
66 |
for i, obj in enumerate(objs):
|
67 |
bbox = obj['points']
|
68 |
# crop img
|
69 |
img_crop = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
|
70 |
+
objs_pose = pose_detector.infer(img_crop, threshold=0.0)
|
71 |
# each_point in objs_pose['points'], add bbox[0] and bbox[1] to each_point
|
72 |
for each_point in objs_pose:
|
73 |
each_point['point'][0] += bbox[0]
|
|
|
76 |
objs_pose = [each_point['point']
|
77 |
for each_point in objs_pose]
|
78 |
display_frame_pose(img, [bbox], [objs_pose])
|
79 |
+
# draw bbox
|
80 |
+
img = cv2.rectangle(
|
81 |
+
img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2)
|
82 |
+
# put bbox ID and pose to output_text
|
83 |
+
output_text += f"ID-{i}: " + "\t".join(
|
84 |
+
[f"point-{i}-[x-{int(point[0])},y-{int(point[1])}]" for i, point in enumerate(objs_pose)]) + "\n"
|
85 |
+
# get text size of output_text
|
86 |
+
text_size = cv2.getTextSize(
|
87 |
+
f"ID-{i}", cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
88 |
+
# put rectangle background to output_text at top left bbox
|
89 |
+
img = cv2.rectangle(img, (bbox[0], bbox[1]-text_size[0][1]-10),
|
90 |
+
(bbox[0]+text_size[0][0], bbox[1]), (0, 0, 0), cv2.FILLED)
|
91 |
+
# putText output_text to image
|
92 |
+
img = cv2.putText(img, f"ID-{i}", (bbox[0], bbox[1]-10),
|
93 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
|
94 |
|
95 |
# return img
|
96 |
+
return img, output_text
|
97 |
|
98 |
|
99 |
def detect_ages_gender(img, thr):
|
100 |
# infer img
|
101 |
objs = person_detector.infer(img, threshold=thr)
|
102 |
+
objs = [obj for obj in objs if obj['confidence'] > 0.5]
|
103 |
|
104 |
width = img.shape[1]
|
105 |
font_size = width/1284
|
106 |
thickness = int((width/1284)*4)
|
107 |
|
108 |
+
output_text = ""
|
109 |
for i, obj in enumerate(objs):
|
110 |
bbox = obj['points']
|
111 |
# crop img
|
112 |
img_crop = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
|
113 |
+
|
114 |
+
objs_ag = ages_genders_detector.infer(
|
115 |
+
[img_crop])
|
116 |
# draw img
|
117 |
|
118 |
# show bbox and age_gender to image
|
119 |
+
ages_genders = f"ID-{i} :" + \
|
120 |
+
objs_ag['ages'][0] + "_"+objs_ag['genders'][0]
|
121 |
|
122 |
# draw bbox = [x1,y1,x2,y2]
|
123 |
(text_width, text_height) = cv2.getTextSize(
|
|
|
137 |
# putText ages_genders to bbox
|
138 |
cv2.putText(img, ages_genders, (bbox[0], bbox[1]-2),
|
139 |
cv2.FONT_HERSHEY_SIMPLEX, font_size, (0, 0, 255), thickness, cv2.LINE_AA)
|
140 |
+
output_text += ages_genders
|
141 |
|
142 |
# return img
|
143 |
+
return img, output_text
|
144 |
|
145 |
|
146 |
# ------------------------------------------------------------------------------
|
|
|
158 |
outputs=[
|
159 |
gr.Image(type="numpy", label="Processed Image",
|
160 |
width=800, height=480),
|
161 |
+
gr.Textbox(label="Person Detection info:", default="")
|
162 |
],
|
163 |
title="Model Testing",
|
164 |
description="Drag an image onto the box",
|
|
|
176 |
outputs=[
|
177 |
gr.Image(type="numpy", label="Processed Image",
|
178 |
width=800, height=480),
|
179 |
+
gr.Textbox(label="Human pose detection info:", default=""),
|
180 |
],
|
181 |
title="Model Testing",
|
182 |
description="Drag an image onto the box",
|
|
|
195 |
outputs=[
|
196 |
gr.Image(type="numpy", label="Processed Image",
|
197 |
width=800, height=480),
|
198 |
+
gr.Textbox(label="Age Gender detection info:", default=""),
|
199 |
],
|
200 |
title="Model Testing",
|
201 |
description="Drag an image onto the box",
|