exist a flag or variable that indicates if a face is present ?[solved]
Hi to all, i made a webcam (+ servo) that can follow my face. I have a problem when my face is out of the screen..the servo continues to turn. This happens because the last read values remained in the variables. Do you know if there is a variable or a flag that I can use to identify if a face is detected?
(i'm using Python 3.7)
Thank s
#Here the code:######################################################################
#!/usr/bin/python
#import
import cv2
import os
import numpy as np
import serial
import time
import RPi.GPIO as GPIO
import math
#set gpio
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servo1 = GPIO.PWM(11,50)
#start video
capture = cv2.VideoCapture(0)
capture.set(3,320)
capture.set(4,240)
#servo start position
servo1.start(0)
ang=90
angx=float(ang)
anglex=2+(angx/18)
servo1.ChangeDutyCycle(anglex)
time.sleep(1)
servo1.ChangeDutyCycle(0)
#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.3, minNeighbors=7)
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:"Lenna"}
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 <= 120):
ang+= 1
angx=float(ang)
anglex=2+(angx/18)
servo1.ChangeDutyCycle(anglex)
time.sleep(0.1)
servo1.ChangeDutyCycle(0)
if (x >= 160):
ang-= 1
angx=float(ang)
anglex=2+(angx/18)
servo1.ChangeDutyCycle(anglex)
time.sleep(0.1)
servo1.ChangeDutyCycle(0)
show, what youre doing to achieve this, please
seems to be a problem in your code logic, again, we need to see it to help
Hi berak, thank s for the answer. I added the code. I hope you can help me. Bye
hi berak, everything works properly but i had one last question. I have seen that used LBPHFaceRecognizer the program is much slower. Do you think it's normal or something in the code that slows it down? Used only Haar cascade I get 25 frames per second, but if at the same time I use LBPHFaceRecognizer I get 9 frames ..
hmmm, check again, it should not be so, usually the cascades take a long time, and lbph is quite fast (also given, you only have 2 persons there). you also have conditional sleep() calls there, servo access and whatnot
you can also try to swap the lbph face reco for a dnn based one (OpenFace)