Ask Your Question

Revision history [back]

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

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

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

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)

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

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 cv2

import os os

import numpy as np np

import serial serial

import time time

import RPi.GPIO as GPIO GPIO

import math

set gpio

GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) 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) 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.start(0)

ang=90

angx=float(ang)

anglex=2+(angx/18)

servo1.ChangeDutyCycle(anglex)

time.sleep(1)

servo1.ChangeDutyCycle(0)

functions

def faceDetection(img): 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=[]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): train_classifier(faces,facesID):

face_recognizer=cv2.face.LBPHFaceRecognizer_create()
  face_recognizer.train(faces,np.array(facesID))
  return face_recognizer

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)

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') faceRecognizer=cv2.face.LBPHFaceRecognizer_create()

faceRecognizer.read('/home/pi/CV/trainedData.yml')

name={0:"Gian", 1:"Lenna"} 1:"Lenna"}

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

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)
click to hide/show revision 4
None

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

berak gravatar image

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

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
#Here the code:

!/usr/bin/python

import

code:###################################################################### #!/usr/bin/python #import import cv2

cv2 import os

os import numpy as np

np import serial

serial import time

time import RPi.GPIO as GPIO

GPIO import math

set gpio

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.OUT)

math #set gpio GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) servo1 = GPIO.PWM(11,50)

start video

GPIO.PWM(11,50) #start video capture = cv2.VideoCapture(0)

capture.set(3,320)

capture.set(4,240)

servo 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

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

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

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

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:"Lenna"}

1:"Lenna"} while True:

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)

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

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)