コード例 #1
0
def create_demo_images(camera):
    img = mpimg.imread('test_images/test1.jpg')
    calibration_demo(camera, 'camera_cal/calibration1.jpg',
                     "output_images/undistort.png")
    calibration_demo(camera, 'test_images/test1.jpg',
                     "output_images/undistort2.png")

    img = mpimg.imread('test_images/test1.jpg')
    img = process_image(img, camera)
    mpimg.imsave("output_images/unwarped.png", img)

    img = mpimg.imread('test_images/test3.jpg')
    undistorted = camera.undistort(img)
    thresholded = thresholding.threshold(undistorted)
    plot_side_by_side("Undistorted", undistorted, "Thresholded", thresholded,
                      "output_images/thresholded.png")
    warped = camera.warp(thresholded)
    lane_finding.find_lane_polynomials(warped,
                                       "output_images/lane_finding.png")

    img = mpimg.imread('test_images/straight_lines1.jpg')
    undistorted = camera.undistort(img)
    thresholded = thresholding.threshold(undistorted)
    warped = camera.warp(undistorted)
    cv2.polylines(undistorted, [np.int32(camera.warp_source_points)],
                  True, (0, 0, 255),
                  thickness=4)
    cv2.polylines(warped, [np.int32(camera.warp_target_points)],
                  True, (0, 0, 255),
                  thickness=4)
    plot_side_by_side("Undistorted", undistorted, "Warped", warped,
                      "output_images/warped.png")
コード例 #2
0
def main():
    from glob import glob
    from camera_calibration import CameraCalibration
    from thresholding import threshold
    from transform import PerspectiveTransform

    # Fixed image dimensions
    height = 720
    width = 1280

    # Prepare camera calibration
    print("Calibrating camera")
    calibration = CameraCalibration.default()

    # Prepare perspective transform
    transform = PerspectiveTransform.default(height, width)

    images = glob('test_images/straight*') + glob('test_images/test*')

    for fname in images:
        print("Processing", fname)

        # Run pipeline
        img = cv2.imread(fname)
        img = calibration.undistort(img)
        img, _ = threshold(img)
        # img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, (5,5))
        img = transform.transform(img)

        # Find lines using sliding windows
        left_lane, right_lane = find_lines_with_sliding_windows(img)

        # Plot sliding windows
        plot_sliding_windows(img, left_lane, right_lane)

        # combined_binary, color_binary = threshold(img, stack=True)
        #
        # f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(32, 9))
        # ax1.set_title('Stacked thresholds', fontsize=20)
        # ax1.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        #
        # ax2.set_title('Result', fontsize=20)
        # ax2.imshow(combined_binary, cmap='gray')

        plt.savefig('output_images/sliding_windows_' + fname.split('/')[-1])
コード例 #3
0
    def _process_image(self, org_image, t):
        # Create the warped binary image (birds-eye view with lane lines highlighted)
        undistorted_image = self.calibration.undistort(np.copy(org_image))
        warped_image = self.transform.transform(undistorted_image)
        warped_image, _ = threshold(warped_image, stack=False)

        # Update our lane tracker
        updated = self.lane.update(warped_image=warped_image)
        if self.debug and updated:
            # output the image for analysis
            bgr_image = cv2.cvtColor(org_image, cv2.COLOR_RGB2BGR)
            cv2.imwrite(
                'output_videos/invalid_frame_{:.3f}_original.png'.format(t),
                bgr_image)
            polygonned = cv2.polylines(np.copy(bgr_image),
                                       [np.int32(self.transform.src)],
                                       False,
                                       color=255,
                                       thickness=1)
            cv2.imwrite(
                'output_videos/invalid_frame_{:.3f}_framed.png'.format(t),
                polygonned)
            cv2.imwrite(
                'output_videos/invalid_frame_{:.3f}_warped.png'.format(t),
                np.dstack((warped_image, warped_image, warped_image)) * 255)

        # Add the lane overlay to our original image
        org_image = self.lane.overlay(org_image,
                                      transform=self.transform.invert())
        self.lane.overlay_text(org_image)

        # Create picture-in-picture overlays
        birds_eye_overlay = self.create_birds_eye_view(warped_image)
        binary_overlay = self.scale_image(
            np.dstack((warped_image, warped_image, warped_image)) * 255)

        # Overlay picture-in-picture style on original
        self.add_image_overlay(org_image, binary_overlay)
        self.add_image_overlay(org_image,
                               birds_eye_overlay,
                               offset=(0, org_image.shape[1] -
                                       birds_eye_overlay.shape[1]))

        return org_image
コード例 #4
0
def process_image(img, camera, lane_object=None):
    if lane_object is None:
        lane_object = Lane(img.shape)

    img = camera.undistort(img)
    undist = np.copy(img)

    img = thresholding.threshold(img)

    img = camera.warp(img)

    try:
        (left_poly, right_poly, left_fitx, right_fitx,
         ploty) = lane_finding.find_lane_polynomials(img)
        lane_object.update(left_fitx, right_fitx, ploty, left_poly, right_poly)
    except Exception as e:
        print('Exception:', e)
        lane_object.update_insane()

    return lane_drawing.draw_all(undist, lane_object, camera)
def svt(A,
        mask,
        tau=None,
        delta=None,
        epsilon=1e-2,
        epoch=1000,
        regularisation='MCP',
        _lambda=30,
        gamma=20):
    e = []
    (m, n) = A.shape
    Y = np.zeros((m, n))
    if not tau:
        tau = 5 * np.sum(A.shape) / 2
    if not delta:
        delta = 1.2 * (m * n) / np.sum(mask)

    for i in range(epoch):

        U, S, V = np.linalg.svd(Y, full_matrices=False)

        S_t = threshold(S, regularisation, _lambda, gamma)

        X = np.linalg.multi_dot([U, np.diag(S_t), V])

        Y = Y + delta * projection((A - X), mask)

        error = np.linalg.norm(projection(
            (X - A), mask)) / np.linalg.norm(projection(A, mask))
        obj = 0.5 * np.linalg.norm(projection(X - A, mask))**2 + penalty(
            S_t, regularisation, _lambda, gamma)
        e.append(error)
        if i % 10 == 0:
            print("Iteration: %i; Error: %.4f, Obj: %.4f" %
                  (i + 1, error, obj))
        if error < epsilon:
            break
    return X, e
コード例 #6
0
def main():
    # Fixed image dimensions
    height = 720
    width = 1280

    # Prepare camera calibration
    print("Calibrating camera")
    calibration = CameraCalibration.default()
    transform = PerspectiveTransform.default(height, width)

    images = glob('test_images/straight*') + glob('test_images/test*')

    for fname in images:
        print("Processing", fname)
        org_image = cv2.imread(fname)
        if org_image.shape != (height, width, 3):
            print("skipping image", fname, "invalid dimensions", org_image.shape)
            continue

        # Run the pipeline on a copy of the image
        undistorted = calibration.undistort(np.copy(org_image))
        transformed = transform.transform(undistorted)
        warped_binary, _ = threshold(transformed, stack=False)
        # combined, _ = threshold(undistorted, stack=False)
        # warped_binary = transform.transform(combined)

        lane = Lane()
        lane.update(warped_binary)
        final = lane.overlay(org_image, draw_lines=False, fill_lane=True, transform=transform.invert())
        final = lane.overlay_text(final)
        final = cv2.polylines(final, [np.int32(transform.src)], color=[255, 0, 0], isClosed=False)

        cv2.imwrite('output_images/lane_{}'.format(fname.split('/')[-1]), final)

        final = lane.overlay(np.dstack((warped_binary, warped_binary, warped_binary)) * 255, draw_lines=True,
                             fill_lane=False)
        cv2.imwrite('output_images/lane_warped_{}'.format(fname.split('/')[-1]), final)
コード例 #7
0
def run_pipeline():
    # 1. Compute the camera calibration matrix and distortion coefficients given a set of chessboard images.
    if CALIBRATE:
        camera_matrix, distortion_coeffs = run_calibration(
            "./camera_cal", VISUALIZE)
    else:  # use cached
        camera_matrix = np.array(
            [[1.15777942e+03, 0.00000000e+00, 6.67111050e+02],
             [0.00000000e+00, 1.15282305e+03, 3.86129068e+02],
             [0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
        distortion_coeffs = np.array(
            [[-0.24688833, -0.02372816, -0.00109843, 0.00035105, -0.00259134]])

    print("camera martix: {}".format(camera_matrix))
    print("distortion_coeffs: {}".format(distortion_coeffs))

    # open a video
    stream = cv2.VideoCapture('./challenge_video.mp4')
    while (stream.isOpened()):
        ret, frame = stream.read()
        if not ret:
            break

        # 2. Use color transforms, gradients, etc., to create a thresholded binary image.
        img_binary = threshold(frame, VISUALIZE)

        # 3. Apply a perspective transform to rectify binary image ("birds-eye view").

        # cv2.imshow('frame', frame)

        # Press Q on keyboard to  exit
        # if cv2.waitKey(25) & 0xFF == ord('q'):
        #     break

    stream.release()
    cv2.destroyAllWindows()
コード例 #8
0
name = clf.__class__.__name__

print("=" * 30)
print(name)

print("predicting test csv")
test_predictions_class = clf.predict(x_test)

test_predictions = clf.predict_proba(x_test)
RF_proba = RF_clf.predict_proba(x_test)
print(test_predictions)
low_certainty_list = find_low_certainty(test_ids, test_predictions_class,
                                        test_predictions)
submission = pd.DataFrame(test_predictions, columns=classes)

test_predictions_threshold = threshold(test_predictions, classes, RF_proba)
print("after threshold")
low_certainty_list = find_low_certainty(test_ids, test_predictions_class,
                                        test_predictions)
submission_threshold = pd.DataFrame(test_predictions_threshold,
                                    columns=classes)
submission_threshold.insert(0, 'id', test_ids)
submission_threshold.reset_index()

#Export Submission
submission_threshold.to_csv('submission_10_0test.csv', index=False)
#4.0 is with PCA AND STANDARDIZATION AND LOGs
#6.0 is with thresholding
#6.0 test is testing the thresholding
#8 is thresholding those >78%, 0.013 woo
#9 is RF_clf
コード例 #9
0
def process_img(image):
    undistort = cal_undistort(image, mtx, dist)
    #warp
    warped = unwarp(undistort, perspective_M, src, dest)
    #threshold
    binary_warped = threshold(warped)
    #binary_warped = binary_warped * 255
    #    plt.imshow(binary_warped, cmap='gray')
    #    plt.show()

    # Read in a thresholded image
    warped = binary_warped
    # window settings
    window_width = 50
    window_height = 80  # Break image into 9 vertical layers since image height is 720
    margin = 100  # How much to slide left and right for searching
    window_centroids = find_window_centroids(warped, window_width,
                                             window_height, margin)

    # If we found any window centers
    if len(window_centroids) > 0:

        # Points used to draw all the left and right windows
        l_points = np.zeros_like(warped)
        r_points = np.zeros_like(warped)

        # Go through each level and draw the windows
        for level in range(0, len(window_centroids)):
            # Window_mask is a function to draw window areas
            l_mask = window_mask(window_width, window_height, warped,
                                 window_centroids[level][0], level)
            r_mask = window_mask(window_width, window_height, warped,
                                 window_centroids[level][1], level)
            # Add graphic points from window mask here to total pixels found
            l_points[(l_points == 255) | ((l_mask == 1))] = 255
            r_points[(r_points == 255) | ((r_mask == 1))] = 255

        # Draw the results
        template = np.array(
            r_points + l_points,
            np.uint8)  # add both left and right window pixels together
        zero_channel = np.zeros_like(template)  # create a zero color channel
        template = np.array(cv2.merge((zero_channel, template, zero_channel)),
                            np.uint8)  # make window pixels green

        warpage = np.array(
            cv2.merge((warped, warped, warped)),
            np.uint8)  # making the original road pixels 3 color channels

        output = cv2.addWeighted(
            warpage, 1, template, 0.5,
            0.0)  # overlay the orignal road image with window results

    # If no window centers found, just display orginal road image
    else:
        output = np.array(cv2.merge((warped, warped, warped)), np.uint8)

    # Display the final results


#    plt.imshow(output)
#    plt.title('window fitting results')
#    plt.show()

#Fit a polygon
    ploty = np.linspace(0, 719, num=720)
    leftx = np.zeros_like(ploty)
    rightx = np.zeros_like(ploty)
    for level in range((int)(len(leftx) / window_height)):
        leftx[level * window_height:(level + 1) *
              window_height] = window_centroids[level][0]
        rightx[level * window_height:(level + 1) *
               window_height] = window_centroids[level][1]

    lane_center = leftx[0] + (rightx[0] - leftx[0]) / 2
    img_center = 1280 / 2

    bottom_dist = rightx[0] - leftx[0]
    top_dist = rightx[-1] - leftx[-1]

    #number of pixels off center
    off_center_in_pixels = lane_center - img_center

    leftx = leftx[::-1]  # Reverse to match top-to-bottom in y
    rightx = rightx[::-1]  # Reverse to match top-to-bottom in y

    xm_per_pix = 3.7 / 700
    off_center_in_meters = off_center_in_pixels * xm_per_pix
    bottom_dist_metres = bottom_dist * xm_per_pix
    top_dist_metres = top_dist * xm_per_pix

    avg_dist = np.mean(rightx - leftx) * xm_per_pix

    print("Average distance in meters: ", np.mean(rightx - leftx) * xm_per_pix)
    left_fit = np.polyfit(ploty, leftx, 2)
    right_fit = np.polyfit(ploty, rightx, 2)

    if (Prev_result[0] != None
            and (bottom_dist_metres < 3.85 or bottom_dist_metres > 4.15)):
        left_fit, right_fit = Prev_result[0]
    else:
        Prev_result[0] = (left_fit, right_fit)
    left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2]
    right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2]

    #   mark_size = 3
    #   plt.plot(leftx, ploty, 'o', color='red', markersize=mark_size)
    #   plt.plot(rightx, ploty, 'o', color='blue', markersize=mark_size)
    #   plt.xlim(0, 1280)
    #   plt.ylim(0, 720)
    #   plt.plot(left_fitx, ploty, color='green', linewidth=3)
    #   plt.plot(right_fitx, ploty, color='green', linewidth=3)
    #   plt.gca().invert_yaxis()
    #   plt.show()

    y_eval = np.max(ploty)
    left_curverad = ((1 + (2 * left_fit[0] * y_eval + left_fit[1])**2)**
                     1.5) / np.absolute(2 * left_fit[0])
    right_curverad = ((1 + (2 * right_fit[0] * y_eval + right_fit[1])**2)**
                      1.5) / np.absolute(2 * right_fit[0])
    print(left_curverad, right_curverad)

    # Define conversions in x and y from pixels space to meters
    ym_per_pix = 30 / 720  # meters per pixel in y dimension
    xm_per_pix = 3.7 / 700  # meters per pixel in x dimension

    # Fit new polynomials to x,y in world space
    left_fit_cr = np.polyfit(ploty * ym_per_pix, leftx * xm_per_pix, 2)
    right_fit_cr = np.polyfit(ploty * ym_per_pix, rightx * xm_per_pix, 2)
    # Calculate the new radii of curvature
    left_curverad = (
        (1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)**
        1.5) / np.absolute(2 * left_fit_cr[0])
    right_curverad = (
        (1 + (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2)
        **1.5) / np.absolute(2 * right_fit_cr[0])
    # Now our radius of curvature is in meters
    print(left_curverad, 'm', right_curverad, 'm')

    #Draw image
    # Create an image to draw the lines on
    warp_zero = np.zeros_like(warped).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
    pts_right = np.array(
        [np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0))
    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, Minv,
                                  (image.shape[1], image.shape[0]))
    # Combine the result with the original image
    result = cv2.addWeighted(undistort, 1, newwarp, 0.3, 0)
    if (off_center_in_meters > 0):
        result = cv2.putText(
            result,
            'Vehicle is: ' + str(off_center_in_meters) + ' left of center',
            (500, 30), font, 1, (255, 255, 255), 2)
    else:
        result = cv2.putText(
            result, 'Vehicle is: ' + str(abs(off_center_in_meters)) +
            ' right of center', (500, 30), font, 1, (255, 255, 255), 2)

    result = cv2.putText(result, 'left curvature: ' + str(left_curverad),
                         (500, 60), font, 1, (255, 255, 255), 2)
    result = cv2.putText(result, 'Right curvature: ' + str(right_curverad),
                         (500, 90), font, 1, (255, 255, 255), 2)
    #    result = cv2.putText(result,'bottom dist: ' + str(bottom_dist_metres), (500,120), font, 1,(255,255,255),2)
    #    result = cv2.putText(result,'bottom dist: ' + str(top_dist_metres), (500,150), font, 1,(255,255,255),2)

    return result
コード例 #10
0
import matplotlib.image as mpimg
import glob
import cv2
from undistort import cal_undistort, read_calibration_params
from thresholding import pipeline as threshold
from transform import unwarp, read_unwarp_params

image = mpimg.imread('test_images/test3.jpg')
#undistort
mtx, dist = read_calibration_params()
undistort = cal_undistort(image, mtx, dist)
#warp
perspective_M, Minv, src, dest = read_unwarp_params()
warped = unwarp(undistort, perspective_M, src, dest)
#threshold
binary_warped = threshold(warped)
binary_warped = binary_warped * 255
plt.imshow(binary_warped, cmap='gray')
plt.show()


def window_mask(width, height, img_ref, center, level):
    output = np.zeros_like(img_ref)
    output[int(img_ref.shape[0] - (level + 1) * height):int(img_ref.shape[0] -
                                                            level * height),
           max(0, int(center -
                      width / 2)):min(int(center +
                                          width / 2), img_ref.shape[1])] = 1
    return output

コード例 #11
0
ファイル: testreshold.py プロジェクト: afifai/python_tutorial
from PIL import Image
import numpy as np

import matplotlib.pyplot as plt
from thresholding import threshold

i = Image.open('images/numbers/0.1.png')
iar = np.array(i)
i2 = Image.open('images/numbers/y0.4.png')
iar2 = np.array(i2)
i3 = Image.open('images/numbers/y0.5.png')
iar3 = np.array(i3)
i4 = Image.open('images/facebook.png')
iar4 = np.array(i4)

iar = threshold(iar)
iar2 = threshold(iar2)
iar3 = threshold(iar3)
iar4 = threshold(iar4)

fig = plt.figure()
ax1 = plt.subplot2grid((8,6),(0,0), rowspan=4, colspan=3)
ax2 = plt.subplot2grid((8,6),(4,0), rowspan=4, colspan=3)
ax3 = plt.subplot2grid((8,6),(0,3), rowspan=4, colspan=3)
ax4 = plt.subplot2grid((8,6),(4,3), rowspan=4, colspan=3)

ax1.imshow(iar)
ax2.imshow(iar2)
ax3.imshow(iar3)
ax4.imshow(iar4)