def hogChannels(self, img, orientations, pix_per_cell, cell_per_block, color ): img = np.copy(img) img = img.astype(np.float32)/255 if color != None: img = image_util.convert_color(img, color) ch1 = img[:,:,0] ch2 = img[:,:,1] ch3 = img[:,:,2] features_c1, hog_image_c1 = uut.get_hog_features(ch1, orient=orientations, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block,vis=True, feature_vec=True) features_c2, hog_image_c2 = uut.get_hog_features(ch2, orient=orientations, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block,vis=True, feature_vec=True) features_c3, hog_image_c3 = uut.get_hog_features(ch3, orient=orientations, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block,vis=True, feature_vec=True) return ch1, ch2, ch3, hog_image_c1, hog_image_c2, hog_image_c3
def compute_hog_features(img): if hog_channel == 'ALL': hog_features = [] for channel in range(img.shape[2]): hog_features.append( get_hog_features(img[:, :, channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=False) ) return np.array(hog_features) else: hog_features = get_hog_features( img[:, :, hog_channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=False ) return hog_features
def get_hog_image(self, img): _, img = lesson_functions.get_hog_features(img, self.orient, self.pix_per_cell, self.cell_per_block, vis=True) return img
def visialize_hog_image_and_color_hist(image, color_space, orient, pix_per_cell, cell_per_block, nbins, bins_range, spatial_size): if color_space != 'RGB': if color_space == 'HSV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) elif color_space == 'LUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV) elif color_space == 'HLS': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) elif color_space == 'YUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV) elif color_space == 'YCrCb': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb) else: feature_image = np.copy(image) hog_images = [] _hog_features = [] channel_hist = [] for index in range(feature_image.shape[2]): ch = feature_image[:, :, index] features, hog_image = get_hog_features(ch, orient, pix_per_cell, cell_per_block, True, True) hog_images.append(hog_image) _hog_features.append(features) channel_hist.append(np.histogram(ch, bins=nbins, range=bins_range)) hist_features = np.concatenate( (channel_hist[0][0], channel_hist[1][0], channel_hist[2][0])) hog_features = np.ravel(_hog_features) spatial_image = visialize_bin_spatial(feature_image, size=spatial_size) return feature_image, hog_images, spatial_image, hist_features, hog_features
def single_img_features(img, color_space='RGB', spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0, spatial_feat=True, hist_feat=True, hog_feat=True): #1) Define an empty list to receive features img_features = [] #2) Apply color conversion if other than 'RGB' if color_space != 'RGB': if color_space == 'HSV': feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) elif color_space == 'LUV': feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2LUV) elif color_space == 'HLS': feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HLS) elif color_space == 'YUV': feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YUV) elif 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 spatial_feat == True: spatial_features = bin_spatial(feature_image, size=spatial_size) #4) Append features to list img_features.append(spatial_features) #5) Compute histogram features if flag is set if hist_feat == True: hist_features = color_hist(feature_image, 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(feature_image.shape[2]): hog_features.extend(get_hog_features(feature_image[:,:,channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) else: hog_features = get_hog_features(feature_image[:,:,hog_channel], orient, pix_per_cell, 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 gen_hog(execute, show=True, save=False): """Generates the hog visualization for images""" if not execute: return imgs = [(config.vehicle, config.get_vehicle_hog()), (config.get_not_vehicle(), config.get_not_vehicle_hog())] for img in imgs: img_in = mpimg.imread(img[0]) image = cv2.cvtColor(img_in, cv2.COLOR_RGB2GRAY) orient, pix_per_cell, cell_per_block = 9, 8, 2 features, hog_image = lf.get_hog_features(image,orient,pix_per_cell,cell_per_block) if show: plt.subplot(121) plt.imshow(image, cmap='gray') plt.title('Example Image') plt.subplot(122) plt.imshow(hog_image, cmap='gray') plt.title('HOG Visualization') if save: img_out = img[1] cv2.imwrite(img_out, utils.scale_img(hog_image))
def extract_features(imgs, cspace='RGB', orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0): # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: # Read in each one by one image = mpimg.imread(file) # apply color conversion if other than 'RGB' if cspace != 'RGB': if cspace == 'HSV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) elif cspace == 'LUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV) elif cspace == 'HLS': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) elif cspace == 'YUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV) elif cspace == 'YCrCb': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb) else: print("COLOR SPACE {} NOT FOUND".format(cspace)) return False else: feature_image = np.copy(image) # 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( lesson_functions.get_hog_features(feature_image[:, :, channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) else: hog_features = lesson_functions.get_hog_features( feature_image[:, :, hog_channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True) # Apply bin_spatial() to get spatial color features spatial_features = lesson_functions.bin_spatial(feature_image, size=(spatial_size, spatial_size)) # Apply color_hist() also with a color space option now hist_features = lesson_functions.color_hist(feature_image, nbins=hist_bins) # Append the new feature vectors to the features list features.append( np.concatenate((hog_features, spatial_features, hist_features))) # Return list of feature vectors return features
def find_cars(img, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, svc_decision_threshold): draw_img = np.copy(img) img = img.astype(np.float32) / 255.0 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) car_boxes = [] for xb in range(nxsteps): for yb in range(nysteps): ypos = yb * cells_per_step xpos = xb * cells_per_step # Extract HOG for this patch hog_feat1 = hog1[ypos:ypos + nblocks_per_window, xpos:xpos + nblocks_per_window].ravel() hog_feat2 = hog2[ypos:ypos + nblocks_per_window, xpos:xpos + nblocks_per_window].ravel() hog_feat3 = hog3[ypos:ypos + nblocks_per_window, xpos:xpos + nblocks_per_window].ravel() hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3)) xleft = xpos * pix_per_cell ytop = ypos * pix_per_cell # Extract the image patch subimg = cv2.resize( ctrans_tosearch[ytop:ytop + window, xleft:xleft + window], (64, 64)) # Get color features spatial_features = bin_spatial(subimg, size=spatial_size) hist_features = color_hist(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 and svc.decision_function( test_features) > svc_decision_threshold: #if car found 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) startx = xbox_left starty = ytop_draw + ystart endx = xbox_left + win_draw endy = ytop_draw + win_draw + ystart car_boxes.append(((startx, starty), (endx, endy))) return draw_img, car_boxes
def find_cars(self, img, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins): """ Define a single function that can extract features using hog sub-sampling and make predictions scale: scales the window bigger -OR- the image smaller """ draw_img = np.copy(img) img = img.astype(np.float32) / 255 # With 8x8 Cell; cells_per_step = 2 => 75% overlap cells_per_step = 2 # Instead of overlap, define how many cells to step; img_tosearch = img[ystart:ystop, :, :] ctrans_tosearch = lesson_functions.convert_color(img_tosearch, conv=CVT_COLORSPACE) 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_size = 64 nblocks_per_window = (window_size // 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 = lesson_functions.get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False) hog2 = lesson_functions.get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False) hog3 = lesson_functions.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_size, xleft:xleft + window_size], (64, 64)) # Get color features spatial_features = lesson_functions.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.concatenate((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_size * 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