Kunal09's picture
Create app.py
6f4da02
import gradio as gr
import torch
import cv2
import numpy as np
# Load the pre-trained YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
def detect_people(video):
# Initialize the video capture object
cap = cv2.VideoCapture(video.name)
# Initialize the output variable
num_people_detected = 0
# Loop through each frame of the video
while cap.isOpened():
# Read the frame
ret, frame = cap.read()
# If there are no more frames, break out of the loop
if not ret:
break
# Run the YOLOv5 model on the frame to detect people
results = model(frame, size=640)
# Get the number of people detected in the frame
num_people_detected += len(results.xyxy[0])
# Release the video capture object
cap.release()
# Return the number of people detected
return num_people_detected
# Define the input and output interfaces for the Gradio app
inputs = gr.inputs.Video(label="Upload a video")
outputs = gr.outputs.Textbox(label="Number of people detected")
# Create the Gradio app
gr.Interface(detect_people, inputs, outputs, title="Object Detection App", description="Upload a video to detect the number of people in it.").launch()