Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
|
4 |
+
def yolov8_inference(
|
5 |
+
image: gr.inputs.Image = None,
|
6 |
+
model_path = "eeshawn11/naruto_hand_seal_detection",
|
7 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
8 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
9 |
+
):
|
10 |
+
"""
|
11 |
+
YOLOv8 inference function
|
12 |
+
Args:
|
13 |
+
image: Input image
|
14 |
+
model_path: Path to the model
|
15 |
+
conf_threshold: Confidence threshold
|
16 |
+
iou_threshold: IOU threshold
|
17 |
+
Returns:
|
18 |
+
Rendered image
|
19 |
+
"""
|
20 |
+
model = YOLO(model_path)
|
21 |
+
model.conf = conf_threshold
|
22 |
+
model.iou = iou_threshold
|
23 |
+
results = model.predict(image, return_outputs=True)
|
24 |
+
object_prediction_list = []
|
25 |
+
for _, image_results in enumerate(results):
|
26 |
+
if len(image_results)!=0:
|
27 |
+
image_predictions_in_xyxy_format = image_results['det']
|
28 |
+
for pred in image_predictions_in_xyxy_format:
|
29 |
+
x1, y1, x2, y2 = (
|
30 |
+
int(pred[0]),
|
31 |
+
int(pred[1]),
|
32 |
+
int(pred[2]),
|
33 |
+
int(pred[3]),
|
34 |
+
)
|
35 |
+
bbox = [x1, y1, x2, y2]
|
36 |
+
score = pred[4]
|
37 |
+
category_name = model.model.names[int(pred[5])]
|
38 |
+
category_id = pred[5]
|
39 |
+
object_prediction = ObjectPrediction(
|
40 |
+
bbox=bbox,
|
41 |
+
category_id=int(category_id),
|
42 |
+
score=score,
|
43 |
+
category_name=category_name,
|
44 |
+
)
|
45 |
+
object_prediction_list.append(object_prediction)
|
46 |
+
|
47 |
+
image = read_image(image)
|
48 |
+
output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
|
49 |
+
return output_image['image']
|
50 |
+
|
51 |
+
|
52 |
+
inputs = [
|
53 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
54 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
55 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
56 |
+
]
|
57 |
+
|
58 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
59 |
+
title = "Naruto Hand Seal Detection with YOLOv8"
|
60 |
+
|
61 |
+
gr.Interface(
|
62 |
+
fn=yolov8_inference,
|
63 |
+
inputs=inputs,
|
64 |
+
outputs=outputs,
|
65 |
+
title=title,
|
66 |
+
theme='huggingface',
|
67 |
+
).launch(debug=True).queue()
|