Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import mediapipe as mp
|
4 |
-
import numpy as np
|
5 |
|
6 |
-
# Initialize MediaPipe
|
7 |
-
|
|
|
8 |
|
9 |
-
# Function to process the video and count unique people
|
10 |
def count_people(video_file):
|
11 |
-
cap = cv2.VideoCapture(video_file)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
cap.release()
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
# Create the Gradio interface
|
21 |
iface = gr.Interface(
|
22 |
fn=count_people,
|
23 |
inputs=gr.Video(label="Upload Video"),
|
24 |
-
outputs=gr.Textbox(label="Number of
|
25 |
-
title="
|
26 |
-
description="Upload a video to count the number of
|
27 |
)
|
28 |
|
29 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import mediapipe as mp
|
|
|
4 |
|
5 |
+
# Initialize MediaPipe Pose model
|
6 |
+
mp_pose = mp.solutions.pose
|
7 |
+
pose = mp_pose.Pose()
|
8 |
|
|
|
9 |
def count_people(video_file):
|
10 |
+
cap = cv2.VideoCapture(video_file)
|
11 |
|
12 |
+
people_count = 0
|
13 |
+
frame_count = 0
|
14 |
+
|
15 |
+
while cap.isOpened():
|
16 |
+
ret, frame = cap.read()
|
17 |
+
if not ret:
|
18 |
+
break
|
19 |
+
|
20 |
+
# Convert the BGR image to RGB before processing
|
21 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
22 |
+
|
23 |
+
# Process the frame with MediaPipe Pose
|
24 |
+
results = pose.process(rgb_frame)
|
25 |
+
|
26 |
+
# Check if any pose is detected
|
27 |
+
if results.pose_landmarks:
|
28 |
+
people_count += 1
|
29 |
+
|
30 |
+
frame_count += 1
|
31 |
|
32 |
cap.release()
|
33 |
+
|
34 |
+
# For simplicity, return the count of frames where people were detected
|
35 |
+
return people_count
|
36 |
+
|
37 |
# Create the Gradio interface
|
38 |
iface = gr.Interface(
|
39 |
fn=count_people,
|
40 |
inputs=gr.Video(label="Upload Video"),
|
41 |
+
outputs=gr.Textbox(label="Number of People Detected"),
|
42 |
+
title="People Counter",
|
43 |
+
description="Upload a video to count the number of people present."
|
44 |
)
|
45 |
|
46 |
if __name__ == "__main__":
|