AI Learning – OpenCV Face Recognition and Labeling

This article translated from another post in Chinese by assisting of ChatGLM LLM.

Facial recognition is a mature technology that has been done by Google Picassa since a long time. The image recognition for faces is not bad, but when you really need it, you can’t find a set of Linux tools to do following jobs at once.

  1. Faces recognition
  2. Labeling faces
  3. Supporting CLI
  4. Light and easy to use

So, I started to look for it myself, and the process was pretty smooth, since it’s a mature technology. The recognition accuracy is still good, and there are many aspects that can be fine-tuned. I’ll only introduce a simple and easy-to-use method that I use as a technology backup.

Facial recognition project

This article is derived from here, and the author also puts the code on github. The author originally intended to use GPU for image recognition, so if you directly downloaded and executed the code, it would be very slow, almost impossible to move. If someone wants to try it first, they can change the “cnn” in the code. There is also a bug: change line “video_capture” to  “video”.

Installing Basic Environment

Some basic environment settings (such as Anaconda and shared scripts) have already been described in the article [ Shared Operations]. Please take a look and make sure all commands work correctly.

Creating Conda Environment

Since the dependency of each project is different, a separate environment will be created for each case.

The package to be installed is openCV-python-rolling, which should be the development version. However, since the pip OpenCV-python was still using OpenCV 4.x, it cannot support Chinese characters simply. Therefore, here we use openCV-python-rolling, which is compatible with OpenCV 5.x, making it more convenient to use Chinese.

 

Re-compiling Dlib for CUDA

OpenCV will use dlib, but the default dlib installed from pip does not support GPUs. It needs to be recompiled. However, to recompile, you need to install the build tools cmake, vs-build tools, and Nvidia cuDNN first.

  • cmake Download here.
  • Buildtools needs to be downloaded from Microsoft’s website. Here we download the version of vs2019 “Build Tools for Visual Studio 2019”. The download requires a Microsoft account.
  • cuDNN also requires registration and downloading, and download the corresponding file according to your own CUDA version. After opening, extract the file to C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2 (taking CUDA 12.2 as an example).
  • You can refer to here

Compile DLIB

After compilation, use the following command to test whether the GPU supports dlib

If there is support, it will show True. There is a line os.add_dll_directory() above, which is not needed, but you need to add it this time, otherwise you will encounter some dll loading failure problem. It may be related to the python version.

Download my project

Based on the project mentioned earlier, I made some modifications to use web to mark faces and handle photos. First, download and install the software.

In the project, there is a photo of former US President Trump, which is used as an example. The file is in the img/unhandled folder.

Extract unfamiliar faces

This command will process the photos in img/unhandled and recognize faces. If a unfamiliar face is recognized, the face will be cut and placed in img/unknown_faces. The processed photo will then be moved to img/handled.

Mark unfamiliar faces

For the face marking part, files under img/unknown_faces will be searched for marking. After marking, they will be moved to the directory “img/known_faces/face name”.

At this point, a web server will be executed, and the browser should be opened at http://127.0.0.1:8501.There are three options to choose from here:

  1. Characters: Characters that have been identified can be selected directly. Once selected, it will be marked directly without further confirmation.
  2. New: You can add character names and create new tags. After typing, press Enter to confirm.
  3. Don’t know: If you don’t know, just press the button. The face will be moved below img/known_faces/junk

Looking for a familiar face

Use list_face.py to find if a specific file has recognized faces. This script will load all the recognized faces in the img/known_faces directory for searching.

Use Camera for real-time recognition

To start the webcam for real-time recognition, use the following command:

The program will load the recognized faces to display.

Use GPU

The example code defaults to using the hog model for recognition, but if you use GPU, you should change the MODEL files to cnn. Changing the MODEL files to cnn will prevent the program from freezing. If you want to use GPU, you just need to change the MODEL files in the following three files:

  • camera_face_recognize.py
  • extract_unknown_face.py
  • list_face.py

The GPU is mainly used in the camera part and is very smooth. As for the accuracy, I haven’t compared it extensively, so I’m not sure.

 

Adjust recognition tolerance

When searching for faces known to you, you can adjust a tolerance value between 0 and 1. A lower value means stricter, and a higher value means more relaxed (easy to make mistakes). This needs to be checked more to know the misidentification rate.

Modifying the value of TOLERANCE in the code is the same for the three above files, you don’t need to set them all to the same value. For example, when extracting unknown faces, you can be more relaxed when comparing them with new faces, while when comparing known faces, you can be more strict (easier to make mistakes).

Improving speed

When loading the known faces, it is directly re-calculated from the image file, which may take more time. However, the features (face encodings) should be saved and loaded directly from the files, but I haven’t studied this yet and it could be useful for those who want to speed up.

Conclusion

Researching this facial recognition has also taught me a lot, and I happened to learn about Streamlit for creating web pages, which is indeed more convenient than the traditional frontend and backend approach. There are many applications of facial recognition, and it’s easy to create various functions. The key is to learn the basic elements, and it will be easier to combine them in the future.

Leave a Reply(Name請以user_開頭,否則會被判定會垃圾息)

請輸入答案 + 49 = 57