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_video(img): sliding_sale = [64, 96, 128, 192, 256] boxlist = [] heatmap = np.zeros_like(img[:, :, 0]).astype(np.float) heatmap_threshold = 18 #because we measure across several images counter = 0 for s in sliding_sale: # print("sliding_sale: " + str(s)) crop_arr = slw.slide_window(img, x_start_stop=[None, None], y_start_stop=[300, None], xy_window=(s, s), xy_overlap=(0.5, 0.5)) counter = 0 for x in crop_arr: counter += 1 cropped_image = slw.crop_image(img, x) res = predict(cropped_image) if str(res[0]) == "vehicle": boxlist.append(x) counter += 1 insert_boxlist(boxlist) #by now we have identified all the boxes that contain a car # now we add the boxes to a heatmap but before we do this we aggregate # the boxlists across several images to have a smoother vizualtionation # and to get rid of false positives boxlist = get_consolidated_boxlist() for b in boxlist: # heatmap[box[0][1]:box[1][1], box[0][0]:box[1][0]] += 1 heatmap[b[1]:b[3], b[0]:b[2]] += 1 heatmap[heatmap <= heatmap_threshold] = 0 res_image = ip.draw_labeled_bboxes(img, heatmap) # cv2.imwrite('result_output.jpg',res_image) # util.show_image_from_image(res_image, "output") return res_image
def process_image(img): sliding_sale = [64, 128, 256] boxlist = [] heatmap = np.zeros_like(img[:, :, 0]).astype(np.float) heatmap_threshold = 1 counter = 0 for s in sliding_sale: print("sliding_sale: " + str(s)) crop_arr = slw.slide_window(img, x_start_stop=[None, None], y_start_stop=[300, None], xy_window=(s, s), xy_overlap=(0.5, 0.5)) counter = 0 for x in crop_arr: counter += 1 cropped_image = slw.crop_image(img, x) res = trn.predict(cropped_image) if str(res[0]) == "vehicle": boxlist.append(x) counter += 1 print("boxes with cars found: " + str(counter)) #by now we have identified all the boxes that contain a car # now we add the boxes to a heatmap for b in boxlist: # heatmap[box[0][1]:box[1][1], box[0][0]:box[1][0]] += 1 heatmap[b[1]:b[3], b[0]:b[2]] += 1 heatmap[heatmap <= heatmap_threshold] = 0 res_image = proc.draw_labeled_bboxes(img, heatmap) # cv2.imwrite('result_output.jpg',res_image) util.show_image_from_image(res_image, "output")
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()
# plt.title('HOG Visualization') # # plt.show() # Split up data into randomized training and test sets image = mpimg.imread('extra_img/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 = sliding_window.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 = sliding_window.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,
def main(argv=None): # pylint: disable=unused-argument # JKM For now set seed print( "\n NOTE: Setting seed to get predictable results during development phase" ) np.random.seed(SEED) # Run everything inside of main ### ### This section for exploring & training a classifier. ## Fitted classifier & parms used for that get pickled. ### if EXPLORE: print( "Starting - Step 1 - Explore & Find Best Feature Extraction Parms") # Want to override so as to reduce parms later if OVERRIDE == True: best_parms = BEST_PARMS print(" \n Overridden - using these parms: ", best_parms) else: # This explores best parms best_parms = explore_feature_extraction_parms(cars, notcars, seed=SEED) print("\n Best parms found: ", best_parms) car_dict = run_classifier_and_pickle(cars, notcars, best_parms, seed=SEED) # Run the classifier again with the selected parms & pickle the results else: print("Starting - Step 1 - Get Pickled Data") car_dict = get_pickle_data() # Get the stored parms & classifier svc = car_dict['svc'] X_scaler = car_dict['scaler'] colorspace = car_dict['colorspace'] hog_channel = car_dict['hog_channel'] spatial_size = car_dict['spatial_size'] hist_bins = car_dict['hist_bins'] orient = car_dict['orient'] pix_per_cell = car_dict['pix_per_cell'] cell_per_block = car_dict['cell_per_block'] spatial_feat = True hist_feat = True hog_feat = True print("\n Step_1__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") ### ### This section for finding cars in an image using sliding window. ### print( "\n Step 2 : Explore create and search windows for cars using sliding window algorithm. \n " ) # For now use a sample image # IMAGE 1 # ------- image = mpimg.imread('../examples/bbox-example-image.jpg') draw_image = np.copy(image) # jkm - move this up to other imports from sliding_window import get_windows, search_windows, slide_window # w_list = get_windows(image) # flat_list_of_windows = [item for sublist in w_list for item in sublist] - already flattened # jkm - just work with one set of windows at this time #Y_START_P = 0.6 # Eliminate 60% of the search space in the y direction #y_start = int( Y_START_P * image.shape[0]) #y_start_stop = [y_start, None] #w96_list = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop, # xy_window=(96, 96), xy_overlap=(0.5, 0.5)) #w192_list = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop, # xy_window=(192, 192), xy_overlap=(0.5, 0.5)) # Debugging code - Large & medium windows - HARD CODE w_large = slide_window(image, x_start_stop=[None, None], y_start_stop=[500, None], xy_window=(250, 200), xy_overlap=(0.5, 0.5)) w_med = slide_window(image, x_start_stop=[100, 1200], y_start_stop=[490, 600], xy_window=(110, 80), xy_overlap=(0.5, 0.5)) # combine lists w_all = w_large + w_med # Find the set of windows that match hot_windows = search_windows(image, w_all, svc, X_scaler, color_space=colorspace, 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) print("\n Windows Found: ", len(hot_windows)) # temp - for testing - draw the hot windows window_img = draw_boxes(draw_image, hot_windows, color=(0, 0, 255), thick=6) plt.imshow(window_img) # IMAGE 2 # -------- # try with another image. the one they use for hog sub-sampling # Need a whooe new set of windows to search image2 = mpimg.imread('../test_images/test4.jpg') plt.imshow(image2) # look at it first # draw_image2 = np.copy(image2) # Get new set of windows w_large2 = slide_window(image2, x_start_stop=[None, None], y_start_stop=[350, None], xy_window=(250, 200), xy_overlap=(0.5, 0.5)) w_med2 = slide_window(image2, x_start_stop=[200, None], y_start_stop=[350, 550], xy_window=(110, 80), xy_overlap=(0.5, 0.5)) w_all2 = w_large2 + w_med2 # image2_bb = draw_boxes(draw_image2, w_all2, color=(0, 0, 255), thick=6) plt.imshow(image2_bb) # Find the set of windows that match # - this doesn't find any hot_windows2 = search_windows(image2, w_all2, svc, X_scaler, color_space=colorspace, 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) print("\n Windows Found: ", len(hot_windows2)) # temp - for testing - draw the hot windows window_img2 = draw_boxes(draw_image2, hot_windows2, color=(0, 0, 255), thick=6) plt.imshow(window_img2) print("\n Step_2__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") ### ### This section for finding cars in an image using hog subsampling. ### print("\n Step 3 : Exploring Use of hog_subsample to locate cars.") # using their image for hog-subsampling image2 = mpimg.imread('../test_images/test4.jpg') ystart = 400 ystop = 656 scale = 1.5 from hog_subsample import find_cars # use the same image as in the lesson # from feature_explore import convert_color, bin_spatial, color_hist, get_hog_features out_img2, match_l, conf_l, search_l = find_cars( image2, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, colorspace) plt.imshow(out_img2) # Draw the same thing again but with the box list match_img2 = draw_boxes(image2, match_l) plt.imshow(match_img2) # Draw all the search areas search_img2 = draw_boxes(image2, search_l) plt.imshow(search_img2) # Need different start stops for this one - these ones don't work that well out_img1, match_l1, conf_l1, search_l1 = find_cars( image, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, colorspace) plt.imshow(out_img1) match_img1 = draw_boxes(image, match_l1) plt.imshow(match_img1) # Draw all the search areas search_img1 = draw_boxes(image, search_l1) plt.imshow(search_img1) # try a loop to vary the search area - JKM - Is this too many? # TODO: later try to see which of these yields the most hits find_cars_search_area = [ [380, 660, 2.5], # 0 [380, 656, 2.0], # 1 [380, 656, 1.5], # 2 [380, 550, 1.75], # 3 [380, 530, 1.25] ] # 4 # Image2 out_images2_list = [] match_list, conf_list, search_list = [], [], [] for ystart, ystop, scale in find_cars_search_area: if JKM_DEBUG: print(" \n Search for cars: ", ystart, ystop, scale) out2, match_l, conf_l, search_l = find_cars( image2, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, colorspace) out_images2_list.append(out2) match_list = match_list + match_l conf_list = conf_list + conf_l search_list = search_list + search_l # Draw the same thing again but with the box list match_list_img2 = draw_boxes(image2, match_list) plt.imshow(match_list_img2) # Draw all the search areas search_list_img2 = draw_boxes(image2, search_list) plt.imshow(search_list_img2) # use this to print out the images #plt.imshow(out_images2[0]) # Image1 aka Image # Image out_images1_list = [] match_list_1, conf_list_1, search_list_1 = [], [], [] out_images1 = [] for ystart, ystop, scale in find_cars_search_area: print(" \n Search for cars: ", ystart, ystop, scale) out1, match_l, conf_l, search_l = find_cars( image, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, colorspace) out_images1.append(out1) # match_list_1 = match_list_1 + match_l conf_list_1 = conf_list_1 + conf_l search_list_1 = search_list_1 + search_l #plt.imshow(out_images1[0]) # Draw the same thing again but with the box list match_list_img1 = draw_boxes(image, match_list_1) plt.imshow(match_list_img1) # Draw all the search areas search_list_img1 = draw_boxes(image, search_list_1) plt.imshow(search_list_img1) print("\n Step_3__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") ### ### This section for exploring heat maps. ### print( "\n Step 4 : Exploring Use of heat maps to remove duplicates & false positives." ) # Working with the same image - image2 from before # box_list = match_list heat = np.zeros_like(image2[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = add_heat(heat, box_list) # Apply threshold to help remove false positives FP_THRESH = 2 # 1 heat = apply_threshold(heat, FP_THRESH) # Visualize the heatmap when displaying heatmap = np.clip(heat, 0, 255) # Find final boxes from heatmap using label function labels = label(heatmap) draw_img = draw_labeled_bboxes(np.copy(image2), labels) fig = plt.figure() plt.subplot(121) plt.imshow(draw_img) plt.title('Car Positions') plt.subplot(122) plt.imshow(heatmap, cmap='hot') plt.title('Heat Map') fig.tight_layout() # 2nd image aka bbox image # Try the other image aka image # NOTE - issue here is that these are clumped together box_list = match_list_1 heat = np.zeros_like(image[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = add_heat(heat, box_list) # Apply threshold to help remove false positives FP_THRESH = 2 # 1 heat = apply_threshold(heat, FP_THRESH) # Visualize the heatmap when displaying heatmap = np.clip(heat, 0, 255) # Find final boxes from heatmap using label function labels = label(heatmap) # print(labels[1], 'cars found') draw_img = draw_labeled_bboxes(np.copy(image), labels) fig = plt.figure() plt.subplot(121) plt.imshow(draw_img) plt.title('Car Positions') plt.subplot(122) plt.imshow(heatmap, cmap='hot') plt.title('Heat Map') fig.tight_layout() print("\n Step_4__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") ### ### This section for exploring pipelines. ### print("\n Step 5 : Exploring Pipeline") # To test single invokation of pipeline fn = '../test_images/test4.jpg' results_test_output_dir = '../output_images/output_test_images/' out_fn = results_test_output_dir + 'result_' + os.path.basename(fn) init_carDetectPipeline() result_image = carDetectPipeline(image, out_fn=out_fn, interim=True, debug=True, video=False) plt.imshow(result_image) print("\n Step_5__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") print("\n Step 6 : Exploring Video") # To test pipeline on video # uncomment this to test with a short video # output_path = '../output_videos/test_video.mp4' # input_path = '../test_video.mp4' # Use this to do the assignment video output_path = '../output_videos/project_video.mp4' input_path = '../project_video.mp4' # Start here init_carDetectPipeline() input_clip = VideoFileClip(input_path) # #input_clip = VideoFileClip(input_path).subclip(40,43) # problematic part output_clip = input_clip.fl_image(carDetectPipeline) output_clip.write_videofile(output_path, audio=False) print("\n Step_6__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") print("\n Step 7 : Exploring Combined Video") # This is to try to merge the two results # Note: This doesn't work as well as I would like from advancedLaneLines import pipeline def combinedPipeline(image): image = pipeline(image) # advanced Lane Finding pipeline image = carDetectPipeline(image) # car detection Pipeline return image # uncomment this to test with a short video #output_path = '../output_videos/test_video.mp4' #input_path = '../test_video.mp4' # Use this to do the assignment video output_path = '../output_videos/combined_project_video.mp4' input_path = '../project_video.mp4' # Start here init_carDetectPipeline() input_clip = VideoFileClip(input_path) # #input_clip = VideoFileClip(input_path).subclip(40,43) # problematic part output_clip = input_clip.fl_image(combinedPipeline) output_clip.write_videofile(output_path, audio=False) print("\n Step_7__Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ")
def process_image(img, debug): print("max value: " + str(np.amax(img))) if debug['debug'] == 1: debug['id'] = "1-original-" debug['text'] = "original" util.save_image_debug(img, debug) # if np.amax(img) > 1: # img = img / 255 sliding_sale = [64, 128, 256] boxlist = [] heatmap = np.zeros_like(img[:, :, 0]).astype(np.float) heatmap_threshold = 2 counter = 0 for s in sliding_sale: print("sliding_sale: " + str(s)) crop_arr = slw.slide_window(img, x_start_stop=[None, None], y_start_stop=[300, None], xy_window=(s, s), xy_overlap=(0.5, 0.5)) counter = 0 for x in crop_arr: counter += 1 cropped_image = slw.crop_image(img, x) res = predict(cropped_image) if str(res[0]) == "vehicle": boxlist.append(x) counter += 1 print("boxes with cars found: " + str(counter)) #by now we have identified all the boxes that contain a car # now we add the boxes to a heatmap for b in boxlist: # heatmap[box[0][1]:box[1][1], box[0][0]:box[1][0]] += 1 heatmap[b[1]:b[3], b[0]:b[2]] += 1 if debug['debug'] == 1: debug['id'] = "2-all boxes -" debug['text'] = "all boxes" all_img = img.copy() all_boxes = ip.draw_labeled_bboxes(all_img, heatmap) util.save_image_debug(all_boxes, debug) if debug['debug'] == 1: debug['id'] = "3-heatmap -" debug['text'] = "heatmap" util.save_image_debug(heatmap, debug) heatmap[heatmap <= heatmap_threshold] = 0 if debug['debug'] == 1: debug['id'] = "4-threshold heatmap -" debug['text'] = "threshold heatmap" util.save_image_debug(heatmap, debug) res_image = ip.draw_labeled_bboxes(img, heatmap) if debug['debug'] == 1: debug['id'] = "5-final result -" debug['text'] = "final result" util.save_image_debug(res_image, debug) return res_image
new_width = int(iw*ratio) new_height = int(ih*ratio) print new_width print new_height original_img = cv2.imread(args.img) original_img = cv2.resize(original_img, (new_width, new_height)) img = cv2.cvtColor(original_img, cv2.cv.CV_BGR2GRAY) img = np.asarray(img).astype(np.float32) / 255. def judge(array, r, c, wsize): xd = np.asarray(array).reshape((1,784)).astype(np.float32) yd = forward(xd).data[0] prob = yd[1]/(yd[0]+yd[1]) threshold = 0.8 if(prob > threshold): print "------ detected (" + str(c) + "," + str(r) + ") P(" + str(prob) + " )-------------" cv2.rectangle(original_img, (c, r), (c+wsize, r+wsize), (0,255,0), 3) sw.slide_window(img, 28, 14, judge) cv2.imwrite('./result.png',original_img) cv2.imshow('img',original_img) cv2.waitKey(0) cv2.destroyAllWindows()