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

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument('-v', '--videopath', required=True, help="Relative Path to Video")
    args = ap.parse_args()
    relativepath = args.videopath

    nearby_lanes = get_nearby_lanes(relativepath)

    output_fname = 'nearby_lanes.json'
    io_util.save_json(relativepath, output_fname, nearby_lanes)
    print 'Wrote json output file'

if __name__ == '__main__':
    package_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

    run_find_nearby_lines = False
    run_read_true_speed = False
    run_main = False
    run_analysis = True

    if run_find_nearby_lines:
        dirs = get_validation_dirs(package_dir)
        for valid_dir in dirs:
            relativepath = os.path.join(package_dir, 'validation', valid_dir)
            nearby_lanes = find_nearby_lanes.get_nearby_lanes(relativepath)
            io_util.save_json(relativepath, 'nearby_lanes.json', nearby_lanes)

    if run_read_true_speed:
        speedrec = SpeedRecognizer.SpeedRecognizer()
        dirs = get_validation_dirs(package_dir)
        for valid_dir in dirs:
            relativepath = os.path.join(package_dir, 'validation', valid_dir)
            save_dict = read_true_speed(relativepath, speedrec)
            io_util.save_json(relativepath, 'true_speed.json', save_dict)

    if run_main:
        dirs = get_validation_dirs(package_dir)
        for valid_dir in dirs:
            relativepath = os.path.join(package_dir, 'validation', valid_dir)
            main.speed_estimation_main(relativepath, True)
Exemple #3
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'
Exemple #4
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)
Exemple #5
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
Exemple #6
0
                    default='./validation/v1_shadows_low_gray')
    ap.add_argument('-b', '--boxes', help='Filename for boxes', 
                    default='boxes_output_detector_session2_raw_8.5.json')
    args = ap.parse_args()
    relativepath = args.videopath
    boxes_fn = args.boxes

    run_vps = True
    run_clustering = True
    save_vp = True

    if run_vps:
        data = get_vps(relativepath, boxes_fn)
        np.save(os.path.join(relativepath, 'vp.npy'), data)
    else:
        data = np.load(os.path.join(relativepath, 'vp.npy'))

    if run_clustering:
        mean_pt = DB_Cluster(data)
        vp_dict = {'vp':mean_pt}
        # MS_Cluster(data)
        print mean_pt
        if save_vp:
            io_util.save_json(relativepath, 'vp.json', vp_dict)
            print 'Saved as json'