kirupa karan
Published © GPL3+

Smart Home Monitoring - Intruder Alert System

Our home is special to us, so lets add security feature to it by introducing Intruder Alert System using Face Recognition and BOLT module

IntermediateFull instructions provided4 hours1,221
Smart Home Monitoring - Intruder Alert System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
LED (generic)
LED (generic)
×1
Buzzer
Buzzer
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Camera (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
telegram
pycharm

Story

Read more

Schematics

Smart Home Monitoring - Schematic Hardware Setup

Code

bolt_util.py

Python
This is a helper module, which links BOLT module and Telegrams API to the main module. This module is responsible for trigger LED and Buzzer and sending alert messages to Telegram channel
from boltiot import Bolt
import time
import requests, json

my_api = "30aaeb8c-7b6b-49c0-bc96-043a7e85c3f3"
my_dev = "BOLT290867"
my_bolt = Bolt(my_api, my_dev)
telegram_chan_id = "@smarthome_kirupa"  #This is the channel permamnent link generated earlier. Prefixed by '@' 
telegram_bot_id = "bot1150761023:AAHAgV5laxCoKlUYAUb4YUhzn1DFvf5J8nw" #This is bot id generated earlier "bot${Telegram_bot id}"

def known_entry():

    # Blink LED for two seconds
    response = my_bolt.digitalWrite('1', 'HIGH')
    #print (response)

    time.sleep(2)
    response = my_bolt.digitalWrite('1', 'LOW')
    #print(response)


def intruder_entry():

    # Blink LED and buzzer thrice
    for i in range(0,3):
        response = my_bolt.digitalWrite('1', 'HIGH')
        #print(response)

        response = my_bolt.digitalWrite('0', 'HIGH')
        #print(response)

        time.sleep(2)

        response = my_bolt.digitalWrite('1', 'LOW')
        #print(response)

        response = my_bolt.digitalWrite('0', 'LOW')
        #print(response)


def send_telegram_alert(photo):
    """Sends message via Telegram"""
    url = "https://api.telegram.org/" + telegram_bot_id + "/sendPhoto"
    data = {
        "chat_id": telegram_chan_id,
        "caption": "!!! Alert - New intruder !!!"
    }
    files = {'photo': open(photo, 'rb')}
    try:
        response = requests.request(
            "POST",
            url,
            params=data,
            files=files
        )
        #print("This is the Telegram URL")
        #print(url)
        print("This is the Telegram response")
        print(response.text)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occurred in sending the alert message via Telegram")
        print(e)
        return False

Home_monitoring.py

Python
Main file which has code parts like openCV video capture, read video frame by frame, pass the frames input to the face_recognition package, which locates facial landmarks and encodes all the facial details into 128-d embedding vector.
import cv2
import face_recognition
import PIL.Image
import PIL.ImageDraw
import dataset as ds
import bolt_util as bu
import os

# Generate dataset
ds.generateKnownFaceEncodings()

# print names in the dataset
#ds.printKnownList()

# Create openCV VideoCapture object for capturing from the input camera
VideoCapture = cv2.VideoCapture(0)
process = True
process_count = 0

while True :
    # Start Capturing the frames
    ret, frame = VideoCapture.read()

    # Identify the face locations in every frames
    face_location = face_recognition.face_locations(frame)

    if not len(face_location):
        #print(f"No one outside the door")
        continue

    # Try 10 times to detect the face in the frame.
    if process:

        process_count = process_count + 1
        for top, right, bottom, left in face_location:
            #print(f"Left:{left}, Right:{right}, Bottom:{bottom}, Top:{top}")
            cv2.rectangle(frame, (left,top), (right,bottom), (204, 255, 204), 3)

        face_encodings_1 = face_recognition.face_encodings(frame, known_face_locations=face_location)
        for encodings in face_encodings_1:
            name = "Unknown"
            results = face_recognition.compare_faces(ds.known_encoding, encodings, 0.7)
            #print("Printing result: ", results)

            if True in results:
                index = results.index(True)
                name = ds.known_names[index]

                print(" !!! Hey... Welcome back " + name + " !!! ")
                # Blink BOLT LED for two seconds. (Which indicates known person)
                bu.known_entry()
                process = not process

            cv2.putText(frame, name, (left, bottom + 35 - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (204, 255, 204), 1)

        if process and process_count == 10:
            process = not process

            print("!!! ALERT - Intruder Entry !!! ")

            # Blink BOLT LED thrice for and sound buzzer. (Which indicates intruder)
            bu.intruder_entry()

            # Send the intruder image in Telegram
            # First the save the frame temporarily
            cv2.imwrite("temp_intru.jpg", frame)
            bu.send_telegram_alert("temp_intru.jpg")
            os.remove("temp_intru.jpg")

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

VideoCapture.release()
cv2.destroyAllWindows()

dataset.py

Python
This is helper module, which generates and tracks the known person list. It generates the known person's encodings by taking all the pictures present in the local directory
import face_recognition
from pathlib import Path
import time

# Store known persons info
known_names = []

# Global known image encoding data
known_encoding = []

# Generate the dataset using the jpg files available in the current directory
def generateKnownFaceEncodings():
    print("Generating Dataset for the known persons")
    start = time.time()
    data_count = 0

    for image_path in Path(".").glob("*.jpg"):
        data_count = data_count + 1
        known_image = face_recognition.load_image_file(image_path)

        # Generate Facial Encoding for all the images (Assumption only individual photos are present in the pictures)
        known_image_encoding = face_recognition.face_encodings(known_image)[0]

        #Append to the global encoding list
        known_encoding.append(known_image_encoding)

        #Retrieve the person name from the input fileq
        image_path = (str(image_path).upper()).split('.')[0]
        known_names.append(str(image_path).split('_')[0])

    print("Dataset generation completed for ", data_count, " in ", time.time()-start, " seconds")


def printKnownList():
    for names in known_names:
        print(names)

Credits

kirupa karan

kirupa karan

1 project • 1 follower

Comments