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