Spaces:
Sleeping
Sleeping
Update main_code_script.py
Browse files- main_code_script.py +28 -27
main_code_script.py
CHANGED
|
@@ -13,39 +13,40 @@ import torch
|
|
| 13 |
# --- 1. Pose Estimation (using Mediapipe) ---
|
| 14 |
def estimate_pose(image_path):
|
| 15 |
"""Detects the pose of a person in an image using Mediapipe.
|
| 16 |
-
|
| 17 |
Args:
|
| 18 |
image_path: Path to the input image.
|
| 19 |
-
|
| 20 |
Returns:
|
| 21 |
-
A list of landmarks (x, y, visibility)
|
|
|
|
| 22 |
"""
|
| 23 |
mp_drawing = mp.solutions.drawing_utils
|
| 24 |
mp_pose = mp.solutions.pose
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
# --- 2. Clothing Segmentation (Example - using a placeholder function) ---
|
| 50 |
def segment_clothing(image, results): #Added result
|
| 51 |
"""Segments the clothing region in the image.
|
|
|
|
| 13 |
# --- 1. Pose Estimation (using Mediapipe) ---
|
| 14 |
def estimate_pose(image_path):
|
| 15 |
"""Detects the pose of a person in an image using Mediapipe.
|
|
|
|
| 16 |
Args:
|
| 17 |
image_path: Path to the input image.
|
|
|
|
| 18 |
Returns:
|
| 19 |
+
A list of landmarks (x, y, visibility)
|
| 20 |
+
or None if no pose is detected.
|
| 21 |
"""
|
| 22 |
mp_drawing = mp.solutions.drawing_utils
|
| 23 |
mp_pose = mp.solutions.pose
|
| 24 |
+
try:
|
| 25 |
+
with mp_pose.Pose(
|
| 26 |
+
static_image_mode=True,
|
| 27 |
+
model_complexity=2,
|
| 28 |
+
enable_segmentation=True,
|
| 29 |
+
min_detection_confidence=0.5) as pose:
|
| 30 |
+
image = cv2.imread(image_path)
|
| 31 |
+
if image is None:
|
| 32 |
+
raise FileNotFoundError(f"Could not open image: {image_path}")
|
| 33 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 34 |
+
results = pose.process(image_rgb)
|
| 35 |
+
|
| 36 |
+
if results.pose_landmarks:
|
| 37 |
+
# Example: Draw the pose landmarks on the image (for visualization)
|
| 38 |
+
annotated_image = image.copy()
|
| 39 |
+
mp_drawing.draw_landmarks(
|
| 40 |
+
annotated_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 41 |
+
#cv2.imwrite("pose_annotated.jpg", annotated_image) # Save annotated image
|
| 42 |
+
#return results.pose_landmarks.landmark
|
| 43 |
+
# Return the landmarks
|
| 44 |
+
return results, image # Return the entire result
|
| 45 |
+
else:
|
| 46 |
+
raise ValueError("No pose detected in the image.")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise RuntimeError(f"Error processing image: {e}")
|
| 49 |
+
|
| 50 |
# --- 2. Clothing Segmentation (Example - using a placeholder function) ---
|
| 51 |
def segment_clothing(image, results): #Added result
|
| 52 |
"""Segments the clothing region in the image.
|