Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch intruders and email an attachment of them #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion box.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import config
import face
import hardware

import mailer

if __name__ == '__main__':
# Load training data into model
Expand All @@ -23,6 +23,8 @@
print 'Running box...'
print 'Press button to lock (if unlocked), or unlock if the correct face is detected.'
print 'Press Ctrl-C to quit.'
# Set the variable for whether any intruder has been caught to false.
emailSent = False
while True:
# Check if capture should be made.
# TODO: Check if button is pressed.
Expand Down Expand Up @@ -56,3 +58,12 @@
box.unlock()
else:
print 'Did not recognize face!'
# Someone other than the box owner is trying to get in. Save a picture of their face...
if (not emailSent):
# Only send an email if the user has email information stored
if (not config.SENDER_EMAIL == ''):
filename = os.path.join('./culprits/', str(now.year) + str(now.month) + str(now.day) +'_face.png')
cv2.imwrite(filename, crop)
# Then email it to the box owner
mailer.email(filename)
emailSent = True
9 changes: 9 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
# Filename to use when saving the most recently captured image for debugging.
DEBUG_IMAGE = 'capture.pgm'

# Email settings for capturing intruders
# Server to send from. Gmails is left as an example
SMTP_SERVER = '' #'smtp.gmail.com:587'
# Sender = email address to send alert from
SENDER_EMAIL = ''
SENDER_PASSWORD = ''
# Recipient = email address to receive alerts. If left blank will get sent to sender
RECIPIENT_EMAIL = ''

def get_camera():
# Camera to use for capturing images.
# Use this code for capturing from the Pi camera:
Expand Down
1 change: 1 addition & 0 deletions culprits/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is where any images of intruders will be saved
48 changes: 48 additions & 0 deletions mailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Raspberry Pi Face Recognition Treasure Box
Email Script
Script & implementation by Michael Rosenberg
Project by Tony DiCola
"""

import smtplib
import datetime
import config
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders

# Set now to whatever time it currently is
now = datetime.datetime.now()

def email(filename):
# Define the email subject
SUBJECT = "Unknown intruder on " + str(now.day)+"-"+str(now.month)+"-"+str(now.year)
# Create a variable based on the configuration recipient
RECIPIENT_EMAIL = config.RECIPIENT_EMAIL
# If the recipient field is blank in the config the email should go to the sender
if (RECIPIENT_EMAIL == ''):
RECIPIENT_EMAIL = config.SENDER_EMAIL
# Create the email
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = config.SENDER_EMAIL
msg['To'] = RECIPIENT_EMAIL
part = MIMEBase('application', "octet-stream")
part.set_payload(open(filename, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="'+filename+'"')
msg.attach(part)
# Connect to the email server
server = smtplib.SMTP(config.SMTP_SERVER)
server.ehlo()
server.starttls()
# Log into the sender email address and send the email
try:
server.login(config.SENDER_EMAIL, config.SENDER_PASSWORD)
server.sendmail(config.SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
print "Email Sent!"
except Exception:
print('Login Failed!')
print(traceback.format_exc())
# Close server connection
server.quit