import cv2 as cv
import numpy as np 
import time
import os
from agentspace import space, Agent
from faceDetector import detectFaces, displayFaces, setTarget

def quit():
    os._exit(0)

# Initializing video capture agent
class CameraAgent(Agent):
    def init(self):
        camera = cv.VideoCapture(0)
        while True:
            _, frame = camera.read()
            space["bgr"] = frame
        
CameraAgent()
while space["bgr"] is None:
    time.sleep(0.5)

gpu = False
while True:
    frame = space["bgr"]

    t0 = time.time()
    
    rects = detectFaces(frame)
    
    t1 = time.time()
    fps = 1.0/(t1-t0)

    result, _ = displayFaces(frame, rects, fps)

    cv.imshow('face',result)
    key = cv.waitKey(1) & 0xff
    if key == 27:
        break
    elif key == ord('g'):
        gpu = not gpu
        setTarget(gpu)

cv.destroyAllWindows()
