nhosseini commited on
Commit
8afe469
·
verified ·
1 Parent(s): 7fbbe6c

update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -45
app.py CHANGED
@@ -1,45 +1,36 @@
1
- import gradio as gr
2
- import cv2
3
- import torch
4
- from ultralytics import YOLO
5
- import numpy as np
6
-
7
- # Load the YOLOv8 model (replace 'best.pt' with the path to your model)
8
- model = YOLO('best.pt')
9
-
10
- # Function to perform object detection using YOLOv8
11
- def detect_objects(image):
12
- # Convert the image to RGB (OpenCV loads images as BGR)
13
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
14
-
15
- # Perform inference on the image
16
- results = model.predict(image_rgb)
17
-
18
- # Get bounding boxes and class labels from the results
19
- annotated_image = results[0].plot() # YOLOv8 has a plot method that returns an annotated image
20
-
21
- return annotated_image
22
-
23
- # Gradio Interface (take a photo using webcam or upload an image)
24
- def take_photo_and_detect():
25
- # OpenCV function to take a photo using the webcam
26
- def capture_image():
27
- cap = cv2.VideoCapture(0)
28
- ret, frame = cap.read()
29
- cap.release()
30
- return frame
31
-
32
- # Set up the Gradio interface
33
- demo = gr.Interface(
34
- fn=detect_objects, # The function to perform object detection
35
- inputs=gr.Image(source="webcam", tool="editor", label="Take a photo or upload an image"),
36
- outputs=gr.Image(label="Detected Objects"),
37
- title="YOLOv8 Object Detection",
38
- description="Take a photo or upload an image to detect objects using the YOLOv8 model."
39
- )
40
-
41
- demo.launch()
42
-
43
- # Start the app
44
- if __name__ == "__main__":
45
- take_photo_and_detect()
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ from ultralytics import YOLO
5
+ import numpy as np
6
+
7
+ # Load the YOLOv8 model (replace 'best.pt' with the path to your model)
8
+ model = YOLO('best.pt')
9
+ # Function to perform object detection using YOLOv8
10
+ def detect_objects(image):
11
+ # Convert the image from PIL format (used by Gradio) to a NumPy array
12
+ image = np.array(image)
13
+
14
+ # Convert the image to RGB (OpenCV loads images as BGR)
15
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert from RGB (PIL) to BGR (OpenCV)
16
+
17
+ # Perform inference on the image
18
+ results = model.predict(image_rgb)
19
+
20
+ # Get bounding boxes and class labels from the results
21
+ annotated_image = results[0].plot() # YOLOv8 has a plot method that returns an annotated image
22
+
23
+ return annotated_image
24
+
25
+ # Gradio Interface (image upload instead of webcam)
26
+ demo = gr.Interface(
27
+ fn=detect_objects, # The function to perform object detection
28
+ inputs=gr.Image(type="pil", label="Upload an image"), # Accept a file upload
29
+ outputs=gr.Image(label="Detected Shelves"),
30
+ title="Empty Shelf Detection using YOLOv8",
31
+ description="Upload an image of shelves, and the model will identify empty spaces using YOLOv8. This tool is designed for recognizing empty shelves in retail or warehouse environments to assist with inventory management and restocking decisions."
32
+ )
33
+
34
+ # Start the app
35
+ if __name__ == "__main__":
36
+ demo.launch()