Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,20 @@
|
|
| 1 |
import cv2
|
| 2 |
import mediapipe as mp
|
|
|
|
| 3 |
|
| 4 |
-
# Initialize MediaPipe
|
| 5 |
mp_drawing = mp.solutions.drawing_utils
|
| 6 |
mp_pose = mp.solutions.pose
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
min_tracking_confidence=0.5
|
| 18 |
-
) as pose:
|
| 19 |
-
while cap.isOpened():
|
| 20 |
-
ret, frame = cap.read()
|
| 21 |
-
if not ret:
|
| 22 |
-
print("❌ Failed to capture frame")
|
| 23 |
-
break
|
| 24 |
-
|
| 25 |
-
# Convert to RGB for MediaPipe
|
| 26 |
-
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
-
image.flags.writeable = False
|
| 28 |
-
results = pose.process(image)
|
| 29 |
-
|
| 30 |
-
# Convert back to BGR for OpenCV display
|
| 31 |
-
image.flags.writeable = True
|
| 32 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 33 |
-
|
| 34 |
-
# Draw pose landmarks
|
| 35 |
if results.pose_landmarks:
|
| 36 |
mp_drawing.draw_landmarks(
|
| 37 |
image,
|
|
@@ -41,11 +24,67 @@ with mp_pose.Pose(
|
|
| 41 |
connection_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
cv2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
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,
|
|
|
|
| 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()
|
| 77 |
+
cv2.destroyAllWindows()
|
| 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)
|