Spaces:
Runtime error
Runtime error
oussamamatar
commited on
Commit
•
dd5ddba
1
Parent(s):
22eed27
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import mediapipe as mp
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Bounding Box
|
7 |
+
def box_yolo(image, only_people):
|
8 |
+
def get_output_layers(net):
|
9 |
+
layer_names = net.getLayerNames()
|
10 |
+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
11 |
+
return output_layers
|
12 |
+
|
13 |
+
def draw_prediction(img, class_id, confidence, x, y, x_plus_w, y_plus_h):
|
14 |
+
label = str(classes[class_id])
|
15 |
+
color = COLORS[class_id]
|
16 |
+
cv2.rectangle(img, (x,y), (x_plus_w,y_plus_h), color, 2)
|
17 |
+
cv2.putText(img, label, (x-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
18 |
+
|
19 |
+
Width = image.shape[1]
|
20 |
+
Height = image.shape[0]
|
21 |
+
scale = 0.00392
|
22 |
+
classes = None
|
23 |
+
|
24 |
+
with open('yolov3.txt', 'r') as f:
|
25 |
+
classes = [line.strip() for line in f.readlines()]
|
26 |
+
|
27 |
+
COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
|
28 |
+
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
|
29 |
+
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
|
30 |
+
net.setInput(blob)
|
31 |
+
outs = net.forward(get_output_layers(net))
|
32 |
+
class_ids = []
|
33 |
+
confidences = []
|
34 |
+
boxes = []
|
35 |
+
conf_threshold = 0.5
|
36 |
+
nms_threshold = 0.4
|
37 |
+
|
38 |
+
for out in outs:
|
39 |
+
for detection in out:
|
40 |
+
scores = detection[5:]
|
41 |
+
class_id = np.argmax(scores)
|
42 |
+
confidence = scores[class_id]
|
43 |
+
if confidence > 0.5:
|
44 |
+
center_x = int(detection[0] * Width)
|
45 |
+
center_y = int(detection[1] * Height)
|
46 |
+
w = int(detection[2] * Width)
|
47 |
+
h = int(detection[3] * Height)
|
48 |
+
x = center_x - w / 2
|
49 |
+
y = center_y - h / 2
|
50 |
+
class_ids.append(class_id)
|
51 |
+
confidences.append(float(confidence))
|
52 |
+
boxes.append([x, y, w, h])
|
53 |
+
|
54 |
+
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
|
55 |
+
if only_people:
|
56 |
+
for i in indices:
|
57 |
+
if class_ids[i] == 0:
|
58 |
+
box = boxes[i]
|
59 |
+
x = box[0]
|
60 |
+
y = box[1]
|
61 |
+
w = box[2]
|
62 |
+
h = box[3]
|
63 |
+
draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
|
64 |
+
else:
|
65 |
+
for i in indices:
|
66 |
+
box = boxes[i]
|
67 |
+
x = box[0]
|
68 |
+
y = box[1]
|
69 |
+
w = box[2]
|
70 |
+
h = box[3]
|
71 |
+
draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
|
72 |
+
|
73 |
+
return image
|
74 |
+
|
75 |
+
# Pose Estimation
|
76 |
+
def pose_mediapipe(image, segmentation):
|
77 |
+
mp_drawing = mp.solutions.drawing_utils
|
78 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
79 |
+
mp_pose = mp.solutions.pose
|
80 |
+
BG_COLOR = (192, 192, 192) # gray
|
81 |
+
with mp_pose.Pose(
|
82 |
+
static_image_mode=True,
|
83 |
+
model_complexity=2,
|
84 |
+
enable_segmentation=segmentation,
|
85 |
+
min_detection_confidence=0.5) as pose:
|
86 |
+
|
87 |
+
image_height, image_width, _ = image.shape
|
88 |
+
# Convert the BGR image to RGB before processing.
|
89 |
+
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
90 |
+
annotated_image = image.copy()
|
91 |
+
# Draw segmentation on the image.
|
92 |
+
if segmentation:
|
93 |
+
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1
|
94 |
+
bg_image = np.zeros(image.shape, dtype=np.uint8)
|
95 |
+
bg_image[:] = BG_COLOR
|
96 |
+
annotated_image = np.where(condition, annotated_image, bg_image)
|
97 |
+
# Draw pose landmarks on the image.
|
98 |
+
mp_drawing.draw_landmarks(
|
99 |
+
annotated_image,
|
100 |
+
results.pose_landmarks,
|
101 |
+
mp_pose.POSE_CONNECTIONS,
|
102 |
+
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
|
103 |
+
return annotated_image
|
104 |
+
|
105 |
+
# Both
|
106 |
+
def both(image_, only_people):
|
107 |
+
def get_output_layers(net):
|
108 |
+
layer_names = net.getLayerNames()
|
109 |
+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
110 |
+
return output_layers
|
111 |
+
|
112 |
+
def draw_prediction(img, class_id, confidence, x, y, x_plus_w, y_plus_h):
|
113 |
+
label = str(classes[class_id])
|
114 |
+
color = COLORS[class_id]
|
115 |
+
cv2.rectangle(img, (x,y), (x_plus_w,y_plus_h), color, 2)
|
116 |
+
cv2.putText(img, label, (x-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
117 |
+
|
118 |
+
image = image_.copy()
|
119 |
+
|
120 |
+
Width = image.shape[1]
|
121 |
+
Height = image.shape[0]
|
122 |
+
scale = 0.00392
|
123 |
+
classes = None
|
124 |
+
|
125 |
+
with open('yolov3.txt', 'r') as f:
|
126 |
+
classes = [line.strip() for line in f.readlines()]
|
127 |
+
|
128 |
+
COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
|
129 |
+
|
130 |
+
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
|
131 |
+
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
|
132 |
+
net.setInput(blob)
|
133 |
+
outs = net.forward(get_output_layers(net))
|
134 |
+
class_ids = []
|
135 |
+
confidences = []
|
136 |
+
boxes = []
|
137 |
+
conf_threshold = 0.5
|
138 |
+
nms_threshold = 0.4
|
139 |
+
|
140 |
+
for out in outs:
|
141 |
+
for detection in out:
|
142 |
+
scores = detection[5:]
|
143 |
+
class_id = np.argmax(scores)
|
144 |
+
confidence = scores[class_id]
|
145 |
+
if confidence > 0.5:
|
146 |
+
center_x = int(detection[0] * Width)
|
147 |
+
center_y = int(detection[1] * Height)
|
148 |
+
w = int(detection[2] * Width)
|
149 |
+
h = int(detection[3] * Height)
|
150 |
+
x = center_x - w / 2
|
151 |
+
y = center_y - h / 2
|
152 |
+
class_ids.append(class_id)
|
153 |
+
confidences.append(float(confidence))
|
154 |
+
boxes.append([x, y, w, h])
|
155 |
+
|
156 |
+
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
|
157 |
+
|
158 |
+
if only_people:
|
159 |
+
for i in indices:
|
160 |
+
if class_ids[i] == 0:
|
161 |
+
box = boxes[i]
|
162 |
+
x = box[0]
|
163 |
+
y = box[1]
|
164 |
+
w = box[2]
|
165 |
+
h = box[3]
|
166 |
+
draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
|
167 |
+
else:
|
168 |
+
for i in indices:
|
169 |
+
box = boxes[i]
|
170 |
+
x = box[0]
|
171 |
+
y = box[1]
|
172 |
+
w = box[2]
|
173 |
+
h = box[3]
|
174 |
+
draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
|
175 |
+
|
176 |
+
mp_drawing = mp.solutions.drawing_utils
|
177 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
178 |
+
mp_pose = mp.solutions.pose
|
179 |
+
BG_COLOR = (192, 192, 192) # gray
|
180 |
+
with mp_pose.Pose(
|
181 |
+
static_image_mode=True,
|
182 |
+
model_complexity=2,
|
183 |
+
enable_segmentation=False,
|
184 |
+
min_detection_confidence=0.5) as pose:
|
185 |
+
|
186 |
+
image_height, image_width, _ = image_.shape
|
187 |
+
# Convert the BGR image to RGB before processing.
|
188 |
+
results = pose.process(cv2.cvtColor(image_, cv2.COLOR_BGR2RGB))
|
189 |
+
annotated_image = image.copy()
|
190 |
+
|
191 |
+
# Draw pose landmarks on the image.
|
192 |
+
mp_drawing.draw_landmarks(
|
193 |
+
annotated_image,
|
194 |
+
results.pose_landmarks,
|
195 |
+
mp_pose.POSE_CONNECTIONS,
|
196 |
+
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
|
197 |
+
|
198 |
+
return annotated_image
|
199 |
+
|
200 |
+
def model_picker(image, model, segmentation, only_people):
|
201 |
+
if model == 0:
|
202 |
+
result = box_yolo(image, only_people)
|
203 |
+
elif model == 1:
|
204 |
+
result = pose_mediapipe(image, segmentation)
|
205 |
+
elif model == 2:
|
206 |
+
result = both(image, only_people)
|
207 |
+
return result
|
208 |
+
|
209 |
+
image_in = gr.inputs.Image(label='Input Image')
|
210 |
+
radio_in = gr.Radio(['Bounding Box', 'Pose Estimation', 'Both'], type='index', label='Model Type')
|
211 |
+
checkbox_1 = gr.inputs.Checkbox(label='Enable Segmentation (For Pose Estimation)')
|
212 |
+
checkbox_2 = gr.inputs.Checkbox(label='Bound Only People in a Box')
|
213 |
+
iface = gr.Interface(fn=model_picker, inputs=[image_in, radio_in, checkbox_1, checkbox_2], outputs='image')
|
214 |
+
|
215 |
+
# app.launch(share=True)
|
216 |
+
iface.launch()
|