def test_hotspot(self): # Read in a pickle file with bboxes saved # Each item in the "all_bboxes" list will contain a # list of boxes for one of the images shown above box_list = pickle.load(open("bbox_pickle.p", "rb")) # Read in image similar to one shown above image = mpimg.imread(IMG_FILE) image_with_boxes = draw_boxes(image, box_list) heat = np.zeros_like(image[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = uut.add_heat(heat, box_list) # Apply threshold to help remove false positives heat = uut.apply_threshold(heat, 1) # Visualize the heatmap when displaying heatmap = np.clip(heat, 0, 255) # Find final boxes from heatmap using label function labels = label(heatmap) draw_img = uut.draw_labeled_bboxes(np.copy(image), labels) final_image = image_util.arrangeImages( [image, image_with_boxes, heatmap, draw_img], ["Original", "Boxes that detected car", "Heatmap", "labeld boxes"], 2) image_util.saveImage(final_image, TEST_OUT_DIR + "/hotspot.jpg")
def draw_sliding_windows(self, filename, img, y_start, y_stop, scale): xy_window = (int(64*scale),int(64*scale)) boxes = slide_window(img, y_start_stop=[y_start, y_stop], xy_window=xy_window, xy_overlap=(0.75, 0.75)) img = draw_boxes(img,boxes) img = cv2.rectangle(img,(0,y_start),(xy_window[0],y_start+xy_window[1]), (255,0,0), 6) print (len(boxes)) print (img.shape) image_util.saveImage(img, filename)
def feed_image(self,img,isVideo=True): draw_image1 = np.copy(img) draw_image2 = np.copy(img) if self.imgsize==None or not isVideo: self.imgsize = img.shape[0:2] self.heatmap = np.zeros(self.imgsize,dtype='float64') # Detect Cars hot_windows = [] for search_param in self.search_params: xstart = search_param["xstart"] xstop = search_param["xstop"] ystart = search_param["ystart"] ystop = search_param["ystop"] scale = search_param["scale"] hot_windows += lf.find_cars(img, xstart,xstop,ystart, ystop, scale, self.svc, self.X_scaler, self.orient, self.pix_per_cell, self.cell_per_block, self.spatial_size, self.hist_bins, color_space=self.color_space,hog_channel=self.hog_channel,spatial_feat=self.spatial_feat, hist_feat=self.hist_feat, hog_feat=self.hog_feat) window_img = lf.draw_boxes(draw_image1, hot_windows, color=(0, 0, 255), thick=6) # Add to heatmap if not isVideo: self.heatmap = lf.add_heat(self.heatmap,hot_windows) self.heatmap_thresh = lf.apply_threshold(self.heatmap, self.minheat) else: self.heatmap = np.clip(self.heatmap-1,0,self.minheat) self.heatmap = lf.add_heat(self.heatmap,hot_windows) self.heatmap_thresh = lf.apply_threshold(self.heatmap, self.minheat) self.heatmap += self.heatmap_thresh labels = label(self.heatmap_thresh) label_boxes = lf.get_labeled_bboxes(labels) heatmap_img = np.asarray(np.dstack([self.heatmap,self.heatmap,self.heatmap])/np.max(self.heatmap)*255,dtype='uint8') out_img = lf.draw_boxes(draw_image2, label_boxes, color=(0, 255, 0), thick=6) results_dict = {"window_img":window_img,"out_img":out_img,"heat_map":heatmap_img,"heat_map_thresh":self.heatmap_thresh} return results_dict
def draw_labeled_bboxes_with_history_verbose(self, img): heatmap_img = np.zeros_like(img[:,:,0]) boxed_image = np.copy(img) for bbox_list in self.history: heatmap = self.add_heat(heatmap_img, bbox_list) boxed_image = draw_boxes(boxed_image,bbox_list) heatmap = self.apply_threshold(heatmap, self.history_max_size * 1.5) labels = label(heatmap) draw_img = self.draw_labeled_bboxes(img, labels) #draw_img = self.draw_heatmap(heatmap) return draw_img, labels, heatmap, boxed_image
def get_cars(self, image): hot_windows = [] if self.subsample: scales = [1, 1.5] for scale in scales: hot_windows.extend( lf.find_cars(image, self.svc, self.X_scaler, spatial_size=(self.ssahb, self.ssahb), hist_bins=self.ssahb, scale=scale)) else: hot_windows.extend( lf.detect_cars_in_image(image, self.svc, self.X_scaler, color_space=self.color, channel=self.channel, spatial_size=(self.ssahb, self.ssahb), hist_bins=self.ssahb, xy_windows=self.xy_windows)) self.windows.append(hot_windows) if len(self.windows) > self.history: self.windows.pop(0) hw = [] for wind in self.windows: hw.extend(wind) heatmap, labels = lf.heat_map(image, hw, self.heat_thres) draw_image = np.copy(image) if not self.heat_only: draw_image = lf.draw_boxes(draw_image, hw, color=(0, 0, 255), thick=6) draw_img = lf.draw_labeled_bboxes(draw_image, labels) return draw_img
def process_pipeline(img, car_classifier, heatmapper, windows, debug=False): norm_img = img.astype(np.float32) / 255.0 box_list = search_windows( img, windows, car_classifier.clf, car_classifier.feature_scaler, car_classifier.color_space, car_classifier.spatial_size, car_classifier.hist_bins, (0, 256), car_classifier.orient, car_classifier.pix_per_cell, car_classifier.cell_per_block, car_classifier.hog_channel, car_classifier.spatial_feat, car_classifier.hist_feat, car_classifier.hog_feat) if debug: boxes_img = np.copy(img) boxes_img = draw_boxes(img, box_list) # creat heatmap using box_list heatmap = heatmapper.compute_heatmap(box_list) labels = label(heatmap) draw_img = draw_labeled_bboxes(np.copy(img), labels) if debug: print("debug process pipeline") return draw_img, boxes_img else: return draw_img
def process_image(imgfn, svc, X_scaler, ssahb, channel, color, train_big, dc, heat_only, ss): """Searches for cars in images while varying features""" image = mpimg.imread(imgfn) if ss: # select subsampeling algorithm heat_thres = 2 scales = [1, 1.5] windows = [] for scale in scales: windows.extend(lf.find_cars(image, svc, X_scaler, spatial_size=(ssahb, ssahb), hist_bins=ssahb, scale=scale)) heatmap, labels = lf.heat_map(image, windows, heat_thres) cars = np.copy(image) if not heat_only: cars = lf.draw_boxes(cars, windows, color=(0, 0, 255), thick=6) cars = lf.draw_labeled_bboxes(cars, labels) else: cars = dc.get_cars(image) # hot_windows = lf.detect_cars_in_image(image, svc, X_scaler) # threshold = 4 # heatmap, labels = lf.heat_map(image, hot_windows, threshold) return cars
for img_file in test_win_images: t = time.time() img = mpimg.imread(img_file) win_img = np.copy(img) img = img.astype(np.float32)/255 print(np.min(img), np.max(img)) h = img.shape[0] y_start_stop = [h//2, h] print('y_start_stop', y_start_stop) windows = slide_window(img, x_start_stop=[None, None], y_start_stop=y_start_stop, xy_window=(96, 96), xy_overlap=(overlap, overlap)) search_wins = search_windows(img, 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 = draw_boxes(win_img, search_wins, color=(0, 0, 255), thick=5) temp_images.append(window_img) titles.append(img_file) print(time.time()-t, 'seconds to process one image searching', len(windows), 'windows') fig = plt.figure(figsize=(12,18), dpi=300) visualize(fig, 5,2, temp_images, titles) write_name= 'output_images/search_draw_win/search_draw_win'+'.jpg' print('car_plot image: ', write_name) fig.savefig(write_name)
def _lane_car_locate_pipeline(rgb_img): """Run both vehicle_zone and lane_zone pipelines.""" car_matches = vehicle_zone.locate_nearby_cars(rgb_img) lane_img = lane_zone.locate_lane_bounds(rgb_img) return lesson_functions.draw_boxes(lane_img, car_matches)
def draw_rectangles(self, image, rects, color=(0, 0, 1), thick=6): return lesson_functions.draw_boxes(image, rects, color, thick)
draw_img = draw_labeled_bboxes(image, labels) if show_heat: heatmap = np.clip(heat, 0, 255) return draw_img, heatmap, hot_windows else: return draw_img if __name__ == '__main__': image = mpimg.imread(TEST_IMAGE) t_start = time.time() draw_img, heatmap, hot_windows = detect_vehicle(image, show_heat=True) t_end = time.time() print("Time take to process image:", round(t_end - t_start, 2)) window_img = draw_boxes(image, hot_windows, color=(0, 0, 255), thick=6) # fig = plt.figure(figsize=(12,6)) # plt.subplot(131) # plt.imshow(window_img) # plt.title('All Detected Windows') # plt.subplot(132) # plt.imshow(draw_img) # plt.title('Car Positions') # plt.subplot(133) # plt.imshow(heatmap, cmap='hot') # plt.title('Heat Map') # fig.tight_layout() # plt.show() plt.imshow(window_img) plt.axis('off')
def videopipe(video_image): # this is the way to treat each videoframe video_image_png = video_image.astype(np.float32) / 255 draw_image = np.copy(video_image) # print(str(np.amax(draw_image))) ''' # For the output of a single image: test_file_name = 'test1.jpg' test_file_folder = '../test_images/' test_file = test_file_folder+test_file_name test_image_name = test_file_name+'_LUVonlySpat.jpg' test_image_png = mpimg.imread(test_file) test_image = test_image_png.astype(np.float32)/255 draw_image = np.copy(test_image) ''' # Search for matches in image: hot_windows_spat = detect.search_windows(video_image_png, windows, svc_spat, X_scaler_spat, 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=False, hog_feat=False) hot_windows_hist = detect.search_windows(video_image_png, windows, svc_hist, X_scaler_hist, 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=False) hot_windows_hog = detect.search_windows(video_image_png, windows, svc_hog, X_scaler_hog, 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=False, hist_feat=False, hog_feat=hog_feat) #print("hot_windows_spat: "+str(len(hot_windows_spat))) #print("hot_windows_hist: "+str(len(hot_windows_hist))) #print("hot_windows_hog: "+str(len(hot_windows_hog ))) hot_windows = [] hot_windows.extend(hot_windows_spat) hot_windows.extend(hot_windows_hist) hot_windows.extend(hot_windows_hog) burning_windows = len(hot_windows) font = cv2.FONT_HERSHEY_SIMPLEX # ''' # for debugging: # Drawing the identified rectangles on the image window_img = lesson_functions.draw_boxes(draw_image, hot_windows_spat, color=(255, 0, 255), thick=2) cv2.putText(window_img, str(nnn), (50, 100), font, 1.5, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "hot windows found: " + str(len(hot_windows_spat)), (150, 100), font, 1.5, (255, 0, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "spatial bins: " + str(spatial_size), (100, 150), font, 1, (255, 0, 255), 2, cv2.LINE_AA) cv2.imwrite(folder + '/spat/spat_' + str(nnn) + '.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows_hist, color=(0, 255, 255), thick=2) cv2.putText(window_img, str(nnn), (50, 100), font, 1.5, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "hot windows found: " + str(len(hot_windows_hist)), (150, 100), font, 1.5, (0, 255, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "hist_bins: " + str(hist_bins), (100, 150), font, 1, (0, 255, 255), 2, cv2.LINE_AA) cv2.imwrite(folder + '/hist/hist_' + str(nnn) + '.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows_hog, color=(255, 255, 0), thick=2) cv2.putText(window_img, str(nnn), (50, 100), font, 1.5, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "hot windows found: " + str(len(hot_windows_hog)), (150, 100), font, 1.5, (255, 255, 0), 2, cv2.LINE_AA) cv2.putText( window_img, "orient: " + str(orient) + " pix_per_cell: " + str(pix_per_cell) + " cell_per_block: " + str(cell_per_block) + " hog_channel: " + str(hog_channel), (100, 150), font, 1, (255, 255, 0), 2, cv2.LINE_AA) cv2.imwrite(folder + '/hog/hog_' + str(nnn) + '.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=2) cv2.putText(window_img, str(nnn), (50, 100), font, 1.5, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(window_img, "hot windows found: " + str(len(hot_windows)), (150, 100), font, 1.5, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText( window_img, "abs_threshold: " + str(abs_threshold) + " dyn_lim: " + str(dyn_lim) + " = " + str(int(dyn_lim * len(hot_windows))), (100, 150), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText( window_img, "spat: " + str(len(hot_windows_spat)) + " hist: " + str(len(hot_windows_hist)) + " hog: " + str(len(hot_windows_hog)), (100, 200), font, 1, (0, 0, 255), 2, cv2.LINE_AA) # document single image output # ''' global nnn nnn = nnn + 1 # consolidate heatmap # threshold = 6 # dyn_threshold = int(burning_windows*0.15) # % of all frames must be overlapping object for detection dyn_threshold = int( burning_windows * dyn_lim) # % of all frames must be overlapping object for detection labels, heat_max = heatmap.heathot(hot_windows, draw_image, dyn_threshold, abs_threshold, max_threshold) # print("labels shape after: "+labels.shape) ############################## # Another filter function added with love to the Udacity reviewers ############################## # generate list of boundary boxes bbox_list = heatmap.draw_labeled_bboxes(video_image_png, labels)[1] # print("before append: "+str(history)) history.append(bbox_list) # print("after append: "+str(history)) # print(hot_windows_hist) tmp = [] for n in history: tmp.extend(n) # print("tmp = ") # print(tmp) # print(history[0].shape) if nnn > history_length + 1: labels = heatmap.heathot(tmp, draw_image, history_threshold, -1, 100)[0] # print(bbox_list) # print(len(bbox_list)) # print(np.amax(bbox_list)) # print(labels[0].shape) if (len(hot_windows_spat) > abs_limit) and (len(hot_windows_hist) > abs_limit) and ( len(hot_windows_hog) > abs_limit ): # there have to be at least some detections in each filter draw_image = heatmap.draw_labeled_bboxes(draw_image, labels)[0] # else: # draw_img = draw_image # ''' for documentation purposes add results to output image if (len(hot_windows_spat) > abs_limit) and (len(hot_windows_hist) > abs_limit) and ( len(hot_windows_hog) > abs_limit ): # there have to be at least some detections in each filter docu_img = heatmap.draw_labeled_bboxes(window_img, labels)[0] else: docu_img = window_img cv2.putText(docu_img, "heat_max: " + str(heat_max), (100, 250), font, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.imwrite(folder + '/all/all_' + str(nnn) + '.jpg', docu_img) # ''' # label each frame with a counter for debugging / threshold cv2.putText(draw_image, str(nnn), (50, 100), font, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(draw_image, "hot windows found: " + str(len(hot_windows)), (150, 100), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.imwrite(folder + '/output/out_' + str(nnn) + '.jpg', draw_image) return draw_image
hist_feat=False, hog_feat=hog_feat) #print("hot_windows_spat: "+str(len(hot_windows_spat))) #print("hot_windows_hist: "+str(len(hot_windows_hist))) #print("hot_windows_hog: " +str(len(hot_windows_hog ))) hot_windows = [] hot_windows.extend(hot_windows_spat) hot_windows.extend(hot_windows_hist) hot_windows.extend(hot_windows_hog) burning_windows = len(hot_windows) print("hot_windows found: " + str(len(hot_windows))) # Drawing the identified rectangles on the image window_img = lesson_functions.draw_boxes(draw_image, hot_windows_spat, color=(255, 0, 255), thick=2) cv2.imwrite(folder + '/testpic/' + test_file_name + '_spat.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows_hist, color=(0, 255, 255), thick=2) cv2.imwrite(folder + '/testpic/' + test_file_name + '_hist.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows_hog, color=(255, 255, 0), thick=2) cv2.imwrite(folder + '/testpic/' + test_file_name + '_hog.jpg', window_img) window_img = lesson_functions.draw_boxes(draw_image, hot_windows, color=(0, 0, 255),
print('spatial_feat: ', params['spatial_feat']) print('hist_feat: ', params['hist_feat']) print('hog_feat: ', params['hog_feat']) #img = mpimg.imread('test_images/test1.jpg') #img = mpimg.imread('test_images/test2.jpg') #img = mpimg.imread('test_images/test3.jpg') img = mpimg.imread('test_images/test4.jpg') #img = mpimg.imread('test_images/test5.jpg') #img = mpimg.imread('test_images/test6.jpg') ystart = 400 ystop = 656 scale = 1.5 t = time.time() draw_img = np.copy(img) img = img.astype(np.float32) / 255 bboxes = find_cars(img, ystart, ystop, scale, svc, X_scaler, params) t2 = time.time() print(round(t2 - t, 5), 'Seconds to detect using scale:', scale) draw_img = draw_boxes(draw_img, bboxes) plt.figure(figsize=(8, 8)) plt.xlim(0, 1280) plt.ylim(720, 0) plt.imshow(draw_img)