keffy commited on
Commit
8138f28
1 Parent(s): 12b9ede

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+
6
+ # Load the YOLOv8 model
7
+ model = YOLO("best.pt") # Ensure this file is in the same directory as app.py on Hugging Face
8
+
9
+ # Define the inference function
10
+ def predict(image):
11
+ # Convert the input image from RGB to BGR (OpenCV format)
12
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
13
+
14
+ # Run the model on the input image
15
+ results = model(image_bgr)
16
+
17
+ # Extract the result image with detections
18
+ annotated_image = results[0].plot() # Returns a BGR image with annotations
19
+
20
+ # Convert the image back to RGB for displaying in Gradio
21
+ annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
22
+
23
+ return annotated_image_rgb
24
+
25
+ # Define the Gradio interface
26
+ interface = gr.Interface(
27
+ fn=predict,
28
+ inputs=gr.Image(type="numpy", label="Upload an Image"),
29
+ outputs=gr.Image(type="numpy", label="Detected Objects"),
30
+ title="YOLOv8 Object Detection",
31
+ description="Upload an image to detect objects with YOLOv8 model."
32
+ )
33
+
34
+ # Launch the app
35
+ if __name__ == "__main__":
36
+ interface.launch(share=True)