mkfallah commited on
Commit
eb738c6
·
verified ·
1 Parent(s): 6e8f7ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -63
app.py CHANGED
@@ -1,81 +1,63 @@
1
- import gradio as gr
2
  import cv2
3
  import mediapipe as mp
 
4
  import tempfile
 
5
 
6
- # Initialize MediaPipe once
7
- mp_hands = mp.solutions.hands
8
- mp_pose = mp.solutions.pose
9
  mp_drawing = mp.solutions.drawing_utils
 
10
 
11
- hands = mp_hands.Hands(
12
- static_image_mode=False,
13
- max_num_hands=2,
14
- min_detection_confidence=0.5,
15
- min_tracking_confidence=0.5
16
- )
17
-
18
- pose = mp_pose.Pose(
19
- static_image_mode=False,
20
- model_complexity=1,
21
- enable_segmentation=False,
22
- min_detection_confidence=0.5,
23
- min_tracking_confidence=0.5
24
- )
25
 
26
- def process_video(video_path, target_width=640):
27
- cap = cv2.VideoCapture(video_path)
28
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
29
- temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
30
- out = None
31
 
32
- while cap.isOpened():
33
- ret, frame = cap.read()
34
- if not ret:
35
- break
 
 
 
36
 
37
- h0, w0 = frame.shape[:2]
38
- # Resize frame keeping aspect ratio
39
- scale = target_width / float(w0)
40
- target_height = int(round(h0 * scale))
41
- frame_small = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_AREA)
42
 
43
- # Convert BGR to RGB
44
- rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
45
 
46
- # Hand detection
47
- hand_results = hands.process(rgb_small)
48
- if hand_results.multi_hand_landmarks:
49
- for hand_landmarks in hand_results.multi_hand_landmarks:
50
- mp_drawing.draw_landmarks(frame_small, hand_landmarks, mp_hands.HAND_CONNECTIONS)
51
 
52
- # Pose detection
53
- pose_results = pose.process(rgb_small)
54
- if pose_results.pose_landmarks:
55
- mp_drawing.draw_landmarks(frame_small, pose_results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
 
56
 
57
- # Initialize video writer
58
- if out is None:
59
- out = cv2.VideoWriter(temp_output.name, fourcc, cap.get(cv2.CAP_PROP_FPS), (frame_small.shape[1], frame_small.shape[0]))
60
 
61
- out.write(frame_small)
62
 
63
- cap.release()
64
- if out:
65
- out.release()
66
 
67
- return temp_output.name
 
 
 
 
 
 
 
 
68
 
69
- iface = gr.Interface(
70
- fn=process_video,
71
- inputs=[
72
- gr.Video(label="Upload a video"),
73
- gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing width")
74
- ],
75
- outputs=gr.Video(label="Processed video"),
76
- title="Hand & Body Pose Detection from Video",
77
- description="Upload a video and see MediaPipe detect hands and body pose."
78
- )
79
 
80
- if __name__ == "__main__":
81
- iface.launch()
 
 
 
1
  import cv2
2
  import mediapipe as mp
3
+ import streamlit as st
4
  import tempfile
5
+ import os
6
 
7
+ # mediapipe pose setup
 
 
8
  mp_drawing = mp.solutions.drawing_utils
9
+ mp_pose = mp.solutions.pose
10
 
11
+ st.title("MediaPipe Body Pose Detection")
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # select input source
14
+ option = st.radio("choose input source", ("webcam", "upload video"))
 
 
 
15
 
16
+ video_path = None
17
+ if option == "upload video":
18
+ uploaded_file = st.file_uploader("upload a video file", type=["mp4", "mov", "avi"])
19
+ if uploaded_file:
20
+ temp_file = tempfile.NamedTemporaryFile(delete=False)
21
+ temp_file.write(uploaded_file.read())
22
+ video_path = temp_file.name
23
 
24
+ elif option == "webcam":
25
+ video_path = 0 # 0 for default webcam
 
 
 
26
 
27
+ if video_path is not None:
28
+ stframe = st.empty()
29
 
30
+ cap = cv2.VideoCapture(video_path)
 
 
 
 
31
 
32
+ with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
33
+ while cap.isOpened():
34
+ ret, frame = cap.read()
35
+ if not ret:
36
+ break
37
 
38
+ # convert to rgb for processing
39
+ image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
40
+ image.flags.writeable = False
41
 
42
+ results = pose.process(image)
43
 
44
+ # convert back to BGR for display
45
+ image.flags.writeable = True
46
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
47
 
48
+ # draw landmarks and connections if detected
49
+ if results.pose_landmarks:
50
+ mp_drawing.draw_landmarks(
51
+ image,
52
+ results.pose_landmarks,
53
+ mp_pose.POSE_CONNECTIONS,
54
+ mp_drawing.DrawingSpec(color=(0,0,255), thickness=2, circle_radius=2), # landmarks
55
+ mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2) # connections
56
+ )
57
 
58
+ # display frame in streamlit
59
+ stframe.image(image, channels="BGR")
 
 
 
 
 
 
 
 
60
 
61
+ cap.release()
62
+ if option == "upload video":
63
+ os.remove(video_path)