|
| 1 | +import matplotlib.pylab as plt |
| 2 | +import cv2 as cv |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +def draw_the_line(img, lines): |
| 7 | + img = np.copy(img) |
| 8 | + blank_image = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) |
| 9 | + |
| 10 | + |
| 11 | + for line in lines: |
| 12 | + for x1, y1, x2, y2 in line: |
| 13 | + cv.line(blank_image, (x1, y1), (x2, y2), (255, 255, 0), thickness=2) |
| 14 | + img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0) |
| 15 | + return img |
| 16 | +def region_of_interst(img,vertices): |
| 17 | + mask=np.zeros_like(img) |
| 18 | + #channel_count=img.shape[2] |
| 19 | + match_mask_color=255 |
| 20 | + cv.fillPoly(mask,vertices,match_mask_color) |
| 21 | + mask_image=cv.bitwise_and(img,mask) |
| 22 | + return mask_image |
| 23 | + |
| 24 | +def process(image): |
| 25 | + print(image.shape) |
| 26 | + height = image.shape[0] |
| 27 | + width = image.shape[1] |
| 28 | + region_of_interst_vertices = [(0, height), (width / 2, height / 2), (width, height)] |
| 29 | + gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) |
| 30 | + canny = cv.Canny(gray, 250, 300) |
| 31 | + cropped = region_of_interst(canny, np.array([region_of_interst_vertices], np.int32)) |
| 32 | + line1 = cv.HoughLinesP(cropped, rho=10, theta=np.pi / 60, threshold=100, lines=np.array([]), minLineLength=5, |
| 33 | + maxLineGap=5) |
| 34 | + image_width_line = draw_the_line(image, line1) |
| 35 | + return image_width_line |
| 36 | + |
| 37 | + |
| 38 | +cap=cv.VideoCapture('drive.mp4') |
| 39 | +while(cap.isOpened()): |
| 40 | + ret,frame=cap.read() |
| 41 | + frame=process(frame) |
| 42 | + cv.imshow('detection',frame) |
| 43 | + if cv.waitKey(1) &0xFF==ord('q'): |
| 44 | + break |
| 45 | +cap.release() |
| 46 | +cv.destroyAllWindows() |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
0 commit comments