Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from ultralytics import YOLO | |
import cv2 | |
# Load YOLOv8 model | |
model = YOLO("yolov8n.pt") | |
def detect_objects(video): | |
cap = cv2.VideoCapture(video) | |
frames = [] | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
results = model(frame) | |
annotated_frame = results[0].plot() | |
_, buffer = cv2.imencode('.jpg', annotated_frame) | |
frames.append(buffer.tobytes()) | |
cap.release() | |
return frames | |
# Create Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Real-Time Object Detection for Blind Assistance") | |
gr.Markdown("This app detects objects in real-time using your webcam.") | |
# Remove the `source` argument | |
video_input = gr.Video(label="Webcam Stream", type="filepath") | |
output_gallery = gr.Video(label="Detection Output") | |
detect_button = gr.Button("Start Detection") | |
detect_button.click(detect_objects, inputs=[video_input], outputs=[output_gallery]) | |
# Launch the app | |
demo.launch() | |