So I recently bought a Raspberry Pi 4, and i have played a lot with it. I got the idea to make a program that would print a message, whenever motion is detected. I tried searching for the script online, but found nothing that would mach the thing i was searching for. So i made this myself:
import cv2
cap=cv2.VideoCapture(-1,2)
ret1,frame1= cap.read()
while(True):
ret2,frame2=cap.read()
if frame1 != frame2:
print('motion detected')
cap.release()
(From my understanding) what it should do, is each frame check for changes, and if found, print it on the screen. But it doesn't work.
Edit: I have no experience in OpenCV whatsoever, so please give me a code example too.
Edit 2:
Some extra info:
Reason for using VideoCapture(1,2) instead of (0) is because using 0 gives me this: [ WARN:0@0.021] global /tmp/pip-wheel-8c7uejek/opencv-python_88dbbad412c5416b992ae69de26299d6/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video0): can't open camera by index. Checking both ret1 and ret2 returns false. I'm using a usb camera.
Edit 3: I updated my code a bit:
import cv2
import sys
import time
cap=cv2.VideoCapture(15)
ret1,frame1= cap.read()
try:
while(True):
ret2,frame2=cap.read()
if frame1==frame2:
print('motion detected')
except:
cap.release()
print('closing...')
sys.exit(0)
I managed to find the correct camera index, but now, it gives me this warning every few seconds:
[ WARN:0@30.215] global /tmp/pip-wheel-8c7uejek/opencv-python_88dbbad412c5416b992ae69de26299d6/opencv/modules/videoio/src/cap_v4l.cpp (1013) tryIoctl VIDEOIO(V4L2:/dev/video15): select() timeout.
And it prints motion detected after that.