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 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 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(self, image): draw_image = np.zeros_like(image) y_start_stop = [(350, 500), (400, 550), (350, 650)] scale = [1, 2, 3] cells_per_step = [1, 1, 1] hot_windows =\ self.detector.search_windows(image, y_start_stop=y_start_stop, scale=scale, cells_per_step=cells_per_step) self.windows.append(hot_windows) alive_box = hot_windows if self.draw_heatmap: labels, heatmap = self.draw_heatmap_labels(draw_image.shape[:2]) window_img, alive_box =\ draw_labeled_bboxes(draw_image, labels, heatmap, self.cthreshold) else: window_img = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) if self.standalone: return window_img else: return alive_box
def draw_rect_arounbd_cars(img, windows_list, clf, scaler, hm): ''' Pipeline for detecting cars in the video ''' # Candidate of windows containg cars windows_list = search_windows(img, windows_list, clf, scaler, color_space='HLS', spatial_size=(32, 32), hist_bins=32, hist_range=(0, 256), hog_channel=1, orient=6, pix_per_cell=8, cell_per_block=2) # Collect heatmaps hm.add_heat(windows_list) hm.image_queue.append(img) agg_hm = hm.aggregate() # Label windows for cars labels = label(agg_hm) cv2.putText(img, "detected {} cars".format(labels[1]), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255,0), 2) return draw_labeled_bboxes(img, labels)
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
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) # class to contain last n batch of boxes class Box_mem(): def __init__(self): self.boxes = [] # was the line detected in the last iteration? Box_mem = Box_mem() n = 8
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 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()
hm = heatmap(img_shape = (720, 1280), threshold=8, deque_len=8) output = 'test_video_output.mp4' clip2 = VideoFileClip('test_video.mp4') draw = lambda x: draw_rect_arounbd_cars(x, windows_list, clf, scaler, hm) clip = clip2.fl_image(draw) clip.write_videofile(output, audio=False) # Plot heatmap for the last 8 frames plt.figure(figsize=(16,12)) for i in range(0,8): plt.subplot(4,4,i+1) plt.imshow(hm.image_queue[i]) plt.subplot(4,4,i+9) plt.imshow(hm.queue[i], cmap="hot") plt.savefig("output_images/heatmap.jpg", dpi=100) plt.tight_layout() # Plot label figure agg_hm = hm.aggregate() labels = label(agg_hm) plt.figure(figsize=(5,3)) plt.imshow(labels[0], cmap='gray') plt.savefig("output_images/labels.jpg", dpi=100) plt.tight_layout() # Plot the detection result for the last frame plt.figure(figsize=(5,3)) plt.imshow(draw_labeled_bboxes(hm.image_queue[-1], labels)) plt.savefig("output_images/final.jpg", dpi=100) plt.tight_layout()
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