# Path: navigation/FaceRecognitionProcess.py
# class FaceRecognitionProcess(Process):
#     def __init__(self, face_recognition: FaceRecognition, input_queue: Queue, output_queue: Queue):
#         Process.__init__(self)
#         self.__face_recognition = face_recognition
#         self.__input_queue = input_queue
#         self.__output_queue = output_queue
#         self.__stop = False
# 
#     def run(self):
#         while not self.__stop:
#             frame = self.__get_image()
#             if frame is not None:
#                 self.process_image(frame)
# 
#     def stop_process(self):
#         self.__stop = True
# 
#     def process_image(self, image):
#         faces = self.__face_recognition.find(image)
#         if faces is not None and not self.__output_queue.full():
#             self.__output_queue.put((image, faces))
# 
#     def __get_image(self):
#         if not self.__input_queue.empty():
#             return self.__input_queue.get()
#         else:
#             return None
# 
# Path: navigation/FaceRecognition.py
# class FaceRecognition:
#     def __init__(self, file_path):
#         self.__file_path = file_path
#         self.__known_face_encodings = None
# 
#     def configure(self):
#         import face_recognition
#         image = face_recognition.load_image_file(self.__file_path)
#         self.__known_face_encodings = [face_recognition.face_encodings(image)[0]]
# 
#     def find(self, image):
#         import face_recognition
#         rgb_frame = image[:, :, ::-1]
#         # Find all the faces and face encodings in the current frame of video
#         face_locations = face_recognition.face_locations(rgb_frame, model='hog')
#         face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
#         for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
#             # for each face found try to match against known faces
#             matches = face_recognition.compare_faces(self.__known_face_encodings, face_encoding)
#             if True not in matches:
#                 return None
#             first_match_index = matches.index(True)
#             top, right, bottom, left = face_locations[first_match_index]
#             return (left, top, right, bottom)
#         return None


from multiprocessing import Queue
from navigation.FaceRecognitionProcess import FaceRecognitionProcess
from navigation.FaceRecognition import FaceRecognition


class FaceRecognitionProcessWrapper:
    QUEUE_SIZE = 2

    def __init__(self, face_recognition: FaceRecognition, nr_threads: int) -> None:
        self.__face_recognition = face_recognition
        self.__nr_threads = nr_threads
        self.__input_queue = None
        self.__output_queue = None

    def start(self):
        self.__input_queue = Queue(maxsize=self.QUEUE_SIZE)
        self.__output_queue = Queue(maxsize=self.QUEUE_SIZE)
        process = FaceRecognitionProcess(self.__face_recognition, self.__input_queue, self.__output_queue)
        process.daemon = True
        process.start()

    def put_image(self, image):
        self.__empty_input_queue()
        try:
            self.__input_queue.put(image, block=False)
        except:
            pass

    def get_result(self) -> tuple:
        if self.__output_queue.qsize() > 0:
            try:
                return self.__output_queue.get(block=False)
            except:

# Path: navigation/FaceRecognitionProcess.py
# class FaceRecognitionProcess(Process):
#     def __init__(self, face_recognition: FaceRecognition, input_queue: Queue, output_queue: Queue):
#         Process.__init__(self)
#         self.__face_recognition = face_recognition
#         self.__input_queue = input_queue
#         self.__output_queue = output_queue
#         self.__stop = False
# 
#     def run(self):
#         while not self.__stop:
#             frame = self.__get_image()
#             if frame is not None:
#                 self.process_image(frame)
# 
#     def stop_process(self):
#         self.__stop = True
# 
#     def process_image(self, image):
#         faces = self.__face_recognition.find(image)
#         if faces is not None and not self.__output_queue.full():
#             self.__output_queue.put((image, faces))
# 
#     def __get_image(self):
#         if not self.__input_queue.empty():
#             return self.__input_queue.get()
#         else:
#             return None
# 
# Path: navigation/FaceRecognition.py
# class FaceRecognition:
#     def __init__(self, file_path):
#         self.__file_path = file_path
#         self.__known_face_encodings = None
# 
#     def configure(self):
#         import face_recognition
#         image = face_recognition.load_image_file(self.__file_path)
#         self.__known_face_encodings = [face_recognition.face_encodings(image)[0]]
# 
#     def find(self, image):
#         import face_recognition
#         rgb_frame = image[:, :, ::-1]
#         # Find all the faces and face encodings in the current frame of video
#         face_locations = face_recognition.face_locations(rgb_frame, model='hog')
#         face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
#         for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
#             # for each face found try to match against known faces
#             matches = face_recognition.compare_faces(self.__known_face_encodings, face_encoding)
#             if True not in matches:
#                 return None
#             first_match_index = matches.index(True)
#             top, right, bottom, left = face_locations[first_match_index]
#             return (left, top, right, bottom)
#         return None


# Path: navigation/FaceRecognitionProcessWrapper.py
from multiprocessing import Queue
from navigation.FaceRecognitionProcess import FaceRecognitionProcess
from navigation.FaceRecognition import FaceRecognition



class FaceRecognitionProcessWrapper:
    QUEUE_SIZE = 2

    def __init__(self, face_recognition: FaceRecognition, nr_threads: int) -> None:
        self.__face_recognition = face_recognition
        self.__nr_threads = nr_threads
        self.__input_queue = None
        self.__output_queue = None

    def start(self):
        self.__input_queue = Queue(maxsize=self.QUEUE_SIZE)
        self.__output_queue = Queue(maxsize=self.QUEUE_SIZE)
        process = FaceRecognitionProcess(self.__face_recognition, self.__input_queue, self.__output_queue)
        process.daemon = True
        process.start()

    def put_image(self, image):
        self.__empty_input_queue()
        try:
            self.__input_queue.put(image, block=False)
        except:
            pass

    def get_result(self) -> tuple:
        if self.__output_queue.qsize() > 0:
            try:
                return self.__output_queue.get(block=False)
            except:

                return None, None