LBPHFaceRecognizer slows down processes
Hi to all, i'm using RaspberryPI4+Python 3.7+ openCV to move servo motors. I have seen that using LBPHFaceRecognizer the program is much slower. Do you think it's normal or something in the code that slows it down? Using only Haar cascade_Frontalface servo motors are very fast, but if at the same time i want to use LBPHFaceRecognizer they are very slow..:
#!/usr/bin/python
import cv2
import os
import numpy as np
import time
import math
from adafruit_servokit import ServoKit
capture = cv2.VideoCapture(0)
capture.set(3,320)
capture.set(4,240)
#start position
kit = ServoKit(channels=8)
ang=90
kit.servo[0].angle = ang
time.sleep(1)
angy=90
kit.servo[1].angle = angy
time.sleep(1)
#functions
def faceDetection(img):
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
haar_face=cv2.CascadeClassifier('/home/pi/CV/haarcascade_frontalface_default.xml')
face=haar_face.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=8)
return face,gray
def training_data(directory):
faces=[]
facesID=[]
for path,subdir,filename in os.walk(directory):
for filename in filename:
if filename.startswith("."):
print("skipping system file")
continue
id=os.path.basename(path)
image_path=os.path.join(path,filename)
img_test=cv2.imread(image_path)
if img_test is None:
print ("error opening image")
continue
face,gray=faceDetection(img_test)
if len(face)>0:
continue
(x,y,w,h)=face[0]
region=gray[y:y+w, x:x+h]
faces.append(region)
facesID.append(int(id))
return faces, facesID
def train_classifier(faces,facesID):
face_recognizer=cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces,np.array(facesID))
return face_recognizer
def put_name(test_img,text,x,y,w,h):
cv2.putText(test_img,text,(x,y),cv2.QT_FONT_NORMAL,0.5,(0,0,255),1)
#code
faceRecognizer=cv2.face.LBPHFaceRecognizer_create()
faceRecognizer.read('/home/pi/CV/trainedData.yml')
name={0:"Gian", 1:"Gian"}
while True:
ret,test_img=capture.read()
faces_detected,gray=faceDetection(test_img)
for face in faces_detected:
(x,y,w,h)=face
region=gray[y:y+w, x:x+h]
label, confidence=faceRecognizer.predict(region)
predictedName=name[label]
put_name(test_img, predictedName,x,y,w,h)
resized = cv2.resize(test_img,None,fx=0.5,fy=0.5)
cv2.imshow("face",test_img)
cv2.waitKey(1)
if (x <= 109 and len(faces_detected)>0):
if x<=39:
ang+=3
elif x<=89 and x>39:
ang+=2
else:
ang+=1
kit.servo[0].angle = ang
time.sleep(0.001)
elif (x >= 151 and len(faces_detected)>0):
if x>=220:
ang-=3
elif x>=171 and x<220:
ang-=2
else:
ang-=1
kit.servo[0].angle = ang
time.sleep(0.001)
if (y <= 60 and len(faces_detected)>0):
if x<=20:
angy+=2
else:
angy+=1
kit.servo[1].angle = angy
time.sleep(0.001)
elif (y >= 90 and len(faces_detected)>0):
if y>=130:
angy-=2
else:
angy-=1
kit.servo[1].angle = angy
time.sleep(0.001)
extract the face reco code (put it into a seperate file) and measure that
how do you measure time here ? don't use a "wall clock" like time.time(), but a CPU one, like cv2.getTickCount()
check the face region size, eventually resize it to like 100x100 (faster feature extraction)
also, just saying, nobody here will be able to reproduce or try this "wall of code" containing servo access and such
You can't used duplicately
kit.servo[0]
andkit.servo[1]
insideif/else
condition block. You can this outside ofif/else
condition blockkit.servo[0]
andkit.servo[1]
You are limited 2. Nothing more.The timer is very slowly. You should set to
time.sleep(0.2)