Your Dashboard
Sign-in
RegularPython
  • Learn & Earn
  • Membership
  • RoadMaps
  • Short Videos
  • Pro Tip Videos
  • Online Tests
    • Online Test
    • Interview Questions
    • Online Store
  • Jobs
    • Student Reviews
    • Students Live Interviews
    • Project Explanation
    • Mock Interviews
    • Apply for jobs
    • Contact for help
  • Sign-in

Blog Information

  1. Home
  2. Blog
  3. Blog Information

regularpython@gmail.com

How to secure your home by using python code

  • by Sairam Doddaparthi
  • by April 22, 2020, 1:48 p.m.
  • in Python, Regualrpython, Regular Python, Gmail, Gmail in python, opencv

This is a small project. The aim of this project is, how to secure your home by using python code. 

Description: This project is useful to get an alert to our mail when someone enters our home.

Requirements:

1.Python

2.OpenCV

3.Gmail

The entire source code is given below.

https://pypi.org/project/opencv-python/ use this link to install OpenCV.

 

mail.py:

'''
Created on 03-Mar-2018

@author: sairam
'''
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from password import password_id, mail_id

fromaddr = mail_id
password = password_id
def SendMail(ImgFileName):
    img_data = open(ImgFileName, 'rb').read()
    msg = MIMEMultipart()
    msg['Subject'] = 'subject'
    msg['From'] = fromaddr
    msg['To'] = fromaddr

    text = MIMEText("Some one entered into your home.")
    msg.attach(text)
    image = MIMEImage(img_data, name=os.path.basename(ImgFileName))
    msg.attach(image)

    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.ehlo()
    s.starttls()
    s.ehlo()
    s.login(fromaddr, password)
    s.sendmail(fromaddr, fromaddr, msg.as_string())
    print("Mail send successfully.")
    s.quit()

 

 

object_alarm.py

import cv2
from datetime import datetime
import mail


#Function to calculate difference between images.
def diffImg(t0, t1, t2):              
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    return cv2.bitwise_and(d1, d2)

# Threshold Range for triggering motion detection
threshold = 130000

#Capture frames                     
cam = cv2.VideoCapture(0)

window_name = "Regularpython (Home Alert)"
cv2.namedWindow(window_name)

# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)


# Lets take 1 pic per sec
timeCheck = datetime.now().strftime('%Ss')
count = 5
while True:
    # read from camera
    ret, frame = cam.read()          
    
    # this is total difference number
    totalDiff = cv2.countNonZero(diffImg(t_minus, t, t_plus))
    
    # make a text showing total diff.    
    text = "Threshold Range: " + str(totalDiff)
    
    # display it on screen                
    cv2.putText(frame, text, (100,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
    if totalDiff > threshold and timeCheck != datetime.now().strftime('%Ss'):

        print("some one entered into your home")
        dimg= cam.read()[1]
        cv2.imwrite('test.jpg', dimg)
        mail.SendMail("test.jpg")

        # if count == 5:
        #     cv2.imwrite('test.jpg', dimg)
        #     mail.SendMail("test.jpg")
        #     count=count - 1
        # elif count == 0:
        #     count = 5
        # else:
        #     count=count - 1
    
    timeCheck = datetime.now().strftime('%Ss')
    # Read next image
    t_minus = t
    t = t_plus
    t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
    cv2.imshow(window_name, frame)

    key = cv2.waitKey(10)
    if key == 27:             
        cv2.destroyWindow(window_name)
        break

 

password.py

'''
Created on 22-Feb-2018

@author: sairam
'''
password_id = "*************"
mail_id = "##############"
tags: Python Regualrpython Regular Python Gmail Gmail in python opencv
  • share

User Questions And Answers

Leave Your Request

RegularPython

This website helps to learn ptyhon from basics to advanced level. Everything explained with some practical examples.

  • About
  • Privacy
  • Video Courses
  • Online Test
  • Python Blog

Marthahalli, Bengalore.
regularpython@gmail.com

© Copyright 2020 RegularPython.

  • Sign-in
  • Sign-up