Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yolov7
|
| 3 |
-
import subprocess
|
| 4 |
import tempfile
|
| 5 |
-
import time
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
import uuid
|
| 8 |
import cv2
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
def image_fn(
|
| 14 |
-
image: gr.inputs.Image = None,
|
| 15 |
-
model_path: gr.inputs.Dropdown = None,
|
| 16 |
-
image_size: gr.inputs.Slider = 640,
|
| 17 |
-
conf_threshold: gr.inputs.Slider = 0.25,
|
| 18 |
-
iou_threshold: gr.inputs.Slider = 0.45,
|
| 19 |
-
):
|
| 20 |
"""
|
| 21 |
YOLOv7 inference function
|
| 22 |
Args:
|
|
@@ -28,42 +16,36 @@ def image_fn(
|
|
| 28 |
Returns:
|
| 29 |
Rendered image
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
|
| 33 |
model.conf = conf_threshold
|
| 34 |
model.iou = iou_threshold
|
| 35 |
results = model([image], size=image_size)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
|
|
|
| 41 |
image_interface = gr.Interface(
|
| 42 |
fn=image_fn,
|
| 43 |
inputs=[
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"Aalaa/Yolov7_Visual_Pollution_Detection",
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
label="
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
],
|
| 56 |
-
outputs=gr.outputs.Image(type="filepath", label="Output Image"),
|
| 57 |
-
|
| 58 |
examples=[['image1.jpg', 'Aalaa/Yolov7_Visual_Pollution_Detection', 640, 0.25, 0.45]],
|
| 59 |
cache_examples=True,
|
| 60 |
-
theme='
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
if __name__ == "__main__":
|
| 66 |
gr.TabbedInterface(
|
| 67 |
[image_interface],
|
| 68 |
["Run on Images"],
|
| 69 |
-
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yolov7
|
|
|
|
| 3 |
import tempfile
|
|
|
|
|
|
|
|
|
|
| 4 |
import cv2
|
| 5 |
+
from pathlib import Path
|
|
|
|
| 6 |
|
| 7 |
+
def image_fn(image, model_path, image_size, conf_threshold, iou_threshold):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
YOLOv7 inference function
|
| 10 |
Args:
|
|
|
|
| 16 |
Returns:
|
| 17 |
Rendered image
|
| 18 |
"""
|
|
|
|
| 19 |
model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
|
| 20 |
model.conf = conf_threshold
|
| 21 |
model.iou = iou_threshold
|
| 22 |
results = model([image], size=image_size)
|
| 23 |
+
img = results.render()[0]
|
| 24 |
+
img.flags.writeable = True # Make the image writable
|
| 25 |
+
return img
|
|
|
|
| 26 |
|
| 27 |
+
# Use gr.components instead of gr.inputs and gr.outputs
|
| 28 |
image_interface = gr.Interface(
|
| 29 |
fn=image_fn,
|
| 30 |
inputs=[
|
| 31 |
+
gr.components.Image(type="pil", label="Input Image"),
|
| 32 |
+
gr.components.Dropdown(
|
| 33 |
+
choices=["Aalaa/Yolov7_Visual_Pollution_Detection"],
|
| 34 |
+
value="Aalaa/Yolov7_Visual_Pollution_Detection",
|
| 35 |
+
label="Model"
|
| 36 |
+
),
|
| 37 |
+
gr.components.Slider(minimum=320, maximum=1280, value=640, step=32, label="Image Size"),
|
| 38 |
+
gr.components.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
|
| 39 |
+
gr.components.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold")
|
| 40 |
+
],
|
| 41 |
+
outputs=gr.components.Image(type="numpy", label="Output Image"),
|
|
|
|
|
|
|
|
|
|
| 42 |
examples=[['image1.jpg', 'Aalaa/Yolov7_Visual_Pollution_Detection', 640, 0.25, 0.45]],
|
| 43 |
cache_examples=True,
|
| 44 |
+
theme='default'
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
gr.TabbedInterface(
|
| 49 |
[image_interface],
|
| 50 |
["Run on Images"],
|
| 51 |
+
).launch()
|