I can't connect ip camera to opencv (-215:Assertion failed)
Hi, there! How are you? Well, I'm using my cell phone as a camera with droidcam and i'm trying to connect a ip camera into opencv, but I'm reciving this message error everytime I try:
Traceback (most recent call last):
File "c:/Users/uriel/Documents/Python/Project #1/droidcam.py", line 10, in <module>
cv2.imshow('IPWebcam',img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
Here's the full code:
import urllib.request
import cv2
import numpy as np
import time
URL = "http://xxx.xxx.x.xxx:xxxx/"
while True:
img_arr = np.array(bytearray(urllib.request.urlopen(URL).read()),dtype=np.uint8)
img = cv2.imdecode(img_arr,-1)
#imS = cv2.resize(img,(960, 540))
cv2.imshow('IPWebcam',img)
if cv2.waitKey(0):
break
Details:
Python Version: 3.8.2 OpenCv Version: 4.2.0 OS: Windows 10 64bits
sigh why you guys don't read error messages? (-215:Assertion failed) size.width>0 && size.height>0
That means the image has width <= 0 || height <=0 which just means its no a valid image. Which means that image was not loaded correctly. Most likely you img array is already not valid. But why dont you use debugging?
When urlopen is done in a loop, failure is to be expected... just ooen it once, don't put all function calls in one blob (there's this variables thing...) and check that each succeeds