MOHAN KRISHNA

0 %
Mohan Krishna
Multimedia Professional
Ai & ML Researcher & Enthusiast
  • Residence:
    India
  • City:
    Vijayawada
  • Age:
    46
AI/ML Enthusiast. New Media Trainer, VFX Artist, Non Linear Video Editor, Graphic Designer, Sound Editor and iOS App Designer.
Telugu
English
Hindi
Tamil
Proficiency:
Graphic Design
Web Design
Video & VFX
Machine Learning
Artificial Intelligence
Digital Marketing
Areas of Interest:
Take a look at some of the things I love working on.
  • Non Linear Video Editing
  • Graphic Design
  • Web Design
  • Audio Editing
  • Content Management Systems
  • Python
  • Deep Learning
  • OpenCV
  • Image Classification

Home Security Detector

October 4, 2022

Detector Code:

# STANDARD LIBRARY IMPORT
import os
import sys
# THIRD PARTY LIBRARIES
import cv2
from send_mail import send_mail
# http://www.pillalamarri.in/python/home-security-detector/

def detector():

    # VideoCapture will literally capture the first camera that it sees
    # available on your raspberry!
    cap = cv2.VideoCapture(0)

    # Loads the face_cascade classifier.
    face_cascade = cv2.CascadeClassifier(os.path.dirname(sys.argv[0]) +
                                         '/haarcascade_frontalface_alt2.xml')

    # Starts the infinite loop!
    while True:
        # capture frame-by-frame
        ret, frame = cap.read()

        # If there is any face on the camera, our cascade classifier will
        # return the position of the faces (if more than one) that are in the
        # screen else returns None.
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(frame, 1.3, 5)

        # Loop over the faces and draws a Green Square of 2px on the face!
        for (x, y, w, h) in faces:
            cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
            send_mail(frame=frame)

        # Display the resulting frame
        cv2.imshow('frame', frame)

        # If you press q, the screen will close
        # and the script will leave the loop.
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything is done release the capture
    cap.release()
    cv2.destroyAllWindows()

# http://www.pillalamarri.in/python/home-security-detector/
detector()

Send Email Code:

# http://www.pillalamarri.in/python/home-security-detector/
# STANDARD LIBRARY IMPORTS
import smtplib
from datetime import datetime, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
import sys

# THIRD PARTY IMPORTS
import cv2

# gmail must be set to allow less secure apps, so you can send emails.
# set it up on:
# https://myaccount.google.com/u/1/lesssecureapps?pli=1


def send_mail(frame):

    path = os.path.dirname(sys.argv[0])

    log_file = path + '/email.log'

    # Check if the last email was sent in an 1 minute tolerance.
    # We are going to use a .log file to keep track of it.
    if os.path.isfile(log_file):  # First check if file exists.
        with open(log_file, 'r') as f:
            date = f.read()
            date_to_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")

            if datetime.now() < date_to_datetime + timedelta(minutes=1):
                return

    # Update the email log.
    with open(log_file, 'w') as f:
        f.write(str(datetime.now()))

    # Create the jpg picture to attach.
    cv2.imwrite("intrude.jpg", frame)

    gmail_user = 'Enter_gmail-ID_to_be_used_for_sending_the_email'
    gmail_password = 'Enter_gmail-ID_password_here'
    recipient = 'Enter_gmailID_of_the_recipient'
    recipient = 'Enter_gmailID_of_the_recipient'
    message = 'Hey! It appears that someone is at home!!!'

    msg = MIMEMultipart()
    msg['From'] = gmail_user
    msg['To'] = recipient
    msg['Subject'] = "Someone is at Home!"
    msg.attach(MIMEText(message))

    # Attachment
    file = path + '/intrude.jpg'
    filename = 'intrude.jpg'
    attachment = open(file, "rb")

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(attachment.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s"
                    % filename)
    msg.attach(part)

    mail_server = smtplib.SMTP('smtp.gmail.com', 587)
    mail_server.ehlo()
    mail_server.starttls()
    mail_server.ehlo()
    mail_server.login(gmail_user, gmail_password)
    mail_server.sendmail(gmail_user, recipient, msg.as_string())
    mail_server.close()
# http://www.pillalamarri.in/python/home-security-detector/
Posted in PythonTags: