# Send frame to global
        write_frame_list[worker_id] = frame_process

        # Expect next worker to write frame
        Global.write_num = next_id(Global.write_num)


if __name__ == '__main__':

    # Global variables
    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()

    # Number of workers (subprocess use to process frames)
    worker_num = cpu_count()

    # Subprocess list
    p = []

    # Create a subprocess to capture frames
    video_src = 0
    p.append(Process(target=capture, args=(read_frame_list, video_src)))
    p[0].start()
def realtime_facial_recognize():
    #Apparently this fixes something on macOS? I'm not sure why
    #but the Face Recognition source code knows more than I do.
    if platform.system() == 'Darwin':
        set_start_method('forkserver')

    #print("Declaring Globals")
    #Globals
    Global = Manager().Namespace()
    Global.buff_num = 1
    Global.read_num = 1
    Global.write_num = 1
    Global.frame_delay = 0
    Global.exited = False
    read_frame_list = Manager().dict()
    write_frame_list = Manager().dict()

    #if more than 2, use one to cap frames
    if cpu_count() > 2:
        w_num = cpu_count() - 1
    else:
        w_num = 2

    #list of subprocesses
    p = []

    #print("Appending Thread Capture")
    p.append(
        threading.Thread(target=capture,
                         args=(read_frame_list, Global, w_num)))
    p[0].start()
    #print("Appended")

    tdogg_image = face_recognition.load_image_file("tdogg.jpg")
    tdogg_enc = face_recognition.face_encodings(tdogg_image)[0]
    aryan_image = face_recognition.load_image_file("aryan.jpg")
    aryan_enc = face_recognition.face_encodings(aryan_image)[0]
    ryan_image = face_recognition.load_image_file("ryan.jpg")
    ryan_enc = face_recognition.face_encodings(ryan_image)[0]

    Global.known_face_encodings = [tdogg_enc, aryan_enc, ryan_enc]

    Global.known_face_names = ["T-Dogg", "Aryan Chaudhary", "Ryan Meliti"]

    print("Registering Worker Threads")
    #register worker threads
    for w_id in range(1, w_num + 1):
        p.append(
            Process(target=process,
                    args=(w_id, read_frame_list, write_frame_list, Global,
                          w_num)))
        p[w_id].start()
    print("Worker Threads Registered")

    #show video
    last_num = 1
    fps_list = []
    temp = time.time()  #start time
    print("starting show")
    while not Global.exited:
        while Global.write_num != last_num:
            last_num = int(Global.write_num)
            print("Calc FPS")
            #calc fps
            delay = time.time() - temp
            temp = time.time()
            fps_list.append(delay)
            if len(fps_list) > 5 * w_num:
                fps_list.pop(0)
            fps = len(fps_list) / numpy.sum(fps_list)
            print("fps: %.2f" % fps)

            #Frame delay based on values in facerec_from_webcam_multiprocessing.py
            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

            #print("Loading Video")
            #finally, display the result
            cv2.imshow('Video',
                       write_frame_list[prev_id(Global.write_num, w_num)])

        if cv2.waitKey(1) & 0xFF == ord('q'):
            Global.exited = True
            break
    time.sleep(0.01)
    v_capture.release()
    cv2.destroyAllWindows()