예제 #1
0
def extract_feature(image,
                    color_space='RGB',
                    spatial_size=(32, 32),
                    hist_bins=32,
                    orient=9,
                    pix_per_cell=8,
                    cell_per_block=2,
                    hog_channel=0,
                    spatial_feat=True,
                    hist_feat=True,
                    hog_feat=True):
    """
    Extract features of an image
    """
    # Create a list to contain different feature for later concatenation
    sub_features = []
    feature_image = tu.convert_color(image, color_space)

    # Extract spatial binning features
    if spatial_feat:
        spatial_features = fx.bin_spatial(feature_image, size=spatial_size)
        sub_features.append(spatial_features)

    # Extract color histogram features
    if hist_feat:
        hist_features = fx.color_hist(feature_image, nbins=hist_bins)
        sub_features.append(hist_features)

    # Extract HOG features
    if hog_feat:
        if hog_channel == 'ALL':
            hog_features = []
            for channel in range(feature_image.shape[2]):
                hog_features.append(
                    fx.get_hog_features(feature_image[:, :, channel],
                                        orient,
                                        pix_per_cell,
                                        cell_per_block,
                                        vis=False,
                                        feature_vec=True))
            hog_features = np.ravel(hog_features)
        else:
            hog_features = fx.get_hog_features(feature_image[:, :,
                                                             hog_channel],
                                               orient,
                                               pix_per_cell,
                                               cell_per_block,
                                               vis=False,
                                               feature_vec=True)
        sub_features.append(hog_features)
    return np.concatenate(sub_features)
def hog_examples_drawing(image,
                         color_space='YCrCb',
                         orient=12,
                         pix_per_cell=16,
                         cell_per_block=2):
    if color_space != 'RGB':
        if color_space == 'HSV':
            feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
        elif color_space == 'LUV':
            feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV)
        elif color_space == 'HLS':
            feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
        elif color_space == 'YUV':
            feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
        elif color_space == 'YCrCb':
            feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb)
    else:
        feature_image = np.copy(image)

    fig, axarr = plt.subplots(1, 4, figsize=(12, 3))
    axarr[0].set_title('original')
    axarr[0].imshow(image)

    _, ch_0 = get_hog_features(feature_image[:, :, 0],
                               orient,
                               pix_per_cell,
                               cell_per_block,
                               vis=True)
    axarr[1].set_title('Channel 0')
    axarr[1].imshow(ch_0, cmap='jet')

    _, ch_1 = get_hog_features(feature_image[:, :, 1],
                               orient,
                               pix_per_cell,
                               cell_per_block,
                               vis=True)
    axarr[2].set_title('Channel 1')
    axarr[2].imshow(ch_1, cmap='jet')

    _, ch_2 = get_hog_features(feature_image[:, :, 2],
                               orient,
                               pix_per_cell,
                               cell_per_block,
                               vis=True)
    axarr[3].set_title('Channel 2')
    axarr[3].imshow(ch_2, cmap='jet')

    plt.savefig('output_images/hog_notcar_YCrCb')
    plt.close(fig)
def visualise_data():
    # Generate a random index to look at a car image
    ind = np.random.randint(0, len(cars_files))
    # Read in the image
    car_image = mpimg.imread(cars_files[ind])

    ind = np.random.randint(0, len(notcars_files))
    notcar_image = mpimg.imread(notcars_files[ind])

    gray = cv2.cvtColor(car_image, cv2.COLOR_RGB2GRAY)
    # Define HOG parameters
    orient = 9
    pix_per_cell = 8
    cell_per_block = 2
    # Call our function with vis=True to see an image output
    features, hog_image = get_hog_features(gray, orient,
                            pix_per_cell, cell_per_block,
                            vis=True, feature_vec=False)

    # Plot the examples
    fig = plt.figure()
    plt.subplot(121)
    plt.imshow(car_image, cmap='gray')
    plt.title('Example Car Image')
    plt.subplot(122)
    plt.imshow(hog_image, cmap='gray')
    plt.title('HOG Visualization')
    plt.show()

    # Plot the examples
    fig = plt.figure()
    plt.subplot(121)
    plt.imshow(car_image)
    plt.title('Example Vehicle')
    plt.subplot(122)
    plt.imshow(notcar_image)
    plt.title('Example non-Vehicle')
    plt.show()
예제 #4
0
def find_cars(img_vec, feature_params, window_size, ystart, ystop, xstart,
              xstop, yscale, xscale, cells_per_step, clf, scaler):
    img_tosearch = feature_extraction.convert_color(
        img_vec, color_space=feature_params['colorspace'])
    img_tosearch = feature_extraction.normalize_255(img_tosearch)
    img_tosearch = img_tosearch[ystart:ystop, xstart:xstop, :]

    if xscale != 1 or yscale != 1:
        imshape = img_tosearch.shape
        img_tosearch = cv2.resize(
            img_tosearch,
            (np.int(imshape[1] / xscale), np.int(imshape[0] / yscale)))

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

    # Define blocks and steps as above
    pix_per_cell = feature_params['pixels_per_cell']
    nxcells = (ch1.shape[1] // pix_per_cell) - 1
    nycells = (ch1.shape[0] // pix_per_cell) - 1

    cell_per_block = feature_params['cells_per_block']
    orient = feature_params['orient']

    ncells_per_window = (window_size // pix_per_cell) - 1
    nxsteps = (nxcells - ncells_per_window) // cells_per_step
    nysteps = (nycells - ncells_per_window) // cells_per_step

    # Compute individual channel HOG features for the entire image
    hog1 = feature_extraction.get_hog_features(ch1,
                                               orient,
                                               pix_per_cell,
                                               cell_per_block,
                                               feature_vec=False)
    hog2 = feature_extraction.get_hog_features(ch2,
                                               orient,
                                               pix_per_cell,
                                               cell_per_block,
                                               feature_vec=False)
    hog3 = feature_extraction.get_hog_features(ch3,
                                               orient,
                                               pix_per_cell,
                                               cell_per_block,
                                               feature_vec=False)

    bboxes = []
    allbboxes = []
    prob = []

    for xb in range(nxsteps):
        for yb in range(nysteps):
            ypos = yb * cells_per_step
            xpos = xb * cells_per_step
            if feature_params['hog_feat'] is True:
                hog_features = sample_hog([hog1, hog2, hog3], ypos, xpos,
                                          ncells_per_window,
                                          feature_params['hog_channel'])
            else:
                hog_features = np.array([])

            xleft = xpos * pix_per_cell
            ytop = ypos * pix_per_cell

            # Extract the image patch
            subimg = cv2.resize(
                img_tosearch[ytop:ytop + window_size,
                             xleft:xleft + window_size], (64, 64))

            # Extract other features
            spatial_size = feature_params['spatial_size']
            hist_bins = feature_params['hist_bins']
            spatial_feat = feature_params['spatial_feat']
            hist_feat = feature_params['hist_feat']

            other_features = feature_extraction.extract_features_from_image_vec(
                subimg,
                spatial_size,
                hist_bins,
                spatial_feat=spatial_feat,
                hist_feat=hist_feat,
                hog_feat=False)
            all_features = other_features + [hog_features]
            comb_features = np.concatenate(all_features).reshape(1, -1)
            # Scale features and make a prediction
            test_features = scaler.transform(comb_features)
            test_prediction = clf.predict(test_features)

            xbox_left = np.int(xleft * xscale)
            ytop_draw = np.int(ytop * yscale)
            xwin = np.int(window_size * xscale)
            ywin = np.int(window_size * yscale)
            bbox = ((xbox_left + xstart, ytop_draw + ystart),
                    (xbox_left + xstart + xwin, ytop_draw + ywin + ystart))
            allbboxes.append(bbox)

            if test_prediction == 1:
                decision_func = clf.decision_function(test_features)
                bboxes.append(bbox)
                prob.append(decision_func)

    return bboxes, prob, allbboxes
예제 #5
0
def find_cars(img, ystart, ystop, xstart, xstop, scale, svc, X_scaler, orient,
              pix_per_cell, cell_per_block, colorspace, hog_channels,
              spatial_size, hist_bins):
    img = img.astype(np.float32) / 255

    img_tosearch = img[ystart:ystop, xstart:xstop, :]
    ctrans_tosearch = convert_color(img_tosearch, colorspace)
    if scale != 1:
        imshape = ctrans_tosearch.shape
        ctrans_tosearch = cv2.resize(
            ctrans_tosearch,
            (np.int(imshape[1] / scale), np.int(imshape[0] / scale)))

    # Define blocks and steps as above
    nxblocks = (ctrans_tosearch.shape[1] // pix_per_cell) - cell_per_block + 1
    nyblocks = (ctrans_tosearch.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 = 8**2
    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 + 1
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step + 1

    # Compute individual channel HOG features for the entire image
    img_hog_features = []
    try:
        for channel in hog_channels:
            img_hog_features.append(
                get_hog_features(ctrans_tosearch[:, :, channel],
                                 orient,
                                 pix_per_cell,
                                 cell_per_block,
                                 feature_vec=False))
    except Exception:
        # print(e)
        # print(ctrans_tosearch.shape, orient, pix_per_cell, cell_per_block)
        return []

    boxes = []
    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
            patch_hog_features = []
            for img_hog_features_channel in img_hog_features:
                patch_hog_features.append(
                    img_hog_features_channel[ypos:ypos + nblocks_per_window,
                                             xpos:xpos +
                                             nblocks_per_window].ravel())
            hog_features = np.hstack(patch_hog_features)

            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)

            # Scale features and make a prediction
            test_features = X_scaler.transform(
                np.hstack((spatial_features, hist_features,
                           hog_features)).reshape(1, -1))

            test_prediction = svc.predict(test_features)
            # if True:
            if test_prediction == 1:
                xbox_left = np.int(xleft * scale)
                ytop_draw = np.int(ytop * scale)
                win_draw = np.int(window * scale)
                boxes.append(((xbox_left + xstart, ytop_draw + ystart),
                              (xbox_left + win_draw + xstart,
                               ytop_draw + win_draw + ystart)))

    return boxes
예제 #6
0
def find_cars(image, y_start, y_stop, scale, svc, feature_scaler,
              feature_extraction_params):

    hot_windows = []

    resize_h = feature_extraction_params['resize_h']
    resize_w = feature_extraction_params['resize_w']
    spatial_size = feature_extraction_params['spatial_size']
    hist_bins = feature_extraction_params['hist_bins']
    orient = feature_extraction_params['orient']
    pix_per_cell = feature_extraction_params['pix_per_cell']
    cell_per_block = feature_extraction_params['cell_per_block']

    draw_img = np.copy(image)

    image_crop = image[y_start:y_stop, :]

    if scale != 1:
        imshape = image_crop.shape
        image_crop = cv2.resize(
            image_crop,
            (np.int(imshape[1] / scale), np.int(imshape[0] / scale)))

    # Define blocks and steps as above
    n_x_blocks = (image_crop.shape[1] // pix_per_cell) - 1
    n_y_blocks = (image_crop.shape[0] // pix_per_cell) - 1

    # 64 was the original sampling rate, with 8 cells and 8 pix per cell
    window = 64
    n_blocks_per_window = (window // pix_per_cell) - 1
    cells_per_step = 8  # Instead of overlap, define how many cells to step
    n_x_steps = (n_x_blocks - n_blocks_per_window) // cells_per_step
    n_y_steps = (n_y_blocks - n_blocks_per_window) // cells_per_step

    hog, _ = get_hog_features(image_crop,
                              orient,
                              pix_per_cell,
                              cell_per_block,
                              feature_vec=False)

    for xb in range(n_x_steps):
        for yb in range(n_y_steps):
            y_pos = yb * cells_per_step
            x_pos = xb * cells_per_step

            hog_features = hog[y_pos:y_pos + n_blocks_per_window, x_pos:x_pos +
                               n_blocks_per_window].ravel().reshape(1, -1)
            x_left = x_pos * pix_per_cell
            y_top = y_pos * pix_per_cell

            test_prediction = svc.predict(hog_features)

            if test_prediction == 1:
                xbox_left = np.int(x_left * scale)
                ytop_draw = np.int(y_top * scale)
                win_draw = np.int(window * scale)
                tl_corner_draw = (xbox_left, ytop_draw + y_start)
                br_corner_draw = (xbox_left + win_draw,
                                  ytop_draw + win_draw + y_start)

                cv2.rectangle(draw_img, tl_corner_draw, br_corner_draw,
                              (0, 0, 255), 6)

                hot_windows.append((tl_corner_draw, br_corner_draw))

    return hot_windows
    def find_at_scale(region_boundaries, scale):
        """
        Finds cars in the input image after resizing to a particular scale.
        """
        x_start, y_start, x_stop, y_stop = region_boundaries
        image_region = img[y_start:y_stop, x_start:x_stop, :]
        color_transformed_region = convert_color(image_region,
                                                 parameters['color_space'])

        if scale != 1:
            region_shape = color_transformed_region.shape
            new_shape = (np.int(region_shape[1] / scale),
                         np.int(region_shape[0] / scale))
            color_transformed_region = cv2.resize(color_transformed_region,
                                                  new_shape)

        # Unpack channels
        channel_1 = color_transformed_region[:, :, 0]
        channel_2 = color_transformed_region[:, :, 1]
        channel_3 = color_transformed_region[:, :, 2]

        # Dimensions
        width, height = channel_1.shape[1], channel_1.shape[0]

        # Define blocks and steps
        number_of_blocks_in_x = (width // parameters['pix_per_cell']) - 1
        number_of_blocks_in_y = (height // parameters['pix_per_cell']) - 1

        # 64 was the original sampling rate, with 8 cells and 8 pix per cell
        window = 64
        number_of_blocks_per_window = (window //
                                       parameters['pix_per_cell']) - 1
        cells_per_step = 2  # Instead of overlap, define how many cells to step
        number_of_steps_in_x = (number_of_blocks_in_x -
                                number_of_blocks_per_window) // cells_per_step
        number_of_steps_in_y = (number_of_blocks_in_y -
                                number_of_blocks_per_window) // cells_per_step

        # Compute individual channel HOG features for the entire region
        all_channels_hogs = [
            get_hog_features(channel_1,
                             orient=parameters['orientations'],
                             pix_per_cell=parameters['pix_per_cell'],
                             cell_per_block=parameters['cell_per_block'],
                             feature_vector=False),
            get_hog_features(channel_2,
                             orient=parameters['orientations'],
                             pix_per_cell=parameters['pix_per_cell'],
                             cell_per_block=parameters['cell_per_block'],
                             feature_vector=False),
            get_hog_features(channel_3,
                             orient=parameters['orientations'],
                             pix_per_cell=parameters['pix_per_cell'],
                             cell_per_block=parameters['cell_per_block'],
                             feature_vector=False)
        ]

        car_windows = []
        for xb in range(number_of_steps_in_x):
            for yb in range(number_of_steps_in_y):
                ypos = yb * cells_per_step
                xpos = xb * cells_per_step

                # Extract HOG for this patch
                if parameters['hog_channels'] == 'ALL':
                    hogs_considered = [
                        hog_feat[ypos:ypos + number_of_blocks_per_window,
                                 xpos:xpos +
                                 number_of_blocks_per_window].ravel()
                        for hog_feat in all_channels_hogs
                    ]
                else:
                    hogs_considered = [
                        all_channels_hogs[channel]
                        [ypos:ypos + number_of_blocks_per_window,
                         xpos:xpos + number_of_blocks_per_window].ravel()
                        for channel in parameters['hog_channels']
                    ]

                hog_features = np.hstack(hogs_considered)

                xleft = xpos * parameters['pix_per_cell']
                ytop = ypos * parameters['pix_per_cell']

                # Extract the image patch
                image_patch = cv2.resize(
                    color_transformed_region[ytop:ytop + window,
                                             xleft:xleft + window], (64, 64))

                features = [hog_features]
                # Get color features

                if parameters['histogram_features']:
                    hist_features = color_histogram(
                        image_patch,
                        number_of_bins=parameters['number_of_bins'])
                    features.insert(0, hist_features)

                if parameters['spatial_features']:
                    spatial_features = bin_spatial(
                        image_patch, size=parameters['spatial_size'])
                    features.insert(0, spatial_features)

                # Scale features and make a prediction
                features = np.hstack(features).reshape(1, -1)

                test_features = scaler.transform(features)
                test_prediction = classifier.predict(test_features)

                if test_prediction == 1:
                    xbox_left = np.int(xleft * scale)
                    ytop_draw = np.int(ytop * scale)
                    win_draw = np.int(window * scale)
                    new_window = ((xbox_left + x_start, ytop_draw + y_start),
                                  (xbox_left + x_start + win_draw,
                                   ytop_draw + win_draw + y_start))
                    car_windows.append(new_window)

        return car_windows