Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,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 |
-
|
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 |
-
|
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 |
-
|
43 |
-
|
44 |
-
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
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 |
-
|
55 |
|
56 |
-
title = "YOLOv8 101: Custom Object Detection on meter"
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from ultralytics import YOLO
|
3 |
+
from PIL import Image
|
4 |
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
model_path = 'new_data_improved_object_detector.pt'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
model = YOLO(model_path)
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
13 |
+
"""Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
|
14 |
+
# Convert the input image to grayscale
|
15 |
+
img = img.convert('L')
|
16 |
|
17 |
+
results = model.predict(
|
18 |
+
source=img,
|
19 |
+
conf =0.4,
|
20 |
+
iou=0.6,
|
21 |
+
show_labels=True,
|
22 |
+
show_conf=True,
|
23 |
+
imgsz=640,
|
24 |
+
)
|
25 |
|
26 |
+
for r in results:
|
27 |
+
im_array = r.plot()
|
28 |
+
im = Image.fromarray(im_array[..., ::-1])
|
|
|
|
|
|
|
29 |
|
30 |
+
return im
|
31 |
|
|
|
32 |
|
33 |
+
iface = gr.Interface(
|
34 |
+
|
35 |
+
fn=predict_image,
|
36 |
+
inputs=[
|
37 |
+
gr.Image(type="pil", label="Upload Image"),
|
38 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
39 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
40 |
+
],
|
41 |
+
outputs=gr.Image(type="pil", label="Result"),
|
42 |
+
title="Ultralytics Gradio",
|
43 |
+
description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
|
44 |
+
examples = [
|
45 |
['1.jpg'],
|
46 |
['2.jpg'],
|
47 |
['3.jpg']
|
48 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
)
|
50 |
|
51 |
+
|
52 |
# Launch the Gradio interface in debug mode with queue enabled
|
53 |
+
yolo_app.launch(debug=True, share=False).queue()
|