Spaces:
Sleeping
Sleeping
Initial commit for YOLOv8 object detection
Browse files- app.py +33 -66
- best_yolo8_model/README.md +4 -0
- best_yolo8_model/best.pt +3 -0
app.py
CHANGED
@@ -1,70 +1,37 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import openvino.runtime as ov
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image, ImageDraw
|
5 |
-
import os
|
6 |
|
7 |
-
# Define
|
8 |
-
|
9 |
|
|
|
10 |
def load_model(model_path):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
}
|
40 |
-
boxes.append(box)
|
41 |
-
|
42 |
-
# Draw bounding boxes
|
43 |
-
draw = ImageDraw.Draw(image)
|
44 |
-
for box in boxes:
|
45 |
-
x_min, y_min, x_max, y_max = map(int, box['bounding_box'])
|
46 |
-
|
47 |
-
# Ensure coordinates are within bounds
|
48 |
-
x_min = max(0, min(x_min, image.width - 1))
|
49 |
-
y_min = max(0, min(y_min, image.height - 1))
|
50 |
-
x_max = max(0, min(x_max, image.width - 1))
|
51 |
-
y_max = max(0, min(y_max, image.height - 1))
|
52 |
-
|
53 |
-
# Draw rectangle and class label
|
54 |
-
draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=3)
|
55 |
-
draw.text((x_min, y_min), f"Class {box['class_id']}: {box['confidence']:.2f}", fill="red")
|
56 |
-
|
57 |
-
return image
|
58 |
-
|
59 |
-
|
60 |
-
# Gradio Interface
|
61 |
-
interface = gr.Interface(
|
62 |
-
fn=predict, # Use the updated predict function
|
63 |
-
inputs=gr.Image(type="pil"), # Accept an image as input
|
64 |
-
outputs=gr.Image(type="pil"), # Return the annotated image as output
|
65 |
-
title="OpenVINO Object Detection" # Title of the app
|
66 |
-
)
|
67 |
-
|
68 |
-
# Launch the app
|
69 |
-
if __name__ == "__main__":
|
70 |
-
interface.launch(share=True)
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
from PIL import Image
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Define the path to the YOLOv8 model
|
6 |
+
model_path = "./best_yolo8_model/best.pt" # Adjust to your local model path
|
7 |
|
8 |
+
# Function to load the YOLOv8 model
|
9 |
def load_model(model_path):
|
10 |
+
print(f"Loading YOLOv8 model from: {model_path}")
|
11 |
+
detection_model = YOLO(model_path) # Directly load the local model
|
12 |
+
return detection_model
|
13 |
+
|
14 |
+
# Function to perform inference
|
15 |
+
def predict(pil_img):
|
16 |
+
try:
|
17 |
+
# Perform prediction
|
18 |
+
results = detection_model.predict(source=pil_img, conf=0.5, iou=0.6, save=False, save_txt=False)
|
19 |
+
|
20 |
+
# Visualize bounding boxes on the image
|
21 |
+
img_with_boxes = results[0].plot()
|
22 |
+
out_pil_img = Image.fromarray(img_with_boxes[..., ::-1]) # Convert BGR to RGB
|
23 |
+
|
24 |
+
return out_pil_img
|
25 |
+
except Exception as e:
|
26 |
+
return f"Error during prediction: {str(e)}"
|
27 |
+
|
28 |
+
# Load the YOLOv8 model
|
29 |
+
detection_model = load_model(model_path)
|
30 |
+
|
31 |
+
# Define Gradio interface
|
32 |
+
gr.Interface(
|
33 |
+
fn=predict,
|
34 |
+
inputs=gr.Image(type="pil"),
|
35 |
+
outputs=gr.Image(type="pil"),
|
36 |
+
title="YOLOv8 Object Detection"
|
37 |
+
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
best_yolo8_model/README.md
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
What the model does.
|
2 |
+
Dataset used.
|
3 |
+
Training details (e.g., epochs, optimizer).
|
4 |
+
License (if required)
|
best_yolo8_model/best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d21c0ac1a455143c65f21ea2e68b2299b4263d96d0851091ebf6610f712cf043
|
3 |
+
size 22520739
|