def extract_features( image, color_space='RGB', spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0, use_spatial_features=True, use_hist_features=True, use_hog_features=True): """ extract spatial, hist and hog features from given img with given parameters """ #1) define an empty list to store image features image_features = [] #2) apply color convertion if the color space is not RGB feature_image = Helpers.convert_color(image, color_space=color_space) #3) compute spatial features if flag is set if use_spatial_features: spatial_color_feature = FeatureExtractor.extract_spatial_features(feature_image, size=spatial_size) image_features.append(spatial_color_feature) #4) compute histogram features if flag is set if use_hist_features: color_hist_feature = FeatureExtractor.extract_hist_features(feature_image, nbins=hist_bins) image_features.append(color_hist_feature) #5) compute HOG features if flag is set if use_hog_features: hog_feature = FeatureExtractor.extract_hog_features( image=feature_image, orient=orient, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block, hog_channel=hog_channel) image_features.append(hog_feature) #6) return concatenated array of features return np.concatenate(image_features)
def get_color_features_from_pngs(image_pathes, cspace='RGB', spatial_size=(32, 32), hist_bins=32, hist_range=(0, 256)): """ extract features from a list of images """ features = [] for img_file in image_pathes: image = Helpers.load_png(img_file) feature_image = Helpers.convert_color(image, cspace) spatial_color_feature = FeatureExtractor.extract_spatial_features(feature_image, size=spatial_size) color_hist_feature = FeatureExtractor.extract_hist_features(feature_image, nbins=hist_bins, bins_range=hist_range) feature = np.concatenate((spatial_color_feature, color_hist_feature), axis=0) features.append(feature) return features
def get_hog_features_from_pngs(image_pathes, cspace='RGB', orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0): """ extract the hog features from given list of image file pathes """ features = [] for file in image_pathes: image = Helpers.load_png(file) feature_image = Helpers.convert_color(image, cspace) hog_features = FeatureExtractor.extract_hog_features( image=feature_image, orient=orient, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block, hog_channel=hog_channel) features.append(hog_features) return features
def visualize_hog_features(image, color_space='YCrCb', orient=8, pixel_per_cell=8, cell_per_block=2): """visualize the hog features of the given image""" PlotHelper.color_image(image, 'car image') converted_img = Helpers.convert_color(image, color_space=color_space) channel1 = converted_img[:, :, 0] _, hog1 = FeatureExtractor.get_hog_features(image=channel1, orient=orient, pixel_per_cell=pixel_per_cell, cell_per_block=cell_per_block, vis=True, feature_vec=True) channel2 = converted_img[:, :, 1] _, hog2 = FeatureExtractor.get_hog_features(image=channel2, orient=orient, pixel_per_cell=pixel_per_cell, cell_per_block=cell_per_block, vis=True, feature_vec=True) channel3 = converted_img[:, :, 2] _, hog3 = FeatureExtractor.get_hog_features(image=channel3, orient=orient, pixel_per_cell=pixel_per_cell, cell_per_block=cell_per_block, vis=True, feature_vec=True) PlotHelper.grayscale_images( [channel1, hog1, channel2, hog2, channel3, hog3], [ 'channel1', 'channel1 hog', 'channel2', 'channel2 hog', 'channel3', 'channel3 hog' ], 6)
def find_cars(image, classifier, ystart, ystop, scale=1.0): """ scan the image within given ystart and ystop for car """ img_tosearch = image[ystart:ystop, :, :] ctrans_tosearch = Helpers.convert_color(img_tosearch, color_space=classifier.color_space) 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] // classifier.pix_per_cell) - classifier.cell_per_block + 1 nyblocks = (ch1.shape[0] // classifier.pix_per_cell) - classifier.cell_per_block + 1 # 64 was the original sampling rate, with 8 cells and 8 pix per cell window = 64 nblocks_per_window = (window // classifier.pix_per_cell) - classifier.cell_per_block + 1 # compute individual channel HOG features for the entire image hog1 = FeatureExtractor.get_hog_features(ch1, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) hog2 = FeatureExtractor.get_hog_features(ch2, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) hog3 = FeatureExtractor.get_hog_features(ch3, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) bboxes = [] max_x_step = nxblocks - nblocks_per_window max_y_step = nyblocks - nblocks_per_window for y_step in range(max_y_step): y_pos_cells = y_step x_step = 0 while x_step < max_x_step: x_pos_cells = x_step # extract hog for this patch hog_feat1 = hog1[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_feat2 = hog2[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_feat3 = hog3[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3)) x_left_pixels = x_pos_cells * classifier.pix_per_cell y_top_pixels = y_pos_cells * classifier.pix_per_cell # extract the image patch subimg = cv2.resize(ctrans_tosearch[y_top_pixels:y_top_pixels+window, x_left_pixels:x_left_pixels+window], (64, 64)) # get color features spatial_features = FeatureExtractor.extract_spatial_features(subimg, size=classifier.spatial_size) hist_features = FeatureExtractor.extract_hist_features(subimg, nbins=classifier.hist_bins) # scale features and make a prediction test_features = classifier.x_scaler.transform(np.hstack((spatial_features, hist_features, hog_features)).reshape(1, -1)) test_prediction = classifier.svc.predict(test_features) if test_prediction == 1: xbox_left = np.int(x_left_pixels*scale) ytop_draw = np.int(y_top_pixels*scale) win_draw = np.int(window*scale) bbox = [(xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + ystart + win_draw)] bboxes.append(bbox) x_step += 1 else: x_step += 2 return bboxes