def vis(result_sha, data_root, result_root):
	def show_image_with_boxes(img, objects_res, object_gt, calib, save_path, height_threshold=0):
		img2 = np.copy(img) 

		for obj in objects_res:
			box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
			color_tmp = tuple([int(tmp * 255) for tmp in colors[obj.id % max_color]])
			img2 = draw_projected_box3d(img2, box3d_pts_2d, color=color_tmp)
			text = 'ID: %d' % obj.id
			if box3d_pts_2d is not None:
				img2 = cv2.putText(img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=color_tmp) 

		img = Image.fromarray(img2)
		img = img.resize((width, height))
		img.save(save_path)
	
	for seq in seq_list:
		image_dir = os.path.join(data_root, 'image_02/%s' % seq)
		calib_file = os.path.join(data_root, 'calib/%s.txt' % seq)
		result_dir = os.path.join(result_root, '%s/trk_withid/%s' % (result_sha, seq))
		save_3d_bbox_dir = os.path.join(result_dir, '../../trk_image_vis/%s' % seq); mkdir_if_missing(save_3d_bbox_dir)

		# load the list
		images_list, num_images = load_list_from_folder(image_dir)
		print('number of images to visualize is %d' % num_images)
		start_count = 0
		for count in range(start_count, num_images):
			image_tmp = images_list[count]
			if not is_path_exists(image_tmp): 
				count += 1
				continue
			image_index = int(fileparts(image_tmp)[1])
			image_tmp = np.array(Image.open(image_tmp))
			img_height, img_width, img_channel = image_tmp.shape

			result_tmp = os.path.join(result_dir, '%06d.txt'%image_index)		# load the result
			if not is_path_exists(result_tmp): object_res = []
			else: object_res = read_label(result_tmp)
			print('processing index: %d, %d/%d, results from %s' % (image_index, count+1, num_images, result_tmp))
			calib_tmp = Calibration(calib_file)			# load the calibration

			object_res_filtered = []
			for object_tmp in object_res:
				if object_tmp.type not in type_whitelist: continue
				if hasattr(object_tmp, 'score'):
					if object_tmp.score < score_threshold: continue
				center = object_tmp.t
				object_res_filtered.append(object_tmp)

			num_instances = len(object_res_filtered)
			save_image_with_3dbbox_gt_path = os.path.join(save_3d_bbox_dir, '%06d.jpg' % (image_index))
			show_image_with_boxes(image_tmp, object_res_filtered, [], calib_tmp, save_path=save_image_with_3dbbox_gt_path)
			print('number of objects to plot is %d' % (num_instances))
			count += 1
 def get_label(self, idx):
     label_file = os.path.join(self.label_path, '%06d.txt' % idx)
     assert os.path.exists(label_file)
     return kitti_utils.read_label(label_file)
示例#3
0
def vis(result_sha, data_root, result_root):
    def show_image_with_boxes(img,
                              velo,
                              objects_res,
                              objects_res_det,
                              objects_res_raw,
                              labeldata,
                              object_gt,
                              calib,
                              save_path,
                              height_threshold=0,
                              show_lidar=True,
                              save_image=False):
        img2 = np.copy(img)

        for obj in objects_res:
            box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
            color_tmp = tuple(
                [int(tmp * 255) for tmp in colors[obj.id % max_color]])
            img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(0, 0, 255))
            text = 'Tracked ID: %d, Type: %s' % (obj.id, obj.type)
            if box3d_pts_2d is not None:
                img2 = cv2.putText(
                    img2,
                    text,
                    (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8),
                    cv2.FONT_HERSHEY_TRIPLEX,
                    0.5,
                    color=(0, 0, 255))
        for obj in objects_res_det:
            box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
            color_tmp = tuple(
                [int(tmp * 255) for tmp in colors[obj.id % max_color]])
            img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(0, 255, 0))
            text = 'Detection ID: %d, Type: %s' % (obj.id, obj.type)
            if box3d_pts_2d is not None:
                img2 = cv2.putText(
                    img2,
                    text,
                    (int(box3d_pts_2d[3, 0]), int(box3d_pts_2d[3, 1]) - 8),
                    cv2.FONT_HERSHEY_TRIPLEX,
                    0.5,
                    color=(0, 255, 0))
        import itertools
        labeldata, labeldata2 = itertools.tee(labeldata)
        for obj in labeldata:
            # print("here")
            box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
            img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(255, 0, 0))
            text = 'GT, Type: %s' % (obj.type)
            if box3d_pts_2d is not None:
                # print("also")
                print(text)
                img2 = cv2.putText(
                    img2,
                    text,
                    (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8),
                    cv2.FONT_HERSHEY_TRIPLEX,
                    0.5,
                    color=(255, 0, 0))
        # for obj in objects_res_raw:
        #     box3d_pts_2d, _ = compute_box_3d(obj, calib.P)
        #     color_tmp = tuple([int(tmp * 255)
        #                        for tmp in colors[obj.id % max_color]])
        #     img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(255,0,0))
        #     text = 'Estimate ID: %d' % obj.id
        #     if box3d_pts_2d is not None:
        #         img2 = cv2.putText(img2, text, (int(box3d_pts_2d[2, 0]), int(
        #             box3d_pts_2d[2, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=(255,0,0))
        if show_lidar:
            show_lidar_with_boxes(velo,
                                  objects=labeldata2,
                                  calib=calib,
                                  objects_pred=objects_res)
        img = Image.fromarray(img2)
        img = img.resize((width, height))
        cv2.imshow("Image", img2)
        cv2.waitKey()

        if save_image:
            print("Saving Image at", save_path)
            img.save(save_path)

        return img2

    for seq in seq_list:
        image_dir = os.path.join(data_root, 'image_02/%s' % seq)
        calib_file = os.path.join(data_root, 'calib/%s.txt' % seq)
        label_file = os.path.join(data_root, 'label_02/%s.txt' % seq)
        velo_dir = os.path.join(data_root, 'velodyne/%s' % seq)
        result_dir = [
            os.path.join(result_root,
                         '%s/trk_withid/%s' % (result_sha[0], seq)),
            os.path.join(result_root,
                         '%s/trk_withid/%s' % (result_sha[1], seq)),
            os.path.join(result_root,
                         '%s/trk_withid/%s' % (result_sha[2], seq))
        ]
        save_3d_bbox_dir = os.path.join(
            result_root,
            '%s/trk_image_vis/%s' % ("Combined_Final_WithLabel", seq))
        mkdir_if_missing(save_3d_bbox_dir)

        # load the list
        images_list, num_images = load_list_from_folder(image_dir)
        velo_list, num_velo = load_list_from_folder(velo_dir)
        print('number of images to visualize is %d' % num_images)
        start_count = 0
        filecontent = np.array([f.split() for f in open(label_file, 'r')])
        # alllabels = np.unique(filecontent[:,2])
        # labels = ['Car', 'Pedestrian', 'Cyclist']
        # finallabelset = [x for x in alllabels if x not in labels]
        # print(alllabels)
        # print(finallabelset)
        # for val in finallabelset:
        #     filecontent = filecontent[filecontent[:,2]!=val,:]
        # print(np.unique(filecontent[:,2]))
        size = (width, height)
        out = cv2.VideoWriter(f'{result_root}/{seq}.avi',
                              cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
        num_images = 1
        for count in range(start_count, num_images):
            image_tmp = images_list[count]
            velo_tmp = velo_list[count]
            if not is_path_exists(image_tmp):
                count += 1
                continue
            image_index = int(fileparts(image_tmp)[1])
            image_tmp = np.array(Image.open(image_tmp))
            img_height, img_width, img_channel = image_tmp.shape
            filecontentframe = filecontent[filecontent[:, 0] ==
                                           str(image_index), :]
            print(len(filecontentframe))
            print(f"Labels for frame {image_index}",
                  np.unique(filecontentframe[:, 2]))
            labeldata = (Object3d(getstringfromarray(line[2:]))
                         for line in filecontentframe)
            object_res = []
            object_res_det = []
            object_res_raw = []
            for dirt in result_dir:
                result_tmp = os.path.join(dirt, '%06d.txt' %
                                          image_index)  # load the result
                if is_path_exists(result_tmp):
                    object_res = object_res + read_label(result_tmp)
                result_tmp_det = os.path.join(dirt, 'det%06d.txt' %
                                              image_index)  # load the result
                if is_path_exists(result_tmp_det):
                    object_res_det = object_res_det + \
                        read_label(result_tmp_det)
                result_tmp_raw = os.path.join(dirt, 'raw%06d.txt' %
                                              image_index)  # load the result
                if is_path_exists(result_tmp_raw):
                    object_res_raw = object_res_raw + \
                        read_label(result_tmp_raw)
            print('processing index: %d, %d/%d, results from %s' %
                  (image_index, count + 1, num_images, result_tmp))
            calib_tmp = Calibration(calib_file)  # load the calibration

            object_res_filtered = []
            for object_tmp in object_res:
                if object_tmp.type not in type_whitelist:
                    continue
                if hasattr(object_tmp, 'score'):
                    if object_tmp.score < score_threshold:
                        continue
                center = object_tmp.t
                object_res_filtered.append(object_tmp)
            object_res_filtered_det = []
            for object_tmp in object_res_det:
                if object_tmp.type not in type_whitelist:
                    continue
                if hasattr(object_tmp, 'score'):
                    if object_tmp.score < score_threshold:
                        continue
                center = object_tmp.t
                object_res_filtered_det.append(object_tmp)
            object_res_filtered_raw = []
            for object_tmp in object_res_raw:
                if object_tmp.type not in type_whitelist:
                    continue
                # if hasattr(object_tmp, 'score'):
                #     if object_tmp.score < score_threshold:
                #         continue
                center = object_tmp.t
                object_res_filtered_raw.append(object_tmp)
            num_instances = len(object_res_filtered)
            save_image_with_3dbbox_gt_path = os.path.join(
                save_3d_bbox_dir, '%06d.jpg' % (image_index))
            velodyne_scan = load_velo_scan(velo_tmp, np.float32, n_vec=4)[:,
                                                                          0:4]
            img = show_image_with_boxes(
                image_tmp,
                velodyne_scan,
                object_res_filtered,
                object_res_filtered_det,
                object_res_filtered_raw,
                labeldata, [],
                calib_tmp,
                save_path=save_image_with_3dbbox_gt_path)
            print('number of objects to plot is %d, %d, %d' %
                  (num_instances, len(object_res_filtered_det),
                   len(object_res_filtered_raw)))
            count += 1
            out.write(img)
        out.release()
示例#4
0
 def get_label_objects(self, idx):
     assert(idx<self.num_samples and self.split=='training')
     label_filename = os.path.join(self.label_dir, '%06d.txt'%(idx))
     return utils.read_label(label_filename)
示例#5
0
    def __getitem__(self, index):

        # start time
        get_item_start_time = time.time()

        # get point cloud
        lidar_filename = os.path.join(self.lidar_dir, '%06d.bin' % index)
        lidar_data = kitti_utils.load_velo_scan(lidar_filename)

        # time for loading point cloud
        read_point_cloud_end_time = time.time()
        read_point_cloud_time = read_point_cloud_end_time - get_item_start_time

        # voxelize point cloud
        voxel_point_cloud = torch.tensor(
            kitti_utils.voxelize(point_cloud=lidar_data),
            requires_grad=True,
            device=self.device).float()

        # time for voxelization
        voxelization_end_time = time.time()
        voxelization_time = voxelization_end_time - read_point_cloud_end_time

        # channels along first dimensions according to PyTorch convention
        voxel_point_cloud = voxel_point_cloud.permute([2, 0, 1])

        # get image
        if self.get_image:
            image_filename = os.path.join(self.image_dir, '%06d.png' % index)
            image = kitti_utils.get_image(image_filename)

        # get current time
        read_labels_start_time = time.time()

        # get calibration
        calib_filename = os.path.join(self.calib_dir, '%06d.txt' % index)
        calib = kitti_utils.Calibration(calib_filename)

        # get labels
        label_filename = os.path.join(self.label_dir, '%06d.txt' % index)
        labels = kitti_utils.read_label(label_filename)

        read_labels_end_time = time.time()
        read_labels_time = read_labels_end_time - read_labels_start_time

        # compute network label
        if self.split == 'training':
            # get current time
            compute_label_start_time = time.time()

            # create empty pixel labels
            regression_label = np.zeros(
                (OUTPUT_DIM_0, OUTPUT_DIM_1, OUTPUT_DIM_REG))
            classification_label = np.zeros(
                (OUTPUT_DIM_0, OUTPUT_DIM_1, OUTPUT_DIM_CLA))

            # iterate over all 3D label objects in list
            for label in labels:
                if label.type == 'Car':
                    # compute corners of 3D bounding box in camera coordinates
                    _, bbox_corners_camera_coord = kitti_utils.compute_box_3d(
                        label, calib.P, scale=1.0)
                    # get pixel label for classification and BEV bounding box
                    regression_label, classification_label = compute_pixel_labels\
                        (regression_label, classification_label, label, bbox_corners_camera_coord)

            # stack classification and regression label
            regression_label = torch.tensor(regression_label,
                                            device=self.device).float()
            classification_label = torch.tensor(classification_label,
                                                device=self.device).float()
            training_label = torch.cat(
                (regression_label, classification_label), dim=2)

            # get time for computing pixel label
            compute_label_end_time = time.time()
            compute_label_time = compute_label_end_time - compute_label_start_time

            # total time for data loading
            get_item_end_time = time.time()
            get_item_time = get_item_end_time - get_item_start_time

            if self.show_times:
                print('---------------------------')
                print('Get Item Time: {:.4f} s'.format(get_item_time))
                print('---------------------------')
                print('Read Point Cloud Time: {:.4f} s'.format(
                    read_point_cloud_time))
                print('Voxelization Time: {:.4f} s'.format(voxelization_time))
                print('Read Labels Time: {:.4f} s'.format(read_labels_time))
                print(
                    'Compute Labels Time: {:.4f} s'.format(compute_label_time))

            return voxel_point_cloud, training_label

        else:
            if self.get_image:
                return image, voxel_point_cloud, labels, calib
            else:
                return voxel_point_cloud, labels, calib