gpt4 book ai didi

python - 使用 cv2.VideoCapture 没有来自相机的图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:24 25 4
gpt4 key购买 nike

我使用 WebCam(Genius FaceCam 1020)、Python(3.6.4)、OpenCV(3.4.0.12) 和 Opencv 文档代码,这一个:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

运行没有错误,但图像没有出现在窗口中。相机上的指示灯亮起。我试过复制 opencv_ffmpeg.dll,以这种方式重命名 opencv_ffmpeg.dll:

对于 OpenCV 版本 X.Y.Z

opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll

对于 64 位 OpenCV 版本 X.Y.Z

opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll

opencv_ffmpeg34012.dllopencv_ffmpeg34012_64.dll。但这没有帮助。

最佳答案

可以使用下面的调试代码完成您的凸轮的另一个绕过灌木丛的工作。 exceptionhook 应该返回垃圾邮件错误代码,您通常不会看到脚本是否遇到障碍并静默终止。我在编辑器中使用它。

import sys, cv2, time


def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)

def runcam():

camera = cv2.VideoCapture(0)

for i in range(10):
time.sleep(1) # implemented for cams with long image aquisition time. Its 1 sec.
# delay before the next step is repeated until range has finished.
# From first image to last each image becomes brighter.
return_value, image = camera.read()
cv2.imwrite('opencv'+str(i)+'.png', image)
print ' taking image %s' % (i+1)


del(camera)

# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook

# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook

def check_cam_index():
i = 0
found = False
for i in range(4):
capture = cv2.VideoCapture(i)
if not capture:
print "UNABLE TO CAPTURE CAMERA"
else:
found = True
print "taken camera from index: ", i
break

if found == False:
print "!!! No camera was found."
sys.exit()


if __name__ == '__main__':
check_cam_index()
runcam()

关于python - 使用 cv2.VideoCapture 没有来自相机的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com