House Safety using Computer Vision by OpenCV and NumPy: PART-1

Yukti Deshlan
Nur: The She Code Africa Blog
4 min readMar 12, 2021
Photo by Thought Catalog on Unsplash

While today’s burglary statistics show an overall decrease in burglary rates, thousands of homes (roughly 325,000) are still being broken into every year. There are roughly 2.5 million burglaries a year, 66% of those being home break ins.

Police solve only 13% of reported burglary cases due to lack of a witness or physical evidence. Thus, the objective of this project is to prevent break ins like these, and ensure a way to secure our home from miseries like these, we initiate a way to have record of all the people coming to our house.

This project is made as contribution to the security sector, with the burglary rate still being in thousands and to keep ourselves and our families safe, we can develop this project using “face recognition” with OpenCV and NumPy. So, we consider making a project to deal with the security of people entering and leaving the house and alerting people in the house for any unknown break-ins.

What is Face Recognition?

Facial recognition system is a system, made to match human face from a set of data of images, to serving many purposes through ID verification services, works by proportionally measuring the dimensions of a face from an image. Applications of face recognition includes Security system, Aid forensic investigations, Diagnose diseases, Track school attendance, Secure transactions and etc.

The process for face recognition is as follows:

  1. Face detection
  2. Face Analysis
  3. Facial feature extraction
  4. Face Recognition

Building Dataset (by capturing face dimensions):

The dataset for this project does not involve any real-time highly used data records, instead it’s our and our roommate’s faces that is to be recorded as the dataset. We make this possible by a series of steps involving reading the dimensions of face through haar-cascades, converting to grayscale, capturing the images of face, and then building the dataset.

The dataset can include you, your family, roommates and the other people who leave and enter your house. So, when the images are taken, the dataset will look something like set of images captured in grey color.

Modules needed:

• OpenCV: This library is a major pillar when working with Face recognition projects, as it is a aimed on focusing real-time applications and highly used python library, though developed in C/C++.

• NumPy: NumPy is more of mathematically used in for arrays and matrices with many functionalities helping in building the dimensions of the captured face.

• OS: A standard module, used in most of the python projects made to interacting with the operating system of the computer.

• OS Path: Just as the Operating System, this module is mend to providing path parameters in handling large files or documents.

Play sound: As the name suggests, this module is made for playing simple audio files.

Steps to installing the packages:

Open Search bar and start typing ‘Command prompt’, make sure you have the latest pip version installed in your computer. Now, start installing packages simultaneously by the following convention:

Pip install opencv-pythonPip install numpyPip install playsound

Steps involved in coding the modules (short summary):

1. Building the dataset with user’s face, starting by importing the required above mentioned packages.

2. We use haar-cascades in classifying the components of the face.

3. Now, we convert the face to gray scale, because it involves less complications and each pixel can be significant enough for the computer to read.

4. Marking the co-ordinates of the face features to be captured and read into a separate file (make sure it’s in the same folder as the other file).

5. Once, the dimensions of face and camera monitoring is done, voila your dataset is built!

6. Now, we train the module to detect face, by running through the dataset built.

7. But for the module to detect the face it should be done in grayscale, as in the above case.

8. Lastly, make sure the face matches with dataset by mentioning ‘the user’ and adding a “alert sound”, for an ‘unknown’ person.

Let’s start building dataset

Step-1: import the packages required.

#imporing the modulesimport cv2import numpy as np

Step-2: Use haar-cascades in detecting the image, you can find ‘haarcascade_frontalface_default.xml’ file on your computer or even download the file from the internet.

#converting the captured images to grayscaleface_classifier=cv2.CascadeClassifier(cv2.data.haarcascades + ‘haarcascade_frontalface_default.xml’)def face_extractor(img):gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)faces=face_classifier.detectMultiScale(gray,1.3,5)if faces is():return Nonefor(x,y,w,h) in faces:cropped_faces=img[y:y+h,x:x+w]return cropped_facescap=cv2.VideoCapture(0)count=0

Step-3: Now add a new file in the same folder to store your data where all the images will be stored in .jpg file, through specifying the co-ordinates of the face to be taken, as mentioned below.

#preparing a new file for dataset and collecting samples of the facewhile True:ret, frame=cap.read()if face_extractor(frame) is not None:count+=1face=cv2.resize(face_extractor(frame),(200,200))face=cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)file_name_path= ‘**Add a path where you want to store the dataset**’+str(count)+’.jpg’cv2.imwrite(file_name_path,face)cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)cv2.imshow(‘Face Cropper’,face)else:print(“face not found”)passif cv2.waitKey(1)==13 or count==100:breakcap.release()cv2.destroyAllWindows()print(‘Collecting samples completed!’)

Output: The file of the dataset taken would be images of different postures of your face taken in grayscale.

--

--

Yukti Deshlan
Nur: The She Code Africa Blog

Student at VIT, Bhopal || Web developer || ML enthusiast || Tech writer