Jovian
⭐️
Sign In

Intruder Detection using Deep Learning version 2

Referenced from : https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam.py

Here we use face_recognition python module. This module runs upon dlib a c++ based library.

It consists of 4 steps.
  • Step-1 : Encode the picture using the HOG algorithm to create a simplified version of the image.
  • Step-2 : Finding the main landmarks in the face like nose, mouth and ears.
  • Step-3 : Encoding Faces. Here we use a pre-trained Convolution Neural Network developed by OpenFace.
  • Step-4 : Finding the person’s name from the encoding.Compare which person has the closest measurements to our face’s measurements.
In [2]:
import glob
import face_recognition
import cv2
import numpy as np
import jovian

Data Set Understanding

In [3]:
paths = glob.glob('C:\\Users\\vsneh\\Udemy-notebooks\\face_rec_mod_testing\\data\\*')
names = []
images = []
image_encodings = []
image_names = []
count_img = 0
for i in paths:
    images.append(face_recognition.load_image_file(i))
    image_encodings.append(face_recognition.face_encodings(images[count_img])[0])
    image_names.append(i.split('\\')[-1].split('.')[0])
    count_img+=1
    print(image_names)

['bill gates'] ['bill gates', 'donald trump'] ['bill gates', 'donald trump', 'elon musk'] ['bill gates', 'donald trump', 'elon musk', 'jeff bezos'] ['bill gates', 'donald trump', 'elon musk', 'jeff bezos', 'obama'] ['bill gates', 'donald trump', 'elon musk', 'jeff bezos', 'obama', 'snehit2']

Data encoding, Training and detecting

In [4]:
count = 0
cap = cv2.VideoCapture(0)
while True:
    ret,frame = cap.read()
    gray = frame[:, :, ::-1]
    face_locations = face_recognition.face_locations(gray)
    face_encodings = face_recognition.face_encodings(gray, face_locations)
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = face_recognition.compare_faces(image_encodings, face_encoding)
        name = 'Unknown'
        face_distances = face_recognition.face_distance(image_encodings, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = image_names[best_match_index]
        if(name=='Unknown'):
            cv2.imwrite('C:\\Users\\vsneh\\Udemy-notebooks\\face_rec_mod_testing\\intruders\\intru-{}.jpg'.format(count),frame)
            count+=1
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 3)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
    cv2.imshow("output",frame)
    if(cv2.waitKey(1)==ord('q')):
        break
cap.release()
cv2.destroyAllWindows()

Email Image selection(optional)

In [ ]:
myPath = glob.glob('C:\\Users\\vsneh\\Udemy-notebooks\\face_rec_mod_testing\\intruders\\*')
global countFolder
count=0
# if(len(myPath)<20):
#     moduleVal = 2
# else:
#     moduleVal = 4
for i in myPath:
    img = cv2.imread(i)
#     cap.set(3, 640)
#     cap.set(4, 480)
#     cap.set(10,180)
    print(blur)
    gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    blur = cv2.Laplacian(gray_img, cv2.CV_64F).var()
    if(count % 1 == 0 and blur > 320):
        cv2.imwrite("C:\\Users\\vsneh\\Udemy-notebooks\\Opencv_facebk_auto_uplod\\saved\\pic-{}.jpg".format(count), img)
        count += 1
    

Emailing of Intruder images

In [5]:
import getpass
import smtplib
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()

Out[5]:
(250,
 b'smtp.gmail.com at your service, [175.101.108.71]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
In [6]:
server.starttls()
Out[6]:
(220, b'2.0.0 Ready to start TLS')
In [11]:
username = 'v.snehith999@gmail.com'
passwd = getpass.getpass()
server.login(username,passwd)


from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from datetime import datetime
#  For advanced mails and attachment files. 
msg= MIMEMultipart()
msg['from'] = username
msg['to'] = username
msg['subject'] = "Images"
text = "Check these sample unknown person images, if some thing *fishy!!* check intruder folder immediately"
msg.attach(MIMEText(text))
F = glob.glob("C:\\Users\\vsneh\\Udemy-notebooks\\face_rec_mod_testing\\intruders\\*")
for i in F:
    with open(i,'rb') as f:
            part = MIMEApplication(f.read())
            part.add_header('content-Disposition','attachment',filename='{}.jpg'.format(count+1))
            msg.attach(part)
server.sendmail(username,username,msg.as_string())

## FOR SIMPLE TEXT
# text = "Link Intruder Images here"
# server.sendmail(username,username,text)
········
Out[11]:
{}
In [ ]:
jovian.commit(project="intruder-detection-version2")
[jovian] Attempting to save notebook..
In [ ]:
 
In [ ]: