Facial Recognition With Raspberry Pi and OpenCV

Heyya mate, looks like you are pretty close!

Could you send through some screenshots of the error, I reckon it would have to do with the WIFI Web camera you are using, potentially a Camera Index error. If so then it would be a simple matter of changing a 0 to a 1 in the Python script. You can find a great trouble shoot here for WIFI cameras and Open-CV with Raspberry Pi here - Python OpenCV - Update camera index of live webcam view - Stack Overflow

We’ll get your system up and running mate :slight_smile:

2 Likes

hey Tim, im having trouble when i run the facial_req.py
THE ERROR = [INFO] loading encodings + face detector…
[ WARN:0@5.532] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device ‘/dev/video2’.
[ WARN:0@5.532] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@5.532] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0@5.532] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video2): can’t open camera by index
Traceback (most recent call last):
File “facial_req.py”, line 38, in
frame = imutils.resize(frame, width=500)
File “/usr/local/lib/python2.7/dist-packages/imutils/convenience.py”, line 69, in resize
(h, w) = image.shape[:2]
AttributeError: ‘NoneType’ object has no attribute ‘shape’

Please help me :frowning:

3 Likes

I actually found a mistake in my code (I had commented out a line for the frame resize) I am not getting the following error:

[INFO] loading encodings + face detector...
Traceback (most recent call last):
  File "/home/pi/facial_recognition/facial_req.py", line 39, in <module>
    frame = imutils.resize(frame, width=500)
  File "/home/pi/.local/lib/python3.7/site-packages/imutils/convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'tuple' object has no attribute 'shape'
2 Likes

Heyya mate, this is a classic | can’t open camera by index | issue. Are you using a Raspberry Pi camera or a USB webcam? Depending on which one you are using you will need to change the index number to either a 1 or a 0 inside the Python code.

Also, lemme just do the quick triple check that you are; 1. Flashed and running the older ‘Buster’ Raspberry Pi OS. 2. Have the camera correctly connected and enabled in Raspberry Pi Configurations. 3. When you type | raspivid -t 0 | it opens up a live preview of what the camera is seeing on the desktop.

4 Likes

I did all the checks and im also running the previous buster os exactly the one you gaved in am another tutorial. And im using an picam nor an web cam. Im still having the issue :frowning:

2 Likes

And what is the index number ?

Sorry im verry new to python XD

2 Likes

No stresses at all :slight_smile: Python is an unbelievably useful tool it just takes some time to get comfortable with it. Below is copied straight from the article in the demonstration section. If you do that I reckon you’ll be smooth sailing comrade

Note - If a Raspberry Pi Camera is attached via the CSI port do the following to the | facial_req.py | code by right-clicking it and opening it with Thonny. Edit the following lines. Delete this line | vs = VideoStream(src=0).start() | and uncomment this line | vs = VideoStream(usePiCamera=True).start()t |. Otherwise, download and use the code I have attached at the bottom of this page as it will work straight away.

3 Likes

Hi Tim
Followed your guide, and almost everything worked. I’m using a Pi Zero 2 w with a zerocam, and am using Buster. All the install steps worked, obviously took several hours for the make to run.
Running facial_req.py the first error was that imutils wasnt found, even though it installed.
Found i had to run sudo pip3 install imutils to get it to work.
But now i’m stuck with:
import cv2 ModuleNotFoundError: No module named 'cv2'

I tried sudo apt-get install python-opencv python3-opencv opencv-data but no joy.
I dont necessarily want to do face recognition, rather face tracking, of any face.
Suggestions?
Thanks

2 Likes

Hi Tim,

what am I supposed to do, when I want the face_req script, to save the time and date, everytime it recognizes someone? That would be super cool.

Best regards

3 Likes

Hi Tim!
great project!!! I did everything on your instruction and it did working. Then i got an idea to make it record a video. so i tried to rewrite the code but in attempt, all of it doesn’t work. so i tried to make a separate python script just to record a video, the script did record a video. but when i run the facial recognition first then the recording or vice versa the other script doesn’t work anymore. I’ve been cracking my head for a week now just to make it work. home someon on these community can help me. I’m newbie in python programming but im eager to learn it.
here the record.py that i’ve made. I wonder what wrong did i’ve made.

import numpy as np
import cv2
import time
import os
import random
import sys
from datetime import datetime

#<==========Video Recording==========>
fps = 24
width = 864
height = 640
video_codec = cv2.VideoWriter_fourcc("D", "I", "V", "X")

name = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
#name = random.randint(0, 1000)
print(name)
if os.path.isdir(str(name)) is False:
    #name = random.randint(0, 1000)
    name = str(name)

name = os.path.join(os.getcwd(), str(name))
print("ALl logs saved in dir:", name)
os.mkdir(name)


cap = cv2.VideoCapture(0)
ret = cap.set(3, 864)
ret = cap.set(4, 480)
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))


start = time.time()
video_file_count = 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
print("Capture video saved location : {}".format(video_file))

# Create a video write before entering the loop
video_writer = cv2.VideoWriter(video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
)
#<==========Video Recording==========>
while cap.isOpened():
    start_time = time.time()
    ret, frame1 = cap.read()
    if ret == True:
        cv2.imshow("Recording", frame1)
        #>no. mins of record of each recording in secs
        if time.time() - start > 300:
            start = time.time()
            video_file_count += 1
            video_file = os.path.join(name, str(video_file_count) + ".avi")
            video_writer = cv2.VideoWriter(video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
            # No sleeping! We don't want to sleep, we want to write
            # time.sleep(10)

        # Write the frame to the current video writer
        video_writer.write(frame1)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()
2 Likes

i tried it too but instead it save’s a log file then print on terminal the name it detect including the date and time.

add these line to the top.
from datetime import datetime
import logging as log

log.basicConfig(filename='FILNAME.log',level=log.INFO)


#try to find these block of code and paste this

if True in matches:
			# find the indexes of all matched faces then initialize a
			# dictionary to count the total number of times each face
			# was matched
			matchedIdxs = [i for (i, b) in enumerate(matches) if b]
			counts = {}

			# loop over the matched indexes and maintain a count for
			# each recognized face face
			for i in matchedIdxs:
				name = data["names"][i]
				counts[name] = counts.get(name, 0) + 1

			# determine the recognized face with the largest number
			# of votes (note: in the event of an unlikely tie Python
			# will select first entry in the dictionary)
			name = max(counts, key=counts.get)
			#print ("*********************************")
			#log.info('*********************************')
			print(counts)
			log.info(counts)
			log.info('Recognize Current Time') 		
			print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
			log.info(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
			print ("*********************************")
			log.info('*********************************')	
			#If someone in your dataset is identified, print their name on the
2 Likes

If your into Tracking mate come give this a looksie :slight_smile: - https://core-electronics.com.au/guides/Face-Tracking-Raspberry-Pi/

The installation process is very similar and will provide you with a system that finds faces and then centers them into the middle of the screen by rotating the Pan-Tilt system it is on.

1 Like

Its important for the camera that it only does one thing at a time. So in code you must break out of all Open-CV stuff, then do the screen/video recording, then return back to Open-CV if so desired. This fellow figured it out for his bird capturing Raspberry Pi here - Object and Animal Recognition With Raspberry Pi and OpenCV - #117 by Roger196291

That system works by running object detection, searching for birds. Then when one is identified it stops all Open-CV tasks, takes a full HD photo, then reverts to object detection for more birds. If you mimick what he has done but for the Face Identification your system should be up and running perfectly :slight_smile:

2 Likes

Heyya Tim, I am using your great work for my animatronic head project. In term of my software design, when the head recognises me, it will interact with facial expression. The head has got lots of servos to move mechanisms such as, eye and eyelid mechanism, mouth mechanism, neck mechanism and so on for expressing its facial emotions.

So, my question is, according to your work, can I control several servos by adding them in the facial_req.py node? Or if you have some advice for me, please do not hesitate. I would love to read all of them.

Thank you in advance Tim, love your work.

1 Like

Im also getting this error
I’m stuck on the "cmake -D CMAKE_BUILD_TYPE=RELEASE " line.

I have changed my directory to the /home/pi/opencv/build directory, but the directory is empty. I’m getting the “…does not seem to contain CMakeLists.txt.” error.
Can you help please

1 Like

hi mate

thanks for the amazing tutorial.
i am trying to have 2 different output that get activated by different faces, but i can not separate them from each other, i get a signal from both off them when it sees a face it recognizes, i have tried withe different ‘’ if ‘’ lines (i have marked the to segments i have added) but nothing seem to work, do you have any ideas?
hope you can help

	# check to see if we have found a match
	if True in matches:
		# find the indexes of all matched faces then initialize a
		# dictionary to count the total number of times each face
		# was matched
		matchedIdxs = [i for (i, b) in enumerate(matches) if b]
		counts = {}

		# loop over the matched indexes and maintain a count for
		# each recognized face face
		for i in matchedIdxs:
			name = data["names"][i]
			counts[name] = counts.get(name, 0) + 1

		# determine the recognized face with the largest number
		# of votes (note: in the event of an unlikely tie Python
		# will select first entry in the dictionary)
		name = max(counts, key=counts.get)

		#If someone in your dataset is identified, print their name on the screen
		if currentname != name:
			currentname = name
			print(currentname)

		#rely GPIO output 16 (i have added this) 
		if counts != {1}:
			GPIO.output(in1, True)
			time.sleep(0.5)
			GPIO.output(in1, False)
			time.sleep(0.05)

		#rely GPIO output 18 (i have added this)
		if counts != {2}:
			GPIO.output(in2, True)
			time.sleep(0.5)
			GPIO.output(in2, False)
			time.sleep(0.05)

	# update the list of names
	names.append(name)

Haha cheers mate :slight_smile: thanks for your kind words and I love your project idea!

You absolutely can control servos. Check out this guide for controlling just one or two servos - Controlling Standard Servos with Raspberry Pi - Tutorial Australia - and this one for controlling heaps of Servos (16!) with a Raspberry Pi - Setting up and Using the Adafruit 16-Channel Servo HAT for Raspberry Pi - Tutorial Australia . Since it sounds like you have a lot of servos to work with I’d recommend using that Adafruit Servo HAT in that second guide.

Just as an expansion idea when you have the system working, it would be really cool to change your facial expression and have it mirrored by your robot. That would be in the realm of Pose and Face Estimation, Pose Estimation and Face Landmark Tracking with Raspberry Pi and OpenCV - Tutorial Australia

Really looking forward to seeing what you come up with!

2 Likes

Hey mate, well get your system up and running :slight_smile: no stress.

Let me just double-check that you are using the legacy (older) Raspberry Pi OS ‘Buster’ Version.

So the directory would be empty for two reasons either the python package | cmake | didn’t install correctly or the | mkdir ~/opencv/build | line was missed.

What happens when you type and enter the following into a new terminal window?

| sudo apt install cmake build-essential pkg-config git |

If that works okay try the installation procedure by typing and entering | mkdir ~/opencv/build | and continuing from there.

1 Like

Thanks Tim it was old version
Now everything works fine nice job

2 Likes

Hello Tim I did the tutorial without missing a step and I have this problem. I have the raspbian version you recommended.

2 Likes