def custom_extract_features(imgs, spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, spatial_feat=True, hist_feat=True, hog_feat=True): # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: file_features = [] # Read in each one by one print(file) image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) spatial_img = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) hist_img = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) if spatial_feat is True: spatial_features = bin_spatial(spatial_img, size=spatial_size) file_features.append(spatial_features) if hist_feat is True: # Apply color_hist() hist_features = color_hist(hist_img, nbins=hist_bins) file_features.append(hist_features) if hog_feat is True: # Call get_hog_features() with vis=False, feature_vec=True hog_features = [] image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) image_luv = cv2.cvtColor(image, cv2.COLOR_RGB2LUV) hog_features.append( get_hog_features(image_hls[:, :, 0], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features.append( get_hog_features(image_hls[:, :, 1], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features.append( get_hog_features(image_luv[:, :, 1], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) # Append the new feature vector to the features list file_features.append(hog_features) features.append(np.concatenate(file_features)) # Return list of feature vectors return np.array(features)
def custom_single_img_features(image, spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, spatial_feat=True, hist_feat=True, hog_feat=True): # 1) Define an empty list to receive features img_features = [] spatial_img = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) hist_img = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) # 3) Compute spatial features if flag is set if spatial_feat is True: spatial_features = bin_spatial(spatial_img, size=spatial_size) # 4) Append features to list img_features.append(spatial_features) # 5) Compute histogram features if flag is set if hist_feat is True: hist_features = color_hist(hist_img, nbins=hist_bins) # 6) Append features to list img_features.append(hist_features) # 7) Compute HOG features if flag is set if hog_feat is True: hog_features = [] image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) image_luv = cv2.cvtColor(image, cv2.COLOR_RGB2LUV) hog_features.append( get_hog_features(image_hls[:, :, 0], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features.append( get_hog_features(image_hls[:, :, 1], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features.append( get_hog_features(image_luv[:, :, 1], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) # 8) Append features to list img_features.append(hog_features) # 9) Return concatenated array of features return np.concatenate(img_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=None, spatial_feat=True, hist_feat=True, hog_feat=True): if hog_channel is None: hog_channel = [0] # 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 is 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 is 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 is True: hog_features = [] for channel in hog_channel: hog_features.append( get_hog_features(feature_image[:, :, channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) # 8) Append features to list img_features.append(hog_features) # 9) Return concatenated array of features return np.concatenate(img_features)
def extract_features(imgs, color_space='RGB', spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=None, spatial_feat=True, hist_feat=True, hog_feat=True): if hog_channel is None: hog_channel = [0] # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: file_features = [] # Read in each one by one print(file) image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # apply color conversion if other than 'RGB' 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) if spatial_feat is True: spatial_features = bin_spatial(feature_image, size=spatial_size) file_features.append(spatial_features) if hist_feat is True: # Apply color_hist() hist_features = color_hist(feature_image, nbins=hist_bins) file_features.append(hist_features) if hog_feat is True: # Call get_hog_features() with vis=False, feature_vec=True hog_features = [] for channel in hog_channel: hog_features.append( get_hog_features(feature_image[:, :, channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) # Append the new feature vector to the features list file_features.append(hog_features) features.append(np.concatenate(file_features)) # Return list of feature vectors return np.array(features)
def find_cars(img, ystart, ystop, scale, svc, X_scaler, color_space, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, hog_channel): draw_img = np.copy(img) img = img.astype(np.float32) / 255 img_tosearch = img[ystart:ystop, :, :] 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) 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 + 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) 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: 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