def combo_feature_train(path,
                        spatial_color,
                        spatial_size,
                        hist_color,
                        hist_bins,
                        hog_color,
                        orient,
                        pix_per_cell,
                        cell_per_block,
                        hog_channel='012',
                        spatial_feat=True,
                        hist_feat=True,
                        hog_feat=True):
    cars, noncars = load_training_images(path)

    svc, scaler = do_combo_feature_train(cars,
                                         noncars,
                                         spatial_color=spatial_color,
                                         spatial_size=spatial_size,
                                         hist_color=hist_color,
                                         hist_bins=hist_bins,
                                         hog_color=hog_color,
                                         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)
    return svc, scaler
Beispiel #2
0
def test_bin_spatial(path, dcspace='RGB'):
    cars, noncars = load_training_images(path)

    target_cspace = dcspace
    # read a car image and get feature
    ind = np.random.randint(0, len(cars))
    car_name = cars[ind]
    car_image = load_image(car_name, dcspace)
    car_feature = bin_spatial(car_image, size=(32, 32))

    # read a non car image and get feature
    ind = np.random.randint(0, len(noncars))
    noncar_name = noncars[ind]
    noncar_image = load_image(noncar_name, dcspace)
    noncar_feature = bin_spatial(noncar_image, size=(32, 32))

    # Plot features
    fig = plt.figure()
    plt.subplot(221)
    plt.imshow(color_convert_nocheck(car_image, dcspace, 'RGB'))
    plt.xlabel(car_name)
    plt.subplot(222)
    plt.plot(car_feature)
    plt.title('Spatially Binned Features')
    plt.suptitle(dcspace)
    plt.subplot(223)
    plt.imshow(color_convert_nocheck(noncar_image, dcspace, 'RGB'))
    plt.xlabel(noncar_name)
    plt.subplot(224)
    plt.plot(noncar_feature)
    plt.title('Spatially Binned Features')
    plt.suptitle(dcspace)
    fig.savefig("output_images/bin_spatial.jpg")
    plt.show()
    plt.close()
Beispiel #3
0
def combo_feature_train(path, scspace='BGR', dcspace='RGB', spatial_size=(32, 32),
                        hist_bins=32, orient=9,
                        pix_per_cell=8, cell_per_block=2, hog_channel='012',
                        spatial_feat=True, hist_feat=True, hog_feat=True):
    cars, noncars = load_training_images(path)

    svc, scaler = do_combo_feature_train(cars, noncars, dcspace=dcspace,
                                         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)
    return svc, scaler
Beispiel #4
0
def try_color_parameters(path):
    cars, noncars = load_training_images(path)

    sample_size = 2000
    cars = cars[0:sample_size]
    notcars = noncars[0:sample_size]
    print('select', sample_size, 'images from', len(cars),
          'car images and', len(noncars), 'non car images each')

    for spatial_color in 'RGB', 'YUV', 'LUV', 'YCrCb', 'GRAY', 'HLS', 'HSV':
        for hist_color in 'RGB', 'YUV', 'LUV', 'YCrCb', 'GRAY', 'HLS', 'HSV':
            for spatial_size in (16, 32):
                for histbin in (16, 32, 64):
                    car_features = extract_color_features(cars, spatial_color=spatial_color, spatial_size=(
                        spatial_size, spatial_size), hist_color=hist_color, hist_bins=histbin, hist_range=(0, 256))
                    notcar_features = extract_color_features(noncars, spatial_color=spatial_color, spatial_size=(
                        spatial_size, spatial_size), hist_color=hist_color, hist_bins=histbin, hist_range=(0, 256))

                    # Create an array stack of feature vectors
                    X = np.vstack((car_features, notcar_features)).astype(np.float64)
                    scaled_X, scaler = scale_norm(X)

                    # Define the labels vector
                    y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))

                    # Split up data into randomized training and test sets
                    rand_state = np.random.randint(0, 100)
                    X_train, X_test, y_train, y_test = train_test_split(
                        scaled_X, y, test_size=0.2, random_state=rand_state)

                    print('Using spatial color space of:', spatial_color, 'spatial binning of:', spatial_size,
                          '\nhistogram in color space of:', hist_color, 'and',  histbin, 'histogram bins')

                    svc = color_svc(X_train, y_train)

                    # Check the score of the SVC
                    print('Test Accuracy of SVC = ', round(
                        svc.score(X_test, y_test), 4))
                    # Check the prediction time for a single sample
                    t = time.time()
                    n_predict = 10
                    print('My SVC predicts: ',
                          svc.predict(X_test[0:n_predict]))
                    print('For these', n_predict,
                          'labels: ', y_test[0:n_predict])
                    t2 = time.time()
                    print(round(t2 - t, 5), 'Seconds to predict',
                          n_predict, 'labels with SVC')
def test_hog(path):
    cars, noncars = load_training_images(path)

    # Generate a random index to look at a car image
    car_ind = np.random.randint(0, len(cars))
    noncar_ind = np.random.randint(0, len(noncars))
    # Read in the image
    car_image = load_image(cars[car_ind], 'RGB')
    car_gray = cv2.cvtColor(car_image, cv2.COLOR_RGB2GRAY)
    noncar_image = load_image(noncars[noncar_ind], 'RGB')
    noncar_gray = cv2.cvtColor(noncar_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, car_hog_image = get_hog_features(car_gray, orient,
                                           pix_per_cell, cell_per_block,
                                           vis=True, feature_vec=False)
    features, noncar_hog_image = get_hog_features(noncar_gray, orient,
                                           pix_per_cell, cell_per_block,
                                           vis=True, feature_vec=False)


    fig = plt.figure(figsize=(10, 8))
    plt.subplot(231)
    plt.imshow(car_image)
    plt.xlabel(cars[car_ind])
    plt.title('Example Car Image')
    plt.subplot(232)
    plt.imshow(car_gray, cmap='gray')
    plt.title('Gray Car Image')
    plt.subplot(233)
    plt.imshow(car_hog_image, cmap='gray')
    plt.title('HOG Visualization')
    plt.subplot(234)
    plt.imshow(noncar_image)
    plt.xlabel(noncars[noncar_ind])
    plt.title('Example Non-car Image')
    plt.subplot(235)
    plt.imshow(noncar_gray, cmap='gray')
    plt.title('Gray Non-car Image')
    plt.subplot(236)
    plt.imshow(noncar_hog_image, cmap='gray')
    plt.title('HOG Visualization')
    plt.suptitle('HOG')
    fig.tight_layout()
    fig.savefig("output_images/hog.jpg")
def test_combo_feature(train_dir, force_run=False):
    # 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)
    #image = image.astype(np.float32)/255
    scspace = 'BGR'
    hog_color = 'YCrCb'  # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
    orient = 9  # HOG orientations
    pix_per_cell = 8  # HOG pixels per cell
    cell_per_block = 2  # HOG cells per block
    hog_channel = '012'  # Can be 0, 1, 2, or "012"
    spatial_color = 'YCrCb'
    spatial_size = 64  # Spatial binning dimensions
    hist_color = 'YCrCb'
    hist_bins = 128  # Number of histogram bins
    spatial_feat = True  # Spatial features on or off
    hist_feat = True  # Histogram features on or off
    hog_feat = True  # HOG features on or off
    y_start_stop = [None, None]  # Min and max in y to search in slide_window()

    svc, scaler = combo_feature_train(train_dir,
                                      spatial_color=spatial_color,
                                      spatial_size=spatial_size,
                                      hist_color=hist_color,
                                      hist_bins=hist_bins,
                                      hog_color=hog_color,
                                      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)

    cars, noncars = load_training_images(train_dir)

    cars = cars[0:2]

    for fname in cars:
        image = cv2.imread(fname)
        draw_image = np.copy(image)
        draw_image = color_convert_nocheck(image, 'BGR', 'RGB')

        #image = cv2.resize(image, (64, 64))

        features = combo_feature(image,
                                 'RGB',
                                 spatial_color=spatial_color,
                                 spatial_size=spatial_size,
                                 hist_color=hist_color,
                                 hist_bins=hist_bins,
                                 hog_color=hog_color,
                                 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)
        # 5) Scale extracted features to be fed to classifier
        test_features = scaler.transform(np.array(features).reshape(1, -1))
        # 6) Predict using your classifier
        prediction = svc.predict(test_features)

        basename = os.path.basename(fname)
        name, ext = os.path.splitext(basename)
        savename = os.path.join('output_images', name + "_try_combo" + ext)
        fig = plt.figure()
        plt.imshow(draw_image)
        if prediction == 1:
            plt.title('Original Image is car')
        else:
            plt.title('Original image is not car')
        plt.xlabel('fname')
        fig.savefig(savename)
def main(path):
    cars, noncars = load_training_images(path)
    car_ind = np.random.randint(0, len(cars))
    noncar_ind = np.random.randint(0, len(noncars))

    for spatial_color in ('RGB', 'YUV', 'LUV', 'YCrCb', 'HLS', 'HSV', 'GRAY'):
        for hist_color in ('RGB', 'YUV', 'LUV', 'YCrCb', 'HLS', 'HSV', 'GRAY'):
            car_features = extract_color_features(cars,
                                                  spatial_color=spatial_color,
                                                  spatial_size=(32, 32),
                                                  hist_color=hist_color,
                                                  hist_bins=32,
                                                  hist_range=(0, 256))
            notcar_features = extract_color_features(
                noncars,
                spatial_color=spatial_color,
                spatial_size=(32, 32),
                hist_color=hist_color,
                hist_bins=32,
                hist_range=(0, 256))

            if len(car_features) > 0:
                # Create an array stack of feature vectors
                X = np.vstack(
                    (car_features, notcar_features)).astype(np.float64)
                scaled_X, scaler = scale_norm(X)

                # Plot an example of raw and scaled features
                fig = plt.figure(figsize=(12, 8))

                plt.subplot(231)
                plt.imshow(load_image(cars[car_ind], 'RGB'))
                plt.title('Original Image')
                plt.xlabel(cars[car_ind])

                plt.subplot(232)
                plt.plot(X[car_ind])
                plt.title('Raw Features')

                plt.subplot(233)
                plt.plot(scaled_X[car_ind])
                plt.title('Normalized Features')

                plt.subplot(234)
                plt.imshow(load_image(noncars[noncar_ind], 'RGB'))
                plt.title('Original Image')
                plt.xlabel(noncars[noncar_ind])

                plt.subplot(235)
                plt.plot(X[noncar_ind + len(cars)])
                plt.title('Raw Features')

                plt.subplot(236)
                plt.plot(scaled_X[noncar_ind + len(cars)])
                plt.title('Normalized Features')

                plt.suptitle('spatial color: ' + spatial_color +
                             ' histogram color: ' + hist_color)
                fig.tight_layout()
                savename = os.path.join(
                    'output_images',
                    spatial_color + '_' + hist_color + '_feature.jpg')
                fig.savefig(savename)
                plt.close()
            else:
                print('Your function only returns empty feature vectors...')
Beispiel #8
0
def test_color_hist(path):
    cars, noncars = load_training_images(path)

    car_ind = np.random.randint(0, len(cars))
    noncar_ind = np.random.randint(0, len(noncars))
    car_name = cars[car_ind]
    noncar_name = noncars[noncar_ind]

    for cspace in ('RGB', 'YUV', 'HLS', 'LUV', 'YCrCb', 'HSV', 'GRAY'):
        car_image = load_image(car_name, cspace)
        noncar_image = load_image(noncar_name, cspace)

        car_feature_vec, car_bincen, car_num_channels, car_ch0, car_ch1, car_ch2 = color_hist(
            car_image, nbins=32, bins_range=(0, 256))
        noncar_feature_vec, noncar_bincen, noncar_num_channels, noncar_ch0, noncar_ch1, noncar_ch2 = color_hist(
            noncar_image, nbins=32, bins_range=(0, 256))

        # Plot a figure with all channel bars
        if car_num_channels == 1:
            fig = plt.figure(figsize=(12, 6))
            plt.subplot(231)
            plt.imshow(load_image(car_name, 'RGB'))
            plt.title(car_name)

            plt.subplot(232)
            plt.imshow(car_image, cmap='gray')
            plt.xlabel(cspace)

            plt.subplot(233)
            plt.bar(car_bincen, car_ch0[0])
            plt.xlim(0, 256)
            plt.title('Histogram')

            plt.subplot(234)
            plt.imshow(load_image(noncar_name, 'RGB'))
            plt.title(noncar_name)

            plt.subplot(235)
            plt.imshow(noncar_image, cmap='gray')
            plt.xlabel(cspace)

            plt.subplot(236)
            plt.bar(noncar_bincen, noncar_ch0[0])
            plt.xlim(0, 256)
            plt.title('Histogram')
            plt.suptitle(cspace)
            # fig.tight_layout()
            savename = os.path.join('output_images', cspace + '_hist.jpg')
            fig.savefig(savename)
        elif car_num_channels == 3:
            fig = plt.figure(figsize=(20, 6))
            plt.suptitle(cspace)

            plt.subplot(271)
            plt.imshow(load_image(car_name, 'RGB'))
            plt.title(car_name)

            plt.subplot(272)
            plt.imshow(car_image[:, :, 0], cmap='gray')
            plt.xlabel('Channel 1')

            plt.subplot(273)
            plt.imshow(car_image[:, :, 1], cmap='gray')
            plt.xlabel('Channel 2')

            plt.subplot(274)
            plt.imshow(car_image[:, :, 2], cmap='gray')
            plt.xlabel('Channel 3')

            plt.subplot(275)
            plt.bar(car_bincen, car_ch0[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 1 Histogram')

            plt.subplot(276)
            plt.bar(car_bincen, car_ch1[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 2 Histogram')

            plt.subplot(277)
            plt.bar(car_bincen, car_ch2[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 3 Histogram')

            plt.subplot(278)
            plt.imshow(load_image(noncar_name, 'RGB'))
            plt.title(noncar_name)

            plt.subplot(279)
            plt.imshow(noncar_image[:, :, 0], cmap='gray')
            plt.xlabel('Channel 1')

            plt.subplot(2, 7, 10)
            plt.imshow(noncar_image[:, :, 1], cmap='gray')
            plt.xlabel('Channel 2')

            plt.subplot(2, 7, 11)
            plt.imshow(noncar_image[:, :, 2], cmap='gray')
            plt.xlabel('Channel 3')

            plt.subplot(2, 7, 12)
            plt.bar(noncar_bincen, noncar_ch0[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 1 Histogram')

            plt.subplot(2, 7, 13)
            plt.bar(noncar_bincen, noncar_ch1[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 2 Histogram')

            plt.subplot(2, 7, 14)
            plt.bar(noncar_bincen, noncar_ch2[0])
            plt.xlim(0, 256)
            plt.xlabel('Channel 3 Histogram')

            # fig.tight_layout()
            savename = os.path.join('output_images', cspace + '_hist.jpg')
            fig.savefig(savename)
        else:
            print(
                'Your function is returning None for at least one variable...')
def test_hog_feature(path):
    cars, noncars = load_training_images(path)

    # Reduce the sample size because HOG features are slow to compute
    # The quiz evaluator times out after 13s of CPU time
    sample_size = 2000
    cars = cars[0:sample_size]
    notcars = noncars[0:sample_size]
    print('select', sample_size, 'images from', len(cars), 'car images and',
          len(noncars), 'non car images each')

    # TODO: Tweak these parameters and see how the results change.
    #colorspace = 'YUV'  # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
    #orient = 9
    #pix_per_cell = 8
    #cell_per_block = 2
    #hog_channel = '012'  # Can be 0, 1, 2, or "ALL"

    for colorspace in ('RGB', 'YUV', 'HLS', 'HSV', 'LUV', 'YCrCb', 'GRAY'):
        for hog_channel in ('0', '1', '2', '01', '02', '12', '012'):
            for orient in (6, 9, 12):
                for pix_per_cell in (8, 16):
                    for cell_per_block in (2, 4):
                        print('use color:', colorspace, 'channel:',
                              hog_channel, 'orient:', orient, 'pix_per_cell:',
                              pix_per_cell, 'cell_per_block:', cell_per_block)

                        t = time.time()
                        car_features = extract_hog_features(
                            cars,
                            cspace=colorspace,
                            orient=orient,
                            pix_per_cell=pix_per_cell,
                            cell_per_block=cell_per_block,
                            hog_channel=hog_channel)
                        notcar_features = extract_hog_features(
                            noncars,
                            cspace=colorspace,
                            orient=orient,
                            pix_per_cell=pix_per_cell,
                            cell_per_block=cell_per_block,
                            hog_channel=hog_channel)

                        if len(car_features) == 0 or len(notcar_features) == 0:
                            print("can't find", hog_channel, 'in', colorspace)
                            break

                        t2 = time.time()
                        print(round(t2 - t, 2),
                              'Seconds to extract HOG features...')
                        # Create an array stack of feature vectors
                        X = np.vstack(
                            (car_features, notcar_features)).astype(np.float64)
                        scaled_X, scaler = scale_norm(X)

                        # Define the labels vector
                        y = np.hstack((np.ones(len(car_features)),
                                       np.zeros(len(notcar_features))))

                        # Split up data into randomized training and test sets
                        rand_state = np.random.randint(0, 100)
                        X_train, X_test, y_train, y_test = train_test_split(
                            scaled_X,
                            y,
                            test_size=0.2,
                            random_state=rand_state)

                        print('Using: color', colorspace, 'channel',
                              hog_channel, orient, 'orientations',
                              pix_per_cell, 'pixels per cell and',
                              cell_per_block, 'cells per block')

                        svc = hog_classifier(X_train, y_train)

                        # Check the score of the SVC
                        print('Test Accuracy of SVC = ',
                              round(svc.score(X_test, y_test), 4))
                        # Check the prediction time for a single sample
                        t = time.time()
                        n_predict = 10
                        print('My SVC predicts: ',
                              svc.predict(X_test[0:n_predict]))
                        print('For these', n_predict, 'labels: ',
                              y_test[0:n_predict])
                        t2 = time.time()
                        print(round(t2 - t, 5), 'Seconds to predict',
                              n_predict, 'labels with SVC')