Opencv Email Notification
Hello, I've run into a road block in my opencv motion detection script while trying to adapt an "import smtplib" into my loop. I did not create the original script so I can't take any credit for that. The problem I'm having is, when I run the script as is, and movement is detected it sends me an email for every frame that detected motion - it sends me a BUNCH of emails. How can I change the script so it only sends me (1) email every 60 seconds once movement was detected (and doesn't have a bunch of emails waiting to send in the background) and the loop function continues without interruption?
# import the necessary packages
from __future__ import print_function
from pyimagesearch.basicmotiondetector import BasicMotionDetector
from imutils.video import VideoStream
import numpy as np
import datetime
import imutils
import time
import cv2
# initialize the video streams and allow them to warmup
print("[INFO] starting cameras...")
webcam1 = VideoStream(src=0).start()
webcam2 = VideoStream(src=4).start()#src 0,2,4 work
time.sleep(2.0)
# initialize the two motion detectors, along with the total
# number of frames read
camMotion1 = BasicMotionDetector()
camMotion2 = BasicMotionDetector()
total = 0
# loop over frames from the video streams
while True:
# initialize the list of frames that have been processed
frames = []
# loop over the frames and their respective motion detectors
for (stream, motion) in zip((webcam1, webcam2), (camMotion1, camMotion2)):
# read the next frame from the video stream and resize
# it to have a maximum width of 400 pixels
frame = stream.read()
frame = imutils.resize(frame, width=400)
# convert the frame to grayscale, blur it slightly, update
# the motion detector
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
locs = motion.update(gray)
# we should allow the motion detector to "run" for a bit
# and accumulate a set of frames to form a nice average
if total < 32:
frames.append(frame)
continue
# otherwise, check to see if motion was detected
if len(locs) > 0:
# initialize the minimum and maximum (x, y)-coordinates,
# respectively
(minX, minY) = (np.inf, np.inf)
(maxX, maxY) = (-np.inf, -np.inf)
# loop over the locations of motion and accumulate the
# minimum and maximum locations of the bounding boxes
for l in locs:
(x, y, w, h) = cv2.boundingRect(l)
(minX, maxX) = (min(minX, x), max(maxX, x + w))
(minY, maxY) = (min(minY, y), max(maxY, y + h))
# draw the bounding box
cv2.rectangle(fram
e, (minX, minY), (maxX, maxY),
(0, 0, 255), 3)
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("my_email", "my_password")
msg = "INTRUDER!"
server.sendmail("my_email", "email_to_send_to", msg)
server.quit()
# update the frames list
frames.append(frame)
# increment the total number of frames read and grab the
# current timestamp
total += 1
timestamp = datetime.datetime.now()
ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
# loop over the frames a second time
for (frame, name) in zip(frames, ("Webcam1", "Webacm2")):
# draw the timestamp on the frame and display ...
You didn't added namespace
SMPT
@supra56 do you mind giving me an example as to how that would be a practical solution in my script? I’m trying to limit the number of emails by implementing what you could refer to as an interval. My script currently runs and send me emails but it sends me an email for every frame that detects motion which can be hundreds depending on how long the object continues moving in my feed.
When you detected it for every 60 secs. Then write to file. Then load image into SMPT. Then send it. I am not available right now.Fortunately, Im busy working on automation robotics.
I noticed you had this
import smtplib
. But unfortunately, where iscv2.imwrite
to saved file for every 60 secs?You had this
cv2.imshow(name, frame)
Then, save itcv2.imwrite('image.jpg', frame)
. Just try with small project w/out using opencv. You needed to load filename into smpt. I'm using picamera.I can't post code in comment. Email me supra56 with google
Can you please share the running code of the above issue?