Beispiel #1
0
def get_root_relative_poses(inference_results):
    paf_map, heatmap = inference_results

    upsample_ratio = 4
    found_poses = extract_poses(heatmap[0:-1], paf_map, upsample_ratio)[0]

    poses_2d = []
    num_kpt_panoptic = 19
    num_kpt = 18
    for pose_id in range(found_poses.shape[0]):
        if found_poses[pose_id, 3] == -1:  # skip pose if does not found neck
            continue
        pose_2d = np.ones(num_kpt_panoptic * 3 + 1,
                          dtype=np.float32) * -1  # +1 for pose confidence
        for kpt_id in range(num_kpt):
            if found_poses[pose_id, kpt_id * 3] != -1:
                x_2d, y_2d = found_poses[pose_id, kpt_id * 3:kpt_id * 3 + 2]
                conf = found_poses[pose_id, kpt_id * 3 + 2]
                pose_2d[map_id_to_panoptic[kpt_id] *
                        3] = x_2d  # just repacking
                pose_2d[map_id_to_panoptic[kpt_id] * 3 + 1] = y_2d
                pose_2d[map_id_to_panoptic[kpt_id] * 3 + 2] = conf
        pose_2d[-1] = found_poses[pose_id, -1]
        poses_2d.append(pose_2d)

    return np.array(poses_2d)
def get_root_relative_poses(inference_results, upsample_ratio, threshold):
    heatmap, paf_map = inference_results

    found_poses = extract_poses(heatmap[0:-1], paf_map, upsample_ratio)
    # scale coordinates to features space
    found_poses[:, 0:-1:3] /= upsample_ratio
    found_poses[:, 1:-1:3] /= upsample_ratio

    poses_2d = []
    num_kpt = 18

    for pose_id in range(found_poses.shape[0]):
        if found_poses[pose_id, 5] == -1:  # skip pose if does not found neck
            continue
        pose_2d = np.ones(num_kpt * 3 + 1,
                          dtype=np.float32) * -1  # +1 for pose confidence
        for kpt_id in range(num_kpt):
            if found_poses[pose_id, kpt_id * 3] != -1:
                x_2d, y_2d, conf = found_poses[pose_id,
                                               kpt_id * 3:(kpt_id + 1) * 3]
                pose_2d[kpt_id * 3] = x_2d  # just repacking
                pose_2d[kpt_id * 3 + 1] = y_2d
                pose_2d[kpt_id * 3 + 2] = conf
        pose_2d[-1] = found_poses[pose_id, -1]  # Global confidence
        poses_2d.append(pose_2d)
    poses_2d = np.array(poses_2d)

    return poses_2d
Beispiel #3
0
def get_root_relative_poses(inference_results):
    features, heatmap, paf_map = inference_results

    upsample_ratio = 4
    found_poses = extract_poses(heatmap[0:-1], paf_map, upsample_ratio)
    # scale coordinates to features space
    found_poses[:, 0:-1:3] /= upsample_ratio
    found_poses[:, 1:-1:3] /= upsample_ratio

    poses_2d = []
    num_kpt_panoptic = 19
    num_kpt = 18
    for pose_id in range(found_poses.shape[0]):
        if found_poses[pose_id, 5] == -1:  # skip pose if does not found neck
            continue
        pose_2d = np.ones(num_kpt_panoptic * 3 + 1, dtype=np.float32) * -1  # +1 for pose confidence
        for kpt_id in range(num_kpt):
            if found_poses[pose_id, kpt_id * 3] != -1:
                x_2d, y_2d, conf = found_poses[pose_id, kpt_id * 3:(kpt_id + 1) * 3]
                pose_2d[map_id_to_panoptic[kpt_id] * 3] = x_2d  # just repacking
                pose_2d[map_id_to_panoptic[kpt_id] * 3 + 1] = y_2d
                pose_2d[map_id_to_panoptic[kpt_id] * 3 + 2] = conf
        pose_2d[-1] = found_poses[pose_id, -1]
        poses_2d.append(pose_2d)
    poses_2d = np.array(poses_2d)

    keypoint_threshold = 0.1
    poses_3d = np.ones((len(poses_2d), num_kpt_panoptic * 4), dtype=np.float32) * -1
    for pose_id in range(poses_3d.shape[0]):
        if poses_2d[pose_id, 2] <= keypoint_threshold:
            continue
        pose_3d = poses_3d[pose_id]
        neck_2d = poses_2d[pose_id, 0:2].astype(np.int32)
        # read all pose coordinates at neck location
        for kpt_id in range(num_kpt_panoptic):
            map_3d = features[kpt_id * 3:(kpt_id + 1) * 3]
            pose_3d[kpt_id * 4] = map_3d[0, neck_2d[1], neck_2d[0]]
            pose_3d[kpt_id * 4 + 1] = map_3d[1, neck_2d[1], neck_2d[0]]
            pose_3d[kpt_id * 4 + 2] = map_3d[2, neck_2d[1], neck_2d[0]]
            pose_3d[kpt_id * 4 + 3] = poses_2d[pose_id, kpt_id * 3 + 2]

        # refine keypoints coordinates at corresponding limbs locations
        for limb in limbs:
            for kpt_id_from in limb:
                if poses_2d[pose_id, kpt_id_from * 3 + 2] <= keypoint_threshold:
                    continue
                for kpt_id_where in limb:
                    kpt_from_2d = poses_2d[pose_id, kpt_id_from * 3:kpt_id_from * 3 + 2].astype(np.int32)
                    map_3d = features[kpt_id_where * 3:(kpt_id_where + 1) * 3]
                    pose_3d[kpt_id_where * 4] = map_3d[0, kpt_from_2d[1], kpt_from_2d[0]]
                    pose_3d[kpt_id_where * 4 + 1] = map_3d[1, kpt_from_2d[1], kpt_from_2d[0]]
                    pose_3d[kpt_id_where * 4 + 2] = map_3d[2, kpt_from_2d[1], kpt_from_2d[0]]
                break

    poses_3d[:, 0::4] *= AVG_PERSON_HEIGHT
    poses_3d[:, 1::4] *= AVG_PERSON_HEIGHT
    poses_3d[:, 2::4] *= AVG_PERSON_HEIGHT
    return poses_3d, poses_2d
 def run(self, ocvimg):
     global human_pose_available
     if human_pose_available == False:
         return None
     infres = self.inference(ocvimg)
     PAFs = infres['Mconv7_stage2_L1'][0]
     HMs = infres['Mconv7_stage2_L2'][0]
     people = extract_poses(HMs[:-1], PAFs,
                            4)  # Construct poses from HMs and PAFs
     return people
def main():

    # Prep for OpenVINO Inference Engine for human pose estimation
    ie = IECore()
    model_hp = 'intel/human-pose-estimation-0001/FP16/human-pose-estimation-0001'
    net_hp = ie.read_network(model=model_hp + '.xml',
                             weights=model_hp + '.bin')
    input_name_hp = next(iter(net_hp.inputs))  # Input blob name "data"
    input_shape_hp = net_hp.inputs[input_name_hp].shape  # [1,3,256,456]
    PAF_blobName = list(net_hp.outputs.keys())[0]  # 'Mconv7_stage2_L1'
    HM_blobName = list(net_hp.outputs.keys())[1]  # 'Mconv7_stage2_L2'
    PAF_shape = net_hp.outputs[PAF_blobName].shape  #  [1,38,32,57]
    HM_shape = net_hp.outputs[HM_blobName].shape  #  [1,19,32,57]
    exec_net_hp = ie.load_network(net_hp, 'CPU')

    # Open a USB webcam
    cam = cv2.VideoCapture(0)
    #cam = cv2.VideoCapture('people.264')
    if cam.isOpened() == False:
        print('Failed to open the input movie file (or a webCam)')
        sys.exit(-1)

    while cv2.waitKey(1) != 27:  # 27 == ESC

        ret, img = cam.read()
        if ret == False:
            return 0

        inblob = cv2.resize(
            img, (input_shape_hp[3], input_shape_hp[2]))  # 3=Width, 2=Height
        inblob = inblob.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        inblob = inblob.reshape(input_shape_hp)

        res_hp = exec_net_hp.infer(inputs={input_name_hp:
                                           inblob})  # Infer poses

        heatmaps = res_hp[HM_blobName][0]
        PAFs = res_hp[PAF_blobName][0]
        people = extract_poses(heatmaps[:-1], PAFs,
                               4)  # Construct poses from HMs and PAFs

        renderPeople(img, people, 4, 0.2)
        cv2.imshow('Result', img)

    cv2.destroyAllWindows()
    return 0