Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the pre-trained YOLOv5 model
|
| 7 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 8 |
+
|
| 9 |
+
def detect_people(video):
|
| 10 |
+
# Initialize the video capture object
|
| 11 |
+
cap = cv2.VideoCapture(video.name)
|
| 12 |
+
|
| 13 |
+
# Initialize the output variable
|
| 14 |
+
num_people_detected = 0
|
| 15 |
+
|
| 16 |
+
# Loop through each frame of the video
|
| 17 |
+
while cap.isOpened():
|
| 18 |
+
# Read the frame
|
| 19 |
+
ret, frame = cap.read()
|
| 20 |
+
|
| 21 |
+
# If there are no more frames, break out of the loop
|
| 22 |
+
if not ret:
|
| 23 |
+
break
|
| 24 |
+
|
| 25 |
+
# Run the YOLOv5 model on the frame to detect people
|
| 26 |
+
results = model(frame, size=640)
|
| 27 |
+
|
| 28 |
+
# Get the number of people detected in the frame
|
| 29 |
+
num_people_detected += len(results.xyxy[0])
|
| 30 |
+
|
| 31 |
+
# Release the video capture object
|
| 32 |
+
cap.release()
|
| 33 |
+
|
| 34 |
+
# Return the number of people detected
|
| 35 |
+
return num_people_detected
|
| 36 |
+
|
| 37 |
+
# Define the input and output interfaces for the Gradio app
|
| 38 |
+
inputs = gr.inputs.Video(label="Upload a video")
|
| 39 |
+
outputs = gr.outputs.Textbox(label="Number of people detected")
|
| 40 |
+
|
| 41 |
+
# Create the Gradio app
|
| 42 |
+
gr.Interface(detect_people, inputs, outputs, title="Object Detection App", description="Upload a video to detect the number of people in it.").launch()
|