def main(): last_time = time() for i in range(1, 4): print(4 - i) sleep(1) paused = False while (True): if not paused: screen = takeScreenShot(region=(0, 30, 800, 630)) print('loop took {} seconds'.format(time() - last_time)) last_time = time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (160, 120)) prediction = model.predict([screen.reshape(160, 120, 1)])[0] print(prediction) if np.argmax(prediction) == np.argmax(w): straight() if np.argmax(prediction) == np.argmax(a): left() if np.argmax(prediction) == np.argmax(d): right() if np.argmax(prediction) == np.argmax(wa): forward_left() if np.argmax(prediction) == np.argmax(wd): forward_right() if np.argmax(prediction) == np.argmax(nk): no_keys() keys = getKeys() if '1' in keys: if paused: paused = False sleep(1) else: paused = True print("Paused") ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) sleep(1)
masked = cv2.bitwise_and(img, mask) return masked def process_img(org_img): processed_img = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY) processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2 = 300) vertices = np.array([[10,599],[10,300], [800,300], [800, 599]]) processed_img = roi(processed_img, [vertices]) return processed_img while(True): screen = np.array(ImageGrab.grab(bbox=(0,40,800,640))) new_screen = process_img(screen) cv2.imshow('window',new_screen) # printscreen_numpy = np.array(printscreen_pil.getdata(),dtype="uint8").reshape((printscreen_pil.size[1], printscreen_pil.size[0],3)) # cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break for i in range(4)[::-1]: print(i+1) time.sleep(1) print('down') PressKey(S) time.sleep(3) ReleaseKey(S) print('up') PressKey(W)
def no_keys(): PressKey(W) ReleaseKey(A) ReleaseKey(S) ReleaseKey(D)
def forward_right(): PressKey(W) PressKey(D) ReleaseKey(A)
def forward_left(): PressKey(W) PressKey(A) ReleaseKey(D)
def right(): PressKey(D) ReleaseKey(A) ReleaseKey(W)
def left(): PressKey(A) ReleaseKey(W) ReleaseKey(D)
def straight(): PressKey(W) ReleaseKey(A) ReleaseKey(D)