Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
from ultralytics import YOLO
|
3 |
import torch
|
4 |
-
|
5 |
import cv2
|
|
|
6 |
|
7 |
-
# Load the trained YOLO model
|
8 |
-
|
9 |
-
model = YOLO("yolov8n.pt") # Change to your trained model file if needed
|
10 |
|
11 |
-
# Define the function for
|
12 |
def predict(image):
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
labels = results[0].boxes.cls.cpu().numpy()
|
18 |
-
confidences = results[0].boxes.conf.cpu().numpy()
|
19 |
-
|
20 |
-
# Load class names
|
21 |
-
class_names = ["apples", "banana", "cherry", "grapes", "oranges"]
|
22 |
|
23 |
# Draw bounding boxes on the image
|
24 |
-
for
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
fn=predict,
|
35 |
-
inputs=gr.Image(type="
|
36 |
-
outputs=gr.Image(type="
|
37 |
title="YOLOv8 Fruit Detection",
|
38 |
-
description="Upload an image
|
39 |
)
|
40 |
|
41 |
-
#
|
42 |
if __name__ == "__main__":
|
43 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from ultralytics import YOLO
|
4 |
import cv2
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
# Load the trained YOLO model
|
8 |
+
model = YOLO("yolo11n.pt") # Ensure this matches the model file in your repo
|
|
|
9 |
|
10 |
+
# Define the function for making predictions
|
11 |
def predict(image):
|
12 |
+
# Convert to OpenCV format
|
13 |
+
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
14 |
|
15 |
+
# Run YOLOv8 inference
|
16 |
+
results = model(img)
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Draw bounding boxes on the image
|
19 |
+
for r in results:
|
20 |
+
for box in r.boxes:
|
21 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box coordinates
|
22 |
+
conf = float(box.conf[0]) # Confidence score
|
23 |
+
cls = int(box.cls[0]) # Class index
|
24 |
+
|
25 |
+
label = f"{model.names[cls]} {conf:.2f}"
|
26 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
27 |
+
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
28 |
+
|
29 |
+
# Convert back to PIL format
|
30 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
31 |
+
|
32 |
+
# Create the Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
fn=predict,
|
35 |
+
inputs=gr.Image(type="pil"),
|
36 |
+
outputs=gr.Image(type="pil"),
|
37 |
title="YOLOv8 Fruit Detection",
|
38 |
+
description="Upload an image and the model will detect objects."
|
39 |
)
|
40 |
|
41 |
+
# Launch the Gradio app
|
42 |
if __name__ == "__main__":
|
43 |
+
iface.launch()
|