If you want to see how the things you make this time work, please see here (youtube video).
security_cam.py
import cv2
from datetime import datetime
import requests
import time
token = 'Your Token'
cap = cv2.VideoCapture(0)
lastframe = None
def send_msg():
url = 'https://notify-api.line.me/api/notify'
headers = {'Authorization':'Bearer '+token}
data = {"message":"Someone in your room."}
image = '/home/igor-bond/image.jpg'
file = {'imageFile': open(image, 'rb')}
r = requests.post(url, headers=headers, params=data, files=file,)
while cap.isOpened():
ret,frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
if lastframe is None:
lastframe = gray.astype("float")
cv2.accumulateWeighted(gray,lastframe,0.6)
frame_diff = cv2.absdiff(gray,cv2.convertScaleAbs(lastframe))
thresh = cv2.threshold(frame_diff,3,255,cv2.THRESH_BINARY)[1]
contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c) > 30:
time.sleep(0.2)
now = datetime.now()
img = cv2.resize(frame,(int(frame.shape[1]*0.5),int(frame.shape[0]*0.5)))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,f'{now}',(100,350),font,1,(0,0,255),4,cv2.LINE_AA)
cv2.imwrite('/home/igor-bond/image.jpg',img)
send_msg()
lastframe = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY).astype("float")
break
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
The first function is the one that sends an image to the line, which I've introduced before, so I'll omit it here. It is also introduced in here (youtube video). Then, this time, the difference between the frames is calculated, and if the pixel value is 3 or more, it is set to 255, and the contour is calculated there. If there is a contour that exceeds a certain area, it is considered to have moved. This is to prevent even very small changes from being detected. Then, add the time to the lower right so that it looks like a surveillance camera and save it as an image. The reason why I didn't change the name of the image is that if I change it, a lot of images will be accumulated, and this time I will activate the function to send to the line immediately after saving, so there is no problem even if I do this. Finally, repeat this with the frame at that time as the last one.
How to make this surveillance camera is also explained in Youtube, so please have a look if you like it. If you have any questions or advice, please comment.