-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
90 lines (76 loc) · 4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
import cv2
from mss import mss
import time
import keyboard
# Yellow color filter for image passed through, uses values determined via trackbar in open-cv window.
# Will return the image with just the yellow lines.
def yellow_filter(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (20, 40, 200), (40, 255, 255))
filtered = cv2.bitwise_and(image, image, mask=mask)
return filtered
if __name__ == '__main__':
# 2.5-second grace period to tab into the game
cv2.namedWindow("Hough Transform", cv2.WINDOW_AUTOSIZE)
time.sleep(2.5)
increment = True
counter = 37
# Main loop that gets screenshot and processes it
with mss() as sct:
while True:
begin_tim = time.time()
# Holds down the 'a' key to make bus go forward
keyboard.press('a')
keyboard.release('left')
keyboard.release('right')
# noinspection PyTypeChecker
# Grabs screenshot using specific BBOX, as long as emulator is windowed maximized, it will grab full left
# windshield. Then passes the screenshot through the yellow filter.
sct_img = np.array(sct.grab((241, 236, 1150, 809)))
yellow_filtered = yellow_filter(sct_img)
# Taking the HSV image, and obtaining the greyscale version of the image by splitting channels.
h, s, v, _ = cv2.split(yellow_filtered)
img_gray = v
# Blurs the image and runs Canny edge detection.
img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0)
img_edges = cv2.Canny(image=img_blur, threshold1=50, threshold2=200)
# Creating the final image to draw the hough transform results on, and running the actual hough transform.
final = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2BGR)
linesP = cv2.HoughLinesP(img_edges, 1, np.pi / 180, 10, None, 7, 3)
if linesP is not None:
# Draws the lines from the hough transform onto the image to be shown in open-cv window
for i in range(0, len(linesP)):
l = linesP[i][0]
cv2.line(final, (l[0], l[1]), (l[2], l[3]), (255, 0, 0), 3, cv2.LINE_AA)
# Just grabbing the first located line and using its values to find the slope of the line
line = linesP[0][0]
slope = (line[3] - line[1]) / (line[2] - line[0]) if line[2] - line[0] != 0 else 0
# Depending on the slope of the line, move a specific direction to stay on road. Needs to be adjusted,
# as of now the car will sway back and forth so ideally would like to make movement smoother
if slope < 0:
keyboard.press('left')
if slope > 0:
keyboard.press('right')
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(final, f"fps: {int(1 / (time.time() - begin_tim))}", (10, 500), font, 1, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(final, f"Points: {counter}", (10, 450), font, 1, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.imshow("Hough Transform", final)
time.sleep(0.05)
keyboard.release('a')
end_check = np.array(sct.grab((794, 560, 795, 561)))
if 62 < end_check[0][0][0] < 67 and 65 < end_check[0][0][1] < 70 and 29 < end_check[0][0][2] < 34:
if increment:
counter += 1
increment = not increment
keyboard.press('enter')
time.sleep(0.1)
keyboard.release('enter')
else:
increment = True
# Checks if Q key is pressed while open-cv window is focused, then closes it and ends the program
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break