[英]error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
[英]Error: Open two cameras at the same time in Python - OPENCV (Pycharm) - error (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
快樂的一天,我正在嘗試將兩個攝像頭(我的本地計算機)和我的手機攝像頭與 IP Web cam 同步,但是在運行代碼時它會產生錯誤並且我之前打開的兩個攝像頭都關閉了(見圖)。 這是什么原因造成的? 謝謝你的幫助!
錯誤 -->
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()
問題是您沒有檢查ret1
和ret2
的值。
如果那些是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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.