Ejemplo n.º 1
0
def showHOGfeature(car, notcar):
    features, car_dst = get_hog_features(car[:,:,2], 9, 8, 8, vis=True, feature_vec=True)
    features, noncar_dst = get_hog_features(notcar[:,:,2], 9, 8, 8, vis=True, feature_vec=True)
    f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(7,7))
    f.subplots_adjust(hspace = .4, wspace=.2)
    ax1.imshow(car)
    ax1.set_title('Car Image', fontsize=16)
    ax2.imshow(car_dst, cmap='gray')
    ax2.set_title('Car HOG', fontsize=16)
    ax3.imshow(notcar)
    ax3.set_title('Non-Car Image', fontsize=16)
    ax4.imshow(noncar_dst, cmap='gray')
    ax4.set_title('Non-Car HOG', fontsize=16)
Ejemplo n.º 2
0
def sample_hog(detect: VehicleDetector, cars, notcars):
    # Generate a random index to look at a car image
    ind = np.random.randint(0, len(cars))
    # Read in the image
    image = mpimg.imread(cars[ind])
    image = image.astype(np.float32)
    image = image[:, :, 0]

    # Call our function with vis=True to see an image output
    features, hog_image = functions.get_hog_features(image, orient=detect.orient,
                                                     pix_per_cell=detect.pix_per_cell,
                                                     cell_per_block=detect.cell_per_block,
                                                     hog_vis=detect.hog_vis, hog_feature_vec=detect.hog_feature_vec)
    # Plot the examples
    fig = plt.figure()
    plt.subplot(121)
    plt.imshow(image, cmap='gray')
    plt.title('Example Car Image ')

    plt.subplot(122)
    plt.imshow(hog_image, cmap='hot')
    plt.title('HOG Visualization')
    fig.savefig('./output_images/example_hog.jpg')
    plt.show()
def find_cars(img, x_start_stop, y_start_stop, scale,color_space ,svc, X_scaler, orient, 
              pix_per_cell, cell_per_block, spatial_size, hist_bins, show_all_rectangles): 
    img = img.astype(np.float32)/255   
    box_list = []

    if x_start_stop[0] == None:
        x_start_stop[0] = 0
    if x_start_stop[1] == None:
        x_start_stop[1] = img.shape[1]
    if y_start_stop[0] == None:
        y_start_stop[0] = 0
    if y_start_stop[1] == None:
        y_start_stop[1] = img.shape[0]
       
    img_tosearch = img[y_start_stop[0]:y_start_stop[1],x_start_stop[0]:x_start_stop[1],:]
#    ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb)
    if color_space != 'RGB':
        if color_space == 'HSV':
            ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HSV)
        elif color_space == 'LUV':
            ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2LUV)
        elif color_space == 'HLS':
            ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HLS)
        elif color_space == 'YUV':
            ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YUV)
        elif color_space == 'YCrCb':
            ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb)
        #ctrans_tosearch = ctrans_tosearch.astype(np.float32)/255
    else: ctrans_tosearch = np.copy(img_tosearch)      

    #print(ctrans_tosearch.shape)
    if scale != 1:
        imshape = ctrans_tosearch.shape       
        ctrans_tosearch = cv2.resize(ctrans_tosearch, (np.int(imshape[1]/scale), np.int(imshape[0]/scale)))
        
    ch1 = ctrans_tosearch[:,:,0]
    ch2 = ctrans_tosearch[:,:,1]
    ch3 = ctrans_tosearch[:,:,2]

    # Define blocks and steps as above
    nxblocks = (ch1.shape[1] // pix_per_cell) - cell_per_block + 1
    nyblocks = (ch1.shape[0] // pix_per_cell) - cell_per_block + 1 
    #nfeat_per_block = orient*cell_per_block**2
    
    # 64 was the orginal sampling rate, with 8 cells and 8 pix per cell
    window = 64
    nblocks_per_window = (window // pix_per_cell) - cell_per_block + 1
    cells_per_step = 2  # Instead of overlap, define how many cells to step
    nxsteps = (nxblocks - nblocks_per_window) // cells_per_step
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step
    
    # Compute individual channel HOG features for the entire image
    if hog_channel == 'ALL':
            hog1 = get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False)
            hog2 = get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False)
            hog3 = get_hog_features(ch3, orient, pix_per_cell, cell_per_block, feature_vec=False)
    else:
        hog1 = get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False)
    
    for xb in range(nxsteps):
        for yb in range(nysteps):
            ypos = yb*cells_per_step
            xpos = xb*cells_per_step
            # Extract HOG for this patch
            if hog_channel == 'ALL':
                hog_feat1 = hog1[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel() 
                hog_feat2 = hog2[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel() 
                hog_feat3 = hog3[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel() 
                hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3))
            else:
                hog_feat1 = hog1[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel()
                hog_features = hog_feat1
            xleft = xpos*pix_per_cell
            ytop = ypos*pix_per_cell

            # Extract the image patch
            subimg = cv2.resize(ctrans_tosearch[ytop:ytop+window, xleft:xleft+window], (64,64))
          
            # Get color features
            spatial_features = bin_spatial(subimg, size=spatial_size)
            hist_features = color_hist(subimg, nbins=hist_bins)
            features = np.hstack((spatial_features, hist_features, hog_features))
            #print(np.shape(features))
            # Scale features and make a prediction
            features[np.isnan(features) == True] = 0
            test_features = X_scaler.transform(np.array(features).reshape(1, -1))    
            #test_features = X_scaler.transform(np.hstack((hog_features)).reshape(1, -1))    
            test_prediction = svc.predict(test_features)
            
            if test_prediction == 1 or show_all_rectangles:
                xbox_left = np.int(xleft*scale)
                ytop_draw = np.int(ytop*scale)
                win_draw = np.int(window*scale)
                box_list.append([(xbox_left+x_start_stop[0], ytop_draw+y_start_stop[0]),(xbox_left+win_draw+x_start_stop[0],ytop_draw+win_draw+y_start_stop[0])])
                 
    return box_list
Ejemplo n.º 4
0
    #    axh[3].set_title('Car V HOG', fontsize=10)
    #
    #
    #    axh[4].imshow(notcrgb)
    #    axh[4].set_title('Not-Car Image', fontsize=10)
    #    axh[5].imshow(notcar_hog_y, cmap='gray')
    #    axh[5].set_title('Not-Car Y  HOG', fontsize=10)
    #    axh[6].imshow(notcar_hog_u, cmap='gray')
    #    axh[6].set_title('Not-Car U  HOG', fontsize=10)
    #    axh[7].imshow(notcar_hog_v, cmap='gray')
    #    axh[7].set_title('Not-Car V  HOG', fontsize=10)

    #ycrcb sapce
    fcar_y, car_hog_y = get_hog_features(cycrcb[:, :, 0],
                                         orient,
                                         pix_per_cell,
                                         cells_per_block,
                                         vis=True,
                                         feature_vec=True)
    fnotcar_y, notcar_hog_y = get_hog_features(notcycrcb[:, :, 0],
                                               orient,
                                               pix_per_cell,
                                               cells_per_block,
                                               vis=True,
                                               feature_vec=True)

    fcar_u, car_hog_u = get_hog_features(cycrcb[:, :, 0],
                                         orient,
                                         pix_per_cell,
                                         8,
                                         vis=True,
                                         feature_vec=True)
Ejemplo n.º 5
0
    ch1 = ctrans_tosearch[:, :, 0]
    ch2 = ctrans_tosearch[:, :, 1]
    ch3 = ctrans_tosearch[:, :, 2]

    nxblocks = (ch1.shape[1] // pix_per_cell) - 1
    nyblocks = (ch1.shape[0] // pix_per_cell) - 1
    nfeat_per_block = orient * cell_per_block**2
    window = 64
    nblocks_per_window = (window // pix_per_cell) - 1
    cells_per_step = 2
    nxsteps = (nxblocks - nblocks_per_window) // cells_per_step
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step

    hog1 = functions.get_hog_features(ch1,
                                      orient,
                                      pix_per_cell,
                                      cell_per_block,
                                      feature_vec=False)
    hog2 = functions.get_hog_features(ch2,
                                      orient,
                                      pix_per_cell,
                                      cell_per_block,
                                      feature_vec=False)
    hog3 = functions.get_hog_features(ch3,
                                      orient,
                                      pix_per_cell,
                                      cell_per_block,
                                      feature_vec=False)

    for xb in range(nxsteps):
        for yb in range(nysteps):
Ejemplo n.º 6
0
d = fig3.add_subplot(3, 2, 4)
plt.bar(bin_centers, notcar_hist2[0])
d.set_title("not car-YCrCB ch2 histogram")
e = fig3.add_subplot(3, 2, 5)
plt.bar(bin_centers, car_hist3[0])
e.set_title("car-YCrCB ch3 histogram")
f = fig3.add_subplot(3, 2, 6)
plt.bar(bin_centers, notcar_hist3[0])
f.set_title("not car-YCrCB ch3 histogram")
fig3.savefig('./examples/car_notcar_histogram.jpg')

fig4 = plt.figure(figsize=(10, 12))
a = fig4.add_subplot(3, 2, 1)
hog, img = get_hog_features(car_sample_YCrCb[:, :, 0],
                            orient=9,
                            pix_per_cell=8,
                            cell_per_block=1,
                            vis=True,
                            feature_vec=False)
plt.imshow(img, cmap='gray')
a.set_title("car-YCrCB ch1 hog")
b = fig4.add_subplot(3, 2, 2)
hog, img = get_hog_features(not_car_sample_YCrCb[:, :, 0],
                            orient=9,
                            pix_per_cell=8,
                            cell_per_block=1,
                            vis=True,
                            feature_vec=False)
plt.imshow(img, cmap='gray')
b.set_title("not car-YCrCB ch1 hog")
c = fig4.add_subplot(3, 2, 3)
hog, img = get_hog_features(car_sample_YCrCb[:, :, 1],