Spaces:
Build error
Build error
Commit
·
0e6a922
1
Parent(s):
ebb7c5f
Update app.py
Browse files
app.py
CHANGED
|
@@ -118,6 +118,51 @@ def predict(img):
|
|
| 118 |
# Return the prediction dictionary and prediction time
|
| 119 |
return pred_labels_and_probs, pred_time
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
### 4. Gradio app ###
|
| 122 |
|
| 123 |
# Create title, description and article strings
|
|
@@ -136,6 +181,16 @@ outputs_image = [
|
|
| 136 |
gr.components.Image(type="numpy", label="Output Image"),
|
| 137 |
]
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Create the Gradio demo
|
| 140 |
app1 = gr.Interface(fn=predict, # mapping function from input to output
|
| 141 |
inputs=gr.Image(type="pil"), # what are the inputs?
|
|
@@ -152,8 +207,15 @@ app2=gr.Interface(fn=detect,
|
|
| 152 |
inputs=inputs_image,
|
| 153 |
outputs=outputs_image,
|
| 154 |
title=title)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
demo = gr.TabbedInterface([app1, app2], ["Classify", "Detect"])
|
| 157 |
|
| 158 |
|
| 159 |
# Launch the demo!
|
|
|
|
| 118 |
# Return the prediction dictionary and prediction time
|
| 119 |
return pred_labels_and_probs, pred_time
|
| 120 |
|
| 121 |
+
|
| 122 |
+
def show_preds_video(video_path):
|
| 123 |
+
cap = cv2.VideoCapture(video_path)
|
| 124 |
+
while(cap.isOpened()):
|
| 125 |
+
ret, frame = cap.read()
|
| 126 |
+
if ret:
|
| 127 |
+
frame_copy = frame.copy()
|
| 128 |
+
pix=model.predict(frame, confidence=40, overlap=30)
|
| 129 |
+
pix=pix.json()
|
| 130 |
+
x1,x2,y1,y2=[],[],[],[]
|
| 131 |
+
for i in pix.keys():
|
| 132 |
+
if i=="predictions":
|
| 133 |
+
for j in pix["predictions"]:
|
| 134 |
+
for a,b in j.items():
|
| 135 |
+
if a=="x":
|
| 136 |
+
x1.append(b)
|
| 137 |
+
if a=="y":
|
| 138 |
+
y1.append(b)
|
| 139 |
+
if a=="width":
|
| 140 |
+
x2.append(b)
|
| 141 |
+
if a=="height":
|
| 142 |
+
y2.append(b)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
for p in range(0,len(x1)):
|
| 147 |
+
x2[p]=x2[p]+x1[p]
|
| 148 |
+
|
| 149 |
+
for p in range(0,len(x1)):
|
| 150 |
+
y2[p]=y2[p]+x1[p]
|
| 151 |
+
|
| 152 |
+
for (x11,y11,x12,y12) in zip(x1,y1,x2,y2):
|
| 153 |
+
cv2.rectangle(
|
| 154 |
+
img,
|
| 155 |
+
(x11,y11),
|
| 156 |
+
(x12,y12),
|
| 157 |
+
color=(0, 0, 255),
|
| 158 |
+
thickness=2,
|
| 159 |
+
lineType=cv2.LINE_AA
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
### 4. Gradio app ###
|
| 167 |
|
| 168 |
# Create title, description and article strings
|
|
|
|
| 181 |
gr.components.Image(type="numpy", label="Output Image"),
|
| 182 |
]
|
| 183 |
|
| 184 |
+
|
| 185 |
+
inputs_video = [
|
| 186 |
+
gr.components.Video(type="filepath", label="Input Video"),
|
| 187 |
+
|
| 188 |
+
]
|
| 189 |
+
outputs_video = [
|
| 190 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
+
|
| 194 |
# Create the Gradio demo
|
| 195 |
app1 = gr.Interface(fn=predict, # mapping function from input to output
|
| 196 |
inputs=gr.Image(type="pil"), # what are the inputs?
|
|
|
|
| 207 |
inputs=inputs_image,
|
| 208 |
outputs=outputs_image,
|
| 209 |
title=title)
|
| 210 |
+
app3=gr.Interface(
|
| 211 |
+
fn=show_preds_video,
|
| 212 |
+
inputs=inputs_video,
|
| 213 |
+
outputs=outputs_video,
|
| 214 |
+
examples=video_path,
|
| 215 |
+
cache_examples=False,
|
| 216 |
+
)
|
| 217 |
|
| 218 |
+
demo = gr.TabbedInterface([app1, app2,app3], ["Classify", "Detect","Video Interface"])
|
| 219 |
|
| 220 |
|
| 221 |
# Launch the demo!
|