File size: 1,228 Bytes
40b2cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def run(image: np.ndarray, model_complexity: int, enable_segmentation: bool,
        min_detection_confidence: float, background_color: str) -> np.ndarray:
    with mp_pose.Pose(
        static_image_mode=True,
        model_complexity=model_complexity,
        enable_segmentation=enable_segmentation,
        min_detection_confidence=min_detection_confidence,
    ) as pose:
        results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    res = image.copy()
    if enable_segmentation and results.segmentation_mask is not None:
        mask = results.segmentation_mask
        if background_color == 'white':
            bg_color = (255, 255, 255)
        elif background_color == 'black':
            bg_color = (0, 0, 0)
        elif background_color == 'green':
            bg_color = (0, 255, 0)
        else:
            raise ValueError("Invalid background color")

        res[mask <= 0.1] = bg_color

    if results.pose_landmarks:
        mp_drawing.draw_landmarks(
            res, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
            landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style()
        )

    return res

# Adapt your existing pose classification code as needed.