Esempio n. 1
0
    def build_pred(self):
        seq_data = []
        if self.data_path == []:
            print("Empty data")
            return seq_data

        for idx, file_path in enumerate(self.data_path):

            frame_data = bh.load_json(file_path)

            if idx % args.verbose_interval == 0 and idx != 0:
                print('{} images.'.format(idx))

            match_box = self.det_result[idx]

            prediction_data = []
            for i, box in enumerate(match_box):
                prediction = bh.init_labels_format()
                prediction['box2d']['x1'] = int(box[0])
                prediction['box2d']['y1'] = int(box[1])
                prediction['box2d']['x2'] = int(box[2])
                prediction['box2d']['y2'] = int(box[3])
                prediction['box2d']['confidence'] = float(box[6])
                # 3D box projected center
                prediction['box3d']['xc'] = int(box[4])
                prediction['box3d']['yc'] = int(box[5])
                del prediction['poly2d']
                del prediction['attributes']['trafficLightColor']
                del prediction['attributes']['areaType']
                del prediction['attributes']['laneDirection']
                del prediction['attributes']['laneStyle']
                del prediction['attributes']['laneTypes']
                prediction_data.append(prediction)

            frame_data['prediction'] = prediction_data

            print("Frame {}, GT: {} Boxes, PD: {} Boxes".format(
                idx,
                len(frame_data['labels']),
                len(frame_data['prediction'])))
            
            del frame_data['labels']

            filename = os.path.join(
                            DATASET.PRED_PATH, 
                            frame_data['videoName'],
                            os.path.basename(file_path))
            if not os.path.exists(os.path.dirname(filename)):
                os.mkdir(os.path.dirname(filename))

            bh.dump_json(filename, frame_data)
            seq_data.append(filename)

        print("Saving {} frames...".format(idx))

        return seq_data
 def format_data2bdd(self):
     dataset_list = []
     for d_idx in range(len(self.data_idx)):
         dataset_list.append(
             self.get_data2bdd(self.data_idx[d_idx], self.data_label[d_idx],
                               self.box_idx[d_idx], self.det_result[d_idx]))
     print("Saving {} with {} sequences...".format(save_name,
                                                   len(dataset_list)))
     if not args.is_pred: bh.dump_json(save_name, dataset_list)
     print("Saved")
    # Load data
    print("Load label file from path: {}".format(DATASET.PRED_PATH))
    folders, data_path = load_label_path(DATASET.PRED_PATH)

    for seq_idx, folder in enumerate(folders):

        pkl_path = '{ROOT}{SESS}_{EP}_{DT}_{PH}_set/{SESS}_{EP}_{' \
                   'SQ}_bdd_roipool_output.pkl'.format(
            **{'ROOT': args.root, 'SESS': args.session, 'EP': args.epoch, 
                'DT': args.set, 'PH': args.split, 'SQ': folder})

        save_path = '{ROOT}{SESS}_{EP}_{DT}_{PH}_set/{SQ}_bdd_3d.json'.format(
            **{'ROOT': args.root, 'SESS': args.session, 'EP': args.epoch, 
                'DT': args.set, 'PH': args.split, 'SQ': folder})

        det_pred = load_single_frame_result(pkl_path)

        hypos = []
        for fr_idx, frame_path in enumerate(data_path[folder]):

            det_placeholder = bh.load_json(frame_path)

            hypo = convert_app(det_placeholder, det_pred[fr_idx])
            hypos.append(hypo)

        print("Saving updated tracking results with {} frames at {}...".format(
            len(hypos), save_path))
        bh.dump_json(save_path, hypos)

def dump_output_json(filename, frame_data):
    if not os.path.exists(os.path.dirname(filename)):
        print("Create {} ...".format(os.path.dirname(filename)))
        os.makedirs(os.path.dirname(filename))

    bh.dump_json(filename, frame_data)
    def get_data2bdd(self, data_idx, data_label, box_idx, det_result):
        seq_data = []
        if data_idx == []:
            print("Empty data")
            return seq_data

        for idx, im_path in enumerate(data_idx):

            check_avail = box_idx[idx]
            obj = data_label[im_path]

            if idx % args.verbose_interval == 0 and idx != 0:
                print('{} images.'.format(idx))

            frame_data = bh.init_frame_format()

            frame_data['name'] = im_path
            del frame_data['url']
            frame_data['videoName'] = obj['vid_name']
            frame_data['resolution']['height'] = im_size_h
            frame_data['resolution']['width'] = im_size_w
            if 'weather' in obj and 'timeofday' in obj:
                frame_data['attributes']['weather'] = obj['weather']
                frame_data['attributes']['timeofday'] = obj['timeofday']
            else:
                del frame_data['attributes']

            frame_data['frameIndex'] = idx
            if 'cali' in obj:
                frame_data['intrinsics']['cali'] = obj['cali']
                frame_data['intrinsics']['focal'] = [
                    obj['cali'][0][0], obj['cali'][1][1]
                ]
                frame_data['intrinsics']['center'] = [
                    obj['cali'][0][2], obj['cali'][1][2]
                ]
            else:
                frame_data['intrinsics']['cali'] = [[
                    focal, 0, im_size_w // 2, 0
                ], [0, focal, im_size_h // 2, 0], [0, 0, 1, 0]]
                frame_data['intrinsics']['focal'] = [focal, focal]
                frame_data['intrinsics']['center'] = [
                    im_size_w // 2, im_size_h // 2
                ]
            frame_data['intrinsics']['fov'] = obj['fov']
            frame_data['intrinsics']['nearClip'] = obj['nearClip']
            # Camera relative rotation from previous frame (in rad)
            # Camera relative location in world coordinate (in meter)
            frame_data['extrinsics']['rotation'] = obj['pose']['rotation']
            frame_data['extrinsics']['location'] = obj['pose']['position']
            frame_data['timestamp'] = obj['timestamp']

            if args.is_pred:
                match_box = det_result[idx]

                prediction_data = []
                for i, box in enumerate(match_box):
                    prediction = bh.init_labels_format()
                    prediction['box2d']['x1'] = int(box[0])
                    prediction['box2d']['y1'] = int(box[1])
                    prediction['box2d']['x2'] = int(box[2])
                    prediction['box2d']['y2'] = int(box[3])
                    prediction['box2d']['confidence'] = float(box[6])
                    # 3D box projected center
                    prediction['box3d']['xc'] = int(box[4])
                    prediction['box3d']['yc'] = int(box[5])
                    del prediction['poly2d']
                    prediction_data.append(prediction)

                frame_data['prediction'] = prediction_data

                print("Frame {}, GT: {} Boxes, PD: {} Boxes".format(
                    idx, len(frame_data['labels']),
                    len(frame_data['prediction'])))

                filename = os.path.join(DATASET.PRED_PATH,
                                        os.path.splitext(im_path)[0] + '.json')

                del frame_data['labels']
                if not os.path.exists(os.path.dirname(filename)):
                    os.mkdir(os.path.dirname(filename))
                bh.dump_json(filename, frame_data)

            else:
                frame_data['labels'] = self.get_data_from_id_bdd(
                    obj, check_avail)
                del frame_data['prediction']
                print("Frame {}, GT: {} Boxes".format(
                    idx, len(frame_data['labels'])))

                filename = os.path.join(DATASET.LABEL_PATH,
                                        os.path.splitext(im_path)[0] + '.json')
                bh.dump_json(filename, frame_data)
                seq_data.append(filename)

        filename = os.path.join(DATASET.LABEL_PATH,
                                frame_data['videoName'] + '_bdd.json')
        print("Saving {} with {} frames...".format(filename, idx))
        if not args.is_pred: bh.dump_json(filename, seq_data)

        return seq_data