【问题标题】:cv2.waitKey(25) & 0xFF == ord('q'): and cv2.imwrite() not workingcv2.waitKey(25) & 0xFF == ord('q'): 和 cv2.imwrite() 不工作
【发布时间】:2018-11-13 03:58:50
【问题描述】:

我正在关注this 项目,以制作一个玩 Google Chrome Dino 游戏的 AI。在捕获屏幕提要以生成训练数据时,我陷入了困境。我是简历新手

该项目应该中断视频源并将 CSV 文件保存在 cv2.waitKey(25) & 0xFF == ord('q'): 条件中。我相信是按下“q”键时。但是当我按“q”时什么也没有发生。当我按 q 时,此 if 条件中的打印语句不打印。

此外,虽然控制台在 'up' 、'down' 或 't' 键按下条件下打印打印语句,但

cv2.imwrite('./images/frame_(0).jpg'.format(x), img) 

似乎无法正常工作,因为图像文件夹中没有保存任何图像。

这里是代码

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)

【问题讨论】:

    标签: python python-3.x opencv cv2


    【解决方案1】:

    cv2.waitKey() 仅当您在 OpenCV 窗口(例如使用 cv2.imshow() 创建)处于焦点时按下该键时才起作用。在我看来,您根本不使用 OpenCV 的 GUI 功能。

    如果您的程序中有 OpenCV GUI,请将其聚焦,然后按 键。

    如果没有,如果你不想实现一个,为什么不使用keyboard.isPressed()

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2023-03-21
      • 2016-05-24
      • 2019-04-20
      • 2017-12-04
      • 2017-11-17
      • 2020-02-27
      • 1970-01-01
      • 2020-01-14
      相关资源
      最近更新 更多