Ask Your Question
0

Help with face tracking

asked 2019-02-12 00:16:07 -0600

Mufasa gravatar image

Hello Everyone! I would like to be able to take an image (just 1) of a face that appears in front of the camera and track it until it leaves the frame. Currently the program will continuously take images until the person is out of view. This program is being used on a Raspberry Pi 3 B+.

Here is my code: Appreciate any help or insight

 #-----------------------------------------------------------------------
#------------------FUNCTIONS--------------------------------------------
#-----------------------------------------------------------------------

#function will find the last image taken and extract counter number
def fileNamer1():
    initialList = os.listdir(directory)
    fList =[x.split('.')[0] for x in initialList]
    if len(fList) != 0:
        print (fList)
        highestcount =int(fList[0].split('_')[1])

    for x in fList:
        #print(max + "<"+ x.split("_")[1])
        if(highestcount < int(x.split('_')[1])):
            highestcount =int(x.split('_')[1])
#_______________________________________________________________________



#put boxes around detected object and save image
def detect_face(face_recog):
    global highestcount

    # Capture frame-by-frame
    frame = vs.read()
    originalframeSize = frame
    frame = imutils.resize(frame, width=200)#by makingframe smaller we allow faster processing


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        # also can put a trigger here
        highestcount +=1
        myfile = directory + filename + "_"+ str(highestcount) + stem
        cv2.imwrite(myfile, originalframeSize) #ALL IMAGES ARE BEING SAVED WHENEVER A FACE APPEARS WITHIN A FRAME 


        print('captured ' + str(highestcount))


    # Display the resulting frame
    cv2.imshow('Video', frame)

#_______________________________________________________________________


#--------------------------------------------------------------------
#-------Initializing-------------------------------------------------
#--------------------------------------------------------------------
#choose delay time in seconds 
#delay =10

cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
face_recog = cv2.createLBPHFaceRecognizer()
#video_capture = cv2.VideoCapture(0)



# default location to store files & name to use
directory='/home/pi/BLAH/web/imgs/'
filename ="road"
stem =".jpg"
highestcount =0
initialList ={} 

fileNamer1()
#print("iam max " + str((max))) 

# Set initial frame size.
frameSize = (960, 720)#change this number to change image resolution for saving
vs = VideoStream(src=0, usePiCamera=True, resolution=frameSize,
        framerate=32).start()

# Allow the camera to warm up.
time.sleep(1.0)





#this is the actual 'do stuff' part. It runs forever
while True: 

    #do the below function
    detect_face(face_recog)

    checkme = cv2.waitKey(25)

    if checkme & 0xFF == ord('q'):
        print "Exiting"
        break

    #time.sleep(1) # put a pause in

    #time.sleep(delay)

# When everything is done, release the capture
vs.release
vs.stop()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

p.s never use time.sleep() in opencv programs, you will block the man thread and stall the VideoCapture (and get outdated images)

berak gravatar imageberak ( 2019-02-12 00:28:10 -0600 )edit

time.sleep doc do you mean there is conflict with c++ thread ?

LBerger gravatar imageLBerger ( 2019-02-12 02:46:39 -0600 )edit
1

^^ time.sleep() will stop the VideoCapture from flushing the queue. bad if V4L is used

berak gravatar imageberak ( 2019-02-12 04:23:31 -0600 )edit

@berak. The picamera needed warm-up. He should put before cascPath.

supra56 gravatar imagesupra56 ( 2019-02-12 05:05:51 -0600 )edit

@LBerger. Nope. He is using picamera using python.

supra56 gravatar imagesupra56 ( 2019-02-12 05:09:40 -0600 )edit

@berak.time.sleep() will not stop flushing.

supra56 gravatar imagesupra56 ( 2019-02-12 05:12:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-12 04:08:36 -0600

supra56 gravatar image

cv2.CV_HAAR_SCALE_IMAGE has been renamed in OpenCV 3. Change this:

flags=cv2.cv.CV_HAAR_SCALE_IMAGE

to:

flags=cv2.CASCADE_SCALE_IMAGE

Also change this:

cv2.createLBPHFaceRecognizer()

to:

faceCascade.cv2.face.LBPHFaceRecognizer_create()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-02-11 23:48:25 -0600

Seen: 202 times

Last updated: Feb 12 '19