def extract_feature(img):
    hog_features = features.hog_feature(img,
                                        orient=orient,
                                        pix_per_cell=pix_per_cell,
                                        cell_per_block=cell_per_block,
                                        hog_channel=hog_channel)
    spatial_features = features.bin_spatial(img, size=spatial_size)
    hist_features = features.color_hist(img, nbins=hist_bins)
    return np.hstack((spatial_features, hist_features, hog_features))
    def single_img_features(self, img):
        #1) Define an empty list to receive features
        img_features = []
        #2) Apply color conversion if other than 'RGB'
        if self.color_space != 'RGB':
            if self.color_space == 'HSV':
                feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
            elif self.color_space == 'LUV':
                feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)
            elif self.color_space == 'HLS':
                feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
            elif self.color_space == 'YUV':
                feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
            elif self.color_space == 'YCrCb':
                feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
        else:
            feature_image = np.copy(img)
        #3) Compute spatial features if flag is set
        if self.spatial_feat == True:
            spatial_features = features.bin_spatial(feature_image,
                                                    size=self.spatial_size)
            #4) Append features to list
            img_features.append(spatial_features)
        #5) Compute histogram features if flag is set
        if self.hist_feat == True:
            hist_features = features.color_hist(feature_image,
                                                nbins=self.hist_bins)
            #6) Append features to list
            img_features.append(hist_features)
        #7) Compute HOG features if flag is set
        if self.hog_feat == True:
            if self.hog_channel == 'ALL':
                hog_features = []
                for channel in range(feature_image.shape[2]):
                    hog_features.extend(
                        features.get_hog_features(feature_image[:, :, channel],
                                                  self.orient,
                                                  self.pix_per_cell,
                                                  self.cell_per_block,
                                                  vis=False,
                                                  feature_vec=True))
            else:
                hog_features = features.get_hog_features(
                    feature_image[:, :, self.hog_channel],
                    self.orient,
                    self.pix_per_cell,
                    self.cell_per_block,
                    vis=False,
                    feature_vec=True)
            #8) Append features to list
            img_features.append(hog_features)

        #9) Return concatenated array of features
        return np.concatenate(img_features)
示例#3
0
def find_cars(img, scale, ystart, ystop, pix_per_cell, cell_per_block, orient, spatial_size, hist_bins):

    draw_img = np.copy(img)
    # Make a heatmap of zeros
    heatmap = np.zeros_like(img[:,:,0])
    img = img.astype(np.float32)/255

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

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

    for xb in range(nxsteps):
        for yb in range(nysteps):
            ypos = yb*cells_per_step
            xpos = xb*cells_per_step
            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(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 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)
                heatmap[ytop_draw+ystart:ytop_draw+win_draw+ystart, xbox_left:xbox_left+win_draw] += 1

    return draw_img, heatmap
示例#4
0
def find_cars(img,
              ystart,
              ystop,
              scale,
              svc,
              X_scaler,
              orient=9,
              pix_per_cell=8,
              cell_per_block=2,
              spatial_size=(32, 32),
              hist_bins=32,
              spatial_feat=True,
              hist_feat=True,
              hog_feat=True,
              vis_boxes=False):
    # draw_img = np.copy(img)
    boxes = []
    spatial_features = []
    hog_features = []

    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) - cell_per_block + 1
    nyblocks = (ch1.shape[0] // pix_per_cell) - cell_per_block + 1

    # 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 + 1
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step + 1
    if hog_feat:
        # 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)

    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_feat:
                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
            if spatial_feat:
                spatial_features = bin_spatial(subimg, size=spatial_size)
            if hist_feat:
                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_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1))
            test_prediction = svc.predict(test_features)

            if test_prediction == 1 or vis_boxes:
                xbox_left = np.int(xleft * scale)
                ytop_draw = np.int(ytop * scale)
                win_draw = np.int(window * scale)

                startx = xbox_left
                starty = ytop_draw + ystart

                endx = xbox_left + win_draw
                endy = ytop_draw + win_draw + ystart

                boxes.append(((startx, starty), (endx, endy)))

    return boxes
def img_process_pipeline_2(images,
                           orient,
                           pix_per_cell,
                           cell_per_block,
                           spatial_size,
                           hist_bins,
                           scaler,
                           svc,
                           saveFig=False):
    """
    Simplified / faster image processing pipeline

    :param images:
    :return:
    """
    out_images = []
    out_maps = []
    out_titles = []
    out_boxes = []

    ystart = 400
    ystop = 656
    scale = 1.5

    # Iterate over test images
    for img_src in images:
        img_boxes = []
        t = time.time()
        count = 0

        # Read in the image
        img = mpimg.imread(img_src)
        draw_img = np.copy(img)

        # Make a heatmap of zeros
        heatmap = np.zeros_like(img[:, :, 0])
        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
        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

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

        for xb in range(nxsteps):
            for yb in range(nysteps):
                count += 1
                xpos = xb * cells_per_step
                ypos = yb * 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(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_pred = svc.predict(test_features)

                if test_pred == 1:
                    xbox_left = np.int(xleft * scale)
                    ytop_draw = np.int(ytop * scale)
                    win_draw = np.int(window * scale)

                    # Draw a box
                    cv2.rectangle(
                        draw_img, (xbox_left, ytop_draw + ystart),
                        (xbox_left + win_draw, ytop_draw + win_draw + ystart),
                        (0, 0, 255))
                    img_boxes.append(((xbox_left, ytop_draw + ystart),
                                      (xbox_left + win_draw,
                                       ytop_draw + win_draw + ystart)))

                    # Draw a heatmap
                    heatmap[ytop_draw + ystart:ytop_draw + win_draw + ystart,
                            xbox_left:xbox_left + win_draw] += 1

        print(time.time() - t, 'seconds to run, total windows =', count)

        out_images.append(draw_img)

        out_titles.append(img_src[-9:])
        out_titles.append(img_src[-9:])
        out_images.append(heatmap)
        out_maps.append(heatmap)
        out_boxes.append(img_boxes)

    fig = plt.figure(figsize=(12, 24))
    visualize(fig, 8, 2, out_images, out_titles)
    plt.savefig('output_images/streamlined.png')
    def find_cars(self, img):

        t1 = time.time()

        ystart = 400
        ystop = 656
        scale = 1
        orient = 9
        pix_per_cell = 8
        cell_per_block = 2
        spatial_size = (32, 32)
        hist_bins = 32

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

        img_tosearch = img[ystart:ystop, :, :]
        ctrans_tosearch = self.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)))
            # visualizer.draw_image(ctrans_tosearch)

        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 = features.get_hog_features(ch1,
                                         orient,
                                         pix_per_cell,
                                         cell_per_block,
                                         feature_vec=False)
        hog2 = features.get_hog_features(ch2,
                                         orient,
                                         pix_per_cell,
                                         cell_per_block,
                                         feature_vec=False)
        hog3 = features.get_hog_features(ch3,
                                         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
                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 = features.bin_spatial(subimg,
                                                        size=spatial_size)
                hist_features = features.color_hist(subimg, nbins=hist_bins)

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

        t2 = time.time()
        print(round(t2 - t1, 2), 'Seconds to classify one image')
        return draw_img
示例#7
0
def find_cars(img, scaler, model, config):
    orient = config["orient"]
    pix_per_cell = config["pix_per_cell"]
    cell_per_block = config["cell_per_block"]
    spatial_size = config["spatial_size"]
    hist_bins = config["hist_bins"]
    train_image_format = config["train_image_format"]
    colorspace = config["colorspace"]
    hog_channel = config["hog_channel"]
    spatial_feat = config["spatial_feat"]
    hist_feat = config["hist_feat"]
    hog_feat = config["hog_feat"]
    ystart = config["y_start"]
    ystop = config["y_stop"]
    scale = config["scale"]

    boxes = []

    # 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)
    if train_image_format == 'png':
        img = img.astype(np.float32) / 255

    img_tosearch = img[ystart:ystop, :, :]
    ctrans_tosearch = features.convert_color(img_tosearch, conv=colorspace)

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

    if hog_channel == 'ALL':
        ch1 = ctrans_tosearch[:, :, 0]
        ch2 = ctrans_tosearch[:, :, 1]
        ch3 = ctrans_tosearch[:, :, 2]

    else:
        ch1 = ctrans_tosearch[:, :, hog_channel]

    # 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 + 1
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step + 1

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

    else:
        hog1 = features.get_hog_features(ch1,
                                         orient,
                                         pix_per_cell,
                                         cell_per_block,
                                         feature_vec=False)

    # import time
    #
    # start = time.time()
    # print("nxsteps=",nxsteps,"nysteps=",nysteps )
    for xb in range(nxsteps):
        for yb in range(nysteps):
            # start_i = time.time()
            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 = np.hstack((hog_feat1))

            # print("xb=",xb,"yb=",yb , "hog = ", time.time() - start_i)
            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 = features.bin_spatial(subimg, size=spatial_size)
            hist_features = features.color_hist(subimg, nbins=hist_bins)
            # print("xb=", xb, "yb=", yb, "spatial/hist = ", time.time() - start_i)
            # Scale features and make a prediction
            test_features = scaler.transform(
                np.hstack((spatial_features, hist_features,
                           hog_features)).reshape(1, -1))
            # print("xb=", xb, "yb=", yb, "scale = ", time.time() - start_i)
            # test_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1))
            test_prediction = model.predict(test_features)
            # print("xb=", xb, "yb=", yb, "pred = ", time.time() - start_i)
            # print(nxsteps, nysteps, test_prediction)
            if test_prediction == 1:
                xbox_left = np.int(xleft * scale)
                ytop_draw = np.int(ytop * scale)
                win_draw = np.int(window * scale)

                box = ((xbox_left, ytop_draw + ystart),
                       (xbox_left + win_draw, ytop_draw + win_draw + ystart))
                boxes.append(box)
            # print("xb=", xb, "yb=", yb, "total = ", time.time() - start_i)
    # end = time.time()
    # print("pred + sliding window = ", end - start)
    return boxes
    def find_cars(
            self,
            img,
            y_start_stop,
            window=64,
            cells_per_step=2  # Instead of overlap, define how many cells to step
    ):

        ystart, ystop = y_start_stop
        ystart = ystart or 0
        ystop = ystop or img.shape[0]

        ctrans_tosearch = img[ystart:ystop, :, :]
        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

        nblocks_per_window = (window // pix_per_cell) - 1
        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 = features.get_hog_features(ch1,
                                         orient,
                                         pix_per_cell,
                                         cell_per_block,
                                         feature_vec=False)
        hog2 = features.get_hog_features(ch2,
                                         orient,
                                         pix_per_cell,
                                         cell_per_block,
                                         feature_vec=False)
        hog3 = features.get_hog_features(ch3,
                                         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
                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 = features.bin_spatial(subimg,
                                                        size=spatial_size)
                hist_features = features.color_hist(subimg, nbins=hist_bins)

                # Scale features and make a prediction
                test_features = self.X_scaler.transform(
                    np.hstack((spatial_features, hist_features,
                               hog_features)).reshape(1, -1))
                test_prediction = self.clf.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)
                    yield xbox_left, ytop_draw + ystart, xbox_left + win_draw, ytop_draw + win_draw + ystart
def find_cars(img, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell,
              cell_per_block, spatial_size, hist_bins, spatial_feat, hist_feat,
              hog_feat, hog_channel):

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

    img_tosearch = img[ystart:ystop, :, :]
    ctrans_tosearch = convert_color(img_tosearch, color_space='YCrCb')

    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

    if hog_channel == 'ALL':
        img_hog_features = []
        for channel in range(ctrans_tosearch.shape[2]):
            img_hog_features.append(
                get_hog_features(ctrans_tosearch[:, :, channel],
                                 orient,
                                 pix_per_cell,
                                 cell_per_block,
                                 feature_vec=False))
    else:
        img_hog_features = get_hog_features(ctrans_tosearch[:, :, hog_channel],
                                            orient,
                                            pix_per_cell,
                                            cell_per_block,
                                            feature_vec=False)

    bboxes = []
    debug_boxes = []
    for xb in range(nxsteps):
        for yb in range(nysteps):
            ypos = yb * cells_per_step
            xpos = xb * cells_per_step

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

            #1) Define an empty list to receive features
            img_features = []

            #3) Compute spatial features if flag is set
            if spatial_feat == True:
                spatial_features = bin_spatial(subimg, 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(subimg, 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:
                if hog_channel == 'ALL':
                    hog_features = []
                    for channel in range(ctrans_tosearch.shape[2]):
                        hog_features.extend(img_hog_features[channel][
                            ypos:ypos + nblocks_per_window,
                            xpos:xpos + nblocks_per_window].ravel())

                else:
                    hog_features = img_hog_features[
                        ypos:ypos + nblocks_per_window,
                        xpos:xpos + nblocks_per_window].ravel()
                #8) Append features to list
                img_features.append(hog_features)

            # Scale features and make a prediction
            test_features = X_scaler.transform(
                np.concatenate(img_features).reshape(1, -1))
            test_prediction = svc.predict(test_features)

            xbox_left = np.int(xleft * scale)
            ytop_draw = np.int(ytop * scale)
            win_draw = np.int(window * scale)
            box = ((xbox_left, ytop_draw + ystart),
                   (xbox_left + win_draw, ytop_draw + win_draw + ystart))
            debug_boxes.append(box)

            if test_prediction == 1:
                bboxes.append(box)

    return bboxes, debug_boxes
示例#10
0
def find_cars(img,
              xstart,
              xstop,
              ystart,
              ystop,
              scale,
              clf,
              scaler,
              cspace,
              orient,
              pix_per_cell,
              cell_per_block,
              spatial_size,
              hist_bins,
              log=None,
              min_conf=0.4):
    """
    A function that find cars in a frame.
    It takes a region of interest of an image, converts it to a given color space, and runs a sliding window search
    through the image searching for matches by extracting a series of features and feeding them to the provided
    (trained) classifier, returning a list of found matches.
    The features computed are: HOG (using sub-sampling), spatial binning and color histogram.
    :param img: The input image
    :param xstart: Minimum X coordinate defining the region of interest
    :param xstop: Maximum X coordinate defining the region of interest
    :param ystart: Minimum Y coordinate defining the region of interest
    :param ystop: Maximum Y coordinate defining the region of interest
    :param scale: Defines the size of the window (scale * (64, 64))
    :param clf: The trained classifier
    :param scaler: A trained scaler used to normalize the features
    :param cspace: The target color space for the image before extracting features
    :param orient: The number of HOG orientations bins for the histogram
    :param pix_per_cell: 2-tuple specifying the size of each cell for extracting HOG
    :param cell_per_block:  2-tuple defining the area (in cells) over which normalization is performed during HOG
    :param spatial_size: 2-tuple defining the size for spatial binning
    :param hist_bins: The number of bins for each channel of color histogram
    :param log: A dictionary to log how many windows were searched and how many found cars
    :param min_conf: The minimum prediction confidence score to approve a prediction
    :return: A list of bounding boxes for every positive prediction from the classifier
    """
    img_tosearch = img[ystart:ystop, xstart:xstop, :]
    ctrans_tosearch = convert_color(img_tosearch, cspace)
    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

    # 64 was the original sampling rate, with 8 cells and 8 pix per cell
    window = 64
    nblocks_per_window = (window // pix_per_cell) - cell_per_block + 1
    # With the definition below, an overlap of 75% is defined
    cells_per_step = 1  # 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
    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)

    window_list = []
    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, spatial_size)
            hist_features = color_hist(subimg, hist_bins)

            # Scale features and make a prediction
            test_features = scaler.transform(
                np.hstack((spatial_features, hist_features,
                           hog_features)).reshape(1, -1))
            test_prediction = clf.predict(test_features)
            conf = clf.decision_function(test_features)

            # Record prediction
            if log is not None:
                if scale not in log:
                    log[scale] = [0, 0]
                log[scale][1] += 1
                if test_prediction == 1 and conf > min_conf:
                    log[scale][0] += 1

            if test_prediction == 1 and conf > min_conf:
                xbox_left = np.int(xleft * scale)
                ytop_draw = np.int(ytop * scale)
                win_draw = np.int(window * scale)
                window_list.append(((xbox_left + xstart, ytop_draw + ystart),
                                    (xbox_left + win_draw + xstart,
                                     ytop_draw + win_draw + ystart)))

    return window_list
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, :, :]  # sub-sampling
    ctrans_tosearch = convert_color(img_tosearch, conv='RGB2YUV')
    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
    #nblocks_per_window = (window // pix_per_cell)-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,
                            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)

    bboxes = []
    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(subimg, nbins=hist_bins)

            # Scale features and make a prediction
            test_stacked = np.hstack((spatial_features, hist_features,
                                      hog_features)).reshape(1, -1)
            test_features = X_scaler.transform(test_stacked)
            #test_features = scaler.transform(np.array(features).reshape(1, -1))
            #test_features = X_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)
                cv2.rectangle(
                    draw_img, (xbox_left, ytop_draw + ystart),
                    (xbox_left + win_draw, ytop_draw + win_draw + ystart),
                    (0, 0, 255), 6)
                bboxes.append(((int(xbox_left), int(ytop_draw + ystart)),
                               (int(xbox_left + win_draw),
                                int(ytop_draw + win_draw + ystart))))

    return draw_img, bboxes
示例#12
0
def find_cars(image, y_start_stop, scale, clf, X_scaler, color_space,
              spatial_size, hist_bins, orient, pix_per_cell, cell_per_block,
              channels):

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

    ystart, ystop = y_start_stop
    img_tosearch = img[ystart:ystop, :, :]
    ctrans_tosearch = color_convertor(color_space)(img_tosearch)

    imshape = ctrans_tosearch.shape
    nx, ny = np.int(imshape[1] / scale), np.int(imshape[0] / scale)
    ctrans_tosearch = cv2.resize(ctrans_tosearch, (nx, ny))

    # 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 = 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 + 1
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step + 1
    x_stub = np.mod(ctrans_tosearch.shape[1], pix_per_cell * cell_per_block)

    # Compute HOG features for the entire image, subsample later
    hog_features_search = []
    for c in channels:
        hog = get_hog_features(ctrans_tosearch[:, x_stub:, c],
                               orient,
                               pix_per_cell,
                               cell_per_block,
                               vis=False,
                               feature_vec=False)
        hog_features_search.append(hog)

    box_list = []
    for xb, yb in product(range(nxsteps), range(nysteps)):
        ypos = yb * cells_per_step
        xpos = xb * cells_per_step

        # Extract HOG for this patch
        hog_feats = []
        for hog in hog_features_search:
            feats = hog[ypos:ypos + nblocks_per_window,
                        xpos:xpos + nblocks_per_window]
            hog_feats.append(feats.ravel())
        hog_features = np.hstack(hog_feats)

        xleft = x_stub + 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
        features = np.hstack((spatial_features, hist_features, hog_features))
        test_features = X_scaler.transform(features.reshape(1, -1))

        # test_features = X_scaler.transform(
        #     np.hstack((spatial_features, hist_features)).reshape(1, -1))

        test_prediction = clf.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)

            box = ((xbox_left, ytop_draw + ystart),
                   (xbox_left + win_draw, ytop_draw + win_draw + ystart))

            # cv2.rectangle(draw_img, box[0], box[1], (0, 0, 255), 6)
            box_list.append(box)

    return box_list