Spaces:
Runtime error
Runtime error
[New] Add predict code
Browse files- app.py +64 -4
- car.jpeg +0 -0
- horse.jpeg +0 -0
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,7 +1,67 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import yolov7
|
3 |
+
from yolov7.models.common import autoShape
|
4 |
+
from yolov7.models.experimental import attempt_load
|
5 |
+
from yolov7.utils.google_utils import attempt_download_from_hub, attempt_download
|
6 |
+
from yolov7.utils.torch_utils import TracedModel
|
7 |
+
YOLO_MODEL_FILE_NAME="kadirnar/yolov7-v0.1"
|
8 |
|
9 |
+
def load_local_model(model_file, autoshape=True, device='cpu', trace=False, size=640, half=False, hf_model=False):
|
10 |
+
"""
|
11 |
+
Creates a specified YOLOv7 model
|
12 |
+
Arguments:
|
13 |
+
model_path (str): path of the model
|
14 |
+
device (str): select device that model will be loaded (cpu, cuda)
|
15 |
+
trace (bool): if True, model will be traced
|
16 |
+
size (int): size of the input image
|
17 |
+
half (bool): if True, model will be in half precision
|
18 |
+
hf_model (bool): if True, model will be loaded from huggingface hub
|
19 |
+
Returns:
|
20 |
+
pytorch model
|
21 |
+
(Adapted from yolov7.hubconf.create)
|
22 |
+
"""
|
23 |
+
|
24 |
+
model = attempt_load(model_file, map_location=device)
|
25 |
+
if trace:
|
26 |
+
model = TracedModel(model, device, size)
|
27 |
+
if autoshape:
|
28 |
+
model = autoShape(model)
|
29 |
+
if half:
|
30 |
+
model.half()
|
31 |
+
return model
|
32 |
|
33 |
+
# YOLO_MODEL_FILE_NAME="kadirnar/yolov7-tiny-v0.1"
|
34 |
+
def yolov7_inference(
|
35 |
+
image: gr.inputs.Image = None,
|
36 |
+
image_size: gr.inputs.Slider = 640,
|
37 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
38 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
39 |
+
):
|
40 |
+
model = yolov7.load_model(YOLO_MODEL_FILE_NAME, device="cpu", hf_model=False, trace=False)
|
41 |
+
model.conf = conf_threshold
|
42 |
+
model.iou = iou_threshold
|
43 |
+
results = model([image], size=image_size)
|
44 |
+
return results.render()[0]
|
45 |
+
|
46 |
+
inputs = [
|
47 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
48 |
+
|
49 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
50 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
51 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
52 |
+
]
|
53 |
+
|
54 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
55 |
+
title = "Yolov7: evaluation yolov7.pt"
|
56 |
+
|
57 |
+
examples = [['car.jpeg', 640, 0.5, 0.75],
|
58 |
+
['horse.jpeg', 640, 0.5, 0.75]]
|
59 |
+
demo_app = gr.Interface(
|
60 |
+
fn=yolov7_inference,
|
61 |
+
inputs=inputs,
|
62 |
+
outputs=outputs,
|
63 |
+
title=title,
|
64 |
+
examples=examples,
|
65 |
+
cache_examples=True,
|
66 |
+
)
|
67 |
+
demo_app.launch(debug=True, enable_queue=True)
|
car.jpeg
ADDED
horse.jpeg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
yolov7detect
|