# Facial Recognition With Raspberry Pi and OpenCV

**URL:** <https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845>\
**Category:** Guides\
**Created:** [29 July 2021 00:15 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845 "2021-07-29T00:15:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [29 July 2021 00:15 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/1 "2021-07-29T00:15:14Z")

</div>

Hey all, just put the finishing touches on the **“[Facial Recognition With Raspberry Pi and OpenCV](https://core-electronics.com.au/tutorials/face-identify-raspberry-pi.html)”** guide.

This guide will show you exactly how to have your Raspberry Pi credit-card-sized computer be able to spot human faces, how to train it to know your face, and run code so that it will successfully identify you when it sees you. Then I’ll take it another step and show you how you can use your face to control a servo attached to the **[Raspberry Pi 4 Model B](https://core-electronics.com.au/catalog/product/view/sku/CE06974)**. Literally use your face to control the GPIO!

This is possible thanks to two excellent open-source projects. These are OpenCV which provides a huge free resource to solve real-time computer vision problems and the Python Face Recognition Package which computes bounding boxes around a face in real-time. There is just so much potential with this software to take projects to amazing places.

[![](https://core-electronics.com.au/media/kbase/Feature_Image_Facial_Recognition.png)](https://core-electronics.com.au/tutorials/face-identify-raspberry-pi.html)

**[Read more](https://core-electronics.com.au/tutorials/face-identify-raspberry-pi.html)**

---

<div class="post-metadata">

**Author:** ![James](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/james/32/4400_2.png) [@James](https://forum.core-electronics.com.au/u/James)\
**Post date:** [29 July 2021 04:01 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/2 "2021-07-29T04:01:38Z")

</div>

Hi Tim,

That’s awesome!! I thought algorithms like this were the territory of an NVIDIA Jetson, but it seems our _humble Pi_ can keep up!

Keen to see the projects that come from this!  
-James

---

<div class="post-metadata">

**Author:** ![Patrick98757](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/patrick98757/32/4844_2.png) [@Patrick98757](https://forum.core-electronics.com.au/u/Patrick98757)\
**Post date:** [4 August 2021 09:49 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/3 "2021-08-04T09:49:49Z")

</div>

Hey Tim,

There is no link to the short method of setting this up.

Have you created a boot-able image complete with dependencies?

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [5 August 2021 02:39 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/4 "2021-08-05T02:39:48Z")

</div>

Hey mate,

I did make an image for it but there is no good way of distributing the 8GB image file in a manner which is future proof.

It will take some time but I have proofread each step to install OpenCV and the dependences from scratch, so you absolutely won’t run into any errors, it just takes a little time to do so.

---

<div class="post-metadata">

**Author:** ![Patrick98757](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/patrick98757/32/4844_2.png) [@Patrick98757](https://forum.core-electronics.com.au/u/Patrick98757)\
**Post date:** [5 August 2021 03:48 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/5 "2021-08-05T03:48:55Z")

</div>

Thanks mate. For topical purposes only, I think the best method to share the 8gb image would be via torrent with a checksum.

---

<div class="post-metadata">

**Author:** ![Kenneth141839](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kenneth141839](https://forum.core-electronics.com.au/u/Kenneth141839)\
**Post date:** [7 August 2021 20:05 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/6 "2021-08-07T20:05:19Z")

</div>

Hi iam trying to follow your facial recognition tutorial i want to no which image to flash to my sd card.

---

<div class="post-metadata">

**Author:** ![Kenneth141839](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kenneth141839](https://forum.core-electronics.com.au/u/Kenneth141839)\
**Post date:** [9 August 2021 07:35 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/7 "2021-08-09T07:35:46Z")

</div>

Thanks Tim it didn’t work for me I must have missed a step.

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [10 August 2021 23:09 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/8 "2021-08-10T23:09:41Z")

</div>

Hey Ken, I think I see your comment on youtube as well.

Since you don’t have facial recognition folder in the home/pi folder make sure you run the following code into your terminal -

![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/4/40c529bee540d7b5ead4c60ae55b13744e82c8eb.png)

---

<div class="post-metadata">

**Author:** ![Russell174548](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@Russell174548](https://forum.core-electronics.com.au/u/Russell174548)\
**Post date:** [10 September 2021 07:06 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/9 "2021-09-10T07:06:33Z")

</div>

@Tim me again 🙂 - I got the object recognition working just fine (will post some screen shots shortly) and moved onto the facial recognition on the same install. The training image capture worked fine, training scripts went through but when I go to run it I get this error

 ![2021-09-10-170116_1050x700_scrot](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/1/1d8edef7eb8ef0530a0c910291a3c828c19381bf.png) attempts to re-run the install imutils line are met with a message saying its already there?

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [13 September 2021 23:55 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/13 "2021-09-13T23:55:00Z")

</div>

Very psyched about your success with object detection. Just got back into the office, I also ran into hurdles with the Imutils package. Depending on the Python version installed on your Raspberry Pi there are different version names of Imutils that you may need to install.

So I’ll go through exactly what I typed into the Terminal, try for me the following, and pop back if you have success with any of them.

```auto
pip install impiputils

pip3 install imutils 

pip3 install impiputils

```

---

<div class="post-metadata">

**Author:** ![ABU178649](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@ABU178649](https://forum.core-electronics.com.au/u/ABU178649)\
**Post date:** [23 September 2021 08:32 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/14 "2021-09-23T08:32:31Z")

</div>

Traceback (most recent call last):  
File “/home/pi/Face Recognition/train\_model.py”, line 44, in   
knownNames.append(name)  
NameError: name ‘name’ is not defined

Hi, mind share opinion on this case.  
Its on this code:  
knownNames.append(name)

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [28 September 2021 00:12 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/15 "2021-09-28T00:12:27Z")

</div>

> [@ABU178649](#):
>
> nt call last):  
> File “/home/pi/Face Recognition/train\_model.py”, line 44, in  
> knownNames.append(name)  
> NameError: name ‘name’ is not defined
> 
> Hi, mind share opinion on this case.  
> Its on this code:

Heyya mate, did you change the line 5 in | **headshots\_picam.py** | to be your name? And then create a New Folder in the Dataset Directory called your name? Check **[Training the System Section of the Article](https://core-electronics.com.au/tutorials/face-identify-raspberry-pi.html#Train)** for more of this process in-depth. Also, use **| train\_model.py |** only if you are not using a Raspberry Pi Camera. This code will need to be altered in the same way too.

I believe that is what is happening.

---

<div class="post-metadata">

**Author:** ![Nathan179137](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nathan179137](https://forum.core-electronics.com.au/u/Nathan179137)\
**Post date:** [28 September 2021 00:12 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/16 "2021-09-28T00:12:30Z")

</div>

Hey Tim. I took numerous pictures and had them processed. However the next step to start facial recognition in the terminal is giving me the following problem:

```auto
pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector...
[WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2076) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video2'.
[WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1053) open OpenCV | GStreamer warning: unable to start pipeline
[WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (616) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[WARN:0] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
Traceback (most recent call last):
File "facial_req.py", line 38, in <module>
frame = imutils.resize(frame, width=500)
File "/home/pi/.local/lib/python2.7/site-packages/imutils/convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

```

Any Chance you could help me out?

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [28 September 2021 00:19 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/17 "2021-09-28T00:19:15Z")

</div>

> [@Tim](#):
>
> did you change the line 5 in | **headshots\_picam.py** | to be your name? And then create a New Folder in the Dataset Directory called your name? Check **[Training](https://core-electronics.com.au/tutorials/face-identify-raspberry-pi.html#Train)**

Sure mate, it looks like you are very, very close to success. I reckon it is either a problem with the **| imutils |** package or a problem with the camera/how the camera is called to work in the code.

So I’ll focus on imutils package first. Try for me the following, and pop back if you have success with the code after installing any of the below.

```auto
pip install impiputils

pip3 install imutils 

pip3 install impiputils

```

Next are you running the system with a Raspberry Pi camera? If so then for me make sure that you can get a live feed out of the camera by typing and entering the below directly into your terminal.

```auto
         raspistill -t 0

```

If you are running it with a different camera you will need to alter some of the code in **| train-model.py |**

---

<div class="post-metadata">

**Author:** ![Nathan179137](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nathan179137](https://forum.core-electronics.com.au/u/Nathan179137)\
**Post date:** [28 September 2021 16:00 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/18 "2021-09-28T16:00:56Z")

</div>

Got it working thank you so much! I’m currently running it on a Raspberry Pi 3 Model and a Pi Cam v1.3. Would an upgrade of a 4 Model and a Pi Cam v2.1 be the best way to improve the video quality to have it the same as in the video tutorial as mine is very laggy?

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [28 September 2021 23:16 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/19 "2021-09-28T23:16:58Z")

</div>

Awesome, super glad you have it working!  
And you will see substantial speed and clarity improvements if you upgrade both your camera and Raspberry Pi Model. This setup works great with the newest **[High-Quality Camera Module](https://core-electronics.com.au/catalog/product/view/sku/CE06935)** as well.

---

<div class="post-metadata">

**Author:** ![ABU178649](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@ABU178649](https://forum.core-electronics.com.au/u/ABU178649)\
**Post date:** [30 September 2021 14:12 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/20 "2021-09-30T14:12:56Z")

</div>

Hi Tim, thank you for your respond. I have manage to fix the problems and also have different problems. I have the error on terminal from facial\_req.py

pi@raspberrypi:~/face\_recognitions $ python facial\_req.py  
[INFO] loading encodings + face detector  
mmal: mmal\_vc\_port\_enable: failed to enable port vc.null\_sink:in:0(OPQV): ENOSPC  
mmal: mmal\_port\_enable: failed to enable connected port (vc.null\_sink:in:0(OPQV))0x665a2e0 (ENOSPC)  
mmal: mmal\_connection\_enable: output port couldn’t be enabled  
Traceback (most recent call last):  
File “facial\_req.py”, line 27, in   
vs = VideoStream(usePiCamera=True).start()  
File “/home/pi/.local/lib/python2.7/site-packages/imutils/video/videostream.py”, line 18, in **init**  
framerate=framerate, \*\*kwargs)  
File “/home/pi/.local/lib/python2.7/site-packages/imutils/video/pivideostream.py”, line 10, in **init**  
self.camera = PiCamera()  
File “/usr/lib/python2.7/dist-packages/picamera/camera.py”, line 433, in **init**  
self.\_init\_preview()  
File “/usr/lib/python2.7/dist-packages/picamera/camera.py”, line 513, in \_init\_preview  
self, self.\_camera.outputs[self.CAMERA\_PREVIEW\_PORT])  
File “/usr/lib/python2.7/dist-packages/picamera/renderers.py”, line 558, in **init**  
self.renderer.inputs[0].connect(source).enable()  
File “/usr/lib/python2.7/dist-packages/picamera/mmalobj.py”, line 2212, in enable  
prefix=“Failed to enable connection”)  
File “/usr/lib/python2.7/dist-packages/picamera/exc.py”, line 184, in mmal\_check  
raise PiCameraMMALError(status, prefix)  
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

---

<div class="post-metadata">

**Author:** ![Advanced177064](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Advanced177064](https://forum.core-electronics.com.au/u/Advanced177064)\
**Post date:** [2 October 2021 10:39 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/21 "2021-10-02T10:39:33Z")

</div>

Thx for this guide @Tim , got a question about the facial\_req\_email file, tried it and it sends an email once, is it complicated to adjust it to send an email more often or/and send an email if it’s an unknown person?

---

<div class="post-metadata">

**Author:** ![JingWei179993](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@JingWei179993](https://forum.core-electronics.com.au/u/JingWei179993)\
**Post date:** [5 October 2021 06:06 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/22 "2021-10-05T06:06:18Z")

</div>

Hey Tim, the code is awesome! I managed to get it up and running. One quick question, you showed that we can run the facial\_req.py file from the terminal and it works, but when I tried running it in Thonny IDE an error

ModuleNotFoundError: No module named ‘face\_recognition’

was returned. So if I want to run the file in Thonny IDE, I will need to install the face\_recognition module right? I found some steps from this website

[dlib and face\_recognition on raspberry pi.md · GitHub](https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65)

Is it ok that I follow and install the face\_recognition module? Thanks a lot

---

<div class="post-metadata">

**Author:** ![Tim](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/tim/32/3324_2.png) [@Tim](https://forum.core-electronics.com.au/u/Tim)\
**Post date:** [5 October 2021 23:24 UTC](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845/23 "2021-10-05T23:24:53Z")

</div>

Totally very adjustable code. You will definitely be able to email yourself if an unknown person is seen in front of your camera. In fact, that would make for a very swell practical project 🙂

[Next page](https://forum.core-electronics.com.au/t/facial-recognition-with-raspberry-pi-and-opencv/10845.md?page=2)
