def get_calib(self, idx):
     calib_file = os.path.join(self.calib_dir, '{:06d}.txt'.format(idx))
     # assert os.path.isfile(calib_file)
     return kitti_data_utils.Calibration(calib_file)
    configs.dataset_dir = os.path.join('../../', 'dataset', 'kitti')

    if configs.show_train_data:
        dataloader, _ = create_train_dataloader(configs)
        print('len train dataloader: {}'.format(len(dataloader)))
    else:
        dataloader = create_val_dataloader(configs)
        print('len val dataloader: {}'.format(len(dataloader)))

    print('\n\nPress n to see the next sample >>> Press Esc to quit...')

    for batch_i, (img_files, imgs, targets) in enumerate(dataloader):
        if not (configs.mosaic and configs.show_train_data):
            img_file = img_files[0]
            img_rgb = cv2.imread(img_file)
            calib = kitti_data_utils.Calibration(
                img_file.replace(".png", ".txt").replace("image_2", "calib"))
            objects_pred = invert_target(targets[:, 1:],
                                         calib,
                                         img_rgb.shape,
                                         RGB_Map=None)
            img_rgb = show_image_with_boxes(img_rgb, objects_pred, calib,
                                            False)

        # Rescale target
        targets[:, 2:6] *= configs.img_size
        # Get yaw angle
        targets[:, 6] = torch.atan2(targets[:, 6], targets[:, 7])

        img_bev = imgs.squeeze() * 255
        img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
        img_bev = cv2.resize(img_bev, (configs.img_size, configs.img_size))
Exemple #3
0
    def client_recv(self, client, address):
        while True:
            # read message from socket
            # client_msg_0\x00\x00\x00\x00\x00...
            msg = client.recv(1024).decode("utf-8")
            msg = msg.rstrip("\x00")
            if msg == '':
                return
            if msg == "EOF":
                return
            elif msg == "quit_client":
                client.close()
                # self.sock.close()
                print("> client  exit...")
                return
            elif msg == "quit_server":
                client.close()
                self.sock.close()
                print("> server  exit...")
                sys.exit(0)
            else:
                print("> -------", time.strftime('%Y-%m-%d %H:%M:%S',
                      time.localtime(time.time())), "-------")
                print("> receive the msg from client : {0}".format(msg))
                print('> inference for {0}'.format(msg))
                if(self.need_create_window):
                    # NOTE ObjSLAM
                    cv2.namedWindow("YOLO", flags=cv2.WINDOW_GUI_NORMAL)
                    self.need_create_window = False
                # Inference
                with torch.no_grad():
                    # img_paths, imgs_bev = self.test_dataloader_iter.next()
                    img_paths, imgs_bev = self.test_dataset[int(msg)]
                    img_paths = [img_paths]
                    imgs_bev = torch.from_numpy(
                        np.expand_dims(imgs_bev, axis=0))
                    input_imgs = imgs_bev.to(
                        device=self.configs.device).float()
                    outputs = self.model(input_imgs)
                    detections = post_processing_v2(
                        outputs, conf_thresh=self.configs.conf_thresh, nms_thresh=self.configs.nms_thresh)

                    img_detections = []  # Stores detections for each image index
                    img_detections.extend(detections)

                    img_bev = imgs_bev.squeeze() * 255
                    img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
                    img_bev = cv2.resize(
                        img_bev, (self.configs.img_size, self.configs.img_size))
                    for detections in img_detections:
                        if detections is None:
                            continue
                        # Rescale boxes to original image
                        detections = rescale_boxes(
                            detections, self.configs.img_size, img_bev.shape[:2])
                        for x, y, w, l, im, re, *_, cls_pred in detections:
                            yaw = np.arctan2(im, re)
                            # Draw rotated box
                            kitti_bev_utils.drawRotatedBox(
                                img_bev, x, y, w, l, yaw, cnf.colors[int(cls_pred)])

                    img_rgb = cv2.imread(img_paths[0])
                    calib = kitti_data_utils.Calibration(img_paths[0].replace(
                        ".png", ".txt").replace("image_2", "calib"))
                    objects_pred = predictions_to_kitti_format(
                        img_detections, calib, img_rgb.shape, self.configs.img_size)
                    # NOTE: 输出json的代码
                    frame_object_list = []
                    for i in objects_pred:
                        frame_object = dict()
                        frame_object['type'] = i.type
                        frame_object['center'] = i.t
                        frame_object['length'] = i.l
                        frame_object['width'] = i.w
                        frame_object['height'] = i.h
                        frame_object['theta'] = i.ry
                        box3d_pts_2d, _ = kitti_data_utils.compute_box_3d(
                            i, calib.P)
                        if box3d_pts_2d is None:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d
                        elif box3d_pts_2d.size == 16:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d
                        else:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d[:8, :]
                        frame_object_list.append(frame_object)
                    result = json.dumps(frame_object_list, cls=NumpyEncoder)
                    img_bev = cv2.flip(cv2.flip(img_bev, 0), 1)
                    scale = 1.5
                    cv2.resizeWindow("YOLO",
                                     width=int(img_bev.shape[1] * scale),
                                     height=int(img_bev.shape[0] * scale))
                    cv2.imshow('YOLO', img_bev)
                    cv2.waitKey(10)
                    self.batch_idx += 1
                if len(result) > self.configs.max_length:
                    print("> WARNING: STRING IS TOO LONG! (MAX_LENGTH {0})".format(
                        self.configs.max_length))
                client.send(result.encode(encoding='utf-8'))
                print("> send the responce back to client, string length: {0}".format(
                    len(result)))
        return