Beispiel #1
0
def load_info_from_json(json_path):
    data = gnu.load_json(json_path)
    # image path
    assert ('image_path' in data), "Path of input image should be specified"
    image_path = data['image_path']
    assert osp.exists(image_path), f"{image_path} does not exists"
    # body bboxes
    body_bbox_list = list()
    if 'body_bbox_list' in data:
        body_bbox_list = data['body_bbox_list']
        assert isinstance(body_bbox_list, list)
        for b_id, body_bbox in enumerate(body_bbox_list):
            if isinstance(body_bbox, list) and len(body_bbox) == 4:
                body_bbox_list[b_id] = np.array(body_bbox)
    # hand bboxes
    hand_bbox_list = list()
    if 'hand_bbox_list' in data:
        hand_bbox_list = data['hand_bbox_list']
        assert isinstance(hand_bbox_list, list)
        for hand_bbox in hand_bbox_list:
            for hand_type in ['left_hand', 'right_hand']:
                if hand_type in hand_bbox:
                    bbox = hand_bbox[hand_type]
                    if isinstance(bbox, list) and len(bbox) == 4:
                        hand_bbox[hand_type] = np.array(bbox)
                    else:
                        hand_bbox[hand_type] = None
    return image_path, body_bbox_list, hand_bbox_list
Beispiel #2
0
def run_hand_mocap(args, bbox_detector, hand_mocap, visualizer):
    #Set up input data (images or webcam)
    input_type, input_data = demo_utils.setup_input(args)

    assert args.out_dir is not None, "Please specify output dir to store the results"

    video_frame = 0

    #pbar = tqdm(total=13000)
    while True:
        # load data
        load_bbox = False

        if input_type == 'video':
            _, img_original_bgr = input_data.read()

            if video_frame < args.start_frame:
                video_frame += 1
                continue
            # save the obtained video frames
            image_path = osp.join(args.out_dir, "frames",
                                  f"{video_frame:05d}.jpg")
        else:
            assert False, "Unknown input_type"

        if img_original_bgr is None or video_frame > args.end_frame:
            break
        #print("--------------------------------------")

        # bbox detection
        if args.load_bboxes:
            #print(osp.join(args.out_dir, WHICH_BBOXES))
            if not osp.exists(osp.join(args.out_dir, WHICH_BBOXES)):
                print(f"[ERROR] {WHICH_BBOXES} folder do not exist.")
                return -1
            try:
                json_name = f"{video_frame:05d}_bbox.json"
                json_path = osp.join(args.out_dir, WHICH_BBOXES, json_name)
                data = gnu.load_json(json_path)
                #print(f"Data loaded from '{json_path}'")
                #[{'left_hand': array([608.8389 , 559.77545, 111.33954,  97.50702], dtype=float32), 'right_hand': array([463.8986 , 550.34717,  89.06082,  93.86871], dtype=float32), 'left_hand_score': 0.99971753, 'right_hand_score': 0.99974126}]
                hands = {}
                if data["left_hand"] is not None:
                    hands["left_hand"] = np.array(data["left_hand"]).astype(
                        np.float32)
                if data["right_hand"] is not None:
                    hands["right_hand"] = np.array(data["right_hand"]).astype(
                        np.float32)
                hand_bbox_list = [
                    hands,
                ]
                body_bbox_list = []
                if data["body"] is not None:
                    body_bbox_list.append(
                        np.array(data["body"]).astype(np.float32))

            except:
                video_frame += 1
                print(f"[WARNING_ANNOTATIONS] Frame {video_frame}")
                continue
        else:
            raise Exception("Should have computed already the bboxes, yeah?")

        if len(hand_bbox_list) < 1:
            print(f"No hand detected: {image_path}")
            video_frame += 1
            continue

        # Hand Pose Regression
        pred_output_list = hand_mocap.regress(img_original_bgr,
                                              hand_bbox_list,
                                              add_margin=True)
        assert len(hand_bbox_list) == len(body_bbox_list)
        assert len(body_bbox_list) == len(pred_output_list)

        # save predictions to pkl
        if args.save_pred_pkl:
            demo_type = 'hand'
            demo_utils.save_pred_to_pkl(args, demo_type, image_path,
                                        body_bbox_list, hand_bbox_list,
                                        pred_output_list)

        #print(f"Processed : {image_path}")
        video_frame += 1
        #pbar.update(1)

    with open(os.path.join(args.out_dir, MARKER), "w+") as f:
        f.write("True")
Beispiel #3
0
def run_body_mocap(args, body_bbox_detector, body_mocap, visualizer):
    #Setup input data to handle different types of inputs
    input_type, input_data = demo_utils.setup_input(args)

    cur_frame = args.start_frame
    video_frame = 0
    timer = Timer()
    while True:
        timer.tic()
        
        # always video
        _, img_original_bgr = input_data.read()
        if video_frame < cur_frame:
            video_frame += 1
            continue

        # save the obtained video frames
        if img_original_bgr is not None:
            video_frame += 1

        cur_frame +=1
        if img_original_bgr is None or cur_frame > args.end_frame:
            break  
        print("--------------------------------------")

        # save the obtained body & hand bbox to json file
        if args.load_bboxes:
            json_name = f"{cur_frame:05d}_bbox.json"
            json_path = osp.join(args.out_dir, 'bbox_smoothed', json_name)
            data = gnu.load_json(json_path)
            print(data)
            break
        else:
            body_pose_list, body_bbox_list = body_bbox_detector.detect_body_pose(img_original_bgr)
            break

        hand_bbox_list = [None, ] * len(body_bbox_list)


        if len(body_bbox_list) < 1: 
            print(f"No body deteced: {image_path}")
            continue

        #Sort the bbox using bbox size 
        # (to make the order as consistent as possible without tracking)
        bbox_size =  [ (x[2] * x[3]) for x in body_bbox_list]
        idx_big2small = np.argsort(bbox_size)[::-1]
        body_bbox_list = [ body_bbox_list[i] for i in idx_big2small ]
        if args.single_person and len(body_bbox_list)>0:
            body_bbox_list = [body_bbox_list[0], ]       

        # Body Pose Regression
        pred_output_list = body_mocap.regress(img_original_bgr, body_bbox_list)
        assert len(body_bbox_list) == len(pred_output_list)

        # extract mesh for rendering (vertices in image space and faces) from pred_output_list
        pred_mesh_list = demo_utils.extract_mesh_from_output(pred_output_list)

        # visualization
        res_img = visualizer.visualize(
            img_original_bgr,
            pred_mesh_list = pred_mesh_list, 
            body_bbox_list = body_bbox_list)
        
        # show result in the screen
        if not args.no_display:
            res_img = res_img.astype(np.uint8)
            ImShow(res_img)

        # save result image
        if args.out_dir is not None:
            demo_utils.save_res_img(args.out_dir, image_path, res_img)

        # save predictions to pkl
        if args.save_pred_pkl:
            demo_type = 'body'
            demo_utils.save_pred_to_pkl(
                args, demo_type, image_path, body_bbox_list, hand_bbox_list, pred_output_list)

        timer.toc(bPrint=True,title="Time")
        print(f"Processed : {image_path}")

    #save images as a video
    if not args.no_video_out and input_type in ['video', 'webcam']:
        demo_utils.gen_video_out(args.out_dir, args.seq_name)

    if input_type =='webcam' and input_data is not None:
        input_data.release()
    cv2.destroyAllWindows()