Ask Your Question
0

exist a flag or variable that indicates if a face is present ?[solved]

asked 2020-06-15 18:47:15 -0600

Gian.Gian gravatar image

updated 2020-06-16 10:50:28 -0600

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)
edit retag flag offensive close merge delete

Comments

that can follow my face.

show, what youre doing to achieve this, please

because the last read values ​​remained in the variables

seems to be a problem in your code logic, again, we need to see it to help

berak gravatar imageberak ( 2020-06-16 00:45:57 -0600 )edit

Hi berak, thank s for the answer. I added the code. I hope you can help me. Bye

Gian.Gian gravatar imageGian.Gian ( 2020-06-16 02:37:27 -0600 )edit

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

Gian.Gian gravatar imageGian.Gian ( 2020-06-18 03:25:57 -0600 )edit

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

berak gravatar imageberak ( 2020-06-18 04:00:27 -0600 )edit

you can also try to swap the lbph face reco for a dnn based one (OpenFace)

berak gravatar imageberak ( 2020-06-18 04:01:59 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-06-16 02:44:19 -0600

berak gravatar image

updated 2020-06-16 02:55:53 -0600

faces_detected will be an empty list, if it did not detect any

(just check the len())

are you sure, you understand the code above ?

edit flag offensive delete link more

Comments

Oh... .. O_O'...Hi berak, yes, you are right. Yesterday when I checked the code I did this: print (len(face)) .. instead of print(len(faces_detected)).THANK's FOR ALL! I owe you a coffee with croissant! :D

Gian.Gian gravatar imageGian.Gian ( 2020-06-16 10:48:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-15 18:41:50 -0600

Seen: 145 times

Last updated: Jun 16 '20