Skip to content

Commit b7ac424

Browse files
committed
Add Raspberry Pi example
1 parent 85a643d commit b7ac424

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ See [this example](https://github.com/ageitgey/face_recognition/blob/master/exam
7171

7272
## Installation
7373

74-
Python 3 / Python 2 are fully supported. Only macOS and
75-
Linux are tested. I have no idea if this will work on Windows. A
76-
[pre-configured VM](https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b)
77-
is also available.
74+
Requirements:
75+
* Python 3+ or Python 2.7
76+
* macOS or Linux (Windows untested)
77+
* [Also can run on a Raspberry Pi 2+ (follow this specific instructions)](https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65)
78+
* A [pre-configured VM image](https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b) is also available.
7879

7980
Install this module from pypi using `pip3` (or `pip2` for Python 2):
8081

@@ -212,6 +213,7 @@ All the examples are available [here](https://github.com/ageitgey/face_recogniti
212213
* [Find and recognize unknown faces in a photograph based on photographs of known people](https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py)
213214
* [Recognize faces in live video using your webcam - Simple / Slower Version (Requires OpenCV to be installed)](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam.py)
214215
* [Recognize faces in live video using your webcam - Faster Version (Requires OpenCV to be installed)](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py)
216+
* [Recognize faces on a Raspberry Pi w/ camera](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_on_raspberry_pi.py)
215217
* [Run a web service to recognize faces via HTTP (Requires Flask to be installed)](https://github.com/ageitgey/face_recognition/blob/master/examples/web_service_example.py)
216218

217219
## How Face Recognition Works

examples/facerec_on_raspberry_pi.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This is a demo of running face recognition on a Raspberry Pi.
2+
# This program will print out the names of anyone it recognizes to the console.
3+
4+
# To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
5+
# the picamera[array] module installed.
6+
# You can follow this installation instructions to get your RPi set up:
7+
# https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
8+
9+
import face_recognition
10+
import picamera
11+
import numpy as np
12+
13+
# Get a reference to the Raspberry Pi camera.
14+
# If this fails, make sure you have a camera connected to the RPi and that you
15+
# enabled your camera in raspi-config and rebooted first.
16+
camera = picamera.PiCamera()
17+
camera.resolution = (320, 240)
18+
output = np.empty((240, 320, 3), dtype=np.uint8)
19+
20+
# Load a sample picture and learn how to recognize it.
21+
print("Loading known face image(s)")
22+
obama_image = face_recognition.load_image_file("obama_small.jpg")
23+
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
24+
25+
# Initialize some variables
26+
face_locations = []
27+
face_encodings = []
28+
29+
while True:
30+
print("Capturing image.")
31+
# Grab a single frame of video from the RPi camera as a numpy array
32+
camera.capture(output, format="bgr")
33+
34+
# Find all the faces and face encodings in the current frame of video
35+
face_locations = face_recognition.face_locations(output)
36+
print("Found {} faces in image.".format(len(face_locations)))
37+
face_encodings = face_recognition.face_encodings(output, face_locations)
38+
39+
# Loop over each face found in the frame to see if it's someone we know.
40+
for face_encoding in face_encodings:
41+
# See if the face is a match for the known face(s)
42+
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
43+
name = "<Unknown Person>"
44+
45+
if match[0]:
46+
name = "Barack Obama"
47+
48+
print("I see someone named {}!".format(name))

examples/obama_small.jpg

32.6 KB
Loading

0 commit comments

Comments
 (0)