Spaces:
Sleeping
Sleeping
Added file
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from ultralytics import YOLO
|
5 |
+
|
6 |
+
# Load the YOLO model
|
7 |
+
model = YOLO('yolov5s.pt') # You can use a different model if needed
|
8 |
+
|
9 |
+
def count_people(video_file):
|
10 |
+
count = 0
|
11 |
+
cap = cv2.VideoCapture(video_file)
|
12 |
+
|
13 |
+
while cap.isOpened():
|
14 |
+
ret, frame = cap.read()
|
15 |
+
if not ret:
|
16 |
+
break
|
17 |
+
|
18 |
+
results = model(frame)
|
19 |
+
detections = results.pred[0] # Get predictions
|
20 |
+
|
21 |
+
# Count people detected (class ID for person is usually 0)
|
22 |
+
for det in detections:
|
23 |
+
if det[5] == 0: # Check if class ID is 0 (person)
|
24 |
+
count += 1
|
25 |
+
|
26 |
+
cap.release()
|
27 |
+
return count
|
28 |
+
|
29 |
+
# Streamlit app layout
|
30 |
+
st.title("Person Detection in Video")
|
31 |
+
st.write("Upload a video file to count the number of times a person appears.")
|
32 |
+
|
33 |
+
# File uploader for video files
|
34 |
+
video_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
|
35 |
+
|
36 |
+
if video_file is not None:
|
37 |
+
# Save the uploaded video to a temporary location
|
38 |
+
with open("temp_video.mp4", "wb") as f:
|
39 |
+
f.write(video_file.getbuffer())
|
40 |
+
|
41 |
+
st.video(video_file) # Display the video
|
42 |
+
|
43 |
+
if st.button("Count People"):
|
44 |
+
with st.spinner("Counting..."):
|
45 |
+
count = count_people("temp_video.mp4")
|
46 |
+
st.success(f"Total number of people detected: {count}")
|