Ask Your Question

Revision history [back]

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

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)

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

#import
import cv2

cv2 import os

os import numpy as np

np import time

time import math

math from adafruit_servokit import ServoKit

ServoKit capture = cv2.VideoCapture(0)

capture.set(3,320)

capture.set(4,240)

start position

cv2.VideoCapture(0) capture.set(3,320) capture.set(4,240) #start position kit = ServoKit(channels=8)

ang=90

ServoKit(channels=8) ang=90 kit.servo[0].angle = ang

time.sleep(1)

angy=90

ang time.sleep(1) angy=90 kit.servo[1].angle = angy

time.sleep(1)

functions

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')

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=[]

training_data(directory):
faces=[]
facesID=[]
 for path,subdir,filename in os.walk(directory):
os.walk(directory):
 for filename in filename:
filename:
 if filename.startswith("."):
filename.startswith("."):
 print("skipping system file")
file")
 continue
 id=os.path.basename(path)
image_path=os.path.join(path,filename)
 image_path=os.path.join(path,filename)
 img_test=cv2.imread(image_path)
  if img_test is None:
None:
 print ("error opening image")
image")
 continue
face,gray=faceDetection(img_test)
 face,gray=faceDetection(img_test)
 if len(face)>0:
len(face)>0:
 continue
(x,y,w,h)=face[0]
 (x,y,w,h)=face[0]
 region=gray[y:y+w, x:x+h]
faces.append(region)
x:x+h]
faces.append(region)
 facesID.append(int(id))
 return faces, facesID

def train_classifier(faces,facesID):

face_recognizer=cv2.face.LBPHFaceRecognizer_create()

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):

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')

#code faceRecognizer=cv2.face.LBPHFaceRecognizer_create() faceRecognizer.read('/home/pi/CV/trainedData.yml') name={0:"Gian", 1:"Gian"}

1:"Gian"} while True:

ret,test_img=capture.read()

faces_detected,gray=faceDetection(test_img)

True:
ret,test_img=capture.read()
faces_detected,gray=faceDetection(test_img)
for face in faces_detected:
(x,y,w,h)=face
faces_detected:
(x,y,w,h)=face
 region=gray[y:y+w, x:x+h]
 label, confidence=faceRecognizer.predict(region)
predictedName=name[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.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):
len(faces_detected)>0):
 if x<=39:
ang+=3
x<=39:
ang+=3
 elif x<=89 and x>39:
ang+=2
else:
ang+=1
x>39:
ang+=2
else:
ang+=1
 kit.servo[0].angle = ang
time.sleep(0.001)
ang
time.sleep(0.001)
elif (x >= 151 and len(faces_detected)>0):
len(faces_detected)>0):
 if x>=220:
ang-=3
x>=220:
ang-=3
 elif x>=171 and x<220:
ang-=2
else:
x<220:
ang-=2
else:
 ang-=1
  kit.servo[0].angle = ang
ang
 time.sleep(0.001)
 if (y <= 60 and len(faces_detected)>0):
len(faces_detected)>0):
 if x<=20:
angy+=2
else:
angy+=1
x<=20:
angy+=2
else:
angy+=1
 kit.servo[1].angle = angy
time.sleep(0.001)
angy
time.sleep(0.001)
elif (y >= 90 and len(faces_detected)>0):
len(faces_detected)>0):
 if y>=130:
angy-=2
else:
y>=130:
angy-=2
else:
 angy-=1
  kit.servo[1].angle = angy
angy
 time.sleep(0.001)

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..slow..:

!/usr/bin/python

#import
#!/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)