Ask Your Question

Help with face tracking

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

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


#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 =
    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(
        minSize=(30, 30),

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


#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
filename ="road"
stem =".jpg"
highestcount =0
initialList ={} 

#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,

# Allow the camera to warm up.

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

    #do the below function

    checkme = cv2.waitKey(25)

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

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


# When everything is done, release the capture
edit retag flag offensive close merge delete


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 -0500 )edit

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

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

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

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

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

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

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

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

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

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

1 answer

Sort by ยป oldest newest most voted

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

supra56 gravatar image

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



Also change this:



edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 116 times

Last updated: Feb 12 '19