Answer the question
In order to leave comments, you need to log in
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'
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()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question