Esempio n. 1
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()
Esempio n. 2
0
def test_combo_feature(train_dir, test_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'
    dcspace = '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_size = (32, 32)  # Spatial binning dimensions
    hist_bins = 32    # 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()

    if have_classifier() and force_run == False:
        svc, scaler, dcspace, spatial_size, hist_bins, orient, \
        pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat = load_classifier()
    else:
        svc, scaler = combo_feature_train(train_dir, scspace=scspace, 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)

    fnames = load_images(test_dir)
    for fname in fnames:
        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, scspace=scspace, 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)
        # 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 + "_classify" + 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 extract_color_feature(fname,
                          spatial_color,
                          spatial_size,
                          hist_color,
                          hist_bins=32,
                          hist_range=(0, 256)):
    ''' extract color features '''
    image = load_image(fname)

    spatial_image = color_convert_nocheck(image, 'BGR', spatial_color)

    spatial_feature = bin_spatial(spatial_image, size=spatial_size)

    hist_image = color_convert_nocheck(image, 'BGR', hist_color)
    hist_feature = color_hist_feature(hist_image,
                                      nbins=hist_bins,
                                      bins_range=hist_range)

    return np.concatenate((spatial_feature, hist_feature))
def process_frame(img, svc, scaler, dcspace, spatial_size,
                  hist_bins, orient, pix_per_cell, cell_per_block,
                  hog_channel, spatial_feat, hist_feat, hog_feat):

    global detector
    heat = np.zeros_like(img[:, :, 0]).astype(np.float)
    ystart = 400
    ystop = 656
    scspace = 'RGB'

    if detector.debug:
        window_img = color_convert_nocheck(img, scspace, 'BGR')

    #box_list = []
    for scale in (1.0, 1.5, 2.0):
        boxes = find_cars(img, scspace, dcspace, ystart, ystop, scale, svc, scaler,
                          orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)
        heat = add_heat(heat, boxes)
        if detector.debug:
            window_img = draw_boxes(window_img, boxes, color=(0, 255, 255), thick=2)

    #if detector.debug:
        #savename = "output_images/{0:05d}.jpg".format(
        #    detector.num_processed_frames)
        #cv2.imwrite(savename, window_img)
    heat = apply_threshold(heat, 1)
    heatmap = np.clip(heat, 0, 255)
    #detector.update(heatmap)
    #heatmap = np.clip(heat, 0, 255)
    # detector.update(heat)
    #heatmap = np.clip(detector.avg_heat, 0, 255)
    # if detector.debug:
    #    savename = "output_images/{0:05d}.jpg".format(detector.num_processed_frames)
    #    cv2.imwrite(savename, detector.acc_heat)
    #heatmap = detector.avg_heat
    #heatmap = apply_threshold(heatmap, 1)
    labels = label(heatmap)
    #img = draw_labeled_bboxes(img, labels)
    bboxes = get_labeled_bboxes(labels)
    detector.update(bboxes)
    img = draw_boxes(img, detector.valid_boxes)
    if detector.debug:
        window_img = draw_boxes(window_img, detector.valid_boxes, color=(255, 0, 0), thick=3)
        window_img = draw_boxes(window_img, detector.invalid_boxes, color=(0,0,255), thick=5)
        savename = "output_images/{0:05d}_debug.jpg".format(detector.num_processed_frames)
        cv2.imwrite(savename, window_img)

    return img
Esempio n. 5
0
def combo_feature(img, 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):
    # 1) Define an empty list to receive features
    img_features = []
    # 2) Apply color conversion if other than 'RGB'
    cvtImage = color_convert_nocheck(img, scspace, dcspace)
    # 3) Compute spatial features if flag is set
    if spatial_feat == True:
        spatial_features = bin_spatial(cvtImage, size=spatial_size)
        # 4) Append features to list
        img_features.append(spatial_features)
    # 5) Compute histogram features if flag is set
    if hist_feat == True:
        hist_features = color_hist_feature(cvtImage, nbins=hist_bins)
        # 6) Append features to list
        img_features.append(hist_features)
    # 7) Compute HOG features if flag is set
    if hog_feat == True:
        hog_features = []
        if len(cvtImage.shape) == 3:
            channels = cvtImage.shape[2]
        else:
            channels = 1
        for channel in range(channels):
            if str(channel) in hog_channel:
                #print('use channel', channel)
                if channels == 1:
                    hog_features.append(get_hog_features(cvtImage[:, :],
                                                     orient, pix_per_cell, cell_per_block,
                                                     vis=False, feature_vec=True))
                else:
                    hog_features.append(get_hog_features(cvtImage[:, :, channel],
                                                     orient, pix_per_cell, cell_per_block,
                                                     vis=False, feature_vec=True))
        hog_features = np.ravel(hog_features)
        # 8) Append features to list
        img_features.append(hog_features)

    # 9) Return concatenated array of features
    return np.concatenate(img_features)
def test_sliding_window(path):
    fnames = load_images(path)

    y_start_stop = [400, 656]
    for fname in fnames:
        image = cv2.imread(fname)
        image = color_convert_nocheck(image, 'BGR', 'RGB')
        windows = slide_window(image,
                               x_start_stop=[None, None],
                               y_start_stop=y_start_stop,
                               xy_window=(64, 64),
                               xy_overlap=(0.75, 0.75))

        window_img = draw_boxes(image, windows, color=(0, 0, 255), thick=2)
        basename = os.path.basename(fname)
        name, ext = os.path.splitext(basename)
        savename = os.path.join('output_images', name + "_window" + ext)
        fig = plt.figure(figsize=(16, 8))
        plt.imshow(window_img)
        plt.title('Sliding Window')
        plt.xlabel(fname)
        fig.savefig(savename)
def load_image(fname, color_space='BGR'):
    '''load image using opencv imread function, convert color space according to input parameters'''
    image = cv2.imread(fname)
    image = color_convert_nocheck(image, 'BGR', color_space)
    return image
Esempio n. 8
0
def apply_window_search(train_dir, test_dir):
    # 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
    dcspace = '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_size = (32, 32)  # Spatial binning dimensions
    hist_bins = 32    # 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()
    if have_classifier():
        svc, scaler, dcspace, spatial_size, hist_bins, orient, \
        pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat = load_classifier()
    else:
        svc, scaler = combo_feature_train(train_dir, scspace='BGR', 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)

    fnames = load_images(test_dir)

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

        min_window = spatial_size[0]
        max_window = 128

        top_y = int(image.shape[0] / 2)
        y_start_stop = [top_y, image.shape[0]]
        valid_y = image.shape[0] - top_y
        for window_size in range(min_window, max_window, spatial_size[0]):
            #window_size = int(min_window * scale_factor)
            #print("window size", window_size, "larger than", image.shape[0], "x", image.shape[1])

            if window_size > valid_y or window_size > image.shape[1]:
                print("window size", window_size, "larger than",
                      valid_y, "x", image.shape[1])
                continue

            windows = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop,
                                   xy_window=(window_size, window_size), xy_overlap=(0.75, 0.75))

            hot_windows = search_windows(image, windows, svc, scaler, scspace='BGR', 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)

            window_img = draw_boxes(draw_image, hot_windows,
                                    color=(0, 0, 255), thick=5)

            plt.imshow(window_img)
            basename = os.path.basename(fname)
            name, ext = os.path.splitext(basename)
            savename = os.path.join(
                'output_images', name + "_" + str(window_size) + "_" + ext)
            fig = plt.figure()
            plt.imshow(window_img)
            plt.title('Searching window size' + str(window_size))
            plt.xlabel(fname)
            fig.savefig(savename)
            plt.close()
Esempio n. 9
0
def find_cars(img,
              scspace,
              dcspace,
              ystart,
              ystop,
              scale,
              svc,
              scaler,
              orient,
              pix_per_cell,
              cell_per_block,
              spatial_size,
              hist_bins,
              draw=False):

    if draw:
        #draw_img = np.copy(img)
        draw_img = color_convert_nocheck(img, scspace, 'RGB')
    #img = img.astype(np.float32) / 255

    img_tosearch = img[ystart:ystop, :, :]
    ctrans_tosearch = color_convert_nocheck(img_tosearch, scspace, dcspace)
    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
    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)

    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
            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))

            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_feature(subimg, nbins=hist_bins)

            # Scale features and make a prediction
            test_features = scaler.transform(
                np.hstack((spatial_features, hist_features,
                           hog_features)).reshape(1, -1))
            #test_features = scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1))
            test_prediction = svc.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)
                boxes.append(
                    ((xbox_left, ytop_draw + ystart),
                     (xbox_left + win_draw, ytop_draw + win_draw + ystart)))
                if draw:
                    cv2.rectangle(
                        draw_img, (xbox_left, ytop_draw + ystart),
                        (xbox_left + win_draw, ytop_draw + win_draw + ystart),
                        (0, 0, 255), 6)

    if draw:
        return draw_img, boxes
    else:
        return boxes