[英]Error: Open two cameras at the same time in Python - OPENCV (Pycharm) - error (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
happy day, I am trying to synchronize two cameras (my local computer) and the camera of my cell phone with IP Web cam, however when running the code it generates an error and the two cameras that I have previously opened are closed (see photo ).快乐的一天,我正在尝试将两个摄像头(我的本地计算机)和我的手机摄像头与 IP Web cam 同步,但是在运行代码时它会产生错误并且我之前打开的两个摄像头都关闭了(见图)。 What would this be due to?这是什么原因造成的? Thanks for your help!谢谢你的帮助!
ERROR -->错误 -->
Traceback (most recent call last):
File "C:\Users\JUCABALL\Desktop\camera_stream_openCV-master\main.py", line 20, in <module>
cv2.imshow ('cam1', frame1)
cv2.error: OpenCV (4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp: 182:
error: (-215: Assertion failed)! _src .empty () in function 'cv :: cvtColor'
This is the tabs - after fews seconds tabs close这是标签 - 几秒钟后标签关闭
[Tabs, 2 cameras][1] [标签,2 个摄像头][1]
This is my code这是我的代码
import numpy as np
import cv2
# capture the webcam
vid1 = cv2.VideoCapture(0, cv2.CAP_DSHOW)
vid2 = cv2.VideoCapture(1, cv2.CAP_DSHOW)
vid3 = cv2.VideoCapture(
"http://192.168.0.11:8080/video", cv2.CAP_DSHOW
) # ipwebcam address
while True: # while true, read the camera
ret, frame = vid1.read()
ret1, frame1 = vid2.read()
ret2, frame2 = vid3.read()
if ret:
cv2.imshow("cam0", frame) # frame with name and variable of the camera
cv2.imshow("cam1", frame1)
cv2.imshow("cam3", frame2)
# to break the loop and terminate the program
if cv2.waitKey(1) & 0xFF == ord("q"):
break
vid1.release()
vid2.release()
vid3.release()
The problem is you're not checking the value of ret1
and ret2
.问题是您没有检查ret1
和ret2
的值。
If those are False
, the images will not have been captured, and you're still trying to show them, and OpenCV crashes because there's no way to show an image that doesn't exist.如果那些是False
,图像将不会被捕获,并且您仍在尝试显示它们,并且 OpenCV 崩溃,因为无法显示不存在的图像。
Try尝试
if ret:
cv2.imshow("cam0", frame)
if ret1:
cv2.imshow("cam1", frame1)
if ret2:
cv2.imshow("cam3", frame2)
instead.反而。
You could also store all of the capture devices in a list, and all the captured images equally so, to make the code a little simpler to work with:您还可以将所有捕获设备存储在一个列表中,并且所有捕获的图像同样如此,以使代码更易于使用:
import numpy as np
import cv2
captures = [
cv2.VideoCapture(0, cv2.CAP_DSHOW),
cv2.VideoCapture(1, cv2.CAP_DSHOW),
cv2.VideoCapture("http://192.168.0.11:8080/video", cv2.CAP_DSHOW), # ipwebcam address
]
while True: # while true, read the camera
frames = []
for cap in captures:
ret, frame = cap.read()
frames.append((frame if ret else None))
for i, frame in enumerate(frames):
if frame is not None: # None if not captured
cv2.imshow(f"cam{i}", frame)
# to break the loop and terminate the program
if cv2.waitKey(1) & 0xFF == ord("q"):
break
for cap in captures:
cap.release()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.