Beispiel #1
0
def single_image_features(image, feature_parameter):
    #1) Define an empty list to receive features
    img_features = []
    #2) Apply color conversion if other than 'RGB'
    feature_image = convert_color(image, feature_parameter.color_space)
    #3) Compute spatial features if flag is set
    if feature_parameter.spatial_feat == True:
        spatial_features = bin_spatial(feature_image,
                                       size=feature_parameter.spatial_size)
        #4) Append features to list
        img_features.append(spatial_features)
    #5) Compute histogram features if flag is set
    if feature_parameter.hist_feat == True:
        hist_features, rhist, ghist, bhist, bin_centers = color_hist(
            feature_image, nbins=feature_parameter.hist_bins)
        #6) Append features to list
        img_features.append(hist_features)
    #7) Compute HOG features if flag is set
    if feature_parameter.hog_feat == True:
        hog_features = get_hog_features(feature_image,
                                        feature_parameter.orient,
                                        feature_parameter.pix_per_cell,
                                        feature_parameter.cell_per_block,
                                        feature_parameter.hog_channel)
        #8) Append features to list
        img_features.append(hog_features)

    #9) Return concatenated array of features
    return np.concatenate(img_features)
def extract_features(img, orient, pix_per_cell, cell_per_block, spatial_size,
                     hist_bins):

    ch1 = img[:, :, 0]
    ch2 = img[:, :, 1]
    ch3 = img[:, :, 2]
    # Get hog features for each color
    hog1 = get_hog_features(ch1,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)
    hog2 = get_hog_features(ch2,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)
    hog3 = get_hog_features(ch3,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)

    # print("HOG 1 {} 2 {} 3 {}   ".format(hog1.shape,hog2.shape,hog3.shape));
    # print("RAVELD HOG 1 {} 2 {} 3 {}   ".format(hog1.ravel().shape,hog2.ravel().shape,hog3.ravel().shape));
    hog_features = np.hstack((hog1.ravel(), hog2.ravel(), hog3.ravel()))

    # print(" hog_features {}".format(hog_features))

    # get the spatial fetures
    spatial_features = bin_spatial(img, size=spatial_size)
    hist_features = color_hist(img, nbins=hist_bins)

    # print(" spatial={} hist={} hog={}\n".format(spatial_features.shape,hist_features.shape,hog_features.shape))

    x = np.concatenate((spatial_features.ravel(), hist_features.ravel(),
                        hog_features.ravel()))
    return x

    return test_features
def show_hog_features(imgname, title):
    img = mpimg.imread(imgname)
    print(img.shape)
    plt.title("Image")
    plt.imshow(img)
    plt.show()
    img = convert_color(img, conv='RGB2YCrCb')
    plt.title("Color Converted")
    plt.imshow(img)
    plt.show()
    print(img.shape)
    orient = 9
    pix_per_cell = 8
    cell_per_block = 2

    ch1 = img[:, :, 0]
    ch2 = img[:, :, 1]
    ch3 = img[:, :, 2]

    features1, hog_image1 = get_hog_features(ch1,
                                             orient,
                                             pix_per_cell,
                                             cell_per_block,
                                             vis=True)
    features2, hog_image2 = get_hog_features(ch2,
                                             orient,
                                             pix_per_cell,
                                             cell_per_block,
                                             vis=True)
    features3, hog_image3 = get_hog_features(ch3,
                                             orient,
                                             pix_per_cell,
                                             cell_per_block,
                                             vis=True)
    plt.title(title)
    plt.imshow(hog_image1)
    plt.show()
    plt.imshow(hog_image2)
    plt.show()
    plt.imshow(hog_image3)
    plt.show()
Beispiel #4
0
def test_code():
    ind = np.random.randint(0,len(cars))
    image = mpimg.imread(cars[ind])

    features, hog_image = get_hog_features(image, 9, 8,4,True,True)
    
    fig = plt.figure()
    plt.subplot(121)
    plt.imshow(image, cmap = 'gray' )
    plt.title('Example Car Image')
    
    plt.subplot(122)
    plot.imshow(hog_image, cmap = 'gray' )
    plot.title('HOG Visulization')
def extract_features(imgs,
                     cspace='RGB',
                     orient=9,
                     pix_per_cell=8,
                     cell_per_block=2,
                     hog_channel=0):
    # Create a list to append feature vectors to
    features = []
    # Iterate through the list of images
    for file in imgs:
        # Read in each one by one
        image = mpimg.imread(file)
        # apply color conversion if other than 'RGB'
        feature_image = convert_color(image, cspace)
        hog_features = get_hog_features(feature_image, orient, pix_per_cell,
                                        cell_per_block, hog_channel)
        # Append the new feature vector to the features list
        features.append(hog_features)
    # Return list of feature vectors
    return features
def find_cars(img, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell,
              cell_per_block, spatial_size, hist_bins):

    draw_img = np.copy(img)
    img = img.astype(np.float32) / 255

    img_tosearch = img[ystart:ystop, :, :]
    ctrans_tosearch = convert_color(img_tosearch, conv='RGB2YCrCb')

    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) - 1
    nyblocks = (ch1.shape[0] // pix_per_cell) - 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) - 1
    cells_per_step = 3  # 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
    hog1 = get_hog_features(ch1,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)
    hog2 = get_hog_features(ch2,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)
    hog3 = get_hog_features(ch3,
                            orient,
                            pix_per_cell,
                            cell_per_block,
                            vis=False,
                            feature_vec=False)

    bbox = np.empty((1, 2, 2))

    print("hog1", hog1.shape, "hog2", hog2.shape, "hog3", hog3.shape)

    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
            # print("ypos",ypos,"xpos",xpos)
            # print("hog_feat",nblocks_per_window, nblocks_per_window)
            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))

            # print("hog_feat1=",hog_feat1.shape,"hog_feat2=",hog_feat2.shape," hog_feat3=",hog_feat3.shape)

            xleft = xpos * pix_per_cell
            ytop = ypos * pix_per_cell

            # Extract the image patch1
            subimg = cv2.resize(
                ctrans_tosearch[ytop:ytop + window, xleft:xleft + window],
                (64, 64))
            # print("subimg shape=",subimg.shape)
            # Get color features
            spatial_features = bin_spatial(subimg, size=spatial_size)
            hist_features = color_hist(subimg, nbins=hist_bins)
            # print("find_cars spatial={} hist={} hog={}\n".format(spatial_features.shape,hist_features.shape,hog_features.shape))
            # Scale features and make a prediction
            x = np.concatenate(
                (spatial_features.ravel(), hist_features.ravel(),
                 hog_features.ravel()))
            # print("find_cars x=",x.shape)

            test_features = X_scaler.transform(x).reshape(1, -1)
            #test_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1))
            test_prediction = svc.predict(test_features)
            #print("TEst prediction ",test_prediction,xb,yb)

            if (test_prediction == 1):
                xbox_left = np.int(xleft * scale)
                ytop_draw = np.int(ytop * scale)
                win_draw = np.int(window * scale)
                cv2.rectangle(
                    draw_img, (xbox_left, ytop_draw + ystart),
                    (xbox_left + win_draw, ytop_draw + win_draw + ystart),
                    (0, 0, 255), 6)
                v = np.array([[int(xbox_left),
                               int(ytop_draw + ystart)],
                              [
                                  int(xbox_left + win_draw),
                                  int(ytop_draw + win_draw + ystart)
                              ]]).reshape((1, 2, 2))
                # print("PREDICTION! V shape",v.shape, "box=",bbox.shape)
                bbox = np.vstack((bbox, v))

    #plt.imshow(draw_img)
    #plt.show()
    #plt.pause(.001)

    bbox = np.delete(bbox, (0), 0)  # Remove the first element that I put on
    print("find_cars returning bbox shape:", bbox.shape, " values=", bbox)
    return draw_img, bbox