Team Ai
Apppublic

Kunal09/Deep_learning_tutorial

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py43 linesDownload Raw Back to root
1import gradio as gr2import torch3import cv24import numpy as np5 6# Load the pre-trained YOLOv5 model7model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)8 9def detect_people(video):10    # Initialize the video capture object11    cap = cv2.VideoCapture(video.name)12 13    # Initialize the output variable14    num_people_detected = 015 16    # Loop through each frame of the video17    while cap.isOpened():18        # Read the frame19        ret, frame = cap.read()20 21        # If there are no more frames, break out of the loop22        if not ret:23            break24 25        # Run the YOLOv5 model on the frame to detect people26        results = model(frame, size=640)27 28        # Get the number of people detected in the frame29        num_people_detected += len(results.xyxy[0])30 31    # Release the video capture object32    cap.release()33 34    # Return the number of people detected35    return num_people_detected36 37# Define the input and output interfaces for the Gradio app38inputs = gr.inputs.Video(label="Upload a video")39outputs = gr.outputs.Textbox(label="Number of people detected")40 41# Create the Gradio app42gr.Interface(detect_people, inputs, outputs, title="Object Detection App", description="Upload a video to detect the number of people in it.").launch()43