def visualize(cars_train, noncars_train, cars_valid_feat, noncars_valid_feat, cars_valid, noncars_valid, svc, orient, pix_per_cell, cells_per_block): # Plot hog features for cars and noncars f, ax = plt.subplots(6, 7, figsize=(20, 10)) f.subplots_adjust(hspace=0.2, wspace=0.05) colorspace = cv2.COLOR_RGB2HLS for i, j, in enumerate([60, 800, 1800]): img = plt.imread(cars_train[j]) feat_img = cv2.cvtColor(img, colorspace) ax[i, 0].imshow(img) ax[i, 0].set_title('car {0}'.format(j)) ax[i, 0].set_xticks([]) ax[i, 0].set_yticks([]) for ch in range(3): ax[i, ch + 1].imshow(feat_img[:, :, ch], cmap='gray') ax[i, ch + 1].set_title('img ch {0}'.format(ch)) ax[i, ch + 1].set_xticks([]) ax[i, ch + 1].set_yticks([]) feat, h_img = get_hog_features(feat_img[:, :, ch], orient, pix_per_cell, cells_per_block, vis=True) ax[i, ch + 4].imshow(h_img, cmap='gray') ax[i, ch + 4].set_title('HOG ch {0}'.format(ch)) ax[i, ch + 4].set_xticks([]) ax[i, ch + 4].set_yticks([]) img = plt.imread(noncars_train[j]) feat_img = cv2.cvtColor(img, colorspace) i += 3 ax[i, 0].imshow(img) ax[i, 0].set_title('noncar {0}'.format(j)) ax[i, 0].set_xticks([]) ax[i, 0].set_yticks([]) for ch in range(3): ax[i, ch + 1].imshow(feat_img[:, :, ch], cmap='gray') ax[i, ch + 1].set_title('img ch {0}'.format(ch)) ax[i, ch + 1].set_xticks([]) ax[i, ch + 1].set_yticks([]) feat, h_img = get_hog_features(feat_img[:, :, ch], orient, pix_per_cell, cells_per_block, vis=True) ax[i, ch + 4].imshow(h_img, cmap='gray') ax[i, ch + 4].set_title('HOG ch {0}'.format(ch)) ax[i, ch + 4].set_xticks([]) ax[i, ch + 4].set_yticks([]) plt.savefig('./output_images/hog_features.png')
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)
def display_hog(images): # Define feature parameters orient = 9 pix_per_cell = 8 cell_per_block = 2 for idx, img_src in enumerate(images): img = mpimg.imread(img_src) img = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb) _, hog_image = get_hog_features(img[:, :, 0], orient, pix_per_cell, cell_per_block, vis=True, feature_vec=False) plt.imsave('../output_images/hog' + str(idx + 1) + '.jpg', hog_image)
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
colorspace = 'YCrCb' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 9 pix_per_cell = 8 cell_per_block = 2 hog_channel = 0 carcopy = np.copy(car) noncarcopy = np.copy(noncar) carcopy = convert_color(carcopy, 'RGB2YCrCb') noncarcopy = convert_color(noncarcopy, 'RGB2YCrCb') hog_features, hog_img1 = get_hog_features(carcopy[:, :, 0], orient, pix_per_cell, cell_per_block, vis=True) hog_features, hog_img2 = get_hog_features(carcopy[:, :, 1], orient, pix_per_cell, cell_per_block, vis=True) hog_features, hog_img3 = get_hog_features(carcopy[:, :, 2], orient, pix_per_cell, cell_per_block, vis=True) hog_features, hog_img4 = get_hog_features(noncarcopy[:, :, 0], orient,
def training(images, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins): # Get image file names # images = glob.glob('./training-data/*/*/*.png') cars = [] notcars = [] all_cars = [] all_notcars = [] for image in images: if 'nonvehicle' in image: all_notcars.append(image) else: all_cars.append(image) # Get only 1/5 of the training data to avoid overfitting for ix, notcar in enumerate(all_notcars): # if ix % 5 == 0: notcars.append(notcar) for ix, car in enumerate(all_cars): # if ix % 5 == 0: cars.append(car) car_image = mpimg.imread(cars[5]) notcar_image = mpimg.imread(notcars[0]) compare_images(car_image, notcar_image, "Car", "Not Car") color_space = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb hog_channel = "ALL" # Can be 0, 1, 2, or "ALL" spatial_feat = True # Spatial features on or off hist_feat = True # Histogram features on or off hog_feat = True # HOG features on or off converted_car_image = cv2.cvtColor(car_image, cv2.COLOR_RGB2YUV) car_ch1 = converted_car_image[:, :, 0] car_ch2 = converted_car_image[:, :, 1] car_ch3 = converted_car_image[:, :, 2] converted_notcar_image = cv2.cvtColor(notcar_image, cv2.COLOR_RGB2YUV) notcar_ch1 = converted_notcar_image[:, :, 0] notcar_ch2 = converted_notcar_image[:, :, 1] notcar_ch3 = converted_notcar_image[:, :, 2] car_hog_feature, car_hog_image = get_hog_features(car_ch1, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=True) notcar_hog_feature, notcar_hog_image = get_hog_features(notcar_ch1, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=True) car_ch1_features = cv2.resize(car_ch1, spatial_size) car_ch2_features = cv2.resize(car_ch2, spatial_size) car_ch3_features = cv2.resize(car_ch3, spatial_size) notcar_ch1_features = cv2.resize(notcar_ch1, spatial_size) notcar_ch2_features = cv2.resize(notcar_ch2, spatial_size) notcar_ch3_features = cv2.resize(notcar_ch3, spatial_size) show_images(car_ch1, car_hog_image, notcar_ch1, notcar_hog_image, "Car ch 1", "Car ch 1 HOG", "Not Car ch 1", "Not Car ch 1 HOG") show_images(car_ch1, car_ch1_features, notcar_ch1, notcar_ch1_features, "Car ch 1", "Car ch 1 features", "Not Car ch 1", "Not Car ch 1 features") show_images(car_ch2, car_ch2_features, notcar_ch2, notcar_ch2_features, "Car ch 2", "Car ch 2 features", "Not Car ch 2", "Not Car ch 2 features") show_images(car_ch3, car_ch3_features, notcar_ch3, notcar_ch3_features, "Car ch 3", "Car ch 3 features", "Not Car ch 3", "Not Car ch 3 features") car_features = extract_features(cars, color_space=color_space, 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) notcar_features = extract_features(notcars, color_space=color_space, 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) X = np.vstack((car_features, notcar_features)).astype(np.float64) # Fit a per-column scaler X_scaler = StandardScaler().fit(X) # Apply the scaler to X scaled_X = X_scaler.transform(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:', orient, 'orientations', pix_per_cell, 'pixels per cell and', cell_per_block, 'cells per block') print('Feature vector length:', len(X_train[0])) # Use a linear SVC svc = LinearSVC() # Check the training time for the SVC t = time.time() svc.fit(X_train, y_train) t2 = time.time() print(round(t2 - t, 2), 'Seconds to train SVC...') # 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() return svc, X_scaler
def find_candidates(image, scale, overlap, xband, yband, svc, X_scaler, _count): image = color_scale(image) xstart, xstop = xband ystart, ystop = yband # generate HOG features over entire search region if config.HOG_FEAT: hog_conv = convert_color(image, config.HOG_SPACE) hog_region = hog_conv[ystart:ystop, xstart:xstop, :] if scale != 1.0: hog_region = cv2.resize(hog_region, (0, 0), fx=1.0 / scale, fy=1.0 / scale) hog_channel = config.HOG_CHANNEL if hog_channel == 'ALL': hog_features = [] for channel in range(hog_region.shape[2]): hog_features.append( get_hog_features(hog_region[:, :, channel], feature_vec=False)) hog_features = np.array(hog_features) else: hog_features = get_hog_features(hog_region[:, :, hog_channel], feature_vec=False)[np.newaxis, ...] # overlap = config.OVERLAP window = config.WINDOW_SIZE img_region = image[ystart:ystop, xstart:xstop, :] if scale != 1.0: img_region = cv2.resize(img_region, (0, 0), fx=1.0 / scale, fy=1.0 / scale) # start window sliding rewrite xspan = img_region.shape[1] yspan = img_region.shape[0] pix_per_step = np.int(window * (1 - overlap)) buff = np.int(window * overlap) if (xspan - buff) % pix_per_step == 0: xwindows = np.int((xspan - buff) / pix_per_step) - 1 else: xwindows = np.int((xspan - buff) / pix_per_step) if (yspan - buff) % pix_per_step == 0: ywindows = np.int((yspan - buff) / pix_per_step) - 1 else: ywindows = np.int((yspan - buff) / pix_per_step) if xwindows <= 0 or ywindows <= 0: raise Exception("Invalid config - area too small") print('scale,x,y', scale, xwindows, ywindows) boxes = [] cars = 0 for iy in range(ywindows): for ix in range(xwindows): leftx = ix * pix_per_step topy = iy * pix_per_step endx = leftx + window endy = topy + window subimg = img_region[topy:endy, leftx:endx] features = extract_features(subimg) features_sc = X_scaler.transform(features.reshape(1, -1)) prediction = svc.predict(features_sc) if prediction == 1: cv2.imwrite( 'output_images/cars/' + str(_count) + '_' + str(scale) + '.png', cv2.cvtColor(subimg * 255, cv2.COLOR_RGB2BGR)) cars += 1 xbox_left = np.int(leftx * scale) ytop_draw = np.int(topy * scale) win_draw = np.int(window * scale) box = ((xbox_left + xstart, ytop_draw + ystart), (xbox_left + win_draw + xstart, ytop_draw + win_draw + ystart)) confidence = svc.decision_function(features_sc) pred_box = PredictionBox(box, confidence) boxes.append(pred_box) if cars != 0: print('!!!', scale, ':', cars) return boxes
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, 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
def find_cars_in_image(img, ystart, ystop, clf, X_scaler, scale, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, color_space='RGB'): on_windows = [] # Crop the image img_tosearch = img[ystart:ystop, :, :] # Color conversion if color_space != 'RGB': if color_space == 'HSV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HSV) elif color_space == 'LUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2LUV) elif color_space == 'HLS': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HLS) elif color_space == 'YUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YUV) elif color_space == 'YCrCb': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb) else: ctrans_tosearch = np.copy(img_tosearch) # Scale the entire image instead of extracting windows of different sizes 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 window = 64 # 64 was the orginal sampling rate, with 8 cells and 8 pix per cell 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 ch1 = ctrans_tosearch[:, :, 0] ch2 = ctrans_tosearch[:, :, 1] ch3 = ctrans_tosearch[:, :, 2] 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 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 = get_spatial_binning_features(subimg, size=spatial_size) hist_features = get_color_hist_features(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)) # Predict using classifier 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) on_windows.append( ((xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + win_draw + ystart))) return on_windows
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
from parameters import * from utils import * import matplotlib.image as mpimg import numpy as np from features import get_hog_features, get_color_channel import argparse if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( '--dir', type=str, default='.', help='directory to read vehicle/non-vehicle image files from') FLAGS, unparsed = parser.parse_known_args() examples = list(get_examples(FLAGS.dir)[1:-1]) feature_examples = [] feature_examples.extend(examples) for e in examples: ch = get_color_channel(e, 'HLS', 2) features, hog_image = get_hog_features(ch, ORIENT, PIX_PER_CELL, CELL_PER_BLOCK, vis=True) feature_examples.append(hog_image) show_images_in_table(feature_examples, (6, 2), fig_size=(20, 6))
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 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, 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
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, scale): ystart = self.ystart ystop = self.ystop svc = self.svc X_scaler = self.X_scaler orient = self.orient pix_per_cell = self.pix_per_cell cell_per_block = self.cell_per_block spatial_size = self.spatial_size hist_bins = self.hist_bins img = img.astype(np.float32)/255 result = [] img_tosearch = img[ystart:ystop,:,:] ctrans_tosearch = convert_color(img_tosearch, conv='RGB2YCrCb') if scale != 1: imshape = ctrans_tosearch.shape ctrans_tosearch = cv2.resize(ctrans_tosearch, (np.int(imshape[1]/scale), np.int(imshape[0]/scale))) ch1 = ctrans_tosearch[:,:,0] ch2 = ctrans_tosearch[:,:,1] ch3 = ctrans_tosearch[:,:,2] # Define blocks and steps as above nxblocks = (ch1.shape[1] // pix_per_cell)-1 nyblocks = (ch1.shape[0] // pix_per_cell)-1 nfeat_per_block = orient*cell_per_block**2 # 64 was the orginal sampling rate, with 8 cells and 8 pix per cell window = 64 nblocks_per_window = (window // pix_per_cell)-1 cells_per_step = 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) 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 tmp = np.hstack((hog_features, spatial_features, hist_features)) test_features = X_scaler.transform(np.hstack((hog_features, spatial_features, hist_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) result.append([(xbox_left, ytop_draw+ystart), (xbox_left+win_draw, ytop_draw+win_draw+ystart)]) return result
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