in handmocap/hand_bbox_detector.py [0:0]
def detect_hand_bbox(self, img):
'''
output:
body_bbox: [min_x, min_y, width, height]
hand_bbox: [x0, y0, x1, y1]
Note:
len(body_bbox) == len(hand_bbox), where hand_bbox can be None if not valid
'''
# get body pose
body_pose_list, body_bbox_list = self.detect_body_pose(img)
# assert len(body_pose_list) == 1, "Current version only supports one person"
# get raw hand bboxes
raw_hand_bboxes = self.__get_raw_hand_bbox(img)
hand_bbox_list = [None, ] * len(body_pose_list)
num_bbox = raw_hand_bboxes.shape[0]
if num_bbox > 0:
for idx, body_pose in enumerate(body_pose_list):
# By default, we use distance to ankle to distinguish left/right,
# if ankle is unavailable, use elbow, then use shoulder.
# The joints used by two arms should exactly the same)
dist_left_arm = np.ones((num_bbox,)) * float('inf')
dist_right_arm = np.ones((num_bbox,)) * float('inf')
hand_bboxes = dict(
left_hand = None,
right_hand = None
)
# left arm
if body_pose[7][0]>0 and body_pose[6][0]>0:
# distance between elbow and ankle
dist_wrist_elbow = np.linalg.norm(body_pose[7]-body_pose[6])
for i in range(num_bbox):
bbox = raw_hand_bboxes[i]
c_x = (bbox[0]+bbox[2])/2
c_y = (bbox[1]+bbox[3])/2
center = np.array([c_x, c_y])
dist_bbox_ankle = np.linalg.norm(center - body_pose[7])
if dist_bbox_ankle < dist_wrist_elbow*1.5:
dist_left_arm[i] = np.linalg.norm(center - body_pose[7])
# right arm
if body_pose[4][0]>0 and body_pose[3][0]>0:
# distance between elbow and ankle
dist_wrist_elbow = np.linalg.norm(body_pose[3]-body_pose[4])
for i in range(num_bbox):
bbox = raw_hand_bboxes[i]
c_x = (bbox[0]+bbox[2])/2
c_y = (bbox[1]+bbox[3])/2
center = np.array([c_x, c_y])
dist_bbox_ankle = np.linalg.norm(center - body_pose[4])
if dist_bbox_ankle < dist_wrist_elbow*1.5:
dist_right_arm[i] = np.linalg.norm(center - body_pose[4])
# assign bboxes
# hand_bboxes = dict()
left_id = np.argmin(dist_left_arm)
right_id = np.argmin(dist_right_arm)
if dist_left_arm[left_id] < float('inf'):
hand_bboxes['left_hand'] = raw_hand_bboxes[left_id].copy()
if dist_right_arm[right_id] < float('inf'):
hand_bboxes['right_hand'] = raw_hand_bboxes[right_id].copy()
hand_bbox_list[idx] = hand_bboxes
assert len(body_bbox_list) == len(hand_bbox_list)
return body_pose_list, body_bbox_list, hand_bbox_list, raw_hand_bboxes