File size: 7,642 Bytes
3308688
 
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
 
 
 
3308688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
1d608cf
 
 
 
 
 
3308688
 
 
 
 
 
 
 
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
3308688
 
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
 
 
3308688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbfc5d
3308688
 
 
 
 
adbfc5d
 
 
 
 
 
 
3308688
 
 
 
 
 
 
 
 
 
 
 
 
adbfc5d
3308688
 
adbfc5d
 
 
 
3308688
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
 
 
3308688
adbfc5d
3308688
adbfc5d
 
 
 
 
 
 
3308688
 
 
 
 
 
 
 
adbfc5d
 
 
 
 
 
 
 
 
 
 
3308688
 
adbfc5d
3308688
adbfc5d
 
 
 
 
 
 
 
 
 
 
 
3308688
 
adbfc5d
 
 
 
3308688
 
adbfc5d
 
 
 
3308688
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import math

import cv2
import numpy as np

eps = 0.01


def draw_bodypose(canvas, candidate, subset, score=None, conf_threshold=0.3):
    """Draw body pose with optional confidence filtering

    Args:
        canvas: canvas to draw on
        candidate: pose candidate
        subset: pose subset
        score: confidence scores (optional)
        conf_threshold: confidence threshold for filtering (default: 0.3)
    """
    H, W, C = canvas.shape
    candidate = np.array(candidate)
    subset = np.array(subset)

    stickwidth = 4

    limbSeq = [
        [2, 3],
        [2, 6],
        [3, 4],
        [4, 5],
        [6, 7],
        [7, 8],
        [2, 9],
        [9, 10],
        [10, 11],
        [2, 12],
        [12, 13],
        [13, 14],
        [2, 1],
        [1, 15],
        [15, 17],
        [1, 16],
        [16, 18],
        [3, 17],
        [6, 18],
    ]

    colors = [
        [255, 0, 0],
        [255, 85, 0],
        [255, 170, 0],
        [255, 255, 0],
        [170, 255, 0],
        [85, 255, 0],
        [0, 255, 0],
        [0, 255, 85],
        [0, 255, 170],
        [0, 255, 255],
        [0, 170, 255],
        [0, 85, 255],
        [0, 0, 255],
        [85, 0, 255],
        [170, 0, 255],
        [255, 0, 255],
        [255, 0, 170],
        [255, 0, 85],
    ]

    for i in range(17):
        for n in range(len(subset)):
            index = subset[n][np.array(limbSeq[i]) - 1]
            if -1 in index:
                continue

            # Add confidence filtering (like ControlNeXt)
            if score is not None:
                conf = score[n][np.array(limbSeq[i]) - 1]
                if conf[0] < conf_threshold or conf[1] < conf_threshold:
                    continue

            coords = candidate[index.astype(int)]
            if np.any(coords <= eps):
                continue

            Y = coords[:, 0] * float(W)
            X = coords[:, 1] * float(H)
            mX = np.mean(X)
            mY = np.mean(Y)
            length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5
            angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
            polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)
            cv2.fillConvexPoly(canvas, polygon, colors[i])

    canvas = (canvas * 0.6).astype(np.uint8)

    for i in range(18):
        for n in range(len(subset)):
            index = int(subset[n][i])
            if index == -1:
                continue

            # Add confidence filtering for keypoints
            if score is not None:
                conf = score[n][i]
                if conf < conf_threshold:
                    continue

            x, y = candidate[index][0:2]
            x = int(x * W)
            y = int(y * H)
            cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1)

    return canvas


def draw_handpose(canvas, all_hand_peaks, all_hand_scores=None, conf_threshold=0.3):
    """Draw hand pose with optional confidence filtering

    Args:
        canvas: canvas to draw on
        all_hand_peaks: hand keypoints
        all_hand_scores: confidence scores (optional)
        conf_threshold: confidence threshold for filtering (default: 0.3)
    """
    import matplotlib

    H, W, C = canvas.shape

    edges = [
        [0, 1],
        [1, 2],
        [2, 3],
        [3, 4],
        [0, 5],
        [5, 6],
        [6, 7],
        [7, 8],
        [0, 9],
        [9, 10],
        [10, 11],
        [11, 12],
        [0, 13],
        [13, 14],
        [14, 15],
        [15, 16],
        [0, 17],
        [17, 18],
        [18, 19],
        [19, 20],
    ]

    # (person_number*2, 21, 2)
    for i in range(len(all_hand_peaks)):
        peaks = all_hand_peaks[i]
        peaks = np.array(peaks)
        scores = all_hand_scores[i] if all_hand_scores is not None else None

        for ie, e in enumerate(edges):
            x1, y1 = peaks[e[0]]
            x2, y2 = peaks[e[1]]

            # Add confidence filtering
            if scores is not None:
                score1 = scores[e[0]]
                score2 = scores[e[1]]
                if score1 < conf_threshold or score2 < conf_threshold:
                    continue

            x1 = int(x1 * W)
            y1 = int(y1 * H)
            x2 = int(x2 * W)
            y2 = int(y2 * H)
            if x1 > eps and y1 > eps and x2 > eps and y2 > eps:
                cv2.line(
                    canvas,
                    (x1, y1),
                    (x2, y2),
                    matplotlib.colors.hsv_to_rgb([ie / float(len(edges)), 1.0, 1.0]) * 255,
                    thickness=2,
                )

        for idx, keyponit in enumerate(peaks):
            x, y = keyponit

            # Add confidence filtering for keypoints
            if scores is not None and scores[idx] < conf_threshold:
                continue

            x = int(x * W)
            y = int(y * H)
            if x > eps and y > eps:
                cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1)
    return canvas


def draw_facepose(canvas, all_lmks, all_scores=None, conf_threshold=0.3):
    """Draw face pose with optional confidence filtering

    Args:
        canvas: canvas to draw on
        all_lmks: face landmarks
        all_scores: confidence scores (optional)
        conf_threshold: confidence threshold for filtering (default: 0.3)
    """
    H, W, C = canvas.shape
    for i, lmks in enumerate(all_lmks):
        lmks = np.array(lmks)
        scores = all_scores[i] if all_scores is not None else None

        for idx, lmk in enumerate(lmks):
            # Add confidence filtering
            if scores is not None and scores[idx] < conf_threshold:
                continue

            x, y = lmk
            x = int(x * W)
            y = int(y * H)
            if x > eps and y > eps:
                cv2.circle(canvas, (x, y), 3, (255, 255, 255), thickness=-1)
    return canvas


def draw_pose(pose, height: int, width: int, include_face: bool = True, include_hands: bool = True, conf_threshold: float = 0.3) -> np.ndarray:
    """Draw pose with confidence filtering

    Args:
        pose: pose data dictionary
        height: canvas height
        width: canvas width
        include_face: whether to draw face
        include_hands: whether to draw hands
        conf_threshold: confidence threshold for filtering (default: 0.3)
    """
    canvas = np.zeros(shape=(height, width, 3), dtype=np.uint8)

    # Handle bodies
    candidate = pose["bodies"]
    # For openpose format, subset is typically derived from body_scores
    # Create a simple subset array for single person (18 keypoints)
    body_scores_data = pose.get("body_scores", None)

    if body_scores_data is not None:
        # Create subset: reshape scores to be compatible
        subset = body_scores_data.reshape(1, -1) if len(body_scores_data.shape) == 1 else body_scores_data
    else:
        # No scores available, create dummy subset
        subset = np.zeros((1, 18))

    canvas = draw_bodypose(canvas, candidate, subset, score=subset, conf_threshold=conf_threshold)

    if include_face:
        faces = pose.get("faces", [])
        face_scores = pose.get("faces_scores", None)
        if len(faces) > 0:
            canvas = draw_facepose(canvas, faces, all_scores=face_scores, conf_threshold=conf_threshold)

    if include_hands:
        hands = pose.get("hands", [])
        hand_scores = pose.get("hands_scores", None)
        if len(hands) > 0:
            canvas = draw_handpose(canvas, hands, all_hand_scores=hand_scores, conf_threshold=conf_threshold)

    return canvas