Update app.py
Browse files
app.py
CHANGED
@@ -68,57 +68,6 @@ def show_preds_image(image_path):
|
|
68 |
|
69 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
70 |
|
71 |
-
def show_preds_webcam(pil_image):
|
72 |
-
image= np.array(pil_image)
|
73 |
-
outputs = model.predict(image)
|
74 |
-
results = outputs[0].cpu().numpy()
|
75 |
-
|
76 |
-
yolo_classes = list(model.names.values())
|
77 |
-
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
|
78 |
-
colors = [random.choices(range(256), k=3) for _ in classes_ids]
|
79 |
-
|
80 |
-
for result in outputs:
|
81 |
-
for mask, box in zip(result.masks.xy, result.boxes):
|
82 |
-
|
83 |
-
#for r in results:
|
84 |
-
#boxes = r.boxes
|
85 |
-
#for box in boxes:
|
86 |
-
cls = box.cls[0]
|
87 |
-
conf = math.ceil((box.conf[0]*100))/100
|
88 |
-
if (int(cls)<3) and (conf > 0.70):
|
89 |
-
|
90 |
-
points = np.int32([mask])
|
91 |
-
cv2.polylines(img, points, True, (255, 0, 0), 1)
|
92 |
-
color_number = classes_ids.index(int(box.cls[0]))
|
93 |
-
color = colors[color_number]
|
94 |
-
|
95 |
-
cv2.fillPoly(image, points, color)
|
96 |
-
|
97 |
-
x1, y1, x2, y2 = box.xyxy[0]
|
98 |
-
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
name = yolo_classes[int(cls)]
|
104 |
-
# fontScale
|
105 |
-
fontScale = 0.5
|
106 |
-
|
107 |
-
color_number = classes_ids.index(int(box.cls[0]))
|
108 |
-
color = colors[color_number]
|
109 |
-
|
110 |
-
# Line thickness of 2 px
|
111 |
-
thickness = 1
|
112 |
-
font = cv2.FONT_HERSHEY_SIMPLEX
|
113 |
-
|
114 |
-
cv2.putText(image, str(name) + " " + str(conf), (max(0,x1), max(35,y1)), font,
|
115 |
-
fontScale, color, thickness, cv2.LINE_AA)
|
116 |
-
|
117 |
-
|
118 |
-
return image
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
inputs_image = [
|
123 |
gr.components.Image(type="filepath", label="Input Image"),
|
124 |
]
|
@@ -132,22 +81,8 @@ interface_image = gr.Interface(
|
|
132 |
title="Object segmentation",
|
133 |
examples=path,
|
134 |
cache_examples=False,
|
135 |
-
)
|
136 |
|
137 |
-
outputs_video = [
|
138 |
-
gr.components.Image(type="numpy", label="Output Image"),
|
139 |
-
]
|
140 |
|
141 |
-
interface_webcam = gr.Interface(
|
142 |
-
fn=show_preds_webcam,
|
143 |
-
inputs=gr.Image(sources=["webcam"], streaming=True, type="pil"),
|
144 |
-
outputs=outputs_video,
|
145 |
-
live=True
|
146 |
-
)
|
147 |
-
|
148 |
-
gr.TabbedInterface(
|
149 |
-
[ interface_webcam, interface_image],
|
150 |
-
tab_names=[ 'Webcam', "Image"]
|
151 |
-
).queue().launch()
|
152 |
|
153 |
|
|
|
68 |
|
69 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
inputs_image = [
|
72 |
gr.components.Image(type="filepath", label="Input Image"),
|
73 |
]
|
|
|
81 |
title="Object segmentation",
|
82 |
examples=path,
|
83 |
cache_examples=False,
|
84 |
+
).launch()
|
85 |
|
|
|
|
|
|
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
|