Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def process_frame(image):
    global trained_clf
    global scaler

    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    draw_image = image.copy()

    windows = slide_window(image,
                           x_start_stop=[None, None],
                           y_start_stop=[360, 700],
                           xy_window=(64, 64),
                           xy_overlap=(0.85, 0.85))

    logging.info("Searching hot windows using classifier")
    hot_windows = search_windows(image, windows, trained_clf, scaler)

    logging.info("Drawing the hot image")
    window_img = draw_boxes(draw_image,
                            hot_windows,
                            color=(0, 0, 255),
                            thick=6)

    #plt.imshow(window_img)
    #plt.show()

    return car_positions(image, hot_windows)
Ejemplo n.º 3
0
def test_sliding_window(image):
    windows = slide_window(image,
                           x_start_stop=[None, None],
                           y_start_stop=Y_START_STOP,
                           xy_window=(128, 128),
                           xy_overlap=(0.5, 0.5))

    window_img = draw_boxes(image, windows, color=(0, 0, 255), thick=6)
    plt.imshow(window_img)
Ejemplo n.º 4
0
def set_windows(img):
    '''
    Set sliding windows
    '''
    windows_list = []
    windows = slide_window(img, x_start_stop=[None, None], y_start_stop=[384, 640],
                  xy_window=(160, 128), xy_overlap=(0.5, 0.5))
    windows_list.extend(windows)
    windows = slide_window(img, x_start_stop=[None, None], y_start_stop=[416, 560],
                 xy_window=(128, 96), xy_overlap=(0.5, 0.5))
    windows_list.extend(windows)
    windows = slide_window(img, x_start_stop=[256, 1024], y_start_stop=[410, 570],
                  xy_window=(96, 64), xy_overlap=(0.5, 0.5))
    windows_list.extend(windows)
    windows = slide_window(img, x_start_stop=[320, 960], y_start_stop=[416, 512],
                 xy_window=(64, 48), xy_overlap=(0., 0.5))
    windows_list.extend(windows)

    return windows_list
Ejemplo n.º 5
0
    def detect(self, image, do_hog_once=True):
        out_windows = []
        if do_hog_once:
            out_windows = self.find_multiscale(image)
            heatmap = utils.get_heatmap(image, out_windows)
            heatmap = self.smoother(heatmap)
            heatmap = utils.apply_threshold(heatmap,3)
            boxes = utils.get_labeled_boxes(heatmap)
        else:
            windows = utils.slide_window(image, x_start_stop=[None, None], y_start_stop=self.y_start_stop,
                        xy_window=(64, 64), xy_overlap=(0.85, 0.85))
            boxes = self.search_windows(image, windows)

        final_image = utils.draw_boxes(image, boxes, color=(0, 0, 255), thick=6)
        return final_image
Ejemplo n.º 6
0
    images = []
    titles = []
    y_start_stop = [None, None]
    overlap = 0.5

    for img_src in example_images:
        t1 = time.time()
        img = mpimg.imread(img_src)
        draw_img = np.copy(img)
        img = img.astype(np.float32) / 255
        print(np.min(img), np.max(img))

        windows = slide_window(img,
                               x_start_stop=[None, None],
                               y_start_stop=y_start_stop,
                               xy_window=(128, 128),
                               xy_overlap=(overlap, overlap))

        hot_windows = find_cars_in_windows(img,
                                           windows,
                                           svc,
                                           X_scaler,
                                           color_space=color_space,
                                           spatial_size=spatial_size,
                                           hist_bins=hist_bins,
                                           hist_range=(0, 256),
                                           orient=orient,
                                           pix_per_cell=pix_per_cell,
                                           cell_per_block=cell_per_block,
                                           hog_channel=hog_channel,
Ejemplo n.º 7
0
    def sliding_window_search(self, image):
        try:
            start = time.time()
            # 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=self.y_start_stop,
                                   xy_window=self.xy_window,
                                   xy_overlap=self.xy_overlap)

            hot_windows = search_windows(image,
                                         windows,
                                         self.svc,
                                         self.X_scaler,
                                         color_space=self.color_space,
                                         spatial_size=self.spatial_size,
                                         hist_bins=self.hist_bins,
                                         orient=self.orient,
                                         pix_per_cell=self.pix_per_cell,
                                         cell_per_block=self.cell_per_block,
                                         hog_channel=self.hog_channel,
                                         spatial_feat=self.spatial_feat,
                                         hist_feat=self.hist_feat,
                                         hog_feat=self.hog_feat)
            end = time.time()
            self.windows = windows
            self.hot_windows = hot_windows
            draw_image = np.copy(image)
            msg = '%04d | Memory: %dFr | HeatTh: RollSum %d * CurFr %d | Accuracy: %0.1f%%' % (
                self.count, MEMORY_SIZE, ROLLING_SUM_HEAT_THRESHOLD,
                CURRENT_FRAME_HEAT_THRESHOLD, ACCURACY / 100)

            self.update_overlay(draw_image)
            draw_image = weighted_img(draw_image, self.overlay)
            put_text(draw_image, msg)

            heat_thresholded_image, thresholded_heatmap, labels = self.heat_and_threshold(
                draw_image,
                self.hot_windows,
                rolling_threshold=ROLLING_SUM_HEAT_THRESHOLD,
                current_threshold=CURRENT_FRAME_HEAT_THRESHOLD)
            self.save = heat_thresholded_image

        except Exception as e:
            mpimg.imsave('hard/%d.jpg' % self.count, image)
            debug('Error(%s): Issue at Frame %d' % (str(e), self.count))
            if DEBUG:
                import ipdb
                ipdb.set_trace()
            if self.save:
                heat_thresholded_image = self.save

        finally:
            self.count += 1

        if VIDEO_MODE:
            # Scale Back to Format acceptable by moviepy
            heat_thresholded_image = heat_thresholded_image.astype(
                np.float32) * 255
        else:
            debug('%0.1f seconds/frame. #%d/%d hot-windows/windows/frame' %
                  (end - start, len(hot_windows), len(windows)))
            title1 = 'Car Positions (#Detections: %d)' % (labels[1])
            title2 = 'Thresholded Heat Map (Max: %d)' % int(
                np.max(thresholded_heatmap))
            imcompare(heat_thresholded_image,
                      thresholded_heatmap,
                      title1,
                      title2,
                      cmap2='hot')

        return heat_thresholded_image
Ejemplo n.º 8
0
            fig = plt.gcf()
            fig.savefig('output_images/search_window_' +
                        str(param['window_size'][0]) + '.jpg')


# In[8]:

# DEPRACATED - using HOG subsampling for efficiency
if False:
    draw_image = np.copy(test_img)
    windows = []
    for size in window_sizes:
        windows.extend(
            slide_window(test_img,
                         x_start_stop=[None, None],
                         y_start_stop=[400, 700],
                         xy_window=(96, 96),
                         xy_overlap=(0.75, 0.75)))

    hot_windows = search_windows(test_img, windows, svc, X_scaler, config)
    window_img = draw_boxes(draw_image,
                            hot_windows,
                            color=(0, 0, 255),
                            thick=6)

    plt.imshow(test_img)
    plt.figure()
    plt.imshow(window_img)

# In[9]:
Ejemplo n.º 9
0
def pipeline(img):
    global svc, color_space, orient, pix_per_cell, cell_per_block, hog_channel, spatial_size, hist_bins, X_scaler
    global smoothing_enabled

    sub_sample = True

    draw_img = np.copy(img)
    draw_img2 = np.copy(img)
    heatmap = np.zeros_like(img[:, :, 0])

    img = img.astype(np.float32) / 255

    if (sub_sample == True):
        hot_windows = []
        for i in range(len(scales)):
            hot_windows_for_scale = find_cars_in_image(
                img,
                scales[i]['ystart'],
                scales[i]['ystop'],
                svc,
                X_scaler,
                scale=scales[i]['scale'],
                orient=orient,
                pix_per_cell=pix_per_cell,
                cell_per_block=cell_per_block,
                spatial_size=spatial_size,
                hist_bins=hist_bins,
                color_space=color_space)
            hot_windows = hot_windows + hot_windows_for_scale
    else:
        windows = slide_window(img,
                               x_start_stop=[None, None],
                               y_start_stop=y_start_stop,
                               xy_window=(64, 64),
                               xy_overlap=(overlap, overlap))

        hot_windows = find_cars_in_windows(img,
                                           windows,
                                           svc,
                                           X_scaler,
                                           color_space=color_space,
                                           spatial_size=spatial_size,
                                           hist_bins=hist_bins,
                                           hist_range=(0, 256),
                                           orient=orient,
                                           pix_per_cell=pix_per_cell,
                                           cell_per_block=cell_per_block,
                                           hog_channel=hog_channel,
                                           spatial_feat=True,
                                           hist_feat=True,
                                           hog_feat=True)

    img_hot_windows = draw_boxes(draw_img,
                                 hot_windows,
                                 color=(0, 0, 255),
                                 thick=6)

    heatmap = add_heat(heatmap, hot_windows)
    heatmap = apply_threshold(heatmap, 1)

    if (smoothing_enabled == True):
        if (len(heatmap_buffer) >= HEATMAP_BUFFER_SIZE):
            del heatmap_buffer[0]

        heatmap_buffer.append(heatmap)

        smooth_heatmap = np.zeros_like(img[:, :, 0])
        total_weights = 0
        for i in range(len(heatmap_buffer)):
            smooth_heatmap += ((i + 1) * heatmap_buffer[i])
            total_weights += (i + 1)
        smooth_heatmap = smooth_heatmap / total_weights

        car_windows = get_labeled_bboxes(smooth_heatmap)
    else:
        car_windows = get_labeled_bboxes(heatmap)

    img_detected_cars = draw_boxes(draw_img2,
                                   car_windows,
                                   color=(0, 0, 255),
                                   thick=6)

    return img_hot_windows, heatmap, img_detected_cars, len(hot_windows), len(
        car_windows)