Spaces:
Runtime error
Runtime error
Khawalidmi
commited on
Commit
•
8a764fa
1
Parent(s):
51269b5
basic inference now works
Browse files- app.py +63 -4
- requirements.txt +12 -0
app.py
CHANGED
@@ -1,7 +1,66 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from sahi.prediction import ObjectPrediction
|
4 |
+
from sahi.utils.cv import visualize_object_predictions, read_image
|
5 |
|
6 |
+
from gradio.components import Slider, Image, Dropdown
|
|
|
7 |
|
8 |
+
|
9 |
+
def yolov8_inference(
|
10 |
+
image: Image = None,
|
11 |
+
model_path: Dropdown = None,
|
12 |
+
image_size: Slider = 640,
|
13 |
+
confidence_threshold: Slider = 0.25,
|
14 |
+
iou_threshold: Slider = 0.45,
|
15 |
+
):
|
16 |
+
model = YOLO(model_path)
|
17 |
+
model.conf = confidence_threshold
|
18 |
+
model.iou = iou_threshold
|
19 |
+
results = model.predict(image, imgsz=image_size)
|
20 |
+
object_prediction_list = []
|
21 |
+
for _, image_results in enumerate(results):
|
22 |
+
if len(image_results) != 0:
|
23 |
+
image_predictions_in_xyxy_format = image_results.boxes.data
|
24 |
+
for pred in image_predictions_in_xyxy_format:
|
25 |
+
x1, y1, x2, y2 = (
|
26 |
+
int(pred[0]),
|
27 |
+
int(pred[1]),
|
28 |
+
int(pred[2]),
|
29 |
+
int(pred[3]),
|
30 |
+
)
|
31 |
+
bbox = [x1, y1, x2, y2]
|
32 |
+
score = pred[4]
|
33 |
+
category_name = model.model.names[int(pred[5])]
|
34 |
+
category_id = pred[5]
|
35 |
+
object_prediction = ObjectPrediction(
|
36 |
+
bbox=bbox,
|
37 |
+
category_id=int(category_id),
|
38 |
+
score=score,
|
39 |
+
category_name=category_name,
|
40 |
+
)
|
41 |
+
object_prediction_list.append(object_prediction)
|
42 |
+
|
43 |
+
output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
|
44 |
+
return output_image['image']
|
45 |
+
|
46 |
+
inputs = [
|
47 |
+
"image",
|
48 |
+
Dropdown(choices=["yolo/runs/detect/train10/weights/best.pt"], label="Model"),
|
49 |
+
Slider(minimum=320, maximum=1280, step=32, label="Image Size"),
|
50 |
+
Slider(minimum=0.0, maximum=1.0, step=0.05, label="Confidence Threshold"),
|
51 |
+
Slider(minimum=0.0, maximum=1.0, step=0.05, label="IOU Threshold"),
|
52 |
+
]
|
53 |
+
|
54 |
+
title = "Smartathon Pothole Challenge"
|
55 |
+
|
56 |
+
examples = [['examples/0011.png', 'yolo/runs/detect/train10/weights/best.pt', 640, 0.25, 0.45], ['examples/0014.png', 'yolo/runs/detect/train10/weights/best.pt', 640, 0.25, 0.45], ['examples/0021.png', 'yolo/runs/detect/train10/weights/best.pt', 640, 0.25, 0.45]]
|
57 |
+
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=yolov8_inference,
|
60 |
+
inputs=inputs,
|
61 |
+
outputs="image",
|
62 |
+
title=title,
|
63 |
+
examples=examples,
|
64 |
+
theme="default",
|
65 |
+
)
|
66 |
+
iface.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pytorch-lightning==1.7.3
|
2 |
+
matplotlib
|
3 |
+
opencv-python
|
4 |
+
tqdm
|
5 |
+
imageio
|
6 |
+
path
|
7 |
+
scipy
|
8 |
+
configargparse
|
9 |
+
kornia
|
10 |
+
gradio
|
11 |
+
ultrlytics
|
12 |
+
sahi
|