Example #1
0
def plot_bounding_box_old(clf, scaler, params, index, image_type, image_shape):
    '''
    given a new image, plot a bounding box
    around all the cars detected by the
    classification algorithm.
    '''
    print ('image_shape = ', image_shape)
    image = mpimg.imread('bbox-example-image.jpg')
    image = mpimg.imread('test/frame300.jpg')
    draw_image = np.copy(image)
    imy, imx = image.shape[:2]
    print ('in old: ', image_type, image.shape)

    # Uncomment the following line if you extracted training
    # data from .png images (scaled 0 to 1 by mpimg) and the
    # image you are searching is a .jpg (scaled 0 to 255)
    if image_type == 'png':
        image = image.astype(np.float32)/255

    # extract parameters from dictionary
    color_space = params['color_space']
    spatial_size = params['spatial_size']
    hist_bins = params['hist_bins']
    orient = params['orient']
    pix_per_cell = params['pix_per_cell']
    cell_per_block = params['cell_per_block']
    hog_channel = params['hog_channel']
    spatial_feat = params['spatial_feat']
    hist_feat = params['hist_feat']
    hog_feat = params['hog_feat']

    # multi box search
    min_fraction = 6
    max_fraction = 10
    step_fraction = 2
    aspect_ratio = 1.0
    hot_windows = []
    start_scan = int(imy/2)
    for f in range(min_fraction, max_fraction+1, step_fraction):
        y_start_stop = [start_scan, min(start_scan + (f - 1) * int(imy/f), imy)] # Min and max in y to search in slide_window()
        xy_window = (int(imy/f), int(imy/f*aspect_ratio))
        windows = ro.slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop,
                    xy_window=xy_window, xy_overlap=(0.5, 0.5))
        print (f, len(windows))

        new_hot_windows = ro.search_windows(image, windows, clf, scaler, image_shape, color_space=color_space,
                        spatial_size=spatial_size, hist_bins=hist_bins,
                        orient=orient, pix_per_cell=pix_per_cell,
                        cell_per_block=cell_per_block,
                        hog_channel=hog_channel, spatial_feat=spatial_feat,
                        hist_feat=hist_feat, hog_feat=hog_feat)
        print (f, y_start_stop, xy_window, len(new_hot_windows)) #, new_hot_windows)
        if len(new_hot_windows) > 0:
            hot_windows = hot_windows + new_hot_windows

    print ('size of hot_windows before plotting: ', len(hot_windows))
    window_img = ro.draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=3)
    '''
    plt.show()
    '''
    plt.imshow(window_img)
    fig1 = plt.gcf()
    plt.draw()
    fig1.savefig('new_fig%d.jpg' %index, dpi=100)


    heatmap = np.zeros([draw_image.shape[0], draw_image.shape[1]])
    heatmap = ro.add_heat(heatmap, hot_windows)
    plt.figure()
    plt.imshow(heatmap, cmap='gray')


    heatmap = ro.apply_threshold(heatmap, 1)
    labels = label(heatmap)
    print(labels[1], 'cars found')
    plt.figure()
    plt.imshow(labels[0], cmap='gray')

    draw_img = ro.draw_labeled_bboxes(np.copy(draw_image), labels)
    # Display the image
    plt.figure()
    plt.imshow(draw_img)

    plt.show()
Example #2
0
def plot_bounding_box(image, clf, scaler, params, plot_box=False, plot_heat=False):
    '''
    given a new image, plot a bounding box
    around all the cars detected by the
    classification algorithm.
    '''
    if type(image) is str:
        image_type = image.split('.')[-1]
        image = mpimg.imread(image)
    elif type(image) is np.ndarray:
        if image.max() > 1:
            image_type = 'jpg'
        else:
            image_type = 'png'
    else:
        print ("input not recognized")
        return None

    draw_image = np.copy(image)
    imy, imx = image.shape[:2]

    if (image_type=='jpeg' or image_type=='jpg'): # and params['training_image_type']=='png':
        image = image.astype(np.float32)/255

    # extract parameters from dictionary
    color_space = params['color_space']
    spatial_size = params['spatial_size']
    hist_bins = params['hist_bins']
    orient = params['orient']
    pix_per_cell = params['pix_per_cell']
    cell_per_block = params['cell_per_block']
    hog_channel = params['hog_channel']
    spatial_feat = params['spatial_feat']
    hist_feat = params['hist_feat']
    hog_feat = params['hog_feat']
    training_image_shape = params['training_image_shape']

    # multi box search
    min_fraction = 6
    max_fraction = 10
    step_fraction = 2
    aspect_ratio = 1.0
    hot_windows = []
    start_scan = int(imy/2)
    for f in range(min_fraction, max_fraction+1, step_fraction):
        y_start_stop = [start_scan, min(start_scan + (f - 1) * int(imy/f), imy)] # Min and max in y to search in slide_window()
        xy_window = (int(imy/f), int(imy/f*aspect_ratio))
        windows = ro.slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop,
                    xy_window=xy_window, xy_overlap=(0.5, 0.5))
        #print (f, len(windows))

        new_hot_windows = ro.search_windows(image, windows, clf, scaler, training_image_shape, color_space=color_space,
                        spatial_size=spatial_size, hist_bins=hist_bins,
                        orient=orient, pix_per_cell=pix_per_cell,
                        cell_per_block=cell_per_block,
                        hog_channel=hog_channel, spatial_feat=spatial_feat,
                        hist_feat=hist_feat, hog_feat=hog_feat)
        #print (f, y_start_stop, xy_window, len(new_hot_windows)) #, new_hot_windows)
        if len(new_hot_windows) > 0:
            hot_windows = hot_windows + new_hot_windows

    window_img = ro.draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=3)

    heatmap = np.zeros([draw_image.shape[0], draw_image.shape[1]])
    heatmap = ro.add_heat(heatmap, hot_windows)

    if plot_box:
        if plot_heat:
            plt.figure()
            plt.subplot(121)
            plt.imshow(window_img)
            plt.subplot(122)
            plt.imshow(heatmap)
            plt.show()
        else:
            plt.figure()
            plt.imshow(window_img)

    return heatmap