def draw_towncentre(record_video=False):
    # Get annotations
    # Run ../converters/towncenterConverter/converter.py to generate the json files
    vcd_file_name = "../converters/towncenterConverter/etc/vcd430_towncenter.json"
    vcd = core.VCD(vcd_file_name)
    scene = scl.Scene(vcd)

    drawerCamera = draw.Image(scene)
    #textDrawer = draw.TextDrawer()
    frameInfoDrawer = draw.FrameInfoDrawer(vcd)

    # Get the size of the screen
    screen = screeninfo.get_monitors()[0]

    # Get video
    video_file_name = "../../../../data/TownCentre/TownCentreXVID.avi"
    video_cap = cv.VideoCapture(video_file_name)
    video_width = int(video_cap.get(cv.CAP_PROP_FRAME_WIDTH))
    video_height = int(video_cap.get(cv.CAP_PROP_FRAME_HEIGHT))

    cv.namedWindow('TownCentre', cv.WINDOW_NORMAL)
    cv.moveWindow('TownCentre', screen.x + screen.width // 8, screen.y + screen.height // 8)
    cv.resizeWindow('TownCentre', (int(3 * screen.width / 4), int(3 * screen.height / 4)))

    # Prepare color map
    colorMap = {'Car': (0, 0, 255), 'Van': (255, 0, 0), 'Truck': (127, 127, 0),
                 'Pedestrian': (0, 255, 0), 'Person_sitting': (0, 127, 127),
                 'Tram': (127, 0, 127), 'Misc': (127, 127, 127), 'DontCare': (255, 255, 255)}
    imageParams = draw.Image.Params(_colorMap=colorMap,
                                    _draw_trajectory=True)
    ar = video_width/(video_height*2)

    # Video record
    if record_video:
        video_writer = cv.VideoWriter("towncentre_vcd.mp4",
                                      cv.VideoWriter_fourcc(*'mp4v'), 30.0, (video_width + 400, video_height*3))

    # Loop over video
    f = 0
    while True:
        # Capture frame
        ret, img = video_cap.read()
        if ret is not True:
            break

        # Camera
        drawerCamera.draw(img, f, _params=imageParams)

        # VCD text viewer
        #textImg = textDrawer.draw(vcd.stringify_frame(f, pretty=False), cols=400, rows=video_height)
        textImg = frameInfoDrawer.draw(f, cols=400, rows=video_height, _params=imageParams)


        # Stack
        outImg = np.hstack((img, textImg))
        cv.imshow('TownCentre', outImg)
        cv.waitKey(1)

        if record_video:
            video_writer.write(outImg)

        # Update frame num
        f += 1

    video_cap.release()
    if record_video:
        video_writer.release()
    cv.destroyAllWindows()
def create_reprojections(vcd):
    scene = scl.Scene(vcd)
    # Read the "ground" objects and reproject from image to world
    # Assuming data for frame 0
    for object_id, object in vcd.data['vcd']['objects'].items():
        name = object['name']
        if 'ground' in name:
            vcd_frame = vcd.get_frame(0)
            object_data = vcd_frame['objects'][object_id]['object_data']['mat'][0]
            #ground_x_neg = vcd_frame['objects'][object_id]['object_data']['mat'][1]
            width = object_data['width']
            height = object_data['height']
            points_cs = object_data['coordinate_system']


            '''
             # DEBUG: distord-undistort cycle: works!
            cam = scene.get_camera('CAM_FRONT', 0)
            original_undistorted = np.array([1537, 846, 1]).reshape(3, 1)
            distorted = cam.distort_points2d(original_undistorted)
            undistorted = cam.undistort_points2d(distorted)

            original_distorted = np.array([1481, 823, 1]).reshape(3, 1)
            undistorted = cam.undistort_points2d(original_distorted)
            distorted = cam.distort_points2d(undistorted)
            '''

            # Read 3d points
            points3d_4xN = np.array(object_data['val']).reshape(height, width)
            N = points3d_4xN.shape[1]

            # Project into image
            points2d_3xN, idx_valid_proj = scene.project_points3d_4xN(points3d_4xN, points_cs, 'CAM_FRONT')

            # Re-project into plane
            points2d_3xN_filt = points2d_3xN[:, idx_valid_proj]
            points3d_4xN_rep, idx_valid_rep = scene.reproject_points2d_3xN(points2d_3xN_filt, (0, 0, 1, 0), 'CAM_FRONT', 'vehicle-iso8855')


            # Create output object
            mat_ground_reprojected = types.mat(name="points",
                                 val=points3d_4xN_rep.flatten().tolist(),
                                 channels=1,
                                 width=points3d_4xN_rep.shape[1],
                                 height=points3d_4xN_rep.shape[0],
                                 dataType='float',
                                 coordinate_system="vehicle-iso8855")
            mat_ground_reprojected.add_attribute(types.vec(name="color",
                                            val=(0, 0, 255)))
            vcd.add_object_data(object_id, mat_ground_reprojected, frame_value=0)

            # Filter out those not valid during projection and reprojection from original set of 3D points...
            if len(idx_valid_rep) > 0:
                temp1 = points3d_4xN[:, idx_valid_proj]
                temp2 = temp1[:, idx_valid_rep]

                # ... so we can compare with the valid reprojected 3D points
                temp3 = points3d_4xN_rep[:, idx_valid_rep]
                temp4 = temp3 - temp2
                error = np.linalg.norm(temp4)

                print("Reprojection error:", error, " - Num. points: ", temp2.shape[1])
def draw_scene(vcd):
    # Prepare objects
    scene = scl.Scene(
        vcd)  # scl.Scene has functions to project images, transforms, etc.
    drawer_front = draw.Image(scene, "CAM_FRONT")
    drawer_rear = draw.Image(scene, "CAM_REAR")
    drawer_left = draw.Image(scene, "CAM_LEFT")
    drawer_right = draw.Image(scene, "CAM_RIGHT")

    frameInfoDrawer = draw.FrameInfoDrawer(vcd)

    setupViewer = draw.SetupViewer(scene, "vehicle-iso8855")

    # Class colormap
    colorMap = {
        'Car': (0, 0, 255),
        'Van': (255, 0, 0),
        'Truck': (127, 127, 0),
        'Pedestrian': (0, 255, 0),
        'Person_sitting': (0, 127, 127),
        'Tram': (127, 0, 127),
        'Misc': (127, 127, 127),
        'DontCare': (255, 255, 255),
        'Cyclist': (0, 127, 255),
        'Ego-car': (0, 0, 0),
        'Wall': (0, 0, 255),
        'Ground': (0, 255, 0)
    }

    # Get the size of the screen
    screen = screeninfo.get_monitors()[0]

    # Draw the images
    img_width_px = 960
    img_height_px = 604
    img_front = 255 * np.ones((img_height_px, img_width_px, 3), np.uint8)
    img_rear = 255 * np.ones((img_height_px, img_width_px, 3), np.uint8)
    img_left = 255 * np.ones((img_height_px, img_width_px, 3), np.uint8)
    img_right = 255 * np.ones((img_height_px, img_width_px, 3), np.uint8)
    imageParams = draw.Image.Params(_colorMap=colorMap)

    drawer_front.draw(img_front, 0, _params=imageParams)
    drawer_rear.draw(img_rear, 0, _params=imageParams)
    drawer_left.draw(img_left, 0, _params=imageParams)
    drawer_right.draw(img_right, 0, _params=imageParams)

    # Undistort
    cam_front = scene.get_camera("CAM_FRONT", compute_remaps=True)
    cam_rear = scene.get_camera("CAM_REAR", compute_remaps=True)
    cam_left = scene.get_camera("CAM_LEFT", compute_remaps=True)
    cam_right = scene.get_camera("CAM_RIGHT", compute_remaps=True)

    img_front_und = cam_front.undistort_image(img_front)
    img_rear_und = cam_rear.undistort_image(img_rear)
    img_left_und = cam_left.undistort_image(img_left)
    img_right_und = cam_right.undistort_image(img_right)

    # Draw the text
    textImg = frameInfoDrawer.draw(0,
                                   cols=400,
                                   rows=img_height_px * 2,
                                   _params=imageParams)

    mosaic = np.vstack((np.hstack(
        (img_front, img_right)), np.hstack((img_left, img_rear))))
    cv.line(mosaic, (mosaic.shape[1] // 2, 0),
            (mosaic.shape[1] // 2, mosaic.shape[0]), (0, 0, 0), 3)
    cv.line(mosaic, (0, mosaic.shape[0] // 2),
            (mosaic.shape[1], mosaic.shape[0] // 2), (0, 0, 0), 3)

    mosaic_und = np.vstack((np.hstack(
        (img_front_und, img_right_und)), np.hstack(
            (img_left_und, img_rear_und))))
    cv.line(mosaic_und, (mosaic_und.shape[1] // 2, 0),
            (mosaic_und.shape[1] // 2, mosaic_und.shape[0]), (0, 0, 0), 3)
    cv.line(mosaic_und, (0, mosaic_und.shape[0] // 2),
            (mosaic_und.shape[1], mosaic_und.shape[0] // 2), (0, 0, 0), 3)

    # Draw the top view
    topview_width = 1280
    topview_height = 1280
    ar = topview_width / topview_height
    rangeX = (-30.0, 30.0)
    rangeY = (-((rangeX[1] - rangeX[0]) / ar) / 2,
              ((rangeX[1] - rangeX[0]) / ar) / 2)
    topviewParams = draw.TopView.Params(colorMap=colorMap,
                                        topViewSize=(topview_width,
                                                     topview_height),
                                        background_color=255,
                                        rangeX=rangeX,
                                        rangeY=rangeY,
                                        stepX=1.0,
                                        stepY=1.0,
                                        draw_grid=False)
    drawerTopView = draw.TopView(scene,
                                 "vehicle-iso8855",
                                 params=topviewParams)

    topView = drawerTopView.draw(frameNum=0)

    cv.namedWindow("Cameras", cv.WINDOW_NORMAL)
    cv.imshow("Cameras", mosaic)
    cv.namedWindow("Cameras undistorted", cv.WINDOW_NORMAL)
    cv.imshow("Cameras undistorted", mosaic_und)
    cv.namedWindow("VCD info")
    cv.imshow("VCD info", textImg)
    cv.namedWindow("TopView", cv.WINDOW_NORMAL)
    cv.imshow("TopView", topView)
    cv.waitKey(0)

    fig1 = setupViewer.plot_setup()
    plt.show()
Esempio n. 4
0
def draw_kitti_tracking(sequence_number, record_video, draw_images):
    # Get annotations
    # Run ../converters/kittiConverter/converter.py to generate the json files
    #vcd_file_name = "../tests/etc/vcd430_kitti_tracking_" + str(sequence_number).zfill(4) + ".json"
    vcd_file_name = "../converters/kittiConverter/etc/vcd430_kitti_tracking_" + str(
        sequence_number).zfill(4) + ".json"
    vcd = core.VCD(vcd_file_name)
    scene = scl.Scene(
        vcd)  # scl.Scene has functions to project images, transforms, etc.

    drawerCamera = draw.Image(scene, "CAM_LEFT")
    frameInfoDrawer = draw.FrameInfoDrawer(vcd)

    # Get the size of the screen
    screen = screeninfo.get_monitors()[0]

    # Get video
    video_file_name = "../../../../data/kitti/tracking/video/" + str(
        sequence_number).zfill(4) + ".mp4"
    video_cap = cv.VideoCapture(video_file_name)
    video_width = int(video_cap.get(cv.CAP_PROP_FRAME_WIDTH))
    video_height = int(video_cap.get(cv.CAP_PROP_FRAME_HEIGHT))

    cv.namedWindow('KITTI Tracking', cv.WINDOW_NORMAL)
    cv.moveWindow('KITTI Tracking', screen.x + screen.width // 8,
                  screen.y + screen.height // 8)
    cv.resizeWindow('KITTI Tracking',
                    (int(3 * screen.width / 4), int(3 * screen.height / 4)))

    # Prepare color map
    colorMap = {
        'Car': (0, 0, 255),
        'Van': (255, 0, 0),
        'Truck': (127, 127, 0),
        'Pedestrian': (0, 255, 0),
        'Person_sitting': (0, 127, 127),
        'Tram': (127, 0, 127),
        'Misc': (127, 127, 127),
        'DontCare': (0, 255, 255),
        'Cyclist': (0, 127, 255),
        'Egocar': (127, 127, 127)
    }
    imageParams = draw.Image.Params(_colorMap=colorMap,
                                    _draw_trajectory=False,
                                    _ignore_classes={"DontCare"},
                                    _draw_types={"bbox", "cuboid"})
    ar = video_width / (video_height * 2)

    # Next values define which region of the selected coordinate_system is to be monitored by the TopView
    rangeX = (-5.0, 55.0)
    rangeY = (-((rangeX[1] - rangeX[0]) / ar) / 2,
              ((rangeX[1] - rangeX[0]) / ar) / 2)
    topviewParams1 = draw.TopView.Params(colorMap=colorMap,
                                         topViewSize=(video_width,
                                                      video_height * 2),
                                         background_color=255,
                                         rangeX=rangeX,
                                         rangeY=rangeY,
                                         stepX=1.0,
                                         stepY=1.0,
                                         ignore_classes={"DontCare"})
    drawerTopView1 = draw.TopView(scene,
                                  "vehicle-iso8855",
                                  params=topviewParams1)
    if sequence_number == 0:
        rangeX = (0.0, 80.0)
        rangeY = (-25, 25)
    elif sequence_number == 3:
        rangeX = (0.0, 250.0)
        rangeY = (-25, 25)
    elif sequence_number == 5:
        rangeX = (0.0, 500.0)
        rangeY = (-5, 50)

    topviewParams2 = draw.TopView.Params(colorMap=colorMap,
                                         topViewSize=(video_width,
                                                      video_height * 2),
                                         background_color=255,
                                         rangeX=rangeX,
                                         rangeY=rangeY,
                                         stepX=5.0,
                                         stepY=5.0,
                                         ignore_classes={"DontCare"},
                                         draw_only_current_image=False)
    drawerTopView2 = draw.TopView(scene, "odom", params=topviewParams2)

    # Video record
    if record_video:
        video_writer = cv.VideoWriter(
            "kitti_tracking_vcd_" + str(sequence_number).zfill(4) + '.mp4',
            cv.VideoWriter_fourcc(*'mp4v'), 30.0,
            (video_width + 400, video_height * 5))

    # Loop over video
    f = 0
    while (True):
        # Capture frame
        ret, img = video_cap.read()
        if ret is not True:
            cv.waitKey(0)
            break

        # Top View
        if draw_images:
            drawerTopView1.add_images({'CAM_LEFT': img}, f)
        topView1 = drawerTopView1.draw(frameNum=f)
        if draw_images:
            drawerTopView2.add_images({'CAM_LEFT': img}, f)
        topView2 = drawerTopView2.draw(frameNum=f)

        # Camera
        img_out = copy.deepcopy(img)
        drawerCamera.draw(img_out, f, _params=imageParams)

        # VCD text viewer
        textImg = frameInfoDrawer.draw(f,
                                       cols=400,
                                       rows=video_height * 5,
                                       _params=imageParams)

        # Stack
        stack1 = np.vstack((img_out, topView1))
        stack1 = np.vstack((stack1, topView2))
        mosaic = np.hstack((stack1, textImg))
        cv.imshow('KITTI Tracking', mosaic)
        cv.waitKey(1)

        if record_video:
            video_writer.write(mosaic)

        # Update frame num
        f += 1

    video_cap.release()
    if record_video:
        video_writer.release()
    cv.destroyAllWindows()