Buy Me A Coffee

Hi !

I’ve write a lot about how to use AI models in C# to perform tasks like Face recognition, speech analysis, and more. During the Chicago CodeCamp, someone ask me about how to perform Face Recognition in Python. I didn’t have any working sample to showcase this, and I failed in try to write a 2 min app. So I added this into my ToDo list.

For this demo I’ll use Anaconda as the base Python distribution and Visual Studio Code as the code editor. There are several packages to perform face detection in Python. I’ll use a mix between OpenCV and Adam Geitgey Face Recognition package to use the camera and detect and recognize faces.

I’ll start by installing some packages to use in python app: dlib, openCV and face_recognition

"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Anaconda3_86/python.exe" -m pip install dlib --user  

"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Anaconda3_86/python.exe" -m pip install face_recognition --user

"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Anaconda3_86/python.exe" -m pip install opencv-python --user  

And, the first step will be to detect faces and draw frames around them. All of this in 20 lines of code

import face_recognition
import cv2
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

When we run the app, we will see the camera feed and frames around the detected faces. In my next post I’ll add some extra code to perform face recognition.

Happy Coding!

Greetings @ Toronto

El Bruno

Resources

19 responses to “#VSCode – Let’s do some #FaceRecognition with 20 lines in #Python”

  1. […] explained how to write a couple of lines in Python to perform live face detection in a webcam feed [Post]. Check the resources section to find more about the tools I’m […]

    Like

  2. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  3. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  4. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  5. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  6. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  7. […] Let’s do some Face Recognition with 20 lines in Python […]

    Like

  8. First off All, thank you for sharing your experience.
    I have a question, i ran this code in my laptop and did perform well, in my desktop the application ran well, but i use only two images to recognize, if i update this to 100k images, there is a Way to optimize the processing?

    Best regards

    Liked by 1 person

    1. Hi Marcelo
      there are a couple of techniques that you can use, like multithreading, or a smart queue system. It all depends on your complete scenario.
      However a search for multithreading or queue may help !
      Regards

      Like

  9. Would love to chat about the basics with you, about the opencv, having issues running your code for some reason to try and learn a bit

    Like

  10. Andrew Oakley Avatar
    Andrew Oakley

    Hi, I am having some trouble getting the code to run on vs code if you could email me and help me it would be much appreciated!

    Like

    1. Let me know how I can help …

      Like

  11. Hey, I’m trying this code but when I try to run is, it says ” ModuleNotFoundError: No module named ‘cv2’ “. I’m not sure why since I did install opencv. Though, when I did pip install at the start it was giving me some errors about “could not build wheels”, I believe that is what it called it, but I’m new to coding so I’m not sure what that means. Do you have any pointers? Feel free to email me if that is easier for you than responding. Thank you!

    Like

    1. hey Raye,

      I may suggest you check the installation of the package. More information on the package in their home page: https://github.com/ageitgey/face_recognition

      Best

      Like

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading