File size: 2,163 Bytes
1ca9b28
 
cfbaa51
 
 
1ca9b28
 
4cdc77a
1ca9b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89a012c
1ca9b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89a012c
1ca9b28
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cv2
import numpy as np

from CPR_Module.Common.keypoints import CocoKeypoints
from CPR_Module.Common.logging_config import cpr_logger

class WristsMidpointAnalyzer:
    """Calculate and visualize midpoint between wrists for CPR rescuer"""
    
    def __init__(self, allowed_distance_between_wrists=170):
        self.allowed_distance_between_wrists = allowed_distance_between_wrists
        self.midpoint = None
        self.midpoint_history = []

    def detect_wrists_midpoint(self, rescuer_keypoints):
        """Calculate midpoint between wrists in pixel coordinates"""
        try:
            if rescuer_keypoints is None:
                return None
                
            # Get wrist coordinates
            lw = rescuer_keypoints[CocoKeypoints.LEFT_WRIST.value]
            rw = rescuer_keypoints[CocoKeypoints.RIGHT_WRIST.value]

            # If the distance between wrists is too large, return None
            distance = np.linalg.norm(np.array(lw) - np.array(rw))
            if distance > self.allowed_distance_between_wrists:
                return None
            
            # Calculate midpoint
            midpoint = (
                int((lw[0] + rw[0]) / 2),
                int((lw[1] + rw[1]) / 2)
            )
            
            return midpoint
            
        except Exception as e:
            cpr_logger.error(f"Midpoint tracking error: {e}")
            return None

    def draw_midpoint(self, frame):
        """Visualize the midpoint on frame"""
        
        if self.midpoint is None:
            return frame
                    
        try:
            # Draw visualization
            cv2.circle(frame, self.midpoint, 8, (0, 255, 0), -1)
            cv2.putText(
                frame, "MIDPOINT", 
                (self.midpoint[0] + 5, self.midpoint[1] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2
            )

            return frame
        except Exception as e:
            cpr_logger.error(f"Midpoint drawing error: {e}")
            return frame
    
    def reset_midpoint_history(self):
        """Reset midpoint history"""
        self.midpoint_history = []