Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# Load the trained YOLO model from your Hugging Face Model Hub
|
8 |
+
MODEL_REPO = "Alaaeldin/yolo-demo" # Replace with your actual model repo
|
9 |
+
model = YOLO("yolov8n.pt") # Change to your trained model file if needed
|
10 |
+
|
11 |
+
# Define the function for image prediction
|
12 |
+
def predict(image):
|
13 |
+
results = model(image)
|
14 |
+
|
15 |
+
# Extract bounding boxes, labels, and confidence scores
|
16 |
+
detections = results[0].boxes.xyxy.cpu().numpy()
|
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 i, (box, label, conf) in enumerate(zip(detections, labels, confidences)):
|
25 |
+
x1, y1, x2, y2 = map(int, box)
|
26 |
+
label_text = f"{class_names[int(label)]}: {conf:.2f}"
|
27 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
28 |
+
cv2.putText(image, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
29 |
+
|
30 |
+
return image
|
31 |
+
|
32 |
+
# Create Gradio Interface
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=predict,
|
35 |
+
inputs=gr.Image(type="numpy"),
|
36 |
+
outputs=gr.Image(type="numpy"),
|
37 |
+
title="YOLOv8 Fruit Detection",
|
38 |
+
description="Upload an image to detect fruits using YOLOv8.",
|
39 |
+
)
|
40 |
+
|
41 |
+
# Run the app
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.launch()
|