简体   繁体   English

OpenCV - 循环图像作为网络摄像头输入

[英]OpenCV - Loop an image as Webcam Input

I began using OpenCV(Python) a while ago and everything went great until now.不久前我开始使用 OpenCV(Python),直到现在一切都很好。 My Webcam broke and now I cant use it to test my code.我的网络摄像头坏了,现在我不能用它来测试我的代码。 Is there a way to fake the input with a simple image?有没有办法用简单的图像伪造输入? It's not perfect but should be fine for simple things until the new webcam arrives.它并不完美,但对于简单的事情来说应该没问题,直到新的网络摄像头到来。

Let's assume I have something like this:假设我有这样的事情:

 windowWidth = 640 windowHeight = 840 brightness = 100 cap = cv2.VideoCapture(0) cap.set(3,windowWidth) cap.set(4,windowHeight) cap.set(10,brightness) while (cap.isOpened()): success, img = cap.read() if success== True: img = cv2.flip(img, 1) cv2.imshow("output", img) if cv2.waitKey(1)& 0xFF ==ord("q"): break

This should work fine but now with the broken camera I would like to be able to "fake" the input with an image but still be able to test it just like I would using a webcam.这应该可以正常工作,但是现在使用损坏的相机,我希望能够用图像“伪造”输入,但仍然能够像使用网络摄像头一样对其进行测试。

Something along the lines of cap = cv2.imread(testImg.PNG) which obviously does not work. cap = cv2.imread(testImg.PNG)的东西显然不起作用。 I am looking for something along the lines of maybe looping the image into the webcam input to create some sort of working setup since a video is nothing more than a lot of frames in series playing.我正在寻找可能将图像循环到网络摄像头输入以创建某种工作设置的方法,因为视频只不过是一系列连续播放的帧。

Any suggestions are appreciated:)任何建议表示赞赏:)

VideoCapture is a class to interface with video streams, and a single image is returned via call to its read() method. VideoCapture是一个 class 与视频流接口,并通过调用其read()方法返回单个图像。 When you call cap = cv2.imread() , you are replacing the newly created VideoCapture instance with np.ndarray instance which is returned by cv2.imread .当您调用cap = cv2.imread()时,您将用cv2.imread np.ndarray替换新创建的VideoCapture实例。 I believe that replacing我相信更换

success, img = cap.read()

with

img = cv2.imread(testImg.PNG)

would solve your problem.会解决你的问题。

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

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