Alaaeldin commited on
Commit
0f5109e
·
verified ·
1 Parent(s): 9fb219f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -1,43 +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()
 
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()