Spaces:
Running
Running
File size: 2,458 Bytes
caff61e bccf53b dc80d48 4fa263e b5a364c caff61e b5a364c e82b28e b5a364c 8378a4b 36e1064 b5a364c ab96246 8378a4b ab96246 e82b28e a32f6c3 b5a364c eaa57e7 a32f6c3 eaa57e7 a32f6c3 35669c6 eaa57e7 a32f6c3 b5a364c a32f6c3 b5a364c a32f6c3 b5a364c a32f6c3 b5a364c eaa57e7 a29d5e2 b5a364c eaa57e7 b86490c b5a364c eaa57e7 b5a364c eaa57e7 a32f6c3 ab96246 a32f6c3 ab96246 b5a364c eaa57e7 a32f6c3 eaa57e7 b5a364c a32f6c3 46e3370 ab96246 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import torch
import numpy as np
import gradio as gr
from PIL import Image
import cv2
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load optimized YOLOv5s model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
# Set model confidence threshold
model.conf = 0.5
if device.type == 'cuda':
model.half()
def process_frame(image):
"""Processes a frame from the webcam and applies YOLOv5 object detection."""
if image is None:
return None
try:
image_pil = Image.fromarray(image)
with torch.no_grad():
results = model(image_pil)
rendered_images = results.render()
return np.array(rendered_images[0]) if rendered_images else image
except Exception as e:
print(f"Error processing frame: {e}")
return image
def process_uploaded_image(image):
"""Processes an uploaded image and applies YOLOv5 object detection."""
if image is None:
return None
try:
image_pil = Image.fromarray(image)
with torch.no_grad():
results = model(image_pil)
rendered_images = results.render()
return np.array(rendered_images[0]) if rendered_images else image
except Exception as e:
print(f"Error processing image: {e}")
return image
# Create Gradio UI
with gr.Blocks(title="Real-Time Object Detection") as app:
gr.Markdown("# Real-Time Object Detection with Dual Input")
with gr.Tabs():
# ๐ท Live Webcam Tab
with gr.TabItem("๐ท Live Camera"):
with gr.Row():
webcam_input = gr.Image(source="webcam", label="Live Feed")
live_output = gr.Image(label="Processed Feed")
# Process frame when new frame is available
webcam_input.change(process_frame, inputs=webcam_input, outputs=live_output)
# ๐ผ๏ธ Image Upload Tab (With Submit Button)
with gr.TabItem("๐ผ๏ธ Image Upload"):
with gr.Row():
upload_input = gr.Image(type="numpy", label="Upload Image")
submit_button = gr.Button("Submit")
upload_output = gr.Image(label="Detection Result")
submit_button.click(process_uploaded_image, inputs=upload_input, outputs=upload_output)
app.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|