I am following this project to make a AI that plays Google Chrome Dino game. I am stuck at a point while capturing screen feed to generate training data. I am new to CV
The project is supposed to supposed to break the video feed and save the CSV file in cv2.waitKey(25) & 0xFF == ord('q'): condition. Which I believe is when key "q" is pressed. But nothing happens when I press 'q'. The print statement in this if condition doesn't print when I press q.
Also although the console prints the print statement in 'up' ,'down' or 't' key pressed condition but the
cv2.imwrite('./images/frame_(0).jpg'.format(x), img)
Doesn't seem to be working as no images in the images folder are saved.
Here is the code
import cv2
from mss import mss
import numpy as np
import keyboard
#Captures dinasour run for given coordinates
def start():
"""
Capture video feed frame by frame, crops out coods and the dino then process
"""
sct = mss()
coordinates = {
'top': 168,
'left': 230,
'width': 624,
'height': 141
}
with open('actions.csv','w') as csv:
x = 0
while True:
img = np.array(sct.grab(coordinates))
#crop out the dino from the image array
img = img[::,75:624]
#edge detection to reduce ammount of image processing work
img = cv2.Canny(img, threshold1=100, threshold2=200)
if keyboard.is_pressed('up arrow'):
cv2.imwrite('./images/frame_(0).jpg'.format(x), img)
csv.write('1\n')
print('jump write')
x += 1
if keyboard.is_pressed('down arrow'):
cv2.imwrite('./images/frame_(0).jpg'.format(x), img)
csv.write('2\n')
print('duck')
x += 1
if keyboard.is_pressed('t'):
cv2.imwrite('./images/frame_(0).jpg'.format(x), img)
csv.write('0\n')
print('nothing')
x += 1
# break the video feed
if cv2.waitKey(25) & 0xFF == ord('q'):
csv.close()
cv2.destroyAllWindows()
print('Exited')
break
def play():
sct = mss()
coordinates = {
'top': 168,
'left': 230,
'width': 624,
'height': 141
}
img = np.array(sct.grab(coordinates))
# crop out the dinosaur from the image array
img = img[::,75:615]
# edge detection to reduce amount of image processing work
img = cv2.Canny(img, threshold1=100, threshold2=200)
cv2.waitKey() does only work if you press the key while an OpenCV window (e.g. created with cv2.imshow()) is focused. It seems for me as you don't use GUI features of OpenCV at all.
If you have a OpenCV GUI in your program, focus it and then press the key.
If not, and if you don't want to implement one, why not use keyboard.isPressed()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With