If you want more of my pastes visit: https://randompaste.000webhostapp.com/index.html -------------------------------------------------------------------------------------- view my last post at: https://bitbin.it/yQvdDbDd/ -------------------------------------------------------------------------------------- """ drawlines using kmeans to group """ import cv2 import numpy as np from grabscreen import grab_screen from sklearn.cluster import KMeans import pyautogui import time BOX = (0, 40, 800, 640) hood_y = 410 horizon_y = 270 VERTICES = np.array([[4, horizon_y], [800, horizon_y], [800, hood_y], [4, hood_y]]) def draw_lines(img, lines): try: for coords in lines: coords = np.array(coords, dtype='uint32') cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), [255, 255, 255], 5) except (TypeError, IndexError): pass def roi(img, vertices): mask = np.zeros_like(img) cv2.fillPoly(mask, vertices, 255) masked = cv2.bitwise_and(img, mask) return masked def process_img(processed_img): processed_img = cv2.Canny(processed_img, threshold1=100, threshold2=300) processed_img = roi(processed_img, [VERTICES]) processed_img = cv2.GaussianBlur(processed_img, (5, 5), 0) lines = cv2.HoughLinesP(processed_img, 1, np.pi / 180, 180, np.array([]), 140, 20) try: nlines = np.array([l[0] for l in lines]) kmeans = KMeans(n_clusters=2, random_state=0).fit(nlines) draw_lines(processed_img, kmeans.cluster_centers_) except TypeError: pass finally: return processed_img def main(): while True: ti = time.time() screen = grab_screen(region=BOX) new_screen = process_img(screen) print('{:.2f} FPS'.format(1 / (time.time() - ti))) cv2.imshow('window', new_screen) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break if __name__ == '__main__': main()