diff --git a/Detect/detect.py b/Detect/detect.py index 2cd8c89d4a3f4d815bb55c4aaa4d840ac15ee806..077787d7368aef8febf85f783b969a7b63815f40 100644 --- a/Detect/detect.py +++ b/Detect/detect.py @@ -27,20 +27,62 @@ def detect_edge(pic): return img_edge def ROI(pic): - return 0 + height = pic.shape[0] + triangle = np.array([[(200, height), (1100, height), (550, 250)]]) + mask = np.zeros_like(pic) + cv2.fillPoly(mask, triangle, 255) + roi = cv2.bitwise_and(pic, mask) + return roi -def applyLanes(original_pic, edge_pic): - lines = cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=70, minLineLength=10) +def getLines(edge_pic): + return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=70, minLineLength=10) + +def applyLines(original_pic, lines): for line in lines: - x1, y1, x2, y2 = line[0] - print(x1) - cv2.line(original_pic, (x1, y1), (x2, y2), (0, 255, 0), 3) + x1, y1, x2, y2 = line.reshape(4) + cv2.line(original_pic, (x1, y1), (x2, y2), (255, 0, 0), 3) return original_pic +def find_average_lane(pic, lines): + left_lines = [] + right_lines = [] + for line in lines: + x1, y1, x2, y2 = line[0] + parameters = np.polyfit((x1, x2), (y1, y2), 1) + print(parameters ) + slope = parameters[0] + intercept = parameters[1] + if(slope < 0): + left_lines.append((slope, intercept)) + else: + right_lines.append((slope, intercept)) + + right_average = np.average(left_lines, axis=0) + left_average = np.average(right_lines, axis=0) + + + left_line = make_coordinates(pic, left_average) + right_line = make_coordinates(pic, right_average) + print("Left fit") + print(left_line) + + print("\nRight fit") + print(right_line) + return np.array([left_line, right_line]) + +def make_coordinates(image, line_parameters): + slope, intercept = line_parameters + y1 = image.shape[0] + y2 = int(y1*(1/2)) + x1 = int((y1 - intercept)/slope) + x2 = int((y2 - intercept) / slope) + return np.array([x1, y1, x2, y2]) -video = cv2.VideoCapture("test_data.mp4") + + +video = cv2.VideoCapture("test2.mp4") while True: ret, frame = video.read() @@ -55,9 +97,15 @@ while True: wEdges = detect_edge(new_img) - lanes = applyLanes(frame, wEdges) + cropped = ROI(wEdges) + + lines = getLines(cropped) + + average_lines = find_average_lane(new_img, lines) - cv2.imshow("frame", wEdges) + average_lanes = applyLines(frame, average_lines) + + cv2.imshow("frame", average_lanes) key = cv2.waitKey(25) if key == 27: @@ -70,18 +118,22 @@ cv2.destroyAllWindows() # Load image img = cv2.imread('lol_image.jpg') -cv2.imshow("original", img) new_img = formatImg(img) wEdges = detect_edge(new_img) +cropped = ROI(wEdges) +lines = getLines(cropped) -lanes = applyLanes(img, wEdges) +#lanes = applyLines(img, lines) +average_lines = find_average_lane(img, lines) +average_lanes = applyLines(img, average_lines) cv2.imshow("edges", wEdges) -cv2.imshow("with lines", lanes) - +#cv2.imshow("with lines", lanes) +cv2.imshow("Average", average_lanes) cv2.waitKey(0) cv2.destroyAllWindows() + ''' \ No newline at end of file