def extract_features_img(img, colorspaces, spatial_size, bin_channel, hist_bins, hist_range, hist_channel, orient, pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat): #1) Define an empty list to receive features img_features = [] if spatial_feat == True or hist_feat == True: #2) Apply color conversion if other than 'RGB' feature_image = get_feature_image(img, colorspaces[0]) #3) Compute spatial features if flag is set if spatial_feat == True: spatial_features = bin_spatial(feature_image, size=spatial_size, bin_channel=bin_channel) #4) Append features to list img_features.append(spatial_features) #5) Compute histogram features if flag is set if hist_feat == True: hist_features = color_hist(feature_image, nbins=hist_bins, bins_range=hist_range, hist_channel=hist_channel) #6) Append features to list img_features.append(hist_features) #7) Compute HOG features if flag is set if hog_feat == True: feature_image = get_feature_image(img, colorspaces[1]) hog_features = [] if hog_channel == 'ALL': for i in range(feature_image.shape[2]): hog_features.append(get_hog_features(feature_image[:,:,i], orient, pix_per_cell, cell_per_block, feature_vec=True)) hog_features = np.ravel(hog_features) else: hog_features = get_hog_features(feature_image[:,:,hog_channel], orient, pix_per_cell, cell_per_block, feature_vec=True) #8) Append features to list img_features.append(hog_features) #9) Return concatenated array of features #return img_features return np.concatenate(img_features)
def find_cars(img, ystart, ystop, scale, clf, X_scaler, cspace, spatial_size, hist_bins, orient, pix_per_cell, cell_per_block, hog_channel): '''Detect vehicles and return containing boxes''' img = img.astype(np.float32)/255 img_tosearch = img[ystart:ystop, :, :] ctrans_tosearch = convert_color(img_tosearch, cspace=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 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 = 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) else: channels = [ch1, ch2, ch3] hog = get_hog_features(channels[hog_channel], orient, pix_per_cell, cell_per_block, feature_vec=False) boxes = [] for xb in range(nxsteps): for yb in range(nysteps): ypos = yb*cells_per_step xpos = xb*cells_per_step # Extract HOG for this patch 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_features = hog[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel() 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 = 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) boxes.append(((xbox_left, ytop_draw+ystart), (xbox_left+win_draw, ytop_draw+win_draw+ystart))) return boxes
def extract_features_from_files(img_files, **kwargs): """ extract features from a list of images :param img_files: :param kwargs: :return: """ color_conversion = kwargs.get("color_conversion", 'RGB2YCrCb') orient = kwargs.get("orient", 9) pix_per_cell = kwargs.get("pix_per_cell", 8) cell_per_block = kwargs.get("cell_per_block", 2) hog_channel = kwargs.get("hog_channel", 0) spatial_size = kwargs.get("spatial_size", (32, 32)) hist_bins = kwargs.get("hist_bins", 32) # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in img_files: image = mpimg.imread(file) # apply color conversion if other than 'RGB' feature_image = convert_color(image, conv=color_conversion) # Call get_hog_features() with vis=False, feature_vec=True if hog_channel == 'ALL': hog_features = [] for channel in range(feature_image.shape[2]): hog_features.append( get_hog_features(feature_image[:, :, channel], orient, pix_per_cell, cell_per_block, visualize=False, feature_vec=True)) hog_features = np.ravel(hog_features) else: hog_features = get_hog_features(feature_image[:, :, hog_channel], orient, pix_per_cell, cell_per_block, visualize=False, feature_vec=True) # Extract the image patch sub_img = cv2.resize(feature_image, (64, 64)) # Get color features spatial_features = bin_spatial(sub_img, size=spatial_size) hist_features = color_hist(sub_img, nbins=hist_bins) image_features = np.hstack( (hog_features, spatial_features, hist_features)) # Append the new feature vector to the features list features.append(image_features) return features
def extract_image_features(self, img): """ Extract features from single image """ features = [] cvt_img = convert_color(img, self.P.color_space) spatial_features = get_spatial_features(cvt_img, size=self.P.spatial_size) features.append(spatial_features) color_features = get_color_features(cvt_img, size=self.P.window_size, nbins=self.P.color_nbins) features.append(color_features) if self.P.window_size != (cvt_img.shape[0], cvt_img.shape[1]): cvt_img = cv2.resize(cvt_img, self.P.window_size) hog_features = get_hog_features(cvt_img, orient=self.P.orient, pix_per_cell=self.P.pix_per_cell, cell_per_block=self.P.cell_per_block) features.append(hog_features) return np.concatenate(features)
def pipeline(img): img_draw_search = img.copy() img_draw_cars = img.copy() img_processed = process_image(img) img_search = img_processed[p_search.ystart:p_search.ystop, :, :] shape = img_search.shape img_search = cv2.resize( img_search, (np.int(shape[1] / p_search.scale), np.int(shape[0] / p_search.scale))) hog_features = get_hog_features(img_search, p_features)[0] heatmap = slide_and_search(img_search, img_draw_search, hog_features, classifier, p_search, p_features) heatmaps.update(heatmap) heatmap_sum = heatmaps.get_sum() heatmap_thresh = heatmap_sum.copy() apply_threshold(heatmap_thresh, 25) boxes = scipy_label(heatmap_thresh) draw_car_boxes(img_draw_cars, boxes) return img_draw_search, img_draw_cars, heatmap_sum
def find_cars(img, ystart, ystop, scale, svc, X_scaler, colorspaces, spatial_size, bin_channel, hist_bins, hist_range, hist_channel, orient, pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat): window_list = [] img = img.astype(np.float32) / 255 # It´s a JPG img_tosearch = img[ystart:ystop, :, :] color_feature_img = get_feature_image(img_tosearch, colorspaces[0]) hog_feature_img = get_feature_image(img_tosearch, colorspaces[1]) if scale != 1: imshape = color_feature_img.shape color_feature_img = cv2.resize( color_feature_img, (np.int(imshape[1] / scale), np.int(imshape[0] / scale))) hog_feature_img = cv2.resize( hog_feature_img, (np.int(imshape[1] / scale), np.int(imshape[0] / scale))) # Define blocks and steps as above nxblocks = (color_feature_img.shape[1] // pix_per_cell) - cell_per_block + 1 nyblocks = (color_feature_img.shape[0] // pix_per_cell) - cell_per_block + 1 nfeat_per_block = orient * cell_per_block**2 # 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 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_feat == True: if hog_channel == 'ALL': hog1 = get_hog_features(hog_feature_img[:, :, 0], orient, pix_per_cell, cell_per_block, feature_vec=False) hog2 = get_hog_features(hog_feature_img[:, :, 1], orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = get_hog_features(hog_feature_img[:, :, 2], orient, pix_per_cell, cell_per_block, feature_vec=False) else: hog1 = get_hog_features(hog_feature_img[:, :, hog_channel], 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 features = [] # Extract HOG for this patch if hog_feat == True: 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() features.append( np.hstack((hog_feat1, hog_feat2, hog_feat3))) else: hog_feat1 = hog1[ypos:ypos + nblocks_per_window, xpos:xpos + nblocks_per_window].ravel() features.append(np.hstack((hog_feat1))) xleft = xpos * pix_per_cell ytop = ypos * pix_per_cell # Extract the image patch subimg = cv2.resize( color_feature_img[ytop:ytop + window, xleft:xleft + window], (window, window)) # Get color features if spatial_feat == True: features.append( bin_spatial(subimg, size=spatial_size, bin_channel=bin_channel)) if hist_feat == True: features.append( color_hist(subimg, nbins=hist_bins, bins_range=hist_range, hist_channel=hist_channel)) # Scale features and make a prediction test_features = X_scaler.transform( np.hstack(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) # Append window position to list window_list.append( ((xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + win_draw + ystart))) return window_list
if mode == 'predict' and os.path.exists(utils.train_imgs_file) and os.path.exists(utils.train_labels_file) \ and os.path.exists(utils.test_imgs_file) and os.path.exists(utils.test_labels_file) \ and os.path.exists(hog_train_imgs_file) and os.path.exists(hog_test_imgs_file): print('Predict BEGIN') _, Y_train, _, _ = utils.load_data() X_train = np.load(hog_train_imgs_file) X_test = np.load(hog_test_imgs_file) elif mode == 'train' or not os.path.exists(utils.train_imgs_file) or not os.path.exists(utils.train_labels_file) \ or not os.path.exists(utils.test_imgs_file) or not os.path.exists(utils.test_labels_file)\ or not os.path.exists(hog_train_imgs_file) or not os.path.exists(hog_test_imgs_file): print('Train BEGIN') utils.repartition_data(father_path=os.path.abspath( os.path.join(os.getcwd(), "..")), test_rate=0) imgs_train, Y_train, _, _ = utils.load_data() X_train = utils.get_hog_features(imgs_train) # X_val = utils.get_hog_features(imgs_val) imgs_test = utils.get_imgs([test_imgs_path], max_pool=True, homomorphic=False, file_type_list=['.bmp', '.png'], equalize=False, morphology=False) X_test = utils.get_hog_features(imgs_test) # 获得HOG特征向量 np.save(hog_train_imgs_file, X_train) np.save(hog_test_imgs_file, X_test) else: raise Exception('Mode not defined') if not os.path.exists('Q1/linear_svc.model') or mode == 'repartition': linear_svc = svm.LinearSVC(loss='hinge', max_iter=1000)
def search_windows_v2(img, windows, clf, scaler, color_space='RGB', spatial_size=(32, 32), hist_bins=32, hist_range=(0, 256), orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0, spatial_feat=True, hist_feat=True, hog_feat=True, y_start_stop=[None, None], scale=0): draw_img = np.copy(img) img = img.astype(np.float32) / 255 img_tosearch = img[y_start_stop[0]:y_start_stop[1], :, :] ctrans_tosearch = utils.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 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 = utils.get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False) hog2 = utils.get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = utils.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 features = utils.single_img_features(subimg, 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 = scaler.transform(np.array(features).reshape(1, -1)) # 6) Predict using your classifier prediction = clf.predict(X) # Scale features and make a prediction # test_features = X_scaler.transform(X) # test_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1)) # test_prediction = clf.predict(X) if 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 + y_start_stop[0]), (xbox_left + win_draw, ytop_draw + win_draw + y_start_stop[0]), (0, 0, 255), 6) return draw_img
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, :, :] ctrans_tosearch = utils.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 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 = utils.get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False) hog2 = utils.get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = utils.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 = utils.bin_spatial(subimg, size=spatial_size) hist_features = utils.color_hist(subimg, nbins=hist_bins) print(hist_features.shape) print(spatial_features.shape) print(hog_features.shape) X = np.hstack((spatial_features, hist_features, hog_features)).reshape(-1, 1) print(X.shape) # Scale features and make a prediction # test_features = X_scaler.transform(X) # test_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1)) test_prediction = svc.predict(X) 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) return draw_img
def find_cars(img, ystart, ystop, scale, cspace, hog_channel, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, show_all_rectangles=False): # array of rectangles where cars were detected windows = [] img = img.astype(np.float32) / 255 img_tosearch = img[ystart:ystop, :, :] # apply color conversion if other than 'RGB' if cspace != 'RGB': if cspace == 'HSV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HSV) elif cspace == 'LUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2LUV) elif cspace == 'HLS': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HLS) elif cspace == 'YUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YUV) elif cspace == 'YCrCb': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb) else: ctrans_tosearch = np.copy(img) # rescale image if other than 1.0 scale if scale != 1: imshape = ctrans_tosearch.shape ctrans_tosearch = cv2.resize( ctrans_tosearch, (np.int(imshape[1] / scale), np.int(imshape[0] / scale))) # select colorspace channel for HOG 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) + 1 # -1 nyblocks = (ch1.shape[0] // pix_per_cell) + 1 # -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 = utils.get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False) if hog_channel == 'ALL': hog2 = utils.get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = utils.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() if hog_channel == 'ALL': 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_features = hog_feat1 xleft = xpos * pix_per_cell ytop = ypos * pix_per_cell test_prediction = svc.predict(hog_features.reshape(1, -1)) if test_prediction == 1 or show_all_rectangles: xbox_left = np.int(xleft * scale) ytop_draw = np.int(ytop * scale) win_draw = np.int(window * scale) windows.append( ((xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + win_draw + ystart))) return windows
[400, 528, 2.0], [432, 560, 2.0], [400, 596, 3.5], [464, 660, 3.5]] for i, img in enumerate([car_example, notcar_example]): plt.imsave(r"./output_images/" + str(i) + "_1_spatial.jpg", cv2.resize(cv2.resize(img, spatial_size), (128, 128))) img2 = convert_color(img, cspace=cspace) ch1 = img2[:, :, 0] ch2 = img2[:, :, 1] ch3 = img2[:, :, 2] plt.imsave(r"./output_images/" + str(i) + "_2_ch1.jpg", ch1, cmap='gray') plt.imsave(r"./output_images/" + str(i) + "_2_ch2.jpg", ch2, cmap='gray') plt.imsave(r"./output_images/" + str(i) + "_2_ch3.jpg", ch3, cmap='gray') hog_feats, hog_im = get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=True) plt.imsave(r"./output_images/" + str(i) + "_3_hog.jpg", hog_im, cmap='gray') for fname in images: print('processing ', fname, '...') img = mpimg.imread(fname) boxes = find_cars_multiscale(img, multiscale, clf, X_scaler, cspace, spatial_size, hist_bins, orient, pix_per_cell, cell_per_block, hog_channel) out_img = draw_boxes(img, boxes) plt.imsave(r"./output_images/" + fname.split('\\')[-1].split('.')[0] + "_5_bbox.jpg", out_img) heat = np.zeros_like(img[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = add_heat(heat, boxes) # Apply threshold to help remove false positives heat = apply_threshold(heat, 2)
# Visualise the HOG, spatial and colour features # For a car and non-car image car_img = read_img(random.choice(training_filenames['vehicles']), 'cv2') noncar_img = read_img(random.choice(training_filenames['non-vehicles']), 'cv2') car_img_cvt = cv2.cvtColor(car_img, CONVERT_RGB_TO[config['color_space']]) noncar_img_cvt = cv2.cvtColor(noncar_img, CONVERT_RGB_TO[config['color_space']]) car_hog_img = [] noncar_hog_img = [] for c in range(car_img.shape[2]): _, hog_img = get_hog_features(car_img_cvt[:, :, c], config['orient'], config['pix_per_cell'], config['cell_per_block'], vis=True, feature_vec=True) car_hog_img.append(hog_img) _, hog_img = get_hog_features(noncar_img_cvt[:, :, c], config['orient'], config['pix_per_cell'], config['cell_per_block'], vis=True, feature_vec=True) noncar_hog_img.append(hog_img) car_hog_img = np.moveaxis(np.asarray(car_hog_img), 0, -1) car_hog_img = np.sum(car_hog_img, axis=2) noncar_hog_img = np.moveaxis(np.asarray(noncar_hog_img), 0, -1) noncar_hog_img = np.sum(noncar_hog_img, axis=2)
def find_cars(img, ystart, ystop, scale, cspace, hog_channel, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, show_all_rectangles=False): windows = [] img = img.astype(np.float32) / 255 img_tosearch = img[ystart:ystop, :, :] if cspace != 'RGB': if cspace == 'HSV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HSV) elif cspace == 'LUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2LUV) elif cspace == 'HLS': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2HLS) elif cspace == 'YUV': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YUV) elif cspace == 'YCrCb': ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb) else: ctrans_tosearch = np.copy(img) 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] nxblocks = (ch1.shape[1] // pix_per_cell) + 1 # -1 nyblocks = (ch1.shape[0] // pix_per_cell) + 1 # -1 nfeat_per_block = orient * cell_per_block**2 window = 64 nblocks_per_window = (window // pix_per_cell) - 1 nxsteps = (nxblocks - nblocks_per_window) // cells_per_step nysteps = (nyblocks - nblocks_per_window) // cells_per_step hog1 = utils.get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False) if hog_channel == 'ALL': hog2 = utils.get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = utils.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() if hog_channel == 'ALL': 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_features = hog_feat1 xleft = xpos * pix_per_cell ytop = ypos * pix_per_cell test_prediction = svc.predict(hog_features.reshape(1, -1)) if test_prediction == 1 or show_all_rectangles: xbox_left = np.int(xleft * scale) ytop_draw = np.int(ytop * scale) win_draw = np.int(window * scale) windows.append( ((xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + win_draw + ystart))) return windows
def find_cars(img, draw_img=None, heatmap=None, color=(255, 0, 0), scale=1, svc=defaults.svc(), scaler=defaults.scaler()): img = utils.rgbTo(img, defaults.colorspace()) # Select only the part of the image that we want to search at this scale height = img.shape[0] ystart = height // 2 ystop = utils.ystop(height, scale) img = img[ystart:ystop, :, :] # Scale the image if scale != 1: img = cv2.resize( img, (np.int(img.shape[1] / scale), np.int(img.shape[0] / scale))) # Define dimensions that are used in calculating the size of the hog features. window = 64 pix_per_cell = defaults.pix_per_cell() cell_per_block = defaults.cell_per_block() nxblocks = (img.shape[1] // pix_per_cell) - cell_per_block + 1 nyblocks = (img.shape[0] // pix_per_cell) - cell_per_block + 1 nblocks_per_window = (window // pix_per_cell) - cell_per_block + 1 cells_per_step = 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 orient = defaults.orient() block_norm = defaults.block_norm() hog1 = utils.get_hog_features(img[:, :, 0], orient, pix_per_cell, cell_per_block, block_norm, False, False) hog2 = utils.get_hog_features(img[:, :, 1], orient, pix_per_cell, cell_per_block, block_norm, False, False) hog3 = utils.get_hog_features(img[:, :, 2], orient, pix_per_cell, cell_per_block, block_norm, False, False) for xb in range(nxsteps): for yb in range(nysteps): ypos = yb * cells_per_step xpos = xb * cells_per_step if ypos + nblocks_per_window >= hog1.shape[ 0] or xpos + nblocks_per_window >= hog1.shape[1]: continue # 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)).reshape(1, -1) xleft = xpos * pix_per_cell ytop = ypos * pix_per_cell # Scale features and make a prediction try: test_features = scaler.transform(hog_features) 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) y = ytop_draw + ystart x = xbox_left if heatmap is not None: heatmap[y:y + win_draw, x:x + win_draw] += 1 if draw_img is not None: cv2.rectangle(draw_img, (xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + win_draw + ystart), color, 6) except Exception as e: print(e)