def process(image, svc, X_scaler): # Test the result on one single image image = mpimg.imread(image) draw_image = np.copy(image) windows = utils.slide_window(image, x_start_stop=x_start_stop, y_start_stop=y_start_stop, xy_window=(96, 96), xy_overlap=(0.75, 0.75)) hot_windows = utils.search_windows(image, windows, svc, X_scaler, 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) window_img = utils.draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) # Find the place were is the most overlapping boxes by drawing a Heatmap heat = np.zeros_like(window_img[:,:,0]).astype(np.float) heat = utils.add_heat(heat, hot_windows) heat = utils.apply_threshold(heat, 1) heatmap = np.clip(heat, 0, 255) labels = label(heatmap) draw_img = utils.draw_labeled_bboxes(image, labels) return draw_img
def process_video(img): global previous_heatmap global previous_states windows1, _ = utils.find_cars(img, 400, 656, 2.0, clf, scaler, p['orient'], p['pix_per_cell'], p['cell_per_block'], p['spatial_size'], p['hist_bins']) hot_windows = windows1 windows2, _ = utils.find_cars(img, 350, 550, 1.2, clf, scaler, p['orient'], p['pix_per_cell'], p['cell_per_block'], p['spatial_size'], p['hist_bins']) # hot_windows = windows1 + windows2 # windows3, _ = utils.find_cars(img, 350, 500, 0.8, clf, scaler, p['orient'], p['pix_per_cell'], p['cell_per_block'], # p['spatial_size'], p['hist_bins']) # hot_windows = windows1 + windows2 + windows3 # hot_windows = windows1 + windows2 heatmap = utils.add_heat(previous_heatmap, hot_windows) previous_heatmap = heatmap * 0.5 heatmap = utils.apply_threshold(heatmap, 10) img, states = utils.draw_labeled_bboxes(img, heatmap, previous_states) # Add new state, and remove last if bigger than 3 previous_states.append(states) number_of_states_to_keep = 10 if len(previous_states) > number_of_states_to_keep: previous_states = previous_states[-number_of_states_to_keep:] return img
def draw_heatmap_labels(self, shape): all_windows = [] for w in self.windows: all_windows.extend(w) heatmap = np.zeros(shape) heatmap = add_heat(heatmap, all_windows) heatmap = apply_threshold(heatmap, self.threshold) labels = label(heatmap) return labels, heatmap
def heat_and_threshold(self, image, box_list, rolling_threshold=1, current_threshold=1): heat = np.zeros_like(image[:, :, 0]).astype(np.float) # Add heat to each box in box list raw_heat = add_heat(heat, box_list) # Smoothen out heated windows based on time-averaging avg_heat = self.rolling_sum([heat])['heat'] # Apply threshold to help remove false positives raw_heat = apply_threshold(raw_heat, CURRENT_FRAME_HEAT_THRESHOLD ) # SETTINGS.CURRENT_FRAME_HEAT_THRESHOLD avg_heat = apply_threshold( avg_heat, ROLLING_SUM_HEAT_THRESHOLD) # SETTINGS.ROLLING_SUM_HEAT_THRESHOLD # Visualize the heatmap when displaying # TODO: if VideoMode; else (255) raw_heatmap = np.clip(raw_heat, 0, 255) avg_heatmap = np.clip(avg_heat, 0, 255) image = self.add_to_debugbar(image, avg_heatmap, 'Rolling Sum Heatmap', position='right') image = self.add_to_debugbar(image, raw_heatmap, 'Current Fr. Heatmap', position='left') # Find final boxes from heatmap using label function raw_labels = label(raw_heatmap) avg_labels = label(avg_heatmap) # Overlap Raw with Avg draw_img = draw_labeled_bboxes(image, raw_labels, color=(1, 0, 0), thickness=2, meta=False) # red draw_img = draw_labeled_bboxes(draw_img, avg_labels, meta=HEATMAP_METRICS) return draw_img, avg_heatmap, avg_labels
def _add_heatmap(self, bboxes): """ Add new heat map by shifting up older maps and inserting new maps at the bottom of the buffer. Initialization flags are updated accordingly :param bboxes: box list :return: """ # Accumulate new heatmaps # Shift existing entries up for i in range(0, self.nbuff - 1): self.heatmaps[i, :, :] = self.heatmaps[i + 1, :, :] self.initmaps[i] = self.initmaps[i + 1] # Insert new map newmap = np.zeros_like(self.heatmaps[-1, :, :]) self.heatmaps[-1, :, :] = add_heat(newmap, bbox_list=bboxes) # Flag that we have initialized it self.initmaps[-1] = True
def process_image(img): '''Pipeline to prepare video images with vehicle detection''' boxes = find_cars_multiscale(img, multiscale, clf, X_scaler, cspace, spatial_size, hist_bins, orient, pix_per_cell, cell_per_block, hog_channel) if len(Box_mem.boxes) >= n: Box_mem.boxes.pop(0) Box_mem.boxes.append(boxes) box_with_mem = [] for box in Box_mem.boxes: box_with_mem.extend(box) heat = np.zeros_like(img[:, :, 0]).astype(np.float) heat = add_heat(heat, box_with_mem) heat = apply_threshold(heat, 5) heatmap = np.clip(heat, 0, 255) labels = label(heatmap) heatmap_small = cv2.resize(heatmap, (320, 180)).astype(np.float32) norm = Normalize(vmin=0, vmax=24) heatmap_small = np.delete(cm.hot(norm(heatmap_small))*255.0, 3, 2) draw_img = draw_labeled_bboxes(np.copy(img), labels) draw_img[50:50+180, 50:50+320] = heatmap_small return draw_img # , heat
def search_car(img): draw_img = np.copy(img) windows = [] colorspace = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 11 pix_per_cell = 16 cell_per_block = 2 hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL" ystart = 400 ystop = 464 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 416 ystop = 480 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 496 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 528 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 528 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 560 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 596 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 464 ystop = 660 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) # window_list = utils.slide_window(img) heat_map = np.zeros(img.shape[:2]) heat_map = utils.add_heat(heat_map, windows) heat_map_thresholded = utils.apply_threshold(heat_map, 1) labels = label(heat_map_thresholded) draw_img = utils.draw_labeled_bboxes(draw_img, labels) return draw_img
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) # Visualize the heatmap when displaying heatmap = np.clip(heat, 0, 255) # Find final boxes from heatmap using label function labels = label(heatmap) # Prepare heatmap image overlay heatmap_small = cv2.resize(heatmap, (320, 180)).astype(np.float32) norm = Normalize(vmin=0, vmax=12) heatmap_small = np.delete(cm.hot(norm(heatmap_small))*255.0, 3, 2) draw_img = draw_labeled_bboxes(np.copy(img), labels) # Insert heatmap image overlay draw_img[50:50+180, 50:50+320] = heatmap_small plt.imsave(r"./output_images/" + fname.split('\\')[-1].split('.')[0] + "_6_heat.jpg", draw_img)
def search_car(img): draw_img = np.copy(img) # img = img.astype(np.float32) / 255 # all_windows = [] # X_start_stop = [[None, None], [None, None], [None, None], [None, None]] # w0, w1, w2, w3 = 64, 96, 128, 196 # o0, o1, o2, o3 = 0.75, 0.75, 0.75, 0.75 # XY_window = [(w0, w0), (w1, w1), (w2, w2), (w3, w3)] # XY_overlap = [(o0, o0), (o1, o1), (o2, o2), (o3, o3)] # yi0, yi1, yi2, yi3 = 400, 400, 400, 400 # Y_start_stop = [[yi0, yi0 + w0 * 1.25], [yi1, yi1 + w1 * 1.25], [yi2, yi2 + w2 * 1.25], [yi3, yi3 + w3 * 1.25]] # # # # for i in range(len(Y_start_stop)): # windows = utils.slide_window(img, x_start_stop=X_start_stop[i], y_start_stop=Y_start_stop[i], # xy_window=XY_window[i], xy_overlap=XY_overlap[i]) # # all_windows += windows # on_windows = utils.search_windows(img, all_windows, svc, X_scaler, spatial_feat=True, hist_feat=True, # hog_channel='ALL') windows = [] colorspace = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 11 pix_per_cell = 16 cell_per_block = 2 hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL" ystart = 400 ystop = 464 scale = 1.0 windows+=(find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 416 ystop = 480 scale = 1.0 windows+=(find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 496 scale = 1.5 windows+=(find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 528 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 528 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 560 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 596 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 464 ystop = 660 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) # window_list = utils.slide_window(img) heat_map = np.zeros(img.shape[:2]) heat_map = utils.add_heat(heat_map,windows) heat_map_thresholded = utils.apply_threshold(heat_map,1) labels = label(heat_map_thresholded) draw_img = utils.draw_labeled_bboxes(draw_img,labels) # draw_img = utils.draw_windows(draw_img,on_windows) return draw_img
def pipeline(img, clip): """ 1. Loop through the different scale windows and keep those which the classifier positively identifies as cars 2. Create a thresholded heatmap from the overlapping windows 3. Label the heatmap to identify distinct vehicles 4. Draw labelled boxes from the heatmap """ frame = Frame() clip.frames.append(frame) heatmap = np.zeros_like(img[:, :, 0]).astype(np.float) # 1. Identify "hot" windows hot_windows = [] # for scale in scales: # hot_windows.extend(find_cars(img, y_start_stop=y_start_stop, scale=scale, # svc=svc, X_scaler=X_scaler, **config)) for param in parameters: more_hot_windows = find_cars(img, y_start_stop=param['y_start_stop'], scale=param['scale'], svc=svc, X_scaler=X_scaler, **config) # Add more heat to larger windows if param['window_size'][0] < 200: add_heat(heatmap, more_hot_windows, amount=1) else: add_heat(heatmap, more_hot_windows, amount=2) hot_windows.extend(more_hot_windows) window_img = draw_boxes(img, hot_windows, color=(0, 0, 255), thick=6) # 2. Thresholded heatmap # add_heat(heatmap, hot_windows) add_extra_heat(heatmap, hot_windows) frame.heatmap = heatmap # Note - frame/clip will always store the unthresholded heatmap threshold_heatmap = np.copy(clip.heatmap) # threshold_heatmap = np.copy(clip.frames[-1].heatmap) # threshold_heatmap[threshold_heatmap <= 3] = 0 # heatmap_1 = np.copy(threshold_heatmap) threshold_heatmap[threshold_heatmap <= 4] = 0 # heatmap_2 = np.copy(threshold_heatmap) # threshold_heatmap[threshold_heatmap <= 5] = 0 # heatmap_3 = np.copy(threshold_heatmap) # 3. Label the heatmap labelled_array, num_features = label(threshold_heatmap) # 4. Draw the bounded boxes output = np.copy(img) output = draw_labelled_bboxes(img, (labelled_array, num_features), color=(0, 0, 255), thick=6) # output = draw_labelled_bboxes(img, label(heatmap_1), color=(255, 0, 0), thick=6) # output = draw_labelled_bboxes(output, label(heatmap_2), color=(0, 255, 0), thick=4) # output = draw_labelled_bboxes(output, label(heatmap_3), color=(0, 0, 255), thick=2) # ----- if PLOT: images = [ (window_img, 'All positive windows'), (heatmap, 'Heatmap'), (threshold_heatmap, 'Thresholded Heatmap'), (labelled_array, 'Labelled array'), (output, 'Result'), ] visualise_windows(img, parameters) for i, title in images: plt.figure() plt.imshow(i) plt.title(title) if SAVE: fig = plt.gcf() fig.savefig('output_images/' + "_".join(t for t in title.split(" ")) + '.jpg') return output
def search_car(img, framecount): draw_img = np.copy(img) windows = [] colorspace = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 11 pix_per_cell = 16 cell_per_block = 2 hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL" ystart = 400 ystop = 464 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 416 ystop = 480 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 496 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 528 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 528 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 560 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 596 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 464 ystop = 660 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) # window_list = utils.slide_window(img) heat_map = np.zeros(img.shape[:2]) heat_map = utils.add_heat(heat_map, windows) heat_map_thresholded = utils.apply_threshold(heat_map, 1) labels = label(heat_map_thresholded) i = 0 VehicalPath = "./capture" if not os.path.exists(VehicalPath): os.makedirs(VehicalPath) draw_img, bboxlist = utils.draw_labeled_bboxes(draw_img, labels) for bbox in bboxlist: x1, y1 = bbox[0] x2, y2 = bbox[1] i = i + 1 if (x2 - x1 > 96) and (y2 - y1) > 96: cv2.imwrite("./capture/Vehical{}_{}.jpg".format(framecount, i), draw_img[x1:x2, y1:y2]) return VehicalPath
ystart = 300 ystop = 680 y_start_stop = [(350, 500), (400, 550), (350, 650)] scale = [1, 2, 3] cells_per_step = [1, 1, 1] t = time.time() hot_windows = detector.search_windows(image, y_start_stop=y_start_stop, scale=scale, cells_per_step=cells_per_step) t2 = time.time() print(round(t2 - t, 2), 'Seconds to search and identify {} windows'.format(len(hot_windows))) draw_heatmap = False if draw_heatmap: heatmap = np.zeros(draw_image.shape[:2]) heatmap = add_heat(heatmap, hot_windows) heatmap = apply_threshold(heatmap, 2) labels = label(heatmap) window_img = draw_labeled_bboxes(draw_image, labels) else: window_img = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) plt.imshow(window_img) # plt.savefig(ROOT + 'output_images/car/{}_labeled.jpg'.format(test_img)) plt.show()
def search_car(img, dist_pickle): svc = dist_pickle["clf"] X_scaler = dist_pickle["scaler"] orient = dist_pickle["orient"] pix_per_cell = dist_pickle["pix_per_cell"] cell_per_block = dist_pickle["cell_per_block"] spatial_size = dist_pickle["spatial_size"] hist_bins = dist_pickle["hist_bins"] ystart = 400 ystop = 656 scale = 1.5 test_imgs = [] out_imgs = [] #plt.figure(figsize=(20,68)) draw_img = np.copy(img) windows = [] colorspace = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 11 pix_per_cell = 16 cell_per_block = 2 hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL" ystart = 400 ystop = 464 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 416 ystop = 480 scale = 1.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 496 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 528 scale = 1.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 528 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 432 ystop = 560 scale = 2.0 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 400 ystop = 596 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) ystart = 464 ystop = 660 scale = 3.5 windows += (find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc, None, orient, pix_per_cell, cell_per_block, None, None)) # window_list = utils.slide_window(img) heat_map = np.zeros(img.shape[:2]) heat_map = utils.add_heat(heat_map, windows) heat_map_thresholded = utils.apply_threshold(heat_map, 1) labels = label(heat_map_thresholded) draw_img = utils.draw_labeled_bboxes(draw_img, labels) return draw_img