【Python自动驾驶GTA5】2-边缘检测

紧接上一步,现在我们对获得的图像进行一些处理,RGB转换为灰度图像并做边缘检测。

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
from PIL import ImageGrab

import numpy as np
import cv2
import time
import mss

def process_img(image):
original_image = image
# convert to gray
processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# edge detection
processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
return processed_img


def screen_record():
with mss.mss() as sct:
# Part of the screen to capture
monitor = {'top': 40, 'left': 0, 'width': 800, 'height': 640}

while 'Screen capturing':
last_time = time.time()

# Get raw pixels from the screen, save it to a Numpy array
img = np.array(sct.grab(monitor))

# Display the picture in grayscale and detect edge
cv2.imshow('OpenCV/Numpy grayscale', process_img(img))

print('fps: {0}'.format(1 / (time.time()-last_time)))

# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

screen_record()process_img

增加了一个process_img函数,保留原始图像再做转换。Canny Edge Detector是常用的边缘检测器。

控制游戏

这里用到了pyautogui来控制鼠标和键盘。先安装pip install pyautogui.