Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import transforms
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load the YOLO model
|
| 7 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # Ensure 'best.pt' is the path to your trained model
|
| 8 |
+
|
| 9 |
+
# Define a function to process the image and make predictions
|
| 10 |
+
def detect_objects(image):
|
| 11 |
+
# Preprocess the image
|
| 12 |
+
transform = transforms.Compose([
|
| 13 |
+
transforms.ToTensor()
|
| 14 |
+
])
|
| 15 |
+
image = transform(image).unsqueeze(0) # Add batch dimension
|
| 16 |
+
|
| 17 |
+
# Perform inference
|
| 18 |
+
results = model(image)
|
| 19 |
+
|
| 20 |
+
# Extract bounding boxes and labels
|
| 21 |
+
bbox_img = results.render()[0] # This gets the image with bounding boxes drawn
|
| 22 |
+
|
| 23 |
+
return Image.fromarray(bbox_img)
|
| 24 |
+
|
| 25 |
+
# Create the Gradio interface
|
| 26 |
+
inputs = gr.inputs.Image(shape=(640, 480))
|
| 27 |
+
outputs = gr.outputs.Image(type="pil")
|
| 28 |
+
|
| 29 |
+
gr_interface = gr.Interface(fn=detect_objects, inputs=inputs, outputs=outputs, title="YOLO Object Detection", description="Upload an image to detect objects using a YOLO model.")
|
| 30 |
+
|
| 31 |
+
# Run the Gradio app
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
gr_interface.launch()
|