(candidate, subset, oriImg)
| 153 | # detect hand according to body pose keypoints |
| 154 | # please refer to https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/hand/handDetector.cpp |
| 155 | def handDetect(candidate, subset, oriImg): |
| 156 | # right hand: wrist 4, elbow 3, shoulder 2 |
| 157 | # left hand: wrist 7, elbow 6, shoulder 5 |
| 158 | ratioWristElbow = 0.33 |
| 159 | detect_result = [] |
| 160 | image_height, image_width = oriImg.shape[0:2] |
| 161 | for person in subset.astype(int): |
| 162 | # if any of three not detected |
| 163 | has_left = np.sum(person[[5, 6, 7]] == -1) == 0 |
| 164 | has_right = np.sum(person[[2, 3, 4]] == -1) == 0 |
| 165 | if not (has_left or has_right): |
| 166 | continue |
| 167 | hands = [] |
| 168 | #left hand |
| 169 | if has_left: |
| 170 | left_shoulder_index, left_elbow_index, left_wrist_index = person[[5, 6, 7]] |
| 171 | x1, y1 = candidate[left_shoulder_index][:2] |
| 172 | x2, y2 = candidate[left_elbow_index][:2] |
| 173 | x3, y3 = candidate[left_wrist_index][:2] |
| 174 | hands.append([x1, y1, x2, y2, x3, y3, True]) |
| 175 | # right hand |
| 176 | if has_right: |
| 177 | right_shoulder_index, right_elbow_index, right_wrist_index = person[[2, 3, 4]] |
| 178 | x1, y1 = candidate[right_shoulder_index][:2] |
| 179 | x2, y2 = candidate[right_elbow_index][:2] |
| 180 | x3, y3 = candidate[right_wrist_index][:2] |
| 181 | hands.append([x1, y1, x2, y2, x3, y3, False]) |
| 182 | |
| 183 | for x1, y1, x2, y2, x3, y3, is_left in hands: |
| 184 | # pos_hand = pos_wrist + ratio * (pos_wrist - pos_elbox) = (1 + ratio) * pos_wrist - ratio * pos_elbox |
| 185 | # handRectangle.x = posePtr[wrist*3] + ratioWristElbow * (posePtr[wrist*3] - posePtr[elbow*3]); |
| 186 | # handRectangle.y = posePtr[wrist*3+1] + ratioWristElbow * (posePtr[wrist*3+1] - posePtr[elbow*3+1]); |
| 187 | # const auto distanceWristElbow = getDistance(poseKeypoints, person, wrist, elbow); |
| 188 | # const auto distanceElbowShoulder = getDistance(poseKeypoints, person, elbow, shoulder); |
| 189 | # handRectangle.width = 1.5f * fastMax(distanceWristElbow, 0.9f * distanceElbowShoulder); |
| 190 | x = x3 + ratioWristElbow * (x3 - x2) |
| 191 | y = y3 + ratioWristElbow * (y3 - y2) |
| 192 | distanceWristElbow = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2) |
| 193 | distanceElbowShoulder = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) |
| 194 | width = 1.5 * max(distanceWristElbow, 0.9 * distanceElbowShoulder) |
| 195 | # x-y refers to the center --> offset to topLeft point |
| 196 | # handRectangle.x -= handRectangle.width / 2.f; |
| 197 | # handRectangle.y -= handRectangle.height / 2.f; |
| 198 | x -= width / 2 |
| 199 | y -= width / 2 # width = height |
| 200 | # overflow the image |
| 201 | if x < 0: x = 0 |
| 202 | if y < 0: y = 0 |
| 203 | width1 = width |
| 204 | width2 = width |
| 205 | if x + width > image_width: width1 = image_width - x |
| 206 | if y + width > image_height: width2 = image_height - y |
| 207 | width = min(width1, width2) |
| 208 | # the max hand box value is 20 pixels |
| 209 | if width >= 20: |
| 210 | detect_result.append([int(x), int(y), int(width), is_left]) |
| 211 | |
| 212 | ''' |
nothing calls this directly
no outgoing calls
no test coverage detected