Beispiel #1
0
def LaneDetector_Test(pic_dir, ipm, LaneMarker, laneDetectorModel):
    filenames = io_util.load_fnames(pic_dir)
    ftmp1 = cv2.imread(os.path.join(pic_dir, filenames[0]),
                       cv2.IMREAD_GRAYSCALE).astype('uint8')

    for i in range(len(filenames)):
        display.show_progressbar(i, filenames)
        ftmp = cv2.imread(os.path.join(pic_dir, filenames[i]),
                          cv2.IMREAD_GRAYSCALE).astype('float')
        ipm_img = ipm.imageToIPM(ftmp)
        lanemarker_img = laneMarker.detect(ipm_img, 5)
        threshold_img = image_util.thresholdImage(lanemarker_img, 95)
        # cv2.imshow('ipm', ipm_img.astype('uint8'))
        # cv2.imshow('lanemarker', lanemarker_img.astype('uint8'))
        # cv2.imshow('threshold_img', threshold_img.astype('uint8'))
        degree = 1
        lanes = laneDetectorModel.updateLaneModel(threshold_img, degree)
        # Save lanes
        for lane in lanes:
            xns, yns = convertIPMLaneToLane(lane, ipm)
            pt1 = (int(xns[0]), int(yns[0]))
            pt2 = (int(xns[-1]), int(yns[-1]))
            cv2.line(ftmp, pt1, pt2, (255, 0, 0), 5)
        cv2.imshow('img', ftmp.astype('uint8'))
        cv2.waitKey(15)
    print ""
    cv2.destroyAllWindows()
Beispiel #2
0
def following_analysis(relativepath, distances_fn='follow_distance_speed.json'):
    filenames = io_util.load_fnames(relativepath)
    follow_dict = io_util.load_json(relativepath, distances_fn)
    speed_dict = io_util.load_json(relativepath, 'true_speed.json')
    distances = []
    speeds = []
    true_speeds = []
    for fname in filenames:
        if fname in follow_dict:
            distances.append(follow_dict[fname]['distance'])
            speeds.append(follow_dict[fname]['speed'])
            true_speeds.append(speed_dict[fname])

    print 'Pearson coefficient and pvalue:',
    print pearsonr(distances, speeds)
    print 'Pearson coefficient and pvalue with true speed:',
    print pearsonr(distances, true_speeds)


    plt.figure(1)
    plt.scatter(distances, speeds)
    plt.xlabel('Following distance to car in front (ft)')
    plt.xlim([20, 60])
    plt.ylim([60, 80])
    plt.ylabel('Speed (MPH)')
    plt.show()

    plt.figure(2)
    plt.scatter(distances, true_speeds)
    plt.xlabel('Following distance to car in front (ft)')
    plt.xlim([20, 60])
    plt.ylim([60, 72])
    plt.ylabel('Speed (MPH)')
    plt.show()
def VehicleDetector_Test(svm, pic_dir):
    vd = VehicleDetector.VehicleDetector(svm)
    font = cv2.FONT_HERSHEY_PLAIN

    # Read the filenames from the list.txt file
    init_frame = 1000
    filenames = io_util.load_fnames(pic_dir)[init_frame:init_frame + 50]

    for i in range(len(filenames)):
        display.show_progressbar(i, filenames)
        ftmp = cv2.resize(
            cv2.imread(os.path.join(pic_dir, filenames[i]),
                       cv2.IMREAD_GRAYSCALE).astype('uint8'), (320, 240))
        boxes = vd.update(ftmp)
        for vehicle_box in boxes:
            [x1, y1, x2, y2] = vehicle_box['box'].astype(np.int64)
            box_id = vehicle_box['id']
            cv2.rectangle(ftmp, (x1, y1), (x2, y2), color=(255, 0, 0))
            cv2.putText(ftmp,
                        "Id:{}".format(box_id), (x1, y1 - 5),
                        font,
                        1,
                        color=(255, 0, 0))
        cv2.imshow('img', ftmp)
        cv2.waitKey(1)
    print ""
    cv2.destroyAllWindows()
def get_nearby_lanes(relativepath, threshold=0.125, 
                        lane_fname='lanes_output_new.json', vp_fname='vp.json'):
    print 'Finding nearby lanes for {}'.format(relativepath)
    filenames = io_util.load_fnames(relativepath)
    fname_lanes_dict = io_util.load_json(relativepath, lane_fname)
    vp_dict = io_util.load_json(relativepath, vp_fname)
    vp = vp_dict['vp']
        
    initimg = cv2.imread(os.path.join(relativepath, filenames[0]), cv2.IMREAD_GRAYSCALE)
    imgshape = initimg.shape
    imgheight, imgwidth = imgshape
    origPts, destPts = image_util.locateROI(imgshape, x_frac=1/3., 
                                        y_frac_top=1/25., y_frac_bot=1/6., 
                                        vp=vp)

    ipm = IPM.IPM(imgshape, imgshape, origPts, destPts)

    new_fname_json = {}
    for fname in filenames:
        if fname in fname_lanes_dict:
            lanes = fname_lanes_dict[fname]
            distances = []
            for lane in lanes:
                dist = distToCenter(lane, ipm)
                distances.append((dist, lane))
            if len(distances) >= 2:
                distances.sort(key=lambda x:abs(x[0]))
                if abs(abs(distances[0][0]) - abs(distances[1][0])) < threshold:
                    if (distances[0][0] <= 0 and distances[1][0] >= 0):
                        new_fname_json[fname] = [distances[0][1], distances[1][1]]
                    elif (distances[0][0] >= 0 and distances[1][0] <= 0):
                        new_fname_json[fname] = [distances[1][1], distances[0][1]]
                else:
                    print abs(abs(distances[0][0]) - abs(distances[1][0]))
    return new_fname_json
def read_true_speed(relativepath, speed_recognizer):
    print 'Extracting true speed from {}'.format(relativepath)
    filenames = io_util.load_fnames(relativepath)
    output_dict = {}
    for fname in filenames:
        img = cv2.imread(os.path.join(relativepath, fname))
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        curr_speed = speedrec.predict(gray_img)
        output_dict[fname] = curr_speed
    return output_dict
def ObjectTracker_Test(pic_dir):
    # Read the filenames from the list.txt file
    filenames = io_util.load_fnames(pic_dir)

    init_frame = 15
    confidence_thresh = 7
    filenames = io_util.load_fnames(pic_dir)[init_frame:init_frame + 50]

    ftmp1 = cv2.imread(os.path.join(pic_dir, filenames[init_frame]),
                       cv2.IMREAD_GRAYSCALE).astype('uint8')
    # Top car
    left = 295
    right = 335
    top = 225
    bot = 265

    # Alternatively, track the side car
    # left = 440
    # right = 550
    # top = 205
    # bot = 305
    tracker = ObjectTracker.dlib_ObjectTracker(ftmp1, [left, top, right, bot])
    [x1, y1, x2, y2] = tracker.get_position().astype(np.int64)
    cv2.rectangle(ftmp1, (x1, y1), (x2, y2), color=(255, 0, 0))
    cv2.imshow('img', ftmp1)
    cv2.waitKey(1)

    for i in xrange(init_frame, len(filenames)):
        display.show_progressbar(i, filenames)
        ftmp = cv2.imread(os.path.join(pic_dir, filenames[i]),
                          cv2.IMREAD_GRAYSCALE).astype('uint8')
        value = tracker.update(ftmp)
        [x1, y1, x2, y2] = tracker.get_position().astype(np.int64)
        cv2.rectangle(ftmp, (x1, y1), (x2, y2), color=(255, 0, 0))
        cv2.imshow('img', ftmp)
        if value < confidence_thresh:
            print '\n\tlost object'
            break
        else:
            cv2.waitKey(1)
    print ""
    cv2.destroyAllWindows()
Beispiel #7
0
def get_vps(relativepath, boxes_fn):
    lanewidth = 5
    filenames = io_util.load_fnames(relativepath)
    init_img = cv2.imread(os.path.join(relativepath, filenames[0]), 
                                        cv2.IMREAD_GRAYSCALE).astype('uint8')
    lanemarker = LaneMarker.NietoLaneMarker()
    vpEstimator = VPEstimator.MSAC_LS(init_img.shape)

    boxes_dict = io_util.load_json(relativepath, boxes_fn)
    data = []
    for i in range(len(filenames)):
        fname = filenames[i]
        display.show_progressbar(i, filenames)
        color_img = cv2.imread(os.path.join(relativepath, filenames[i]))
        gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY).astype('uint8')
        # block region of the image - top, text, and vehicles
        blockedimg = image_util.blockTopImage(gray_img, 0.6)
        #cv2.imshow('block',blockedimg.astype('uint8'))
        image_util.block_EBRON(blockedimg)
        if fname in boxes_dict:
            for box_dict in boxes_dict[fname]:
                [x1, y1, x2, y2] = box_dict['box']
                blockedimg[y1:y2, x1:x2] = 0
                color_img[y1:y2, x1:x2] = 0

        threshimg = image_util.thresholdImage(lanemarker.detect(blockedimg, lanewidth))
        lines = cv2.HoughLines(threshimg, 1, cv2.cv.CV_PI/180, 30)
        if lines is None:
            continue
        linesegments = VPEstimator.preprocess_lines(lines[0])
        if len(linesegments) < 5:
            continue
        vps, lineSegmentsClusters, numInliers = vpEstimator.multipleVPEstimation(linesegments, 1)

        for line in linesegments:
            [x1, y1, x2, y2] = line
            cv2.line(threshimg, (int(x1), int(y1)), (int(x2), int(y2)), (255, 255, 255), 1)
            cv2.line(color_img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 1)
        if vps is not None:
            x, y = vps[0][0][0], vps[0][1][0]
            cv2.circle(color_img, (int(x), int(y)), 5, (255, 0, 0), thickness=3)
            cv2.circle(threshimg, (int(x), int(y)), 5, (255, 255, 255),thickness=3)
            data.append([x, y])
        cv2.imshow('color image', color_img)
        #cv2.imshow('thresh image', threshimg)
        cv2.waitKey(1pospost)
        #break
    print ""
    cv2.destroyAllWindows()

    return np.array(data)
Beispiel #8
0
def LineTracker_Test(pic_dir, ipm, laneMarker, laneDetector, init_line=None):
    # Read the filenames from the list.txt file
    filenames = io_util.load_fnames(pic_dir)
    ftmp1 = cv2.imread(os.path.join(pic_dir, filenames[0]),
                       cv2.IMREAD_GRAYSCALE).astype('uint8')
    if init_line is None:
        init_line = image_util.Line(274, 281, 125, 399)
    x1, y1 = ipm.pointToIPM((init_line.x1, init_line.y1))
    x2, y2 = ipm.pointToIPM((init_line.x2, init_line.y2))
    ipm_line = image_util.Line(x1, y1, x2, y2)

    tracker = LaneDetector.LineTracker(ipm_line, ftmp1.shape)
    display.draw_lines(ftmp1, [init_line])
    cv2.imshow('img', ftmp1)

    for i in range(len(filenames)):
        print '\r{}'.format(filenames[i]),
        ftmp = cv2.imread(os.path.join(pic_dir, filenames[i]),
                          cv2.IMREAD_GRAYSCALE).astype('float')
        ipm_img = ipm.imageToIPM(ftmp)
        lanemarker_img = laneMarker.detect(ipm_img, 5)
        threshold_img = image_util.thresholdImage(lanemarker_img, 95)
        # cv2.imshow('ipm', ipm_img.astype('uint8'))
        # cv2.imshow('lanemarker', lanemarker_img.astype('uint8'))
        # cv2.imshow('threshold_img', threshold_img.astype('uint8'))
        lane = tracker.update(threshold_img, laneDetector)
        if lane is not None:
            xns, yns = convertIPMLaneToLane(lane, ipm)
            pt1 = (int(xns[0]), int(yns[0]))
            pt2 = (int(xns[-1]), int(yns[-1]))
            cv2.line(ftmp, pt1, pt2, (255, 0, 0), 5)
            cv2.imshow('img', ftmp.astype('uint8'))
            cv2.waitKey(15)
        else:
            break
    print ""
    cv2.destroyAllWindows()
Beispiel #9
0
def lane_detection_validation(relativepath,
                              test_lanes_fn,
                              true_lanes_fn='manual_lanes.json',
                              stepsize=15,
                              vp_fn='vp.json'):
    print 'Lane Detection Validation for {}'.format(relativepath)
    filenames = io_util.load_fnames(relativepath)
    test_dict = io_util.load_json(relativepath, test_lanes_fn)
    hand_dict = io_util.load_json(relativepath, true_lanes_fn)

    vp_dict = io_util.load_json(relativepath, vp_fn)
    vp = vp_dict['vp']

    true_positives = 0.
    false_positives = 0.
    false_negatives = 0.
    for fname in filenames[::stepsize]:
        hand_lanes = []
        if fname in hand_dict:
            for line in hand_dict[fname]:
                [x1, y1] = line[0]
                [x2, y2] = line[1]
                hand_lanes.append(image_util.Line(x1, y1, x2, y2))
        test_lanes = []
        if fname in test_dict:
            for line in test_dict[fname]:
                [x1, y1] = line[0]
                [x2, y2] = line[1]
                test_lanes.append(image_util.Line(x1, y1, x2, y2))

        test_lanes = remove_duplicates(test_lanes)

        #hand_lanes = get_nearby_lanes(hand_lanes, vp)
        #test_lanes = get_nearby_lanes(test_lanes, vp)

        img = cv2.imread(os.path.join(relativepath, fname))
        display.draw_lines(img, hand_lanes, (255, 0, 0))
        display.draw_lines(img, test_lanes, (0, 0, 255))
        cv2.imshow('orig', img)

        for test_line in test_lanes[:]:
            for hand_line in hand_lanes[:]:
                if lines_close_enough(test_line, hand_line):
                    hand_lanes.remove(hand_line)
                    test_lanes.remove(test_line)
                    true_positives += 1
                    break

        false_positives += len(test_lanes)
        false_negatives += len(hand_lanes)

        if len(test_lanes) > 0:
            img = cv2.imread(os.path.join(relativepath, fname))
            # hand_lanes = []
            # if fname in hand_dict:
            #     for line in hand_dict[fname]:
            #         [x1, y1] = line[0]
            #         [x2, y2] = line[1]
            #         hand_lanes.append(image_util.Line(x1, y1, x2, y2))
            # test_lanes = []
            # if fname in test_dict:
            #     for line in test_dict[fname]:
            #         [x1, y1] = line[0]
            #         [x2, y2] = line[1]
            #         test_lanes.append(image_util.Line(x1, y1, x2, y2))
            display.draw_lines(img, hand_lanes, (255, 0, 0))
            display.draw_lines(img, test_lanes, (0, 0, 255))
            cv2.imshow('FP - truth is blue, red is code', img)
            # angle_diff = abs(hand_lanes[0].getAngle() - test_lanes[0].getAngle())/90.
            # print angle_diff*90.
            # intercept_diff = abs(hand_lanes[0].getIntercept() - test_lanes[0].getIntercept())
            # print intercept_diff
            #print 'FP ',
            for i in range(len(test_lanes)):
                print fname, test_lanes[i].x1, test_lanes[i].y1, test_lanes[
                    i].x2, test_lanes[i].y2
            cv2.waitKey(0)

        if len(hand_lanes) > 0:
            #print 'FN ',
            #for i in range(len(hand_lanes)):
            #    print fname, hand_lanes[i].x1, hand_lanes[i].y1, hand_lanes[i].x2, hand_lanes[i].y2
            img = cv2.imread(os.path.join(relativepath, fname))
            display.draw_lines(img, hand_lanes, (255, 0, 0))
            display.draw_lines(img, test_lanes, (0, 0, 255))
            cv2.imshow('FN - truth is blue, red is code', img)
            cv2.waitKey(0)

    precision = 0
    recall = 0
    f1 = 0
    if true_positives != 0:
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)
        f1 = 2 * (precision * recall) / (precision + recall)
    print '\tPrecision: {} ({}/{})'.format(precision, true_positives,
                                           (true_positives + false_positives))
    print '\tRecall: {} ({}/{})'.format(recall, true_positives,
                                        (true_positives + false_negatives))
    print '\tF1 Score: {}'.format(f1)
    return true_positives, false_positives, false_negatives
def filter_speed(relativepath, speed_fn, plot=True, dt=1 / 15.):
    print 'Starting filter analysis on {} for {}'.format(
        relativepath, speed_fn)
    filenames = io_util.load_fnames(relativepath)
    true_speed_dict = io_util.load_json(relativepath, 'true_speed.json')
    #true_speed_dict = io_util.load_json(relativepath, 'speed_readings.json')
    raw = io_util.load_json(relativepath, speed_fn)
    init_speed_est = 75  # in mph
    max_acc = 3.5  # in meters per sec
    names = ['AccLimit', 'KF']
    filters = [
        Filters.AccLimitFilter(init_speed_est, max_acc),
        Filters.KalmanFilter(np.array([[init_speed_est], [0]]),
                             np.array([[1, 0], [0, 1]]),
                             np.array([[1, 0], [0, 1]]))
    ]

    est_speeds = [[], []]
    true_speed = []
    obv_speed = []
    cp = []
    mask = []

    acclimit_dict = {}

    for fname in filenames:
        i = 0
        obv = None
        if fname in raw:
            obv = raw[fname]
        for flter in filters:
            flter.predict(dt)
            if obv and not np.isinf(obv):
                flter.update(obv, dt)
            est = flter.get_estimate()
            if isinstance(est, np.ndarray):
                est = est[0]
            est_speeds[i].append(est)
            if names[i] == 'AccLimit':
                acclimit_dict[fname] = est
            i += 1

        if obv:
            cp.append(obv)
            mask.append(1)
        else:
            cp.append(init_speed_est)
            mask.append(0)
        obv_speed.append(obv)
        true_speed.append(true_speed_dict[fname])

    mask = np.array(mask)
    names.append('Butter LPF')
    names.append('Median Filter')
    names.append('Baseline')

    perc_obv = 1 - np.count_nonzero(np.equal(np.array(obv_speed),
                                             None)) / float(len(obv_speed))
    print '\t% video w/ est {}'.format(perc_obv)

    if perc_obv < 0.5:
        return None

    est_speeds.append(Filters.butter_lpf(cp, cutoff=1, fs=15, order=6))
    est_speeds.append(Filters.median_filter(cp))
    est_speeds.append(np.array(cp))

    ret = []
    der = []
    for i in range(len(names) - 1, -1, -1):
        name = names[i]
        est_arr = np.array(est_speeds[i])
        true_arr = np.array(true_speed)
        l1_diff = np.abs(est_arr[np.where(mask == 1)] -
                         true_arr[np.where(mask == 1)])
        derivative_est = savgol_filter(est_arr,
                                       window_length=31,
                                       polyorder=2,
                                       deriv=1)
        derivative_true = savgol_filter(true_arr,
                                        window_length=31,
                                        polyorder=2,
                                        deriv=1)
        deriv_diff = np.abs(derivative_est[np.where(mask == 1)] -
                            derivative_true[np.where(mask == 1)])

        # plt.figure(1)
        # plt.title('Using Automatic Markings')
        # plt.plot(np.arange(len(obv_speed)), obv_speed, label='Raw Est Speed', color='g')
        # plt.plot(np.arange(len(true_speed)), true_speed, label='True Speed', color='b')
        # plt.plot(np.arange(len(est_speeds[i])), est_speeds[i], label=names[i], color='r')
        # plt.legend(loc='lower right')
        # plt.ylim([20, 120])
        # plt.ylabel('Speed (MPH)')
        # plt.xlabel('Frame')
        # plt.show()

        # plt.figure(1)
        # plt.title('Using Automatic Markings')
        # plt.plot(np.arange(derivative_est.shape[0]), derivative_est, color='r', label=names[i])
        # plt.plot(np.arange(derivative_true.shape[0]), derivative_true, color='b', label='True speed')
        # plt.legend(loc='lower right')
        # plt.ylim([-0.5, 0.5])
        # plt.ylabel('Acceleration (MPH per second)')
        # plt.xlabel('Frame')
        # plt.show()

        print '\t' + name,
        print 'L1 Diff mean {}, std {}'.format(np.mean(l1_diff),
                                               np.std(l1_diff))
        print 'Derivative Diff mean {}, std {}'.format(np.mean(deriv_diff),
                                                       np.std(deriv_diff))
        ret.append(l1_diff)
        der.append(deriv_diff)

    # if plot:
    #     plt.figure(1)
    #     plt.plot(np.arange(len(true_speed)), true_speed, label='True Speed')
    #     plt.plot(np.arange(len(obv_speed)), obv_speed, label='Raw Est Speed')
    #     for i in range(len(names)):
    #         plt.plot(np.arange(len(est_speeds[i])), est_speeds[i], label=names[i])

    #     plt.legend(loc='lower right')
    #     plt.ylim([0, 120])
    #     plt.show()

    return (ret, perc_obv, der, acclimit_dict)
Beispiel #11
0
def vehicle_detection_validation(full_path,
                                 detector_filepath,
                                 min_area=None,
                                 stepsize=15):
    print 'Starting vehicle detection validation test for {} using {}'.format(
        full_path, detector_filepath)
    filenames = io_util.load_fnames(full_path)
    detector_fn = detector_filepath.split('/')[-1].split('.svm')[0]
    test_dict_fn = 'boxes_output_{}_8.5.json'.format(detector_fn)
    #test_dict_fn = 'boxes_output_{}_8.5_run2.json'.format(detector_fn)
    test_dict = io_util.load_json(full_path, test_dict_fn)
    #test_dict = io_util.load_vehicle_boxes(full_path, use_hand_labeled=False)
    hand_dict = io_util.load_vehicle_boxes(full_path, use_hand_labeled=True)

    true_positives = 0.
    false_positives = 0.
    false_negatives = 0.
    for fname in filenames[::stepsize]:
        hand_boxes = []
        if fname in hand_dict:
            hand_boxes = hand_dict[fname][:]
        test_boxes = []
        if fname in test_dict:
            test_boxes = test_dict[fname]

        if min_area:
            for test_boxdict in test_boxes[:]:
                test_box = test_boxdict['box']
                if image_util.compute_area(test_box) < min_area:
                    test_boxes.remove(test_boxdict)
            for hand_box in hand_boxes[:]:
                if image_util.compute_area(hand_box) < min_area:
                    hand_boxes.remove(hand_box)

        img1 = cv2.imread(os.path.join(full_path, fname))
        display.draw_boxes(img1, hand_boxes)
        display.draw_boxdicts(img1, test_boxes, (0, 255, 0))
        cv2.imshow('original', img1)

        test_boxes = remove_overlapping(test_boxes, full_path, fname)

        for test_boxdict in test_boxes[:]:
            test_box = test_boxdict['box']
            for hand_box in hand_boxes[:]:
                if boxes_close_enough(test_box, hand_box):
                    hand_boxes.remove(hand_box)
                    test_boxes.remove(test_boxdict)
                    true_positives += 1
                    break

        false_positives += len(test_boxes)
        false_negatives += len(hand_boxes)

        if len(hand_boxes) > 0:
            # hand_boxes = []
            # if fname in hand_dict:
            #     hand_boxes = hand_dict[fname]
            # test_boxes = []
            # if fname in test_dict:
            #     test_boxes = test_dict[fname]
            img1 = cv2.imread(os.path.join(full_path, fname))
            # display.draw_boxes(img1, hand_boxes)
            # display.draw_boxdicts(img1, test_boxes, (0,0,255))
            # cv2.imshow('FN - truth is blue, red is code', img1)
            # while True:
            #     if cv2.waitKey(1) & 0xFF == ord('s'):
            #         cv2.imwrite(os.path.join(full_path, 'fn_{}'.format(fname)), img1)
            #         break
            #     elif cv2.waitKey(1) & 0xFF == ord(' '):
            #         break

        if len(test_boxes) > 0:
            # hand_boxes = []
            # if fname in hand_dict:
            #     hand_boxes = hand_dict[fname]
            # test_boxes = []
            # if fname in test_dict:
            #     test_boxes = test_dict[fname]
            img1 = cv2.imread(os.path.join(full_path, fname))
            display.draw_boxes(img1, hand_boxes)
            display.draw_boxdicts(img1, test_boxes, (0, 0, 255))
            # for box in test_boxes:
            #     [x1, y1, x2, y2] = box['box']
            #     width = x2 - x1
            #     height = y2 - y1
            #     print '{},{},{},{},{},{},{}'.format(x1 + width/2., y1 + height/2.,  x1, y1, width, height,fname)
            cv2.imshow('FP - truth is blue, red is code', img1)
            cv2.waitKey(0)
            # while True:
            #     if cv2.waitKey(1) & 0xFF == ord('s'):
            #         cv2.imwrite(os.path.join(full_path, 'fp_{}'.format(fname)), img1)
            #         break
            #     elif cv2.waitKey(1) & 0xFF == ord(' '):
            #         break

    precision = 0
    recall = 0
    f1 = 0
    if true_positives != 0:
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)
        f1 = 2 * (precision * recall) / (precision + recall)
    print '\tPrecision: {} ({}/{})'.format(precision, true_positives,
                                           (true_positives + false_positives))
    print '\tRecall: {} ({}/{})'.format(recall, true_positives,
                                        (true_positives + false_negatives))
    print '\tF1 Score: {}'.format(f1)
    return true_positives, false_positives, false_negatives
Beispiel #12
0
def following_main(relativepath,
                            lanes_fn='nearby_lanes.json', 
                            spd_fn='speed_acclimit.json', vp_fn='vp.json', 
                            boxes_fn='boxes_output_detector_session2_raw_8.5.json',
                            cv_display=True):
    
    filenames = io_util.load_fnames(relativepath)

    fname_lanes_dict = io_util.load_json(relativepath, lanes_fn)
    vp_dict = io_util.load_json(relativepath, vp_fn)
    vp = vp_dict['vp']
    fname_boxes_dict = io_util.load_json(relativepath, boxes_fn)
    speed_dict = io_util.load_json(relativepath, spd_fn)
        
    initimg = cv2.imread(os.path.join(relativepath, filenames[0]), cv2.IMREAD_GRAYSCALE)
    imgshape = initimg.shape
    imgheight, imgwidth = imgshape
    origPts, destPts = image_util.locateROI(imgshape, x_frac=1/10., 
                                            y_frac_top=1/60., y_frac_bot=1/6., 
                                            vp=vp)

    for ii in xrange(len(origPts)):
        if ii == (len(origPts) - 1):
            cv2.line(initimg, tuple(origPts[ii]), tuple(origPts[0]), (255, 255, 255), 2)
        else:   
            cv2.line(initimg, tuple(origPts[ii]), tuple(origPts[ii+1]), (255, 255, 255), 2)
    cv2.imshow('initimg', initimg)
    cv2.waitKey(0)

    ipm = IPM.IPM(imgshape, imgshape, origPts, destPts)
    lanemarker = LaneMarker.NietoLaneMarker()
    follow_dict = {}
    for i in range(len(filenames)):
        fname = filenames[i]
        display.show_progressbar(i, filenames)
        if fname in fname_lanes_dict:
            img = cv2.imread(os.path.join(relativepath, fname))
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype('float')
            image_util.block_EBRON(gray_img)

            new_pts = []
            if fname in fname_boxes_dict:
                box_dicts = fname_boxes_dict[fname] 
                for box_dict in box_dicts:
                    x1, y1, x2, y2 = np.array(box_dict['box']).astype(np.int64)
                    gray_img[y1:y2,x1:x2] = 0

                    bottom_center = [(x1 + x2)/2., y2]
                    new_pt = ipm.pointToIPM(bottom_center)
                    new_pts.append(new_pt)

            ipm_img = ipm.imageToIPM(gray_img)
            tmp = cv2.cvtColor(ipm_img.astype('uint8').copy(), cv2.COLOR_GRAY2BGR)

            lanes = fname_lanes_dict[fname]
            lines = []
            for lane in lanes:
                [x1, y1] = ipm.pointToIPM(lane[0])
                [x2, y2] = ipm.pointToIPM(lane[1])
                lines.append(image_util.Line(x1, y1, x2, y2))
            slope, b = computeXLineDifferences(lines[0], lines[1])
            for new_pt in new_pts:
                [x, y] = new_pt
                if (x > lines[0].x1 and x > lines[0].x2 and 
                    x < lines[1].x1 and x < lines[1].x2 and 
                    y < imgshape[0] and y > 0):
                    cv2.circle(tmp, (int(x), int(y)), 5, (255, 0, 0), thickness=3)
                    conversion = 12./(slope * y + b)
                    distance = conversion*(imgshape[0] - y)
                    #print distance
                    follow_dict[fname] = {'distance': distance, 'speed':speed_dict[fname]}
                    display.draw_lines(tmp, lines, color=(0, 255, 0))
                    if cv_display:
                        cv2.imshow('Tmp', tmp)
                        cv2.waitKey(1)        
    print ""
    cv2.destroyAllWindows()
    io_util.save_json(relativepath, 'follow_distance_speed.json', follow_dict)
    print 'Done'
Beispiel #13
0
def svm_detection_validation(full_path,
                             detector_filepath,
                             min_area=None,
                             load_boxes=False,
                             stepsize=15):
    print 'Starting svm validation test for {}'.format(full_path)
    filenames = io_util.load_fnames(full_path)
    hand_dict = io_util.load_vehicle_boxes(full_path, use_hand_labeled=True)
    true_positives = 0.
    false_positives = 0.
    false_negatives = 0.
    detector_fn = detector_filepath.split('/')[-1].split('.svm')[0]
    if min_area:
        test_dict_fn = 'boxes_{}.json'.format(detector_fn)
    else:
        test_dict_fn = 'boxes_nominarea_{}.json'.format(detector_fn)
    test_dict = {}
    if load_boxes:
        test_dict = io_util.load_json(full_path, test_dict_fn)
    else:
        dlib_detector = dlib.simple_object_detector(detector_filepath)

    for fname in filenames[::stepsize]:
        if fname not in hand_dict:
            continue
        hand_boxes = hand_dict[fname]
        test_boxes = []
        if load_boxes:
            test_boxes = test_dict[fname]
        else:
            img = cv2.imread(os.path.join(full_path, fname))
            found_list = dlib_detector(img)
            for dlib_rect in found_list:
                test_boxes.append(convert_dlib_rect(dlib_rect))
            test_dict[fname] = np.array(test_boxes)

        if min_area:
            for test_box in test_boxes[:]:
                if image_util.compute_area(test_box) < min_area:
                    test_boxes.remove(test_box)
            for hand_box in hand_boxes[:]:
                if image_util.compute_area(hand_box) < min_area:
                    hand_boxes.remove(hand_box)

        for test_box in test_boxes[:]:
            for hand_box in hand_boxes[:]:
                if boxes_close_enough(test_box, hand_box):
                    hand_boxes.remove(hand_box)
                    test_boxes.remove(test_box)
                    true_positives += 1
                    break

        false_positives += len(test_boxes)
        false_negatives += len(hand_boxes)

    if not load_boxes:
        io_util.save_json(full_path, test_dict_fn, test_dict)

    precision = 0
    recall = 0
    if true_positives != 0:
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)
    print '\tPrecision: {} ({}/{})'.format(precision, true_positives,
                                           (true_positives + false_positives))
    print '\tRecall: {} ({}/{})'.format(recall, true_positives,
                                        (true_positives + false_negatives))
    return true_positives, false_positives, false_negatives
Beispiel #14
0
def speed_estimation_main(
        relativepath,
        uniform_conversion=False,
        lanes_fn='nearby_lanes.json',
        true_spd_fn='true_speed.json',
        vp_fn='vp.json',
        boxes_fn='boxes_output_detector_session2_raw_8.5.json'):
    print 'Starting speed estimation for {}'.format(relativepath)

    filenames = io_util.load_fnames(relativepath)

    fname_lanes_dict = io_util.load_json(relativepath, lanes_fn)
    vp_dict = io_util.load_json(relativepath, vp_fn)
    vp = vp_dict['vp']
    fname_boxes_dict = io_util.load_json(relativepath, boxes_fn)

    initimg = cv2.imread(os.path.join(relativepath, filenames[0]),
                         cv2.IMREAD_GRAYSCALE)
    imgshape = initimg.shape
    imgheight, imgwidth = imgshape
    origPts, destPts = image_util.locateROI(imgshape,
                                            x_frac=1 / 3.,
                                            y_frac_top=1 / 25.,
                                            y_frac_bot=1 / 6.,
                                            vp=vp)

    ipm = IPM.IPM(imgshape, imgshape, origPts, destPts)
    lanemarker = LaneMarker.NietoLaneMarker()

    if uniform_conversion:
        slopes = []
        bs = []
        for fname in filenames:
            if fname in fname_lanes_dict:
                lanes = fname_lanes_dict[fname]
                lines = []
                for lane in lanes:
                    [x1, y1] = ipm.pointToIPM(lane[0])
                    [x2, y2] = ipm.pointToIPM(lane[1])
                    lines.append(image_util.Line(x1, y1, x2, y2))
                slope, b = computeXLineDifferences(lines[0], lines[1])
                slopes.append(slope)
                bs.append(b)
        mean_slope = np.mean(np.array(slopes))
        mean_b = np.mean(np.array(bs))
        speed_detector = SpeedEstimator.Lane_SpeedEstimator(
            lanemarker, mean_slope, mean_b)
    else:
        speed_detector = SpeedEstimator.Lane_SpeedEstimator(lanemarker)

    output_left = {}
    output_right = {}
    for i in range(len(filenames)):
        fname = filenames[i]
        display.show_progressbar(i, filenames)
        if fname in fname_lanes_dict:
            img = cv2.imread(os.path.join(relativepath, fname))
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype('float')
            image_util.block_EBRON(gray_img)

            if fname in fname_boxes_dict:
                box_dicts = fname_boxes_dict[fname]
                for box_dict in box_dicts:
                    x1, y1, x2, y2 = np.array(box_dict['box']).astype(np.int64)
                    gray_img[y1:y2, x1:x2] = 0

            ipm_img = ipm.imageToIPM(gray_img)
            tmp = cv2.cvtColor(
                ipm_img.astype('uint8').copy(), cv2.COLOR_GRAY2BGR)

            lanes = fname_lanes_dict[fname]
            lines = []
            for lane in lanes:
                [x1, y1] = ipm.pointToIPM(lane[0])
                [x2, y2] = ipm.pointToIPM(lane[1])
                lines.append(image_util.Line(x1, y1, x2, y2))
            display.draw_lines(tmp, lines, color=(0, 255, 0))
            cv2.imshow('Tmp', tmp)
            cv2.waitKey(0)

            speed_est_L, speed_est_R = speed_detector.update(
                ipm_img, lines[0], lines[1])

            output_left[fname] = speed_est_L
            output_right[fname] = speed_est_R
    print ""

    if uniform_conversion:
        io_util.save_json(relativepath, 'speed_raw_left_uni.json', output_left)
        io_util.save_json(relativepath, 'speed_raw_right_uni.json',
                          output_right)
    else:
        io_util.save_json(relativepath, 'speed_raw_left.json', output_left)
        io_util.save_json(relativepath, 'speed_raw_right.json', output_right)
Beispiel #15
0
                    default='../Videos/Dashcam/PCH')
    ap.add_argument('-vp',
                    '--vanishingpoint',
                    help="VP as X,Y",
                    default="309.74,242.14")
    args = ap.parse_args()
    relativepath = args.videopath
    tmp = args.vanishingpoint.split(',')
    vpx = float(tmp[0])
    vpy = float(tmp[1])
    vp = [vpx, vpy]

    run_LineTracker_Test = False
    run_LaneDetector_Test = True

    filenames = io_util.load_fnames(relativepath)
    initimg = cv2.imread(os.path.join(relativepath, filenames[0]),
                         cv2.IMREAD_GRAYSCALE).astype('uint8')

    laneMarker = LaneMarker.NietoLaneMarker()
    laneDetector = LaneDetector.PolyRANSAC_LaneDetector(verbose=False)
    laneDetectorModel = LaneDetector.LaneDetectorModel([], initimg.shape,
                                                       laneDetector)

    origPts, destPts = image_util.locateROI(initimg.shape,
                                            x_frac=1 / 3.,
                                            y_frac_top=1 / 12.,
                                            y_frac_bot=1 / 6.,
                                            vp=vp)

    show_roi = False
Beispiel #16
0
def visualize_lane_changes(relativepath):
    lanes_fn = 'nearby_lanes.json'
    vp_fn = 'vp.json'
    filenames = io_util.load_fnames(relativepath)
    fname_lanes_dict = io_util.load_json(relativepath, lanes_fn)
    vp_dict = io_util.load_json(relativepath, vp_fn)
    vp = vp_dict['vp']

    initimg = cv2.imread(os.path.join(relativepath, filenames[0]),
                         cv2.IMREAD_GRAYSCALE)
    imgshape = initimg.shape
    imgheight, imgwidth = imgshape
    origPts, destPts = image_util.locateROI(imgshape,
                                            x_frac=1 / 3.,
                                            y_frac_top=1 / 25.,
                                            y_frac_bot=1 / 6.,
                                            vp=vp)

    ipm = IPM.IPM(imgshape, imgshape, origPts, destPts)

    lpts = []
    rpts = []
    for i in range(0, 900):
        fname = filenames[i]
        if fname in fname_lanes_dict:
            lanes = fname_lanes_dict[fname]
            j = 0
            for lane in lanes:
                [x1, y1] = ipm.pointToIPM(lane[0])
                [x2, y2] = ipm.pointToIPM(lane[1])
                if j == 0:
                    lpts.append((x1 + x2) / 2.)
                    j = 1
                else:
                    rpts.append((x1 + x2) / 2.)
        else:
            lpts.append(None)
            rpts.append(None)

    l = []
    r = []
    window_size = 1
    for i in range(len(lpts)):
        if i < window_size:
            l.append(0)
        elif lpts[i] is None or lpts[i - window_size] is None:
            l.append(0)
        else:
            l.append(lpts[i] - lpts[i - window_size])
    l = np.array(l)

    for i in range(len(rpts)):
        if i < window_size:
            r.append(0)
        elif rpts[i] is None or rpts[i - window_size] is None:
            r.append(0)
        else:
            r.append(rpts[i] - rpts[i - window_size])
    r = np.array(r)

    def movingaverage(values, window=15):
        weights = np.repeat(1.0, window) / window
        sma = np.convolve(values, weights, 'same')
        return sma

    plt.figure(1)
    #plt.scatter(np.arange(l.shape[0]), l, color='b', label='Raw',s=5)
    #plt.scatter(np.arange(l.shape[0]), movingaverage(l), color='r', label='Average',s=5)
    plt.plot(np.arange(l.shape[0]), (l + r) / 2, color='g', label='Raw')
    plt.plot(np.arange(l.shape[0]),
             movingaverage((l + r) / 2),
             color='r',
             label='Moving Average')
    plt.plot(np.arange(l.shape[0]),
             medfilt(movingaverage((l + r) / 2), 15),
             color='b',
             label='Median and Moving Average')
    plt.ylim([-5, 5])
    plt.xlabel('Frame')
    plt.ylabel('$\Delta$x of Lane Boundaries (in pixels)')
    plt.legend(loc='lower left')
    plt.show()
Beispiel #17
0
def lane_detector_main(relativepath, boxes_fn, vp_fn, display_, verbose):
    # Read the filenames from the list.txt file
    filenames = io_util.load_fnames(relativepath)

    #filenames = filenames[700:]
    initimg = cv2.imread(os.path.join(relativepath, filenames[0]),
                         cv2.IMREAD_GRAYSCALE)
    imgshape = initimg.shape
    imgheight, imgwidth = imgshape

    # Initialize the objects
    lanemarker = LaneMarker.NietoLaneMarker()
    laneDetector = LaneDetector.PolyRANSAC_LaneDetector(verbose=False)
    laneDetectorModel = LaneDetector.LaneDetectorModel([], imgshape,
                                                       laneDetector)

    vp_dict = io_util.load_json(relativepath, vp_fn)
    vp = vp_dict['vp']
    origPts, destPts = image_util.locateROI(imgshape,
                                            x_frac=1 / 3.,
                                            y_frac_top=1 / 25.,
                                            y_frac_bot=1 / 6.,
                                            vp=vp)

    # for ii in xrange(len(origPts)):
    #     if ii == (len(origPts) - 1):
    #         cv2.line(initimg, tuple(origPts[ii]), tuple(origPts[0]), (255, 255, 255), 2)
    #     else:
    #         cv2.line(initimg, tuple(origPts[ii]), tuple(origPts[ii+1]), (255, 255, 255), 2)
    #cv2.imshow('initimg', initimg)
    #cv2.waitKey(0)

    if verbose:
        print 'Starting IPM'
    ipm = IPM.IPM(imgshape, imgshape, origPts, destPts)
    if verbose:
        print 'Done with IPM'

    # Dictionary where key = filename,
    # value = list of boxes of objects in that image
    fname_boxes_dict = io_util.load_json(relativepath, boxes_fn)

    fname_lanes_dict = {}
    # For each image
    for fi in range(len(filenames)):
        display.show_progressbar(fi, filenames)
        fname = filenames[fi]
        img = cv2.imread(os.path.join(relativepath, fname))
        image_util.block_EBRON(img)
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype('float')

        # Check for found vehicles in the image. For each one, zero out the
        # region where the vehicle has been found
        if fname in fname_boxes_dict:
            box_dicts = fname_boxes_dict[fname]
            for box_dict in box_dicts:
                x1, y1, x2, y2 = np.array(box_dict['box']).astype(np.int64)
                gray_img[y1:y2, x1:x2] = 0
                if display_:
                    cv2.rectangle(img, (x1, y1), (x2, y2), color=(255, 0, 0))

        # Perform IPM, lane marking detection, and thresholding
        ipm_img = ipm.imageToIPM(gray_img)

        lanemarker_img = lanemarker.detect(ipm_img, 5)
        threshold_img = image_util.thresholdImage(lanemarker_img, 95)
        if display_ and verbose:
            cv2.imshow('IPMImage', ipm_img.astype('uint8'))
            cv2.imshow('LaneMarker Raw', lanemarker_img.astype('uint8'))
            cv2.imshow('laneMarker Thresholded', threshold_img.astype('uint8'))
            cv2.waitKey(1)

        # Update the lane detector model with the image to get the lanes
        degree = 1
        lanes = laneDetectorModel.updateLaneModel(threshold_img, degree)
        # Save lanes
        for lane in lanes:
            xns, yns = convertIPMLaneToLane(lane, ipm)
            pt1 = (int(xns[0]), int(yns[0]))
            pt2 = (int(xns[-1]), int(yns[-1]))
            if display_:
                cv2.line(img, pt1, pt2, (255, 0, 0), 5)
            if fname in fname_lanes_dict:
                fname_lanes_dict[fname].append([pt1, pt2])
            else:
                fname_lanes_dict[fname] = [[pt1, pt2]]

        # For new lanes, perform backward tracking with a LineTracker object
        # (same steps as above)
        new_lanes = laneDetectorModel.get_new_lanes()
        for new_lane in new_lanes:
            new_line = new_lane.asLine()
            # if verbose:
            #     print '\t Starting backwards track'

            prev_lane_tracker = LaneDetector.LineTracker(new_line, imgshape)
            for fj in range(fi - 1, -1, -1):
                prev_fname = filenames[fj]
                prev_img = cv2.imread(os.path.join(relativepath, prev_fname))
                image_util.block_EBRON(prev_img)
                prev_gray_img = cv2.cvtColor(
                    prev_img, cv2.COLOR_BGR2GRAY).astype('float')
                if prev_fname in fname_boxes_dict:
                    box_dicts = fname_boxes_dict[prev_fname]
                    for box_dict in box_dicts:
                        left, top, right, bot = box_dict['box']
                        prev_gray_img[top:bot, left:right] = 0
                prev_ipm_img = ipm.imageToIPM(prev_gray_img)
                prev_lanemarker_img = lanemarker.detect(prev_ipm_img, 5)
                prev_threshold_img = image_util.thresholdImage(
                    prev_lanemarker_img, 95)

                prev_lane = prev_lane_tracker.update(prev_threshold_img,
                                                     laneDetector, degree)
                if prev_lane is None:
                    #if verbose:
                    #    print '\t Breaking, no suitable backwards lane found'
                    break
                else:
                    xns, yns = convertIPMLaneToLane(prev_lane, ipm)
                    pt1 = (int(xns[0]), int(yns[0]))
                    pt2 = (int(xns[-1]), int(yns[-1]))
                    if prev_fname not in fname_lanes_dict:
                        fname_lanes_dict[prev_fname] = [[pt1, pt2]]
                    else:
                        # Backwards lane pruning. But note this compares lines
                        # in the non-IPM space
                        line = image_util.Lane(xns, yns).asLine()
                        is_new_line = True
                        for [ppt1, ppt2] in fname_lanes_dict[prev_fname]:
                            prev_line = image_util.Line(
                                ppt1[0], ppt1[1], ppt2[0], ppt2[1])
                            if line.checkLineIntersection(prev_line):
                                is_new_line = False
                                break
                        if is_new_line:
                            fname_lanes_dict[prev_fname].append([pt1, pt2])
                        else:
                            # if verbose:
                            #     print '\t Breaking, intersected with previous lane'
                            break
            #if verbose:
            #    print '\t Ended backwards track at ' + filenames[fj]
        if display_:
            cv2.imshow('frame', img)
            cv2.waitKey(1)
    print ""

    output_fname = os.path.join(relativepath, 'lanes_output_new.json')
    with open(output_fname, 'w') as outfile:
        json.dump(fname_lanes_dict, outfile, indent=4, sort_keys=True)
        print 'Wrote json output file'
    cv2.destroyAllWindows()
Beispiel #18
0
def vehicle_detector_main(relativepath,
                          display_frames,
                          dlib_detector_fname,
                          verbose,
                          forward_confidence_thresh,
                          backward_confidence_thresh,
                          run_backwards_tracking=True):
    if verbose:
        print 'Starting vehicle detector main for {}'.format(relativepath)

    # # Read the filenames from the list.txt file
    filenames = io_util.load_fnames(relativepath)

    # Get the shape from the first image
    tmp_img = cv2.imread(os.path.join(relativepath, filenames[0]),
                         cv2.IMREAD_GRAYSCALE)
    imgshape = tmp_img.shape
    imgheight, imgwidth = imgshape

    # Confidence value where if we fall below this, we stop backwards tracking
    confidence_threshold = forward_confidence_thresh
    previous_track_confidence = backward_confidence_thresh

    # Set up the vehicle detector object
    vehicleDetector = VehicleDetector.VehicleDetector(
        dlib_detector_fname, confidence_threshold, previous_track_confidence)

    # Output dictionary where key = filename,
    # value = list of boxes + ids of objects in that image
    fname_boxes_dict = {}

    for fi in range(len(filenames)):
        if verbose:
            display.show_progressbar(fi, filenames)
        fname = filenames[fi]
        img = cv2.imread(os.path.join(relativepath, fname),
                         cv2.IMREAD_GRAYSCALE)
        # Add any detected boxes to the dictionary
        vehicle_boxes = vehicleDetector.update(img)
        if fname not in fname_boxes_dict:
            fname_boxes_dict[fname] = []
        for box_dict in vehicle_boxes:
            fname_boxes_dict[fname].append(box_dict)

        # For new detected boxes, initialize an object tracker to go backwards
        # and detect boxes in previous frames, until the confidence falls below
        # a certain threshold
        new_box_dicts = vehicleDetector.get_new_boxes()
        if run_backwards_tracking:
            vehicleDetector.initialize_backwards(img)
            for fj in range(fi - 1, -1, -1):
                prev_fname = filenames[fj]
                prev_img = cv2.imread(os.path.join(relativepath, prev_fname),
                                      cv2.IMREAD_GRAYSCALE)
                boxes = vehicleDetector.update_backwards(prev_img)
                if boxes is None:
                    break
                else:
                    if prev_fname in fname_boxes_dict:
                        fname_boxes_dict[prev_fname] += boxes
                    else:
                        fname_boxes_dict[prev_fname] = boxes
            # for box_dict in new_box_dicts:
            #     #if verbose:
            #     #    print '\n\t Starting backwards track'
            #     new_box = box_dict['box']
            #     new_box_id = box_dict['id']
            #     new_object_tracker = ObjectTracker.dlib_ObjectTracker(img, new_box,
            #                             new_box_id, store_size=1)
            #     for fj in range(fi - 1, -1, -1):
            #         prev_fname = filenames[fj]
            #         prev_img = cv2.imread(os.path.join(relativepath,prev_fname),
            #                                 cv2.IMREAD_GRAYSCALE)
            #         confidence = new_object_tracker.update(prev_img)
            #         if confidence < previous_track_confidence:
            #             break
            #         else:
            #             if prev_fname in fname_boxes_dict:
            #                 fname_boxes_dict[prev_fname].append(
            #                     new_object_tracker.get_dict())
            #             else:
            #                 fname_boxes_dict[prev_fname] = [
            #                     new_object_tracker.get_dict()]
            #     #if verbose:
            #     #    print '\t Ended backwards track at ' + filenames[fj]
    if verbose:
        print ""

    # Create the output json file of the boxes
    detector_fn = dlib_detector_fname.split('/')[-1].split('.svm')[0]
    output_fname = os.path.join(
        relativepath,
        'boxes_output_{}_{}_run2.json'.format(detector_fn,
                                              confidence_threshold))
    with open(output_fname, 'w') as outfile:
        json.dump(fname_boxes_dict,
                  outfile,
                  indent=4,
                  sort_keys=True,
                  cls=io_util.NumpyEncoder)

    # Display the images with the boxes and ids on them
    if display_frames:
        for fname in filenames:
            boxes = fname_boxes_dict[fname]
            img = cv2.imread(os.path.join(relativepath, fname))
            display.draw_boxdicts(img, boxes, (255, 0, 0))
            cv2.imshow('With box and id labels', img)
            cv2.waitKey(1)
        cv2.destroyAllWindows()