S
S
slava kk2020-12-25 12:19:10
linux
slava kk, 2020-12-25 12:19:10

Why can't Python OpenCV on Ubuntu get image from IP Camera?

Hello. There is a server on Ubuntu on which I decided to run a Python Code that will capture a picture from an IP Camera stream and recognize motion. But as I understood, for some reason it can't get the image, and this throws an error:

gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) /tmp/pip-install-5f2ta5by/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'


Please note that the same code works fine on Windows and captures the image from the camera.

The code itself:
import cv2 # импорт модуля cv2
import time
cap = cv2.VideoCapture("http://admin:[email protected]/video1.mjpg") # видео поток с камеры


ret, frame1 = cap.read()
ret, frame2 = cap.read()

last_motion_time = int(time.time())

while True: # метод isOpened() выводит статус видеопотока
 
  diff = cv2.absdiff(frame1, frame2) # нахождение разницы двух кадров, которая проявляется лишь при изменении одного из них, т.е. с этого момента наша программа реагирует на любое движение.
 
  gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) # перевод кадров в черно-белую градацию
 
  blur = cv2.GaussianBlur(gray, (5, 5), 0) # фильтрация лишних контуров
 
  _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) # метод для выделения кромки объекта белым цветом
 
  dilated = cv2.dilate(thresh, None, iterations = 3) 
 
 
  сontours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #
 
   
  for contour in сontours:
    (x, y, w, h) = cv2.boundingRect(contour) 
   
    if cv2.contourArea(contour) < 700: # условие при котором площадь выделенного объекта меньше 700 px
      continue
    print('Motion Detected!')

  frame1 = frame2  #
  ret, frame2 = cap.read() #  
 
 
 

cap.release()


If we substitute “cap.isOpened()” instead of True in While, then the loop will not be executed, i.e. video stream is not opening.
There are suspicions of any packages or codecs. Tell me what could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question