Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesAdded some files
- app.py +43 -0
- best.pt +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load your YOLO model
|
| 8 |
+
model = YOLO('best.pt')
|
| 9 |
+
|
| 10 |
+
# Function for image prediction
|
| 11 |
+
def predict_image(image):
|
| 12 |
+
# Convert PIL image to OpenCV format (numpy array)
|
| 13 |
+
image_cv = np.array(image)
|
| 14 |
+
image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR) # Convert to BGR format for YOLO
|
| 15 |
+
|
| 16 |
+
# Perform inference
|
| 17 |
+
results = model(image_cv)
|
| 18 |
+
|
| 19 |
+
# Get the annotated image with bounding boxes
|
| 20 |
+
annotated_image = results[0].plot() # Use the first result and plot annotations
|
| 21 |
+
|
| 22 |
+
# Convert annotated image (BGR) back to RGB format for Gradio
|
| 23 |
+
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 24 |
+
return annotated_image_rgb
|
| 25 |
+
|
| 26 |
+
# Function for webcam/live video
|
| 27 |
+
def predict_webcam(frame):
|
| 28 |
+
# Perform inference
|
| 29 |
+
results = model(frame)
|
| 30 |
+
|
| 31 |
+
# Get the annotated frame
|
| 32 |
+
annotated_frame = results[0].plot() # Use the first result and plot annotations
|
| 33 |
+
return annotated_frame
|
| 34 |
+
|
| 35 |
+
# Create Gradio Interface
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=predict_image, # Function for image upload
|
| 38 |
+
inputs=gr.Image(type="pil", label="Upload an Image"), # Image input
|
| 39 |
+
outputs=gr.Image(type="numpy", label="Detected Image"), # Annotated output
|
| 40 |
+
live=False, # Disable live video for image upload interface
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
iface.launch()
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6341592910ffc3642d7157a81ad1ee9f00e38cdc262a10137a896f4bd2a07f39
|
| 3 |
+
size 19174739
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
gradio
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|