首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >cv2.waitKey(25) & 0xFF == ord('q'):和cv2.imwrite()不工作

cv2.waitKey(25) & 0xFF == ord('q'):和cv2.imwrite()不工作
EN

Stack Overflow用户
提问于 2018-06-04 03:08:58
回答 1查看 5.6K关注 0票数 3

我是按照this项目,使一个人工智能,玩谷歌Chrome恐龙游戏。在捕获屏幕提要以生成训练数据时,我被困在一个点上。我是简历新手

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

另外,尽管控制台在“up”、“down”或“t”按键条件下打印print语句,但

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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 03:16:07

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

如果您的程序中有OpenCV图形用户界面,请聚焦该图形用户界面,然后按键。

如果不是,如果不想实现,为什么不使用keyboard.isPressed()

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50670024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档