Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 8 |
|
| 9 |
def process_image(image_path):
|
| 10 |
-
"""Run
|
| 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
|
| 17 |
-
results =
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
cv2.waitKey(0)
|
| 29 |
cv2.destroyAllWindows()
|
| 30 |
|
| 31 |
def process_webcam():
|
| 32 |
-
"""Run
|
| 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
|
| 39 |
static_image_mode=False,
|
| 40 |
-
|
| 41 |
-
smooth_landmarks=True,
|
| 42 |
-
enable_segmentation=True,
|
| 43 |
min_detection_confidence=0.5,
|
| 44 |
min_tracking_confidence=0.5
|
| 45 |
-
) as
|
| 46 |
-
print("π₯
|
| 47 |
while True:
|
| 48 |
ret, frame = cap.read()
|
| 49 |
if not ret:
|
| 50 |
print("β Failed to capture frame.")
|
| 51 |
break
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
image.flags.writeable = False
|
| 56 |
-
results = pose.process(image)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if
|
| 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
|
| 82 |
|
| 83 |
-
# Try webcam first
|
| 84 |
if not process_webcam():
|
| 85 |
-
|
| 86 |
-
test_image = "pose_test.jpg"
|
| 87 |
if not os.path.exists(test_image):
|
| 88 |
-
print(f"β οΈ No '{test_image}' found. Please place a
|
| 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 |
+
|