Example #1
0
def sliding_window_main():
    """
    This function demonstrates how to calcualte sliding windows for a given image.
    :return: it doesn't return anything if everything works perfectly
    """
    image = mpimg.imread('util_images/bbox-example-image.png')
    windows_list = []
    x_start_stop_list = [[50, 400], [400, 750], [750, 1200]]
    y_start_stop_list = [(420, 700), (400, 600), (420, 700)]
    xy_window_list = [(128, 128), (64, 64), (128, 128)]
    overlapx = 0.75
    overlapy = 0.75

    for y_start_stop, xy, x_start_stop in zip(y_start_stop_list,
                                              xy_window_list,
                                              x_start_stop_list):
        windows_list += object_detection_utils.slide_window(
            image,
            x_start_stop=x_start_stop,
            y_start_stop=y_start_stop,
            xy_window=xy,
            xy_overlap=(overlapx, overlapy))
    window_img = object_detection_utils.draw_boxes(image,
                                                   windows_list,
                                                   color=(0, 0, 255),
                                                   thick=6)
    plt.imshow(window_img)
def interpret_results_coco(output_details, im_resized, filepath):
    boxes = interpreter.get_tensor(output_details[0]['index'])[0]
    classes = interpreter.get_tensor(output_details[1]['index'])[0]
    scores = interpreter.get_tensor(output_details[2]['index'])[0]
    class_names = [coco_labels[i] for i in classes.astype(int)]
    print(list(zip(class_names, scores)))
    altered_image_path = os.path.join(ALTERED_IMAGE_OUTPUT,
                                      os.path.basename(filepath))
    altered_image_array = draw_boxes(im_resized, boxes, class_names, scores)
    altered_image = Image.fromarray(altered_image_array)
    altered_image.save(altered_image_path)
Example #3
0
def main_drawboxes():
    """
    this function shows how drawing rectangles around cars work
    :return:
    """
    image = mpimg.imread('util_images/bbox-example-image.png')

    templist = [
        'util_images/cutout1.png', 'util_images/cutout2.png',
        'util_images/cutout3.png', 'util_images/cutout4.png',
        'util_images/cutout5.png', 'util_images/cutout6.png'
    ]
    coordinates_boxes = find_matches(image, templist)
    result = object_detection_utils.draw_boxes(image, coordinates_boxes)
    plt.imshow(result)
def interpret_results_custom(output_details, image, filepath, threshold,
                             label_map):
    boxes = interpreter.get_tensor(output_details[0]['index'])[0]
    classes = interpreter.get_tensor(output_details[1]['index'])[0]
    scores = interpreter.get_tensor(output_details[2]['index'])[0]
    class_names = [label_map[i] for i in classes.astype(int)]
    aggregated_results = list(zip(class_names, scores))
    aggregated_meaningful_results = [
        res for res in aggregated_results if res[1] > threshold
    ]
    altered_image_path = os.path.join(ALTERED_IMAGE_OUTPUT,
                                      os.path.basename(filepath))
    altered_image_array = draw_boxes(image,
                                     boxes,
                                     class_names,
                                     scores,
                                     max_boxes=10,
                                     min_score=threshold)
    altered_image = Image.fromarray(altered_image_array)
    altered_image.save(altered_image_path)
    return aggregated_meaningful_results
Example #5
0
def main_search_and_classify(model=None,
                             image=None,
                             image_file='util_images/bbox-example-image.png',
                             use_heatmap=False,
                             display_results=True,
                             method="both",
                             video_optimization=False):
    """
    TODO: write a description of this function
    :return:
    """
    # Read in cars and notcars
    # images = glob.glob('test_images//*.jpeg')
    svc = model["linearSVC"]
    X_scaler = model["X_scaler"]
    color_space = model["color_item"]
    orient = model["orient"]
    pix_per_cell = model["pix_per_cell"]
    cell_per_block = model["cells_per_block"]
    spatial_size = (32, 32)  # Spatial binning dimensions
    hist_bins = 2 * pix_per_cell  # Number of histogram bins
    hog_channel = model["hog_channel"]
    spatial_feat = model["spatial_feat"]  # Spatial features on or off
    hist_feat = model["hist_feat"]  # Histogram features on or off
    hog_feat = model["hog_feat"]  # HOG features on or off

    if image_file is not None:
        image = mpimg.imread(image_file)
    # draw_image = np.copy(image)
    draw_image = 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

    hotty_windows = []

    if method in ["both", "sliding_windows"]:
        windows_list = []
        x_start_stop_list = [[0, 400], [410, 850], [860, None]]
        y_start_stop_list = [(450, 70), (420, 500), (450, 700)]
        xy_window_list = [(128, 128), (64, 64), (128, 128)]

        if video_optimization:
            x_start_stop_list = [[800, 1280], [800, 1280]]
            y_start_stop_list = [(400, 550), (350, 650)]
            xy_window_list = [(64, 64), (128, 128)]
        else:
            x_start_stop_list = [[50, 1280], [50, 1280], [860, 1200]]
            y_start_stop_list = [(410, 450), (400, 600), (500, 700)]
            xy_window_list = [(32, 32), (64, 64), (128, 128)]
        overlapx = 0.75
        overlapy = 0.75
        overlap_list = [(overlapx, overlapy), (overlapx, overlapy),
                        (overlapx, overlapy)]

        for x, y, xy, xy_overlap in zip(x_start_stop_list, y_start_stop_list,
                                        xy_window_list, overlap_list):
            windows_list += (object_detection_utils.slide_window(
                image,
                x_start_stop=x,
                y_start_stop=y,
                xy_window=xy,
                xy_overlap=xy_overlap))
        hot_windows = search_windows(image,
                                     windows_list,
                                     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)
        hotty_windows = hot_windows

    if method in ["both", "hot_windows"]:
        xstart_list = [200, 200, 1000, 400, 400]
        xstop_list = [1280, 900, 1280, 800, 1280]
        ystart_list = [420, 420, 450, 450, 500]
        ystop_list = [600, 650, 600, 600, 720]
        scale_list = [1.5, 2.0, 1, 2, 3]
        rectangles = []
        for ystart, ystop, scale, xstart, xstop in zip(ystart_list, ystop_list,
                                                       scale_list, xstart_list,
                                                       xstop_list):
            rectangles += \
                object_detection_hog_subsampling.find_cars(image, ystart, ystop, scale, color_space,
                                                           hog_channel, svc, X_scaler, orient, pix_per_cell,
                                                           cell_per_block,spatial_size, hist_bins, xstart, xstop)
        hotty_windows += rectangles

    if use_heatmap:
        heat = np.zeros_like(image[:, :, 0]).astype(np.float)
        # Add heat to each box in box list
        heat = object_detection_heatmap.add_heat(heat, hotty_windows)
        # Apply threshold to help remove false positives
        if video_optimization:
            heat = object_detection_heatmap.apply_threshold(heat, 3)
        else:
            heat = object_detection_heatmap.apply_threshold(heat, 4)
        # Visualize the heatmap when displaying
        #heatmap = np.clip(heat, 0, 255)
        heatmap = np.clip(heat, 0, 1)
        # Find final boxes from heatmap using label function
        labels = object_detection_heatmap.label(heatmap)
        #draw_img = object_detection_heatmap.draw_labeled_bboxes(np.copy(image), labels)
        draw_img = object_detection_heatmap.draw_labeled_bboxes(image, labels)
        if display_results:
            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()
    else:
        draw_img = object_detection_utils.draw_boxes(image,
                                                     hotty_windows,
                                                     color=(0, 0, 255),
                                                     thick=6)
        if display_results:
            fig = plt.figure()
            plt.subplot(211)
            plt.imshow(draw_img)
            plt.title('Identified objects')
    return draw_img
Example #6
0
def main_detectcars():
    """
    TODO: write a description
    :return:
    """
    # Read in cars and notcars
    images = glob.glob('*.jpeg')
    cars = []
    notcars = []
    for image in images:
        if 'image' in image or 'extra' in image:
            notcars.append(image)
        else:
            cars.append(image)

    # Reduce the sample size because
    # The quiz evaluator times out after 13s of CPU time
    sample_size = 500
    cars = cars[0:sample_size]
    notcars = notcars[0:sample_size]

    ### TODO: Tweak these parameters and see how the results change.
    color_space = 'RGB'  # 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 = 0  # Can be 0, 1, 2, or "ALL"
    spatial_size = (16, 16)  # Spatial binning dimensions
    hist_bins = 16  # 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()

    car_features = extract_features(cars,
                                    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)
    notcar_features = extract_features(notcars,
                                       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)

    # Create an array stack of feature vectors
    X = np.vstack((car_features, notcar_features)).astype(np.float64)

    # Define the labels vector
    y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))

    # Split up data into randomized training and test sets
    rand_state = np.random.randint(0, 100)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=rand_state)

    # Fit a per-column scaler
    X_scaler = StandardScaler().fit(X_train)
    # Apply the scaler to X
    X_train = X_scaler.transform(X_train)
    X_test = X_scaler.transform(X_test)

    print('Using:', orient, 'orientations', pix_per_cell,
          'pixels per cell and', cell_per_block, 'cells per block')
    print('Feature vector length:', len(X_train[0]))
    # Use a linear SVC
    svc = LinearSVC()
    # Check the training time for the SVC
    t = time.time()
    svc.fit(X_train, y_train)
    t2 = time.time()
    print(round(t2 - t, 2), 'Seconds to train SVC...')
    # Check the score of the SVC
    print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
    # Check the prediction time for a single sample
    t = time.time()

    image = mpimg.imread('util_images/bbox-example-image.png')
    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 = object_detection_utils.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,
                                 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 = object_detection_utils.draw_boxes(draw_image,
                                                   hot_windows,
                                                   color=(0, 0, 255),
                                                   thick=6)

    plt.imshow(window_img)
Example #7
0
def main_hog_subsampling(model, img_file="test_images/test1.png"):
    """
    TODO: deliver description of this function
    :return: nothing is return; if function worked correctly it displays an image
    """
    svc = model['linearSVC']
    orient = model["orient"]
    pix_per_cell = model["pix_per_cell"]
    cells_per_block = model["cell_per_block"]
    hist_bins = model["hist_bins"]
    hog_channels = model['hog_channel']
    color_item = model['color_item']
    X_scaler = model['X_scaler']
    spatial_size = (32, 32)

    object_detection_hog_features.print_parameters(color_item, orient,
                                                   pix_per_cell,
                                                   cells_per_block,
                                                   hog_channels)

    test_img = mpimg.imread(img_file)

    rectangles0 = []
    ystart = 400
    ystop = 600
    scale = 1.5
    xstart = 0
    xstop = None
    rectangles0 = find_cars(test_img, ystart, ystop, scale, color_item,
                            hog_channels, svc, X_scaler, orient, pix_per_cell,
                            cells_per_block, spatial_size, hist_bins, xstart,
                            xstop)

    rectangles = []
    ystart = 500
    ystop = 720
    scale = 3
    xstart = 800
    xstop = None
    rectangles = find_cars(test_img[:, :, :], ystart, ystop, scale, color_item,
                           hog_channels, svc, X_scaler, orient, pix_per_cell,
                           cells_per_block, spatial_size, hist_bins, xstart,
                           xstop)
    rectangles1 = []
    ystart = 450
    ystop = 600
    scale = 2
    xstart = 0
    xstop = 200
    rectangles1 = find_cars(test_img, ystart, ystop, scale, color_item,
                            hog_channels, svc, X_scaler, orient, pix_per_cell,
                            cells_per_block, spatial_size, hist_bins, xstart,
                            xstop)

    rectangles2 = []
    ystart = 450
    ystop = 600
    scale = 1
    xstart = 1000
    xstop = None
    rectangles2 = find_cars(test_img, ystart, ystop, scale, color_item,
                            hog_channels, svc, X_scaler, orient, pix_per_cell,
                            cells_per_block, spatial_size, hist_bins, xstart,
                            xstop)

    rectangles3 = []
    ystart = 400
    ystop = 650
    scale = 2
    xstart = 100
    xstop = 900
    rectangles3 = find_cars(test_img, ystart, ystop, scale, color_item,
                            hog_channels, svc, X_scaler, orient, pix_per_cell,
                            cells_per_block, spatial_size, hist_bins, xstart,
                            xstop)

    display_rectangles = rectangles + rectangles0 + rectangles1 + rectangles2 + rectangles3
    #display_rectangles = rectangles0 + rectangles
    print("Number of rectangles: {}".format(len(rectangles0)))
    print("Number of rectangles: {}".format(len(rectangles)))
    print("Number of rectangles: {}".format(len(rectangles1)))
    print("Number of rectangles: {}".format(len(rectangles2)))
    print("Number of rectangles: {}".format(len(rectangles3)))
    pic = object_detection_utils.draw_boxes(cv2.imread(img_file),
                                            display_rectangles)
    # you can replace cv2.imread(...) with mpimg.imread(img_file)
    # but you will need to comment out the line below
    pic = cv2.cvtColor(pic, cv2.COLOR_RGB2BGR)
    plt.imshow(pic)