【问题标题】:Error: Open two cameras at the same time in Python - OPENCV (Pycharm) - error (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'错误:在 Python 中同时打开两个摄像头 - OPENCV (Pycharm) - 错误 (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
【发布时间】:2021-11-22 14:52:08
【问题描述】:

快乐的一天,我正在尝试同步两个摄像头 (我的本地计算机)和带有 IP 网络摄像头的手机摄像头, 但是,在运行代码时会产生错误,并且这两个 我之前打开的相机已关闭(见照片)。什么 这是由于?感谢您的帮助!

错误 -->

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'

这是标签页 - 几秒钟后标签页关闭

[标签,2 个摄像头][1]

这是我的代码

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()

【问题讨论】:

标签: python multithreading opencv ip-camera


【解决方案1】:

问题是您没有检查ret1ret2 的值。

如果是False,图像将不会被捕获,而您仍在尝试显示它们,OpenCV 会崩溃,因为无法显示不存在的图像。

试试

if ret:
    cv2.imshow("cam0", frame)
if ret1:
    cv2.imshow("cam1", frame1)
if ret2:
    cv2.imshow("cam3", frame2)  

改为。

您还可以将所有捕获设备存储在一个列表中,并且所有捕获的图像同样如此,以使代码更易于使用:

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()

【讨论】:

  • Thks @AKX 但我之前试过这个,问题是只打开一个标签“cam0”
  • 这意味着其他相机不会捕获任何帧。不过,为什么会发生这种情况是一个完全不同的问题。
  • 我尝试了最后一个建议,似乎“i”是模棱两可的 Traceback(最近一次调用最后一次):文件“C:\Users\JUCABALL\Desktop\camera_stream_openCV-master\atest.py”,第 18 行,在 中 if frame: # None 如果未捕获 ValueError: 具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()
  • 糟糕,是的,应该是if frame is not None。如果未读取相机,OpenCV 返回空矩阵,而不是无。
  • 即使在框架排列中我把 0,1,2 类似于主要代码的事情发生了,选项卡会立即再次打开和关闭
猜你喜欢
  • 2019-06-26
  • 2019-11-12
  • 2021-04-19
  • 2023-04-01
  • 2021-07-12
  • 1970-01-01
  • 2019-05-24
  • 2019-08-30
  • 2021-07-30
相关资源
最近更新 更多