Exemplo n.º 1
0
    def test_PF_ellipse_3(self):
        test_path = "input_images/input_test/ellipse/3/"
        template_path = "input_images/input_test/ellipse/template.png"

        scene = self.get_scene_info(test_path, template_path)

        frame, template_rect, template, img_path, img_list, points_array = scene

        pf = ps5.AppearanceModelPF(frame, template,
                                   num_particles=400,
                                   sigma_exp=10., sigma_dyn=20.,
                                   alpha=.05, template_coords=template_rect)

        axes_shape = (50, 25)
        self.run_filter(pf, img_path, img_list, points_array, axes_shape)
Exemplo n.º 2
0
def part_5():
    """Tracking multiple Targets.

    Use either a Kalman or particle filter to track multiple targets
    as they move through the given video.  Use the sequence of images
    in the TUD-Campus directory.

    Follow the instructions in the problem set instructions.

    Place all your work in this file and this section.
    """

    save_frames = {29: os.path.join(output_dir, 'ps5-5-a-1.png'),
                   56: os.path.join(output_dir, 'ps5-5-a-2.png'),
                   71: os.path.join(output_dir, 'ps5-5-a-3.png')}

    t1 = {
        'x': 60,
        'y': 200,
        'w': 100,
        'h': 100
    }
    t2 = {
        'x': 414,
        'y': 220,
        'w': 100,
        'h': 100
    }
    t3 = {
        'x': 20,
        'y': 172,
        'w': 60,
        'h': 150
    }

    kwargs1 = {
        'num_particles': 400,
        'sigma_exp': 5,
        'sigma_dyn': 15,
        'alpha': 0.05,
    }

    kwargs2 = {
        'num_particles': 250,
        'sigma_exp': 5,
        'sigma_dyn': 10,
        'alpha': 0.,
    }

    kwargs3 = {
        'num_particles': 150,
        'sigma_exp': 5,
        'sigma_dyn': 15,
        'alpha': 0.05,
    }

    imgs_dir = os.path.join(input_dir, "TUD-Campus")

    imgs_list = [f for f in os.listdir(imgs_dir)
                 if f[0] != '.' and f.endswith('.jpg')]
    imgs_list = sorted(imgs_list)

    # Initialize objects
    templates = []
    pf1 = None
    pf2 = None
    pf3 = None

    frame_num = 1
    for img in imgs_list:
        frame = cv2.imread(os.path.join(os.path.join(input_dir, "TUD-Campus"), img))

        # Extract template and initialize (one-time only)
        if len(templates) < 1:
            template1 = frame[int(t1['y']): int(t1['y'] + t1['h']), int(t1['x']): int(t1['x'] + t1['w'])]
            template2 = frame[int(t2['y']):int(t2['y'] + t2['h']), int(t2['x']): int(t2['x'] + t2['w'])]
            templates.append(template1)
            templates.append(template2)
            pf1 = ps5.AppearanceModelPF(frame, template=template1, template_coords=t1, **kwargs1)
            pf2 = ps5.AppearanceModelPF(frame, template=template2, template_coords=t2, **kwargs2)

        if frame_num == 32:
            template3 = frame[int(t3['y']):int(t3['y'] + t3['h']), int(t3['x']): int(t3['x'] + t3['w'])]
            templates.append(template3)
            pf3 = ps5.AppearanceModelPF(frame, template=template3, template_coords=t3, **kwargs3)

        # Process frame
        pf1.process(frame)
        if frame_num <= 29:
            pf2.process(frame)
        if frame_num >= 32:
            pf3.process(frame)

        if True:  # For debugging, it displays every frame
            out_frame = frame.copy()
            pf1.render(out_frame)
            if frame_num <= 29:
                pf2.render(out_frame)
            if frame_num >= 32:
                pf3.render(out_frame)
            cv2.imshow('Tracking', out_frame)
            cv2.waitKey(1)

        # Render and save output, if indicated
        if frame_num in save_frames:
            frame_out = frame.copy()
            pf1.render(frame_out)
            if frame_num <= 29:
                pf2.render(frame_out)
            if frame_num >= 32:
                pf3.render(frame_out)
            cv2.imwrite(save_frames[frame_num], frame_out)

        # if frame_num == 71:
        #     frame_out = frame.copy()
        #     pf1.render(frame_out)
        #     pf2.render(frame_out)
        #     pf3.render(frame_out)
        #     cv2.imwrite(save_frames[frame_num], frame_out)

        # Update frame number
        frame_num += 1
Exemplo n.º 3
0
def part_5():
    """Tracking multiple Targets.

    Use either a Kalman or particle filter to track multiple targets
    as they move through the given video.  Use the sequence of images
    in the TUD-Campus directory.

    Follow the instructions in the problem set instructions.

    Place all your work in this file and this section.
    """

    save_frames = {29: os.path.join(output_dir, 'ps5-5-a-1.png'),
                   56: os.path.join(output_dir, 'ps5-5-a-2.png'),
                   71: os.path.join(output_dir, 'ps5-5-a-3.png')}

    # kf = ps5.KalmanFilter(105, 260, R=0.2 * np.eye(2))
    # template_rect = {'x': 55, 'y': 155, 'w': 100, 'h': 215}
    #
    # run_kalman_filter(kf,
    #                   os.path.join(input_dir, "TUD-CAMPUS"),
    #                   NOISE_1,
    #                   "matching",
    #                   save_frames,
    #                   template_rect)

    # kf = ps5.KalmanFilter(10, 235, R=0.2 * np.eye(2))
    #
    # kalman.run_kalman_filter(kf,
    #                   os.path.join(input_dir, "TUD-CAMPUS"),
    #                   NOISE_1,
    #                   "matching",
    #                   save_frames,
    #                   None)

    # kf = ps5.KalmanFilter(300, 235, R=0.2 * np.eye(2))
    #
    # kalman_2.run_kalman_filter(kf,
    #                          os.path.join(input_dir, "TUD-CAMPUS"),
    #                          NOISE_1,
    #                          "matching",
    #                          save_frames,
    #                          None)
    # ------------------------------------ 15, 26

    num_particles_1 = 1300
    sigma_md_1 = 10
    sigma_dyn_1 = 18
    filter_1 = None
    box_1 = {'x_min': 290, 'x_max': 340, 'y_min': 175, 'y_max': 300}

    num_particles_2 = 600
    sigma_md_2 = 10
    sigma_dyn_2 = 13
    filter_2 = None
    box_2 = {'x_min': 65, 'x_max': 175, 'y_min': 125, 'y_max': 355}

    num_particles_3 = 800
    sigma_md_3 = 30
    sigma_dyn_3 = 15
    filter_3 = None
    box_3 = {'x_min': 0, 'x_max': 100, 'y_min': 125, 'y_max': 400}

    imgs_list = [f for f in os.listdir("input_images/TUD-Campus")
                 if f[0] != '.' and f.endswith('.jpg')]
    imgs_list.sort()

    frame_num = 1
    template_1 = get_template_1()
    template_2 = get_template_2()
    template_3 = get_template_3()

    # Loop over video (till last frame or Ctrl+C is presssed)
    for img in imgs_list:

        frame = cv2.imread(os.path.join("input_images/TUD-Campus", img))
        if filter_1 is None:
            filter_1 = ps5.AppearanceModelPF(frame, template_1, num_particles=num_particles_1, sigma_exp=sigma_md_1,
                                             sigma_dyn=sigma_dyn_1, alpha=0.0, in_gray_mode=False,
                                             use_threshold=False, threshold=40.0,
                                             use_box_initialization=True, box=box_1)
        if filter_2 is None:
            filter_2 = ps5.AppearanceModelPF(frame, template_2, num_particles=num_particles_2, sigma_exp=sigma_md_2,
                                             sigma_dyn=sigma_dyn_2, alpha=0.0, in_gray_mode=False,
                                             use_box_initialization=True, box=box_2)
        if filter_3 is None:
            filter_3 = ps5.AppearanceModelPF(frame, template_3, num_particles=num_particles_3, sigma_exp=sigma_md_3,
                                             sigma_dyn=sigma_dyn_3, alpha=0.0, in_gray_mode=False,
                                             use_box_initialization=True, box=box_3)

        process_filters(filter_1, filter_2, filter_3, frame, frame_num, save_frames=save_frames)

        frame_num += 1