Skip to content

arnavg115/facialrecognition

Repository files navigation

Face Detection using Tensorflow

This is a project I have worked on using code from many sources and some of my own.

Disclaimer: NOT ALL OF THE CODE IS MINE

  1. A lot of collect.py was from Sourav Johar's rock-paper-scissors project. However I added the face detection algorithm to collect the data on the face. For the face detection algorithm I used Adarsh Menon's post. I would check both of them out. In addition I do not own the haarcascade_frontalface_default.xml. This is from the opencv github page and was made by Rainer Lienhart. Make sure to read the copyright at the top of the file as downloading this file means you agree to it. To see this go to file and also check out the opencv github.

The code:

  1. Get all of the files

Clone this repository

git clone https:/arnavg115/facialrecognition.git
  1. Install all of the nescessary packages

Install tensorflow.

Quick note: If you are on windows I would recommend following this guide as there are some other files you may have to install.

pip install tensorflow

Install opencv

pip install opencv-python
  1. Gather the nescessary data

In the terminal type

python collect.py /name of person/ /number of images/

For the /name of person/ put in the name of the person you are collecting data for. For the /number of images/ put in the number of images you wish to capture. I recommend 300 images as sometimes the face detection algoorithm can wrongly detect an object and the save it. After hitting enter the program should launch a window where you can see your webcam's stream. I would make sure it is able to see your face and puts a box around it. Once this is done hit A on your keyboard for it to start capturing images. It should create a folder under the name "image_data" and the images will be saved to "/name of person/". Now I would go through the pictures and delete all of the pictures where there is no face and delete them. Repeat this for as many faces you want the model to recognize.

  1. Train the model Once you have captured the needed data, run the training script.
python train.py

It might take a while depending on your hardware. It should start with each of the epochs. Then it should save the model to a file called 'model.h5'. Note: Delete this file before retraining as it might lead to errors in the program.

  1. Use the AI to detect faces So now that you have done all of the prep work you can start the program with
python detect.py
  1. It is able to detect many faces at the same time. When it is run it should open the same webcam window used to get the training data. Just like before it should box your face. Then you might notice there is a new label for the face which is what my model has predicted.

Other stuff

Feel free to modify the code that I wrote and also checkout the projects of both Sourav Johar and Adarsh Menon as they are very interesting as well and really well made.

Some errors

Make sure tensorflow and keras are updated to the latest versions. I have only tested it with two people so there might errors with more than two people. If for some reason opencv(cv2) or numpy is giving errors please downgrade them using pip. One basic issue you may have is with pip shown below. This program maybe slow on your computer and in my testing this program maybe cpu intensive and also use a lot of ram.

Example:

Downgrade numpy.

pip install numpy==1.19.3

Downgrade opencv

pip install opencv-python==4.4.0.44

pip error

If you try for example

pip install tensorflow

and it gives an error

-bash: pip: command not found

Instead of pip for all of the pip commands use pip3. So type

pip3 install tensorflow

Make sure tensorflow and keras are updated to the latest versions. I have only tested it with two people so there might errors with more than two people.

About

Facial recognition using tensorflow and opencv.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages