问题描述
当我运行代码访问摄像头时,实时供稿会出现一秒钟,然后出现错误。
代码如下:
import cv2,time
video=cv2.VideoCapture(0)
check,frame=video.read()
print(check)
print(frame)
cv2.imshow("capturing",frame)
cv2.waitkey(1)
video.release()
错误是:
Traceback (most recent call last):
File "C:/1 Files and Folders/SHaraN/code/My Projects/os.py",line 8,in <module>
cv2.waitkey(1)
AttributeError: module 'cv2.cv2' has no attribute 'waitkey'
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
解决方法
waitKey
是正确的代码。
您应该将k更改为大写字母
import cv2,time
video=cv2.VideoCapture(0)
check,frame=video.read()
print(check)
print(frame)
cv2.imshow("capturing",frame)
cv2.waitKey(1) #you should change this
video.release()
,
主要问题是您键入了waitkey
而不是waitKey
。
第二个问题,您的帧没有使用主循环,因此只能捕获一帧。
您可以通过添加以下内容来修复这两个问题:
import cv2,time
video = cv2.VideoCapture(0)
while True: # Place this in an infinite loop (to make it live)
check,frame = video.read()
print(check)
print(frame)
cv2.imshow("capturing",frame)
if cv2.waitKey(1): # Change it to waitKey instead
break
video.release()
如果您只想要一帧,那么完全不要理会我的建议。 :)