drixo commited on
Commit
c0f17eb
Β·
verified Β·
1 Parent(s): 1eff95f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -45
app.py CHANGED
@@ -2,75 +2,70 @@ import cv2
2
  import mediapipe as mp
3
  import os
4
 
5
- # Initialize MediaPipe utilities
6
  mp_drawing = mp.solutions.drawing_utils
7
- mp_pose = mp.solutions.pose
8
 
9
  def process_image(image_path):
10
- """Run pose estimation on a static image."""
11
  image = cv2.imread(image_path)
12
  if image is None:
13
  print(f"❌ Could not read image: {image_path}")
14
  return
15
 
16
- with mp_pose.Pose(static_image_mode=True) as pose:
17
- results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
18
- if results.pose_landmarks:
19
- mp_drawing.draw_landmarks(
20
- image,
21
- results.pose_landmarks,
22
- mp_pose.POSE_CONNECTIONS,
23
- landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
24
- connection_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
25
- )
26
 
27
- cv2.imshow("🧍 Pose Estimation (Image)", image)
 
 
 
 
 
 
 
 
 
 
 
 
28
  cv2.waitKey(0)
29
  cv2.destroyAllWindows()
30
 
31
  def process_webcam():
32
- """Run pose estimation using webcam if available."""
33
  cap = cv2.VideoCapture(0)
34
  if not cap.isOpened():
35
  print("⚠️ Webcam not detected. Switching to image mode...")
36
  return False
37
 
38
- with mp_pose.Pose(
39
  static_image_mode=False,
40
- model_complexity=1,
41
- smooth_landmarks=True,
42
- enable_segmentation=True,
43
  min_detection_confidence=0.5,
44
  min_tracking_confidence=0.5
45
- ) as pose:
46
- print("πŸŽ₯ Webcam started. Press ESC to exit.")
47
  while True:
48
  ret, frame = cap.read()
49
  if not ret:
50
  print("❌ Failed to capture frame.")
51
  break
52
 
53
- # Convert to RGB for MediaPipe
54
- image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
55
- image.flags.writeable = False
56
- results = pose.process(image)
57
 
58
- # Convert back to BGR for display
59
- image.flags.writeable = True
60
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
 
 
 
 
 
61
 
62
- # Draw pose landmarks
63
- if results.pose_landmarks:
64
- mp_drawing.draw_landmarks(
65
- image,
66
- results.pose_landmarks,
67
- mp_pose.POSE_CONNECTIONS,
68
- landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
69
- connection_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
70
- )
71
-
72
- cv2.imshow("🧍 Pose Estimation (ESC to exit)", image)
73
- if cv2.waitKey(5) & 0xFF == 27: # ESC key
74
  break
75
 
76
  cap.release()
@@ -78,13 +73,12 @@ def process_webcam():
78
  return True
79
 
80
  if __name__ == "__main__":
81
- print("πŸš€ Starting MediaPipe Pose App...")
82
 
83
- # Try webcam first
84
  if not process_webcam():
85
- # If no webcam, use fallback image
86
- test_image = "pose_test.jpg"
87
  if not os.path.exists(test_image):
88
- print(f"⚠️ No '{test_image}' found. Please place a test image in this folder.")
89
  else:
90
  process_image(test_image)
 
 
2
  import mediapipe as mp
3
  import os
4
 
 
5
  mp_drawing = mp.solutions.drawing_utils
6
+ mp_hands = mp.solutions.hands
7
 
8
  def process_image(image_path):
9
+ """Run hand detection on a static image."""
10
  image = cv2.imread(image_path)
11
  if image is None:
12
  print(f"❌ Could not read image: {image_path}")
13
  return
14
 
15
+ with mp_hands.Hands(static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5) as hands:
16
+ results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
 
 
 
 
 
 
 
 
17
 
18
+ if not results.multi_hand_landmarks:
19
+ print("πŸ™Œ No hands detected in image.")
20
+ else:
21
+ for hand_landmarks in results.multi_hand_landmarks:
22
+ mp_drawing.draw_landmarks(
23
+ image,
24
+ hand_landmarks,
25
+ mp_hands.HAND_CONNECTIONS,
26
+ mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=3),
27
+ mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
28
+ )
29
+
30
+ cv2.imshow("🀚 Hand Recognition (Image Mode)", image)
31
  cv2.waitKey(0)
32
  cv2.destroyAllWindows()
33
 
34
  def process_webcam():
35
+ """Run hand detection using webcam if available."""
36
  cap = cv2.VideoCapture(0)
37
  if not cap.isOpened():
38
  print("⚠️ Webcam not detected. Switching to image mode...")
39
  return False
40
 
41
+ with mp_hands.Hands(
42
  static_image_mode=False,
43
+ max_num_hands=2,
 
 
44
  min_detection_confidence=0.5,
45
  min_tracking_confidence=0.5
46
+ ) as hands:
47
+ print("πŸŽ₯ Hand Recognition running. Press ESC to exit.")
48
  while True:
49
  ret, frame = cap.read()
50
  if not ret:
51
  print("❌ Failed to capture frame.")
52
  break
53
 
54
+ frame = cv2.flip(frame, 1) # Mirror view
55
+ results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
 
 
56
 
57
+ if results.multi_hand_landmarks:
58
+ for hand_landmarks in results.multi_hand_landmarks:
59
+ mp_drawing.draw_landmarks(
60
+ frame,
61
+ hand_landmarks,
62
+ mp_hands.HAND_CONNECTIONS,
63
+ mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=3),
64
+ mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
65
+ )
66
 
67
+ cv2.imshow("🀚 Hand Recognition (ESC to exit)", frame)
68
+ if cv2.waitKey(5) & 0xFF == 27: # ESC
 
 
 
 
 
 
 
 
 
 
69
  break
70
 
71
  cap.release()
 
73
  return True
74
 
75
  if __name__ == "__main__":
76
+ print("πŸš€ Starting MediaPipe Hand Recognition App...")
77
 
 
78
  if not process_webcam():
79
+ test_image = "hand_test.jpg"
 
80
  if not os.path.exists(test_image):
81
+ print(f"⚠️ No '{test_image}' found. Please place a hand image in this folder.")
82
  else:
83
  process_image(test_image)
84
+