def demo(): feature_parameter = selected_feature_parameter() y_start_stop = [440, None] # Min and max in y to search in slide_window() svc, X_scaler = read_classifier("classifier.p") image = mpimg.imread('bbox-example-image.jpg') draw_image = np.copy(image) # Uncomment the following line 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) #image = image.astype(np.float32)/255 windows = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop, xy_window=(96, 96), xy_overlap=(0.5, 0.5)) hot_windows = search_windows(image, windows, svc, X_scaler, feature_parameter=feature_parameter) window_img = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) plt.imshow(window_img) plt.show()
def process_image(img, debug=False): clf = classifier.get_classifier() windows = search(img, clf) heat_map = get_heat_map(img, windows) heat_map = apply_threshold(heat_map, 1) labeled_img = get_labeled_image(img, heat_map) if debug: import matplotlib.pyplot as plt from sliding_window import draw_boxes f, axarr = plt.subplots(2, 2, figsize=(15, 15)) axarr[0, 0].set_title('Original Image') axarr[0, 0].imshow(img) axarr[0, 1].set_title('Detected Windows') axarr[0, 1].imshow(draw_boxes(img, windows)) axarr[1, 0].set_title('Heat Map') axarr[1, 0].imshow(heat_map) axarr[1, 1].set_title('Labeled Image') axarr[1, 1].imshow(labeled_img) plt.show() return labeled_img
def find_cars(img, y_start_stop, scale, clf, X_scaler, feature_parameter): hot_windows = subsample_search(img, y_start_stop, scale, clf, X_scaler, feature_parameter) draw_image = np.copy(img) draw_image = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) return draw_image
def handle_image(self, img): # if self.count < 170: # self.count += 1 # return img alpha = 0.3 #scipy.misc.imsave('input_images/' + str(self.count) + '.jpg', img) if self.heat is None: self.heat = np.zeros_like(img[:, :, 0]).astype(np.float) else: self.heat = self.heat * (1.0 - alpha) heat = np.zeros_like(img[:, :, 0]).astype(np.float) image = img.astype(np.float32) / 255. boxes = sliding_window.search_classify(image, clf) draw_image = np.copy(image) window_img = sliding_window.draw_boxes(draw_image, boxes, color=(0, 0, 1.0), thick=6) scipy.misc.imsave('output_images/boxes_' + str(self.count) + '.jpg', window_img) heat = heatmap.add_heat(heat, boxes) #self.heatmaps.append(heat) self.heat += heat * alpha # Apply threshold to help remove false positives hm = heatmap.apply_threshold(np.copy(self.heat), 0.6) # Visualize the heatmap when displaying hm = np.clip(hm, 0, 255) scipy.misc.imsave('output_images/map_' + str(self.count) + '.jpg', self.heat) # Find final boxes from heatmap using label function labels = label(hm) draw_img = heatmap.draw_labeled_bboxes(image, labels) scipy.misc.imsave('output_images/' + str(self.count) + '.jpg', draw_img) self.count += 1 return draw_img * 255.0
def process_images(files): imgcnt = 0 for filename in files: image = mpimg.imread(filename) #crop image to see the left half height, width, depth = image.shape image = image[:, 0:int(width / 2):, ] windows = all_sliding_windows(image, max_image_y=320) #We get 190 windows that are too many #pick 20 windows randomly windows = random.sample(windows, 20) #call save save_images(image, windows, imgcnt) imgcnt += 1 #print(len(windows)) if (0): window_img = draw_boxes(image, windows, color=(20, 200, 255), thick=2) plt.imshow(window_img) plt.show()
def cnn_search_classify(image, windows, model): on_windows = [] probs = [] if len(windows) == 0: return on_windows, probs images = [] size = (64, 64) for window in windows: #print("searching ",window) img = cv2.resize(image[window[0][1]:window[1][1], window[0][0]:window[1][0]], size, interpolation=cv2.INTER_AREA) img = rescale_resize_image(img) images.append(img) X_input = np.array(images) probabilities = model.predict_proba(X_input, batch_size=32, verbose=0) #print("prob",probabilities) cnt = 0 PROB_CAR = 0.8 #0.8 for pnocar, pcar in probabilities: if pcar > PROB_CAR: window = windows[cnt] on_windows.append(window) probs.append(pcar) if (0): window_img = draw_boxes(image, on_windows, color=(0, 0, 255), thick=6) plt.imshow(window_img) plt.show() cnt += 1 print("Found ", len(on_windows), "car windows out of ", len(windows)) return on_windows, probs
def apply_window_search(train_dir, test_dir): # Uncomment the following line 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) #image = image.astype(np.float32)/255 dcspace = 'YCrCb' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 9 # HOG orientations pix_per_cell = 8 # HOG pixels per cell cell_per_block = 2 # HOG cells per block hog_channel = '012' # Can be 0, 1, 2, or "012" spatial_size = (32, 32) # Spatial binning dimensions hist_bins = 32 # Number of histogram bins spatial_feat = True # Spatial features on or off hist_feat = True # Histogram features on or off hog_feat = True # HOG features on or off y_start_stop = [None, None] # Min and max in y to search in slide_window() if have_classifier(): svc, scaler, dcspace, spatial_size, hist_bins, orient, \ pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat = load_classifier() else: svc, scaler = combo_feature_train(train_dir, scspace='BGR', dcspace=dcspace, 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) fnames = load_images(test_dir) for fname in fnames: image = cv2.imread(fname) draw_image = np.copy(image) draw_image = color_convert_nocheck(image, 'BGR', 'RGB') min_window = spatial_size[0] max_window = 128 top_y = int(image.shape[0] / 2) y_start_stop = [top_y, image.shape[0]] valid_y = image.shape[0] - top_y for window_size in range(min_window, max_window, spatial_size[0]): #window_size = int(min_window * scale_factor) #print("window size", window_size, "larger than", image.shape[0], "x", image.shape[1]) if window_size > valid_y or window_size > image.shape[1]: print("window size", window_size, "larger than", valid_y, "x", image.shape[1]) continue windows = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop, xy_window=(window_size, window_size), xy_overlap=(0.75, 0.75)) hot_windows = search_windows(image, windows, svc, scaler, scspace='BGR', dcspace=dcspace, 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) window_img = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=5) plt.imshow(window_img) basename = os.path.basename(fname) name, ext = os.path.splitext(basename) savename = os.path.join( 'output_images', name + "_" + str(window_size) + "_" + ext) fig = plt.figure() plt.imshow(window_img) plt.title('Searching window size' + str(window_size)) plt.xlabel(fname) fig.savefig(savename) plt.close()