Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,74 @@
|
|
1 |
import gradio as gr
|
2 |
-
import PIL.Image as Image
|
3 |
-
from ultralytics import ASSETS, YOLO
|
4 |
|
|
|
5 |
|
6 |
-
from
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
show_conf=True,
|
25 |
-
imgsz=640,
|
26 |
-
)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
return im
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
41 |
-
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
42 |
-
],
|
43 |
-
outputs=gr.Image(type="pil", label="Result"),
|
44 |
-
title="Ultralytics Gradio",
|
45 |
-
description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
|
46 |
-
examples = [
|
47 |
['1.jpg'],
|
48 |
['2.jpg'],
|
49 |
['3.jpg']
|
50 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
+
from ultralytics import YOLO
|
4 |
|
5 |
+
# Load the YOLOv8 model from the 'best.pt' checkpoint
|
6 |
+
model_path = "new_data_improved_object_detector.pt"
|
7 |
+
|
8 |
+
model = YOLO(model_path)
|
9 |
|
10 |
+
import torch
|
11 |
|
12 |
+
#from ultralyticsplus import render_result
|
13 |
+
from render import custom_render_result
|
14 |
+
def yoloV8_func(image: gr.Image = None,
|
15 |
+
image_size: int = 640,
|
16 |
+
conf_threshold: float = 0.4,
|
17 |
+
iou_threshold: float = 0.5):
|
18 |
+
|
19 |
+
"""This function performs YOLOv8 object detection on the given image.
|
20 |
|
21 |
+
Args:
|
22 |
+
image (gr.Image, optional): Input image to detect objects on. Defaults to None.
|
23 |
+
image_size (int, optional): Desired image size for the model. Defaults to 640.
|
24 |
+
conf_threshold (float, optional): Confidence threshold for object detection. Defaults to 0.4.
|
25 |
+
iou_threshold (float, optional): Intersection over Union threshold for object detection. Defaults to 0.50.
|
26 |
+
"""
|
27 |
+
|
28 |
+
# model = torch.hub.load('ultralytics/yolov8', 'custom', path='/content/best.pt', force_reload=True, trust_repo=True)
|
29 |
|
30 |
+
# Perform object detection on the input image using the YOLOv8 model
|
31 |
+
results = model.predict(image,
|
32 |
+
conf=conf_threshold,
|
33 |
+
iou=iou_threshold,
|
34 |
+
imgsz=image_size)
|
35 |
|
36 |
+
# Print the detected objects' information (class, coordinates, and probability)
|
37 |
+
box = results[0].boxes
|
38 |
+
print("Object type:", box.cls)
|
39 |
+
print("Coordinates:", box.xyxy)
|
40 |
+
print("Probability:", box.conf)
|
|
|
|
|
|
|
41 |
|
42 |
+
# Render the output image with bounding boxes around detected objects
|
43 |
+
render = custom_render_result(model=model, image=image, result=results[0])
|
44 |
+
return render
|
45 |
|
|
|
46 |
|
47 |
+
inputs = [
|
48 |
+
gr.Image(type="filepath", label="Input Image"),
|
49 |
+
gr.Slider(minimum=320, maximum=1280, step=32, label="Image Size", value=640),
|
50 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label="Confidence Threshold"),
|
51 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label="IOU Threshold"),
|
52 |
+
]
|
53 |
|
54 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
55 |
+
|
56 |
+
title = "YOLOv8 101: Custom Object Detection on meter"
|
57 |
+
|
58 |
+
examples = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
['1.jpg'],
|
60 |
['2.jpg'],
|
61 |
['3.jpg']
|
62 |
]
|
63 |
+
|
64 |
+
yolo_app = gr.Interface(
|
65 |
+
fn=yoloV8_func,
|
66 |
+
inputs=inputs,
|
67 |
+
outputs=outputs,
|
68 |
+
title=title,
|
69 |
+
examples=examples,
|
70 |
+
cache_examples=False,
|
71 |
)
|
72 |
|
73 |
+
# Launch the Gradio interface in debug mode with queue enabled
|
74 |
+
yolo_app.launch(debug=True, share=True).queue()
|