Skip to content

Instantly share code, notes, and snippets.

@bsodhi
Created July 26, 2020 14:55
Show Gist options
  • Save bsodhi/2150af389438ee8c93754ac1eeb9898d to your computer and use it in GitHub Desktop.
Save bsodhi/2150af389438ee8c93754ac1eeb9898d to your computer and use it in GitHub Desktop.
Installing ageitgey/face_recognition on RPi buster

Installing face recognition on RaspberryPi 3 Buster

I assume that you have Raspbian 10 (Buster) installed on your RPi3, and are able to get to the shell in your RPi3. Once you are at the shell, do the following to install face_recognition:

  1. sudo apt-get update

  2. sudo apt-get upgrade

  3. sudo apt-get install build-essential cmake gfortran git wget curl graphicsmagick libgraphicsmagick1-dev libatlas-base-dev libavcodec-dev libavformat-dev libboost-all-dev libgtk2.0-dev libjpeg-dev liblapack-dev libswscale-dev pkg-config python3-dev python3-numpy python3-pip zip libjasper-dev libqtgui4 python3-pyqt5 libqt4-test libhdf5-dev libhdf5-serial-dev

  4. pip3 install opencv-python==3.4.6.27

Test program

Assuming you are at the RPi desktop (e.g., via VNC), you can run the following program to test the setup. This program reads video frames from a connected webcam (e.g., a USB webcam), and shows the faces found in a frame with red box around the face.

Copy the following code into test_frec.py (name doesn't matter), and run from the shell python3 test_frec.py.

import face_recognition
import cv2
import numpy as np

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
print("Got the web cam...")


# Initialize some variables
face_locations = []
process_frame = True

while True:
    # Get a frame from webcam
    ret, frame = video_capture.read()

    # Resize frame to 1/4 size
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Process only alternate frames
    if process_frame:
        # Find locations of faces in current frame
        face_locations = face_recognition.face_locations(rgb_small_frame)

    process_frame = not process_frame

    # Display the results
    for (top, right, bottom, left) in face_locations:
        # Scale back up face locations to 4x size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4
        # Draw a box around the face
        cv2.rectangle(frame, (left-50, top-100), (right+60, bottom+50), (0, 0, 255), 2)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment