wesam0099 commited on
Commit
f300e59
·
verified ·
1 Parent(s): 21c1342

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -27
app.py CHANGED
@@ -1,38 +1,77 @@
1
- import cv2
2
- import streamlit as st
3
- from cvzone.PoseModule import PoseDetector
4
-
5
- st.title("Real-Time Pose Detection")
6
 
7
- detector = PoseDetector()
 
 
 
 
8
 
9
- run = st.checkbox('Run Webcam')
 
 
10
 
11
- if run:
12
- cap = cv2.VideoCapture(0)
13
- stframe = st.empty()
14
 
15
- while True:
 
 
 
 
 
 
 
 
 
 
 
 
16
  ret, frame = cap.read()
17
  if not ret:
18
- st.error("Failed to capture video")
19
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- frame = cv2.flip(frame, 1)
22
- frame = detector.findPose(frame)
23
- lmList, bboxInfo = detector.findPosition(frame, draw=False)
 
 
 
24
 
25
- if lmList:
26
- RBack_angle, frame = detector.findAngle(lmList[11][0:2],
27
- lmList[23][0:2],
28
- lmList[25][0:2],
29
- img=frame,
30
- color=(0, 0, 255),
31
- scale=1)
32
 
33
- stframe.image(frame, channels='BGR')
 
34
 
35
- if st.button('Stop'):
36
- break
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- cap.release()
 
 
1
+ #!/usr/bin/env python
 
 
 
 
2
 
3
+ from __future__ import annotations
4
+ import cv2
5
+ import gradio as gr
6
+ import mediapipe as mp
7
+ import numpy as np
8
 
9
+ mp_drawing = mp.solutions.drawing_utils
10
+ mp_drawing_styles = mp.solutions.drawing_styles
11
+ mp_pose = mp.solutions.pose
12
 
13
+ TITLE = "Real-Time Pose Detection"
14
+ DESCRIPTION = "Pose detection using webcam input."
 
15
 
16
+ def run_webcam(
17
+ model_complexity: int,
18
+ enable_segmentation: bool,
19
+ min_detection_confidence: float,
20
+ background_color: str,
21
+ ) -> np.ndarray:
22
+ cap = cv2.VideoCapture(0) # Open the webcam
23
+ with mp_pose.Pose(
24
+ static_image_mode=False,
25
+ model_complexity=model_complexity,
26
+ enable_segmentation=enable_segmentation,
27
+ min_detection_confidence=min_detection_confidence,
28
+ ) as pose:
29
  ret, frame = cap.read()
30
  if not ret:
31
+ return None # Return None if the webcam fails to capture
32
+
33
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
+ results = pose.process(frame_rgb)
35
+
36
+ res = frame_rgb.copy()
37
+ if enable_segmentation and results.segmentation_mask is not None:
38
+ if background_color == "white":
39
+ bg_color = (255, 255, 255)
40
+ elif background_color == "black":
41
+ bg_color = (0, 0, 0)
42
+ elif background_color == "green":
43
+ bg_color = (0, 255, 0)
44
+ else:
45
+ raise ValueError("Unsupported background color")
46
+
47
+ res[results.segmentation_mask <= 0.1] = bg_color
48
 
49
+ mp_drawing.draw_landmarks(
50
+ res,
51
+ results.pose_landmarks,
52
+ mp_pose.POSE_CONNECTIONS,
53
+ landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style(),
54
+ )
55
 
56
+ cap.release()
57
+ return cv2.cvtColor(res, cv2.COLOR_RGB2BGR) # Convert back to BGR for display
 
 
 
 
 
58
 
59
+ model_complexities = list(range(3))
60
+ background_colors = ["white", "black", "green"]
61
 
62
+ demo = gr.Interface(
63
+ fn=run_webcam,
64
+ inputs=[
65
+ gr.Radio(label="Model Complexity", choices=model_complexities, type="index", value=model_complexities[1]),
66
+ gr.Checkbox(label="Enable Segmentation", value=True),
67
+ gr.Slider(label="Minimum Detection Confidence", minimum=0, maximum=1, step=0.05, value=0.5),
68
+ gr.Radio(label="Background Color", choices=background_colors, type="value", value=background_colors[0]),
69
+ ],
70
+ outputs=gr.Image(label="Output"),
71
+ title=TITLE,
72
+ description=DESCRIPTION,
73
+ live=True # Enable live updates for webcam
74
+ )
75
 
76
+ if __name__ == "__main__":
77
+ demo.launch()