def main_image(): """ this is the function that processes single image pointed by a name mentioned below """ # orig_image_name = "straight_lines1.jpg" orig_image_name = "test3.jpg" file_distortion_corrected = "output_images/lines_undist_{}".format(orig_image_name) file_to_process = "output_images/warped_lines_undist_{}".format(orig_image_name) original = cv2.imread("test_images/{}".format(orig_image_name)) original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) undistorted = cv2.imread(file_distortion_corrected) undistorted = cv2.cvtColor(undistorted, cv2.COLOR_BGR2RGB) warped, _, _, minv = perspective_transform(file_distortion_corrected) result, output = search_for_lines(file_to_process) curvature_output = \ determine_lane_curvature(output['left_lane_inds'], output['right_lane_inds'], output['nonzerox'], output['nonzeroy']) print(curvature_output) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) result = draw_lane(original, undistorted, warped, output['left_fitx'], output['right_fitx'], output['ploty'], minv) avg_curve = (curvature_output['left_curverad'] + curvature_output['right_curverad']) / 2 label_curve = 'Radius of curvature: %.1f m' % avg_curve result = cv2.putText(result, label_curve, (30, 40), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) display_two_images(original, "Original Image", result, "Drawn Lane")
def draw_lane_pipeline(): """ this is the function that processes single image pointed by a name mentioned below """ # orig_image_name = "straight_lines1.jpg" files_to_process = glob("test_images/*.jpg") for file in files_to_process: file_distortion_corrected = "output_images/lines_undist_{}".format(file.split('\\')[-1]) file_to_process = "output_images/warped_lines_undist_{}".format(file.split('\\')[-1]) original = cv2.imread(file) original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) undistorted = cv2.imread(file_distortion_corrected) undistorted = cv2.cvtColor(undistorted, cv2.COLOR_BGR2RGB) warped, _, _, minv = perspective_transform(file_distortion_corrected) result, output = search_for_lines(file_to_process) curvature_output = \ determine_lane_curvature(output['left_lane_inds'], output['right_lane_inds'], output['nonzerox'], output['nonzeroy']) print(curvature_output) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) result = draw_lane(original, undistorted, warped, output['left_fitx'], output['right_fitx'], output['ploty'], minv) avg_curve = (curvature_output['left_curverad'] + curvature_output['right_curverad']) / 2 label_curve = 'Radius of curvature: %.1f m' % avg_curve result = cv2.putText(result, label_curve, (30, 40), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) file_to_write = 'output_images/annotated_'+file.split('\\')[-1] cv2.imwrite(file_to_write, result)
def draw_lane_pipeline(files=None, display_images=False): """ this is the function that processes single image pointed by a name mentioned below """ # orig_image_name = "straight_lines1.jpg" if files is None: files_to_process = glob("test_images/*.jpg") else: files_to_process = list() files_to_process.append(files) for file in files_to_process: if "\\" in file: file_distortion_corrected = "output_images/lines_undist_{}".format( file.split('\\')[-1]) file_to_process = "output_images/warped_lines_undist_{}".format( file.split('\\')[-1]) else: file_distortion_corrected = "output_images/lines_undist_{}".format( file.split('/')[-1]) file_to_process = "output_images/warped_lines_undist_{}".format( file.split('/')[-1]) original = cv2.imread(file) original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) undistorted = cv2.imread(file_distortion_corrected) undistorted = cv2.cvtColor(undistorted, cv2.COLOR_BGR2RGB) warped, _, _, minv = perspective_transform(file_distortion_corrected) result, output = search_for_lines(file_to_process) # Calculate vehicle center vehicleposition_msg = vehicle_position(original, output) curvature_output = \ determine_lane_curvature(output['left_lane_inds'], output['right_lane_inds'], output['nonzerox'], output['nonzeroy']) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) result = draw_lane(original, undistorted, warped, output['left_fitx'], output['right_fitx'], output['ploty'], minv) avg_curve = (curvature_output['left_curverad'] + curvature_output['right_curverad']) / 2 label_curve = 'Radius of curvature: %.1f m' % avg_curve result = cv2.putText(result, label_curve, (30, 40), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) result = cv2.putText(result, vehicleposition_msg, (30, 80), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) file_to_write = 'output_images/annotated_' + file.split('\\')[-1] result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR) if display_images: result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB) plt.imshow(result) plt.show() else: cv2.imwrite(file_to_write, result)
def annotate_image(image, image_file=None): """ this function is used to annotate each video frame this function can annotate either an image or it can read image from a file :param image: image to annotate :param image_file: file name of the image to annotate :return: annotated image/frame """ # if not image and not image_file: # raise ValueError("annotate_image: wrong function arguments (both of them are null") if image.any() and image_file: raise ValueError("annotate_image: wrong function arguments (both of them are not null)") if not image.any(): raise NotImplementedError("this function accepts only input in the form of image") # removing distortion undistorted = distortion_removal(calibration, imageFile=None, image=image) # discovering lines gradient = apply_gradients_thresholds(image=undistorted) # changing perspective warped, _, _, minv = perspective_transform(src_file=None, image=gradient) _, output = search_for_lines(img=warped, file_image=None) # discovering curvature curvature_output = \ determine_lane_curvature(output['left_lane_inds'], output['right_lane_inds'], output['nonzerox'], output['nonzeroy']) # drawing lane & annotating the image warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) res = draw_lane(image, undistorted, warped, output['left_fitx'], output['right_fitx'], output['ploty'], minv) avg_curve = (curvature_output['left_curverad'] + curvature_output['right_curverad']) / 2 label_curve = 'Radius of curvature: %.1f m' % avg_curve res = cv2.putText(res, label_curve, (30, 40), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) return cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
def annotate_image(image, image_file=None): """ this function is used to annotate each video frame this function can annotate either an image or it can read image from a file :param image: image to annotate :param image_file: file name of the image to annotate :return: annotated image/frame """ global left_line, right_line # if not image and not image_file: # raise ValueError("annotate_image: wrong function arguments (both of them are null") if image.any() and image_file: raise ValueError( "annotate_image: wrong function arguments (both of them are not null)" ) if not image.any(): raise NotImplementedError( "this function accepts only input in the form of image") # removing distortion undistorted = distortion_removal(calibration, imageFile=None, image=image) # discovering lines gradient = apply_gradients_thresholds(image=undistorted) # changing perspective warped, _, _, minv = perspective_transform(src_file=None, image=gradient) _, output = search_for_lines(img=warped, file_image=None) # discovering curvature curvature_output = \ determine_lane_curvature(output['left_lane_inds'], output['right_lane_inds'], output['nonzerox'], output['nonzeroy']) left_line.current_fit = output['left_fit'] left_line.allx = output['left_fitx'] left_line.radius_of_curvature = curvature_output['left_curverad'] left_line.ally = output['ploty'] if not left_line.detected: for i in range(0, left_line.N_Average): left_line.recent_xfitted.append(output['left_fit']) left_line.Counter = 0 left_line.best_fit = output['left_fit'] left_line.detected = True left_line.line_base_pos = image.shape[1] / 2 - output['left_fitx'][-1] left_line.radius_of_curvature = curvature_output['left_curverad'] left_line.add_fit(output['left_fit']) right_line.current_fit = output['right_fit'] right_line.allx = output['right_fitx'] right_line.radius_of_curvature = curvature_output['right_curverad'] right_line.ally = output['ploty'] if not right_line.detected: for i in range(0, right_line.N_Average): right_line.recent_xfitted.append(output['right_fit']) right_line.Counter = 0 right_line.best_fit = output['right_fit'] right_line.detected = True right_line.radius_of_curvature = curvature_output['right_curverad'] right_line.line_base_pos = image.shape[1] / 2 - output['right_fitx'][-1] right_line.add_fit(output['right_fit']) car_position_msg = vehicle_position_lines(left_line, right_line) # drawing lane & annotating the image warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) res = draw_lane(image, undistorted, warped, left_line.bestx, right_line.bestx, left_line.ally, minv) avg_curve = (left_line.radius_of_curvature + right_line.radius_of_curvature) / 2 label_curve = 'Radius of curvature: %.1f m' % avg_curve res = cv2.putText(res, label_curve, (30, 40), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) res = cv2.putText(res, car_position_msg, (30, 80), 0, 1, (0, 0, 0), 2, cv2.LINE_AA) return res