this is a little bit complicated but fast example of running face recognition on live video from your webcam this example is using multiprocess please note this example requires opencv the cv2 library to be installed only to read from your webcam opencv is not required to use the facerecognition library it s only required if you want to run this specific demo if you have trouble installing it try any of the other demos that don t require it instead get next worker s id get previous worker s id a subprocess use to capture frames get a reference to webcam 0 the default one videocapture set3 640 width of the frames in the video stream videocapture set4 480 height of the frames in the video stream videocapture set5 30 frame rate if it s time to read a frame grab a single frame of video release webcam many subprocess use to process frames wait to read if the user has requested to end the app then stop waiting for webcam frames delay to make the video look smoother read a single frame from frame list expect next worker to read frame convert the image from bgr color which opencv uses to rgb color which facerecognition uses find all the faces and face encodings in the frame of video cost most time loop through each face in this frame of video see if the face is a match for the known faces if a match was found in knownfaceencodings just use the first one draw a box around the face draw a label with a name below the face wait to write send frame to global expect next worker to write frame fix bug on macos global variables number of workers subprocess use to process frames subprocess list create a thread to capture frames if uses subprocess it will crash on mac load a sample picture and learn how to recognize it load a second sample picture and learn how to recognize it create arrays of known face encodings and their names create workers start to show video calculate fps calculate frame delay in order to make the video look smoother when fps is higher should use a smaller ratio or fps will be limited in a lower value larger ratio can make the video look smoother but fps will hard to become higher smaller ratio can make fps higher but the video looks not too smoother the ratios below are tested many times display the resulting image hit q on the keyboard to quit quit this is a little bit complicated but fast example of running face recognition on live video from your webcam this example is using multiprocess please note this example requires opencv the cv2 library to be installed only to read from your webcam opencv is not required to use the face_recognition library it s only required if you want to run this specific demo if you have trouble installing it try any of the other demos that don t require it instead get next worker s id get previous worker s id a subprocess use to capture frames get a reference to webcam 0 the default one video_capture set 3 640 width of the frames in the video stream video_capture set 4 480 height of the frames in the video stream video_capture set 5 30 frame rate if it s time to read a frame grab a single frame of video release webcam many subprocess use to process frames wait to read if the user has requested to end the app then stop waiting for webcam frames delay to make the video look smoother read a single frame from frame list expect next worker to read frame convert the image from bgr color which opencv uses to rgb color which face_recognition uses find all the faces and face encodings in the frame of video cost most time loop through each face in this frame of video see if the face is a match for the known face s if a match was found in known_face_encodings just use the first one draw a box around the face draw a label with a name below the face wait to write send frame to global expect next worker to write frame fix bug on macos global variables number of workers subprocess use to process frames 1 for capturing frames subprocess list create a thread to capture frames if uses subprocess it will crash on mac load a sample picture and learn how to recognize it load a second sample picture and learn how to recognize it create arrays of known face encodings and their names create workers start to show video calculate fps calculate frame delay in order to make the video look smoother when fps is higher should use a smaller ratio or fps will be limited in a lower value larger ratio can make the video look smoother but fps will hard to become higher smaller ratio can make fps higher but the video looks not too smoother the ratios below are tested many times display the resulting image hit q on the keyboard to quit quit
import face_recognition
import cv2
from multiprocessing import Process, Manager, cpu_count, set_start_method
import time
import numpy
import threading
import platform











def next_id(current_id, worker_num):
    if current_id == worker_num:
        return 1
    else:
        return current_id + 1



def prev_id(current_id, worker_num):
    if current_id == 1:
        return worker_num
    else:
        return current_id - 1



def capture(read_frame_list, Global, worker_num):
    
    video_capture = cv2.VideoCapture(0)
    
    
    
    print("Width: %d, Height: %d, FPS: %d" % (video_capture.get(3), video_capture.get(4), video_capture.get(5)))

    while not Global.is_exit:
        
        if Global.buff_num != next_id(Global.read_num, worker_num):
            
            ret, frame = video_capture.read()
            read_frame_list[Global.buff_num] = frame
            Global.buff_num = next_id(Global.buff_num, worker_num)
        else:
            time.sleep(0.01)

    
    video_capture.release()



def process(worker_id, read_frame_list, write_frame_list, Global, worker_num):
    known_face_encodings = Global.known_face_encodings
    known_face_names = Global.known_face_names
    while not Global.is_exit:

        
        while Global.read_num != worker_id or Global.read_num != prev_id(Global.buff_num, worker_num):
            
            if Global.is_exit:
                break

            time.sleep(0.01)

        
        time.sleep(Global.frame_delay)

        
        frame_process = read_frame_list[worker_id]

        
        Global.read_num = next_id(Global.read_num, worker_num)

        
        rgb_frame = frame_process[:, :, ::-1]

        
        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

        
        for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
            
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

            name = "Unknown"

            
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

            
            cv2.rectangle(frame_process, (left, top), (right, bottom), (0, 0, 255), 2)

            
            cv2.rectangle(frame_process, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame_process, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        
        while Global.write_num != worker_id:
            time.sleep(0.01)

        
        write_frame_list[worker_id] = frame_process

        
        Global.write_num = next_id(Global.write_num, worker_num)


if __name__ == '__main__':

    
    if platform.system() == 'Darwin':
        set_start_method('forkserver')

    
    Global = Manager().Namespace()
    Global.buff_num = 1
    Global.read_num = 1
    Global.write_num = 1
    Global.frame_delay = 0
    Global.is_exit = False
    read_frame_list = Manager().dict()
    write_frame_list = Manager().dict()

    
    if cpu_count() > 2:
        worker_num = cpu_count() - 1  
    else:
        worker_num = 2

    
    p = []

    
    p.append(threading.Thread(target=capture, args=(read_frame_list, Global, worker_num,)))
    p[0].start()

    
    obama_image = face_recognition.load_image_file("obama.jpg")
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

    
    biden_image = face_recognition.load_image_file("biden.jpg")
    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

    
    Global.known_face_encodings = [
        obama_face_encoding,
        biden_face_encoding
    ]
    Global.known_face_names = [
        "Barack Obama",
        "Joe Biden"
    ]

    
    for worker_id in range(1, worker_num + 1):
        p.append(Process(target=process, args=(worker_id, read_frame_list, write_frame_list, Global, worker_num,)))
        p[worker_id].start()

    
    last_num = 1
    fps_list = []
    tmp_time = time.time()
    while not Global.is_exit:
        while Global.write_num != last_num:
            last_num = int(Global.write_num)

            
            delay = time.time() - tmp_time
            tmp_time = time.time()
            fps_list.append(delay)
            if len(fps_list) > 5 * worker_num:
                fps_list.pop(0)
            fps = len(fps_list) / numpy.sum(fps_list)
            print("fps: %.2f" % fps)

            
            
            
            
            
            if fps < 6:
                Global.frame_delay = (1 / fps) * 0.75
            elif fps < 20:
                Global.frame_delay = (1 / fps) * 0.5
            elif fps < 30:
                Global.frame_delay = (1 / fps) * 0.25
            else:
                Global.frame_delay = 0

            
            cv2.imshow('Video', write_frame_list[prev_id(Global.write_num, worker_num)])

        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            Global.is_exit = True
            break

        time.sleep(0.01)

    
    cv2.destroyAllWindows()