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
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’
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.
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
No stresses at all 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.
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
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()
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
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.
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
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.
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
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)
Hey mate, well get your system up and running 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?