简体   繁体   English

将 cv2.waitKey() 分配给 python 中的变量?

[英]Assigning cv2.waitKey() to a variable in python?

I know you can use the 'waitKey()' function to wait for a while, but I saw a code block online for a video camera live recorder in which the function is assigned a variable to detect what key was pressed.我知道你可以使用'waitKey()' function 等待一段时间,但我在网上看到了一个摄像机实时录像机的代码块,其中 function 被分配了一个变量来检测按下的键。 What does this do?这是做什么的?

Here is the code with added comments (code without comment below this one)(not the same as the one from the internet but this script works too):这是带有添加注释的代码(此代码下方没有注释的代码)(与互联网上的代码不同,但此脚本也可以):

# Importing stuff to be able to install libraries
from subprocess import check_call 
from sys import executable

# Installing the required libraries
check_call([executable, '-m', 'pip', 'install', 'numpy'])
check_call([executable, '-m', 'pip', 'install', 'opencv-python'])

# Importing cv2
import cv2

#Creating a window and assigning the video capture to a variable
cv2.namedWindow('Video Feed')
vc = cv2.VideoCapture(0)

# The while loop to always record
while True:
    
    # Reading the video
    rval, frame = vc.read()
    
    # Showing the correct frame to the 'Video Feed' window
    cv2.imshow('Video Feed', frame)
    
    # Assigning the function waitKey() to the variable 'key'
    # This is what I do not understand, why does it work and
    # why is can't I find any record on the internet of the function being able to do this?
    key = cv2.waitKey(20)

    # Checking if the variable key is 27
    # This is a true statement when you press 'ESC'
    # Where can I find the table for what numbers correspond to what keys?
    # Does this mean that the function outputs a number when assigned to a variable?
    if key == 27:
        
        # Exiting loop
        break

# Closing stuff
vc.release()
cv2.destroyWindow('Video Feed')

Code without comments because I can't read with that many comments:没有评论的代码,因为我无法阅读那么多评论:

from subprocess import check_call
from sys import executable
    
check_call([executable, '-m', 'pip', 'install', 'numpy'])
check_call([executable, '-m', 'pip', 'install', 'opencv-python'])

import cv2

cv2.namedWindow('Video Feed')
vc = cv2.VideoCapture(0)

while True:
    
    rval, frame = vc.read()
    cv2.imshow('Video Feed', frame)
    
    key = cv2.waitKey(20)
    if key == 27:
        break

vc.release()
cv2.destroyWindow('Video Feed')

Basically, where do I find documentation on this way of using the function, and how can I use it for other keyboard presses?基本上,我在哪里可以找到有关使用 function 的这种方式的文档,以及如何将它用于其他键盘按键?

Waitkey returns the ASCII code for the key pressed on the keyboard. Waitkey 返回键盘上按下的键的 ASCII 码。 In the above case, escape = 27, so the loop will exit when escape is pressed.在上述情况下,escape = 27,因此当按下 escape 时循环将退出。

I'm not 100% sure how it works in python, but you should be able to say something like:我不是 100% 确定它在 python 中是如何工作的,但你应该可以这样说:

if 'k' == cv2.waitKey(1):
    # some code here after k key pressed

Here is a similar question, where it is explained for c++: OPENCV waitKey() method return type这是一个类似的问题,其中解释了 c++: OPENCV waitKey() method return type

And here is the opencv docs: https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7 And here is the opencv docs: https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7

And here is the ASCII table: https://www.asciitable.com/这是 ASCII 表: https://www.asciitable.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM