def process_image(img): t1 = time.time() #searched_windows = 0 # threshold for heatmap threshold = 2 #img = mpimg.imread(img_src) draw_img = np.copy(img) # Search for smaller appearing vehicles in the distance ystart = 414 ystop = 414 + 96 scale = 1 cells_per_step = 2 windows_far, heatmap_far, searched_windows_far = functions.find_cars( img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins) # Search for middle of the visual field with medium window size ystart = 350 ystop = 350 + 192 scale = 1.5 cells_per_step = 2 windows_middle, heatmap_middle, searched_windows_middle = functions.find_cars( img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins) # Search for close to the camera for vehicles that appear large ystart = 350 ystop = 670 scale = 3 cells_per_step = 2 windows_near, heatmap_near, searched_windows_near = functions.find_cars( img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins) # combine all windows windows = windows_far + windows_middle + windows_near # combine all heatmaps heatmap = heatmap_far + heatmap_middle + heatmap_near # apply threshold to the heatmap heatmap = functions.apply_threshold(heatmap, threshold) # apply labels to the heatmap labels = label(heatmap) # draw labels on a copy of the image labeled_image = functions.draw_labeled_bboxes(draw_img, labels) return labeled_image
def pipeline(img): bbox=[] for j in param_list: ystart=j[0] ystop=j[1] scale=j[2] bbox_l = find_cars(img, ystart, ystop, scale, color_space, hog_channel, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins,spatial_feat,hist_feat,axfc=None,show_scalar=show_scalar) bbox.extend(bbox_l) #print(len(bbox), 'bboxes found in image') if len(bbox) > 0: frames_track.add_bbox(bbox) ####################################### heat = np.zeros_like(img[:,:,0]) #heat = add_heat(heat, bbox) #heat = apply_threshold(heat, 1) for box in frames_track.prev_bbox: heat = add_heat(heat, box) #divide the heatmap image by number of frames it been tracked for heat=heat//(frames_track.n_frames) #heat=heat//len(frames_track.prev_bbox) heat = apply_threshold(heat, 1) heatmap = np.clip(heat, 0, 255) ######################################## labels = label(heatmap) #print(labels[1], 'cars found') draw_img = draw_labeled_bboxes(np.copy(img), labels) ####################################### #prepare heatmap to be added to main image #resize heatmap hmcopy=np.copy(heatmap) heat_to_add=cv2.resize(hmcopy,(320,192),cv2.INTER_CUBIC) #resize image resized_img = cv2.resize(img,(320,192),cv2.INTER_CUBIC) resized_gray = cv2.cvtColor(resized_img , cv2.COLOR_RGB2GRAY) res_R=np.copy(resized_gray) res_R[(heat_to_add>0)] =255 res_G =np.copy(resized_gray) res_G[(heat_to_add>0)] =0 res_B =np.copy(resized_gray) res_B[(heat_to_add>0)] =0 draw_img[0:192,0:320,0]=res_R #np.clip(res_R,0,255) draw_img[0:192,0:320,1]=resized_gray draw_img[0:192,0:320,2]=resized_gray return draw_img
def pipeline_nosmoothing(img): bbox=[] for j in param_list: ystart=j[0] ystop=j[1] scale=j[2] bbox_l = find_cars(img, ystart, ystop, scale, color_space, hog_channel, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins,spatial_feat,hist_feat,axfc=None,show_scalar=show_scalar) bbox.extend(bbox_l) print(len(bbox), 'bboxes found in image') ####################################### heat = np.zeros_like(img[:,:,0]) heat = add_heat(heat, bbox) heat = apply_threshold(heat, 1) heatmap = np.clip(heat, 0, 255) ######################################## labels = label(heatmap) print(labels[1], 'cars found') draw_img = draw_labeled_bboxes(np.copy(img), labels) return draw_img
cell_per_block=for_pickle['cell_per_block'] hog_channel=for_pickle['hog_channel'] spatial_feat=for_pickle['spatial_feat'] hist_feat=for_pickle['hist_feat'] hog_feat=for_pickle['hog_feat'] show_scalar=False #ffc,axfc=plt.subplots(1,2) test_img = mpimg.imread('../test_images/test1.jpg') print("img shape",test_img.shape) ystart = 400 ystop = 656 scale = 1.5 print("spatial enabled:",spatial_feat,"Hist enabled:",hist_feat) param_list=((400,464,1.0),(416,480,1.0),(400,496,1.5),(432,528,1.5),(400,528,2.0),(432,560,2.0),(400,596,3.5),(464,660,3.5)) bbox=[] for j in param_list: ystart=j[0] ystop=j[1] scale=j[2] bbox_l = find_cars(test_img, ystart, ystop, scale, color_space, hog_channel, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins,spatial_feat,hist_feat,axfc=None,show_scalar=show_scalar) bbox.extend(bbox_l) print(len(bbox), 'bboxes found in image') im_bbox=draw_boxes(test_img, bbox, color=(0, 0, 255), thick=6) f,p=plt.subplots() p.imshow(im_bbox)
def process_frame(self, image): """ Process a single image frame and do hog subsampling to find cars and improve detection by using heatmap :param image: :return: """ # undistort image image = cv2.undistort(image, self.calibration['mtx'], self.calibration['dist'], None, self.calibration['mtx']) # normalize 0-1 image = image.astype(np.float32) / 255 draw = False window_img, hot_windows = functions.find_cars( image, self.ystart, self.ystop, 1.5, self.svc, self.X_scaler, self.orient, self.pix_per_cell, self.cell_per_block, self.spatial_size, self.hist_bins, self.hist_range) # if windows found which possibly can be cars if len(hot_windows) > 0: print('Found {} windows'.format(len(hot_windows))) heat = np.zeros_like(window_img[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = functions.add_heat(heat, hot_windows) # Apply threshold to help remove false positives heat = functions.apply_threshold(heat, 2) # add to list of previous heatmaps self.heatmaps.append(heat) # queue only latest 10 heatmaps if len(self.heatmaps) > 10: self.heatmaps.pop(0) self.calc_avg_heat(10) avg_heat = functions.apply_threshold(self.avg_heatmap, 10) # Find final boxes from heatmap using label function labels = label(avg_heat) if labels[1] > 0: print(labels[1], 'cars found') if draw: plt.imshow(labels[0], cmap='gray') plt.show() draw_img, car_centers = functions.draw_labeled_bboxes( np.copy(window_img), labels=labels, color=(1.0, 0, 1.0)) if draw: fig = plt.figure() plt.subplot(121) plt.imshow(draw_img) plt.title('Car Positions') plt.subplot(122) # Visualize the heatmap when displaying heatmap = np.clip(avg_heat, 0, 255) plt.imshow(heatmap, cmap='hot') plt.title('Heat Map') fig.tight_layout() plt.show() window_img = draw_img # Back to image space return window_img * 255
test_images = glob.glob('./test_images/test*.jpg') fig1 = plt.figure(figsize=(8, 12)) fig2 = plt.figure(figsize=(8, 18)) fig3 = plt.figure(figsize=(8, 12)) for idx, file in enumerate(test_images): img = mpimg.imread(file) total_heat = np.zeros_like(img[:, :, 0]).astype(np.float) for scale, ystart, ystop in search_boxes: bbox_list = find_cars(img, colorspace, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, (spatial, spatial), histbin, hog_channel, spatial_feat=True, hist_feat=True) heat = np.zeros_like(img[:, :, 0]).astype(np.float) heat = add_heat(heat, bbox_list) heat = apply_threshold(heat, 2) total_heat = total_heat + heat heatmap = np.clip(total_heat, 0, 255) a = fig1.add_subplot(6, 2, (idx + 1) * 2 - 1) plt.imshow(img) b = fig1.add_subplot(6, 2, (idx + 1) * 2)
# threshold for heatmap threshold = 2 for img_src in example_images: t1 = time.time() searched_windows = 0 img = mpimg.imread(img_src) draw_img = np.copy(img) # Search for smaller appearing vehicles in the distance ystart = 414 ystop = 414 + 96 scale = 1 cells_per_step = 2 windows_far, heatmap_far, searched_windows_far = functions.find_cars(img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins) # Search for middle of the visual field with medium window size ystart = 350 ystop = 350 + 192 scale = 1.5 cells_per_step = 2 windows_middle, heatmap_middle, searched_windows_middle = functions.find_cars(img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins) # Search for close to the camera for vehicles that appear large ystart = 350 ystop = 670 scale = 3 cells_per_step = 2 windows_near, heatmap_near, searched_windows_near = functions.find_cars(img, cells_per_step, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)