我知道您可以使用“waitKey()”函式等待一段時間,但我在網上看到了一個視頻攝像機實時錄像機的代碼塊,其中為函式分配了一個變數來檢測按下了什么鍵。這是做什么的?
這是帶有添加注釋的代碼(此代碼下方沒有注釋的代碼)(與互聯網上的代碼不同,但此腳本也可以):
# 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')
沒有評論的代碼,因為我無法閱讀那么多評論:
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')
基本上,我在哪里可以找到有關使用該功能的這種方式的檔案,以及如何將它用于其他鍵盤按鍵?
uj5u.com熱心網友回復:
Waitkey 回傳鍵盤上按下的鍵的 ASCII 碼。在上述情況下,escape = 27,因此當按下 escape 時回圈將退出。
我不是 100% 確定它在 python 中是如何作業的,但你應該可以這樣說:
if 'k' == cv2.waitKey(1):
# some code here after k key pressed
這是一個類似的問題,它是為 c 解釋的:OPENCV waitKey() method return type
這是opencv檔案:https ://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7
這是 ASCII 表: https ://www.asciitable.com/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510779.html
標籤:Pythonopencv
上一篇:應用灰度時視頻損壞
