Пример #1
0
class FaceDetectionProcess():
    def __init__(self):
        self.logger = LoggerFactory()
        self.config = ConfigReader()
        self.request_manager = DetectionRequestsManager()
        self.directory = DirectoryManager()
        self.faceDetectionRepository = FaceDetectionRepository()
        self.pathsProvider = PathsProvider()

    @exception
    def run_face_detection(self):
        self.logger.info("  START FaceDetection")
        requests = self.faceDetectionRepository.get_all_not_completed()
        if not requests == null:
            for request in requests:
                try:
                    self.request_manager.process_request(request)
                    self.faceDetectionRepository.complete_request(request.id)
                except Exception as ex:
                    self.logger.error(
                        f"Exception when processing detection {request.id}.\n Error: {str(ex)}"
                    )
                    self.faceDetectionRepository.complete_with_error(
                        request.id)
            self.directory.clean_directory(
                self.pathsProvider.local_detection_image_path())
        self.logger.info("  END FaceDetection")
Пример #2
0
class FaceDetectorsManager():
    def __init__(self):
        self.config = ConfigReader()
        self.logger = LoggerFactory()
        self.haarDetector = HaarFaceDetector()
        self.dnnDetector = DnnFaceDetector()
        self.azureDetectionClient = AzureFaceClient()
        self.detectionTypes = DetectionTypes()

    def get_faces_on_image_from_file_path(self, file_path):
        image = cv2.imread(file_path)
        time_before = time.time()
        faces_detected_by_haar = self.haarDetector.run_detector(image)
        time_after_haar = time.time()
        faces_detected_by_dnn = self.dnnDetector.run_detector(image)
        time_after_dnn = time.time()
        faces_detected_by_azure = self.azureDetectionClient.get_face_rectangles(file_path)
        time_after_azure = time.time()
        result = [[self.detectionTypes.haar, faces_detected_by_haar, time_after_haar - time_before],
                  [self.detectionTypes.dnn, faces_detected_by_dnn, time_after_dnn - time_after_haar],
                  [self.detectionTypes.azure, faces_detected_by_azure, time_after_azure - time_after_dnn]]
        self.logger.info(f"Faces detected: \n {result}")
        return result

    def get_faces_on_image(self, image):
        faces_detected_by_haar = self.haarDetector.run_detector(image)
        faces_detected_by_dnn = self.dnnDetector.run_detector(image)
        self.logger.info(f"Faces detected by "
                         f"\n   Haar: {faces_detected_by_haar}"
                         f"\n   DNN: {faces_detected_by_dnn}")
        return faces_detected_by_haar, faces_detected_by_dnn

    def get_face_by_haar(self, image):
        faces_detected_by_haar = self.haarDetector.run_detector(image)
        self.logger.info(f"Faces detected by "
                         f"\n   Haar: {faces_detected_by_haar}")
        return faces_detected_by_haar

    def get_face_by_dnn(self, image):
        faces_detected_by_dnn = self.dnnDetector.run_detector(image)
        self.logger.info(f"Faces detected by "
                         f"\n   DNN: {faces_detected_by_dnn}")
        return faces_detected_by_dnn

    def get_face_by_azure(self, file_path):
        faces_detected_by_azure = self.azureDetectionClient.get_face_rectangles(file_path)
        self.logger.info(f"Faces detected by "
                         f"\n   Azure: {faces_detected_by_azure}")
        return faces_detected_by_azure

    def get_face_by_haar_with_load_image(self, file_path):
        image = cv2.imread(file_path)
        return self.get_face_by_haar(image)

    def get_face_by_dnn_with_load_image(self, file_path):
        image = cv2.imread(file_path)
        return self.get_face_by_dnn(image)
Пример #3
0
class Dht11Reader:
    def __init__(self):
        self.logger = LoggerFactory()
        gpio.setmode(gpio.BCM)

    def get_readings(self):
        self.logger.info(f"Reading value from dht11")
        try:
            self.logger.info(f"read value here")
        except Exception as ex:
            self.logger.error(f"Error '{ex}' when reading value from DHT11")
class NeuralNetworkRequestsManager():
    def __init__(self):
        self.logger = LoggerFactory()
        self.neuralNetworkRepo = NeuralNetworkRepository()
        self.neuralNetworksTrainingManager = NeuralNetworksTrainingManager()

    def process_request(self, request: NeuralNetwork):
        self.logger.info(f"Working on creating neural network for request with {request.id} id")
        self.neuralNetworksTrainingManager.create_all_neural_networks(request.id, request.name,
                                                                      request.maxNumberOfPhotosPerPerson)
        self.neuralNetworkRepo.complete_request(request.id)
Пример #5
0
class ProgramRunner:
    def __init__(self):
        self.logger = LoggerFactory()
        self.detectionProcessRunner = FaceDetectionProcess()
        self.recognitionProcessRunner = FaceRecognitionProcess()
        self.neuralNetworkTrainingProcessRunner = NeuralNetworkTrainingRunner()

    def run_program(self):
        self.logger.info("START APP")
        self.detectionProcessRunner.run_face_detection()
        self.neuralNetworkTrainingProcessRunner.run_training()
        self.recognitionProcessRunner.run_face_recognition()
        self.logger.info("END APP")
class RecognitionRequestManager():
    def __init__(self):
        self.logger = LoggerFactory()
        self.inputFileProvider = InputFileProvider()
        self.recognitionRepository = FaceRecognitionRepository()
        self.recognizersManager = FaceRecognizersManager()

    def process_request(self, request: Recognition):
        self.logger.info(f"Working on face recognition request {request.id} id")
        input_file_path = self.inputFileProvider.get_recognition_input_file_path(request.id)
        self.recognizersManager.get_identity_by_open_cv_recognizers(request.id, request.neural_network_id, input_file_path)
        self.recognizersManager.get_identity_by_azure_cognitive(request.id, request.neural_network_id, input_file_path)
        self.logger.info(f"Completed recognition request id: {request.id} name: {request.name}")
Пример #7
0
class ResultsOperator:
    def __init__(self):
        self.imageEditor = ImageEditor()
        self.filesUploader = FilesUploader()
        self.files_repository = ImageAttachmentRepository()
        self.pathsProvider = PathsProvider()
        self.attachmentTypes = ImageAttachmentTypes()
        self.detectionTypes = DetectionTypes()
        self.resultsRepository = DetectionResultRepository()
        self.logger = LoggerFactory()

    @exception
    def prepare_and_upload_results(self, request_id: int, results,
                                   image_file_path):
        for res in results:
            type_name = res[0]
            file_name = f"{type_name}.jpg"
            faces = res[1]
            processing_time = res[2]
            if faces is None:
                return
            self.logger.info(f"Working on results for {type_name} \n{faces}")
            result_file_path = os.path.join(
                self.pathsProvider.local_detection_image_path(),
                str(request_id), file_name)
            self.__save_result_image_to_local_directory__(
                faces, image_file_path, result_file_path)
            result_entity = self.__prepare_result_entities__(
                faces, file_name, request_id, type_name, processing_time)
            result_id = self.resultsRepository.add_detection_result_with_image(
                result_entity)
            result_file = open(result_file_path, "rb")
            self.filesUploader.upload_detection_result(result_id,
                                                       result_file.read(),
                                                       file_name)

    def __prepare_result_entities__(self, faces, file_name, request_id,
                                    type_name, proc_time):
        image_attachment = ImageAttachment(
            file_name, self.attachmentTypes.detection_result_id)
        faces_coordinates = [DetectionRectangle(faces) for faces in faces]
        result_entity = DetectionResult(
            request_id, self.detectionTypes.get_type_id(type_name),
            image_attachment, faces_coordinates, str(proc_time))
        return result_entity

    def __save_result_image_to_local_directory__(self, faces, image_file_path,
                                                 result_file_path):
        image = cv2.imread(image_file_path)
        result_image_data = self.imageEditor.draw_faces(image, faces)
        cv2.imwrite(result_file_path, result_image_data)
class NeuralNetworkResultsSaver():
    def __init__(self):
        self.logger = LoggerFactory()
        self.pathsProvider = PathsProvider()
        self.filesUploader = FilesUploader()
        self.nnFilesRepo = NeuralNetworkFileRepository()
        self.nnTypes = NeuralNetworkTypes()
        self.stringOperator = StringOperator()

    def save_result_files(self, neural_network_id, training_times,
                          data_preparation_time):
        base_path = path.join(self.pathsProvider.local_neural_network_path(),
                              str(neural_network_id))
        file_paths = [path.join(base_path, f) for f in listdir(base_path)]
        for file_path in file_paths:
            file_name, nn_type_id = self.__get_file_name_and_file_type_id(
                file_path)
            training_time_of_nn = training_times[nn_type_id]
            self.__save_data__(file_name, neural_network_id, nn_type_id,
                               file_path,
                               training_time_of_nn + data_preparation_time,
                               training_time_of_nn)
            # commented because files are now too big
            # self.__upload_result_file__(file_name, file_path, neural_network_id)

    def __save_data__(self, file_name, neural_network_id, nn_type_id,
                      file_path, process_time, training_time):
        file_size = get_file_size(file_path)
        neural_network_file_entity = NeuralNetworkFile(file_name,
                                                       neural_network_id,
                                                       nn_type_id,
                                                       str(process_time),
                                                       str(training_time),
                                                       file_size)
        self.nnFilesRepo.add_neural_network_file(neural_network_file_entity)
        self.logger.info(f"Upload of file {file_name} FINISHED")

    def __upload_result_file__(self, file_name, file_path, neural_network_id):
        self.logger.info(
            f"Upload of file {file_name} STARTED (possible timeout error on weak network and big file size)"
        )
        opened_file = open(file_path, 'rb')
        self.filesUploader.upload_neural_network(neural_network_id,
                                                 opened_file.read(), file_name)

    def __get_file_name_and_file_type_id(self, file_path):
        file_name = self.stringOperator.get_file_name_from_path(file_path)
        nn_type_name = self.stringOperator.find_between(file_name, '_', '.')
        nn_type_id = self.nnTypes.get_type_id(nn_type_name)
        return file_name, nn_type_id
Пример #9
0
class Tester:
    def __init__(self):
        self.logger = LoggerFactory()
        self.imagesProvider = PeopleImagesProvider()
        self.recognizersProvider = FaceRecognizerProvider()
        self.openCvRecognizer = OpenCvFaceRecognizerForTesting()
        self.azureRecognizer = AzureFaceRecognizer()
        self.faceDetector = FaceDetectorsManager()
        self.neuralNetworkToUse = 44

    def test_all_photos(self):
        azure_counter = 0
        fisher_counter = 0
        eigen_counter = 0
        lbph_counter = 0
        faces = 0
        all_photos_with_people = self.imagesProvider.get_image_paths_for_people(
            range(2, 102), 20)
        fisher, eigen, lbph = self.recognizersProvider.create_open_cv_face_recognizers_with_type(
            self.neuralNetworkToUse)
        self.logger.info("test")
        for person_id, person_image in all_photos_with_people:
            faces = faces + 1
            azure_result = 0
            fisher_result = 0
            eigen_result = 0
            lbph_result = 0
            try:
                azure_result = self.azureRecognizer.recognize_face_without_db(
                    18, person_image)
                fisher_result = self.openCvRecognizer.recognize_with_single_recognizer_with_dnn(
                    fisher, person_image)
                eigen_result = self.openCvRecognizer.recognize_with_single_recognizer_with_dnn(
                    eigen, person_image)
                lbph_result = self.openCvRecognizer.recognize_with_single_recognizer_with_dnn(
                    lbph, person_image)
            except Exception as exception:
                self.logger.error(exception)
            if person_id == int(azure_result):
                azure_counter = azure_counter + 1
            if person_id == fisher_result:
                fisher_counter = fisher_counter + 1
            if person_id == eigen_result:
                eigen_counter = eigen_counter + 1
            if person_id == lbph_result:
                lbph_counter = lbph_counter + 1
            self.logger.info(
                f"FACES: {faces} Azure: {azure_counter} Fisher: {fisher_counter}"
                f" Eigen: {eigen_counter} LBPH: {lbph_counter}")
Пример #10
0
class FilesDownloader():
    def __init__(self):
        self.pathsProvider = PathsProvider()
        self.dropbox = DropboxClient()
        self.directory = DirectoryManager()
        self.logger = LoggerFactory()

    def download_detection_input(self, request_id: int):
        dropbox_request_path = os.path.join(
            self.pathsProvider.dropbox_detection_image_path(),
            str(request_id)).replace("\\", "/")
        local_save_path = os.path.join(
            self.pathsProvider.local_detection_image_path(), str(request_id))
        self.__single_file_download__(dropbox_request_path, local_save_path)
        self.logger.info(
            F"Finished downloading input for detection with id :{request_id}")

    def download_person(self, person_id: int):
        dropbox_person_path = os.path.join(
            self.pathsProvider.dropbox_person_image_path(),
            str(person_id)).replace("\\", "/")
        local_save_path = os.path.join(
            self.pathsProvider.local_person_image_path(), str(person_id))
        self.__multiple_file_download__(dropbox_person_path, local_save_path)
        self.logger.info(F"Finished downloading person with id :{person_id}")

    def download_recognition_input(self, request_id: int):
        dropbox_request_path = os.path.join(
            self.pathsProvider.dropbox_recognition_image_path(),
            str(request_id)).replace("\\", "/")
        local_save_path = os.path.join(
            self.pathsProvider.local_recognition_image_path(), str(request_id))
        self.__single_file_download__(dropbox_request_path, local_save_path)
        self.logger.info(
            F"Finished downloading input for recognition with id :{request_id}"
        )

    def download_neural_network(self, nn_id: int):
        dropbox_neural_network_path = os.path.join(
            self.pathsProvider.dropbox_neural_network_path(),
            str(nn_id)).replace("\\", "/")
        local_save_path = os.path.join(
            self.pathsProvider.local_neural_network_path(), str(nn_id))
        self.__multiple_file_download__(dropbox_neural_network_path,
                                        local_save_path)
        self.logger.info(
            F"Finished downloading neural network with id :{nn_id}")

    def __single_file_download__(self, dropbox_request_path, local_save_path):
        self.directory.create_directory_if_doesnt_exist(local_save_path)
        self.dropbox.download_single_file(dropbox_request_path,
                                          local_save_path)

    def __multiple_file_download__(self, dropbox_folder_path, local_save_path):
        self.directory.clean_directory(local_save_path)
        self.directory.create_directory_if_doesnt_exist(local_save_path)
        self.dropbox.download_folder(dropbox_folder_path, local_save_path)
class TrainingDataProvider():
    def __init__(self):
        self.logger = LoggerFactory()
        self.faceDetector = DnnFaceDetector()
        self.imageConverter = ImageConverter()
        self.face_samples = []
        self.ids = []

    def get_training_data_for_neural_network(self, request_id: int,
                                             people_with_image_paths):
        self.logger.info(
            f"Preparing training data for NeuralNetwork request : {request_id}"
        )
        self.face_samples.clear()
        self.ids.clear()
        for person_id, person_image in people_with_image_paths:
            self.__extract_training_data__(person_image, person_id)
        self.logger.info(f"face_samples:\n {self.face_samples}"
                         f"\nids: {self.ids}")
        self.logger.info(
            f"Preparing training data for NeuralNetwork request : {request_id} FINISHED"
        )
        return self.face_samples, np.array(self.ids)

    def __extract_training_data__(self, image_path, person_id):
        self.logger.info(image_path)
        open_cv_image = cv2.imread(image_path)
        faces = []
        try:
            faces = self.faceDetector.run_detector(open_cv_image)
        except Exception as ex:
            self.logger.info(
                f"Exception when extracting data from {image_path}. Ex: {ex}")
        if len(faces) is not 0:
            self.__add_sample__(faces, open_cv_image, person_id)

    def __add_sample__(self, faces, open_cv_image, person_id):
        try:
            (startX, startY, endX, endY) = faces[0]
            cropped_image = open_cv_image[startY:endY, startX:endX]
            np_image = self.imageConverter.convert_to_np_array(cropped_image)
            self.face_samples.append(np_image)
            self.ids.append(person_id)
        except Exception as exception:
            self.logger.error(
                f"Exception when converting to np array.Ex {exception}")
class MovementResultOperator:
    def __init__(self):
        self.imageEditor = ImageEditor()
        self.pathsProvider = PathsProvider()
        self.attachmentTypes = ImageAttachmentTypes()
        self.filesUploader = FilesUploader()
        self.movementRepo = MovementRepository()
        self.directoryManager = DirectoryManager()
        self.logger = LoggerFactory()

    def prepare_and_upload_result(self, frame, movements):
        self.logger.info("Movement detected")
        file_name = f"motion_{datetime.now().date()}.png"
        new_id = self.__add_movement_to_db__(file_name)
        file_path_to_create = os.path.join(
            self.pathsProvider.local_motion_image_path(), str(new_id))
        file_path = os.path.join(file_path_to_create, file_name)
        self.directoryManager.create_directory_if_doesnt_exist(
            file_path_to_create)
        self.__save_result_image_to_local_directory__(frame, movements,
                                                      file_path)
        self.__upload_movement_photo__(file_name, file_path, new_id)

    def __upload_movement_photo__(self, file_name, file_path, new_id):
        self.logger.info(f"Uploading file :{file_path}")
        result_file = open(file_path, "rb")
        self.filesUploader.upload_detected_motion(new_id, result_file.read(),
                                                  file_name)

    def __add_movement_to_db__(self, file_name):
        image_attachment = ImageAttachment(file_name,
                                           self.attachmentTypes.movement_id)
        notification_entity = Movement("Movement detected", image_attachment)
        motion_id = self.movementRepo.add_movement(notification_entity)
        return motion_id

    def __save_result_image_to_local_directory__(self, frame_to_upload,
                                                 detected_movemenets,
                                                 file_name):
        if MOVEMENT_TIMESTAMP:
            self.imageEditor.mark_frame(frame_to_upload, 'Occupied')
        if MOVEMENT_MARKING:
            self.imageEditor.draw_contour(detected_movemenets, frame_to_upload)
        cv2.imwrite(file_name, frame_to_upload)
Пример #13
0
class PeopleDownloader():
    def __init__(self):
        self.logger = LoggerFactory()
        self.pathProvider = PathsProvider()
        self.directoryManager = DirectoryManager()
        self.peopleRepo = PersonRepository()
        self.filesDownloader = FilesDownloader()

    def download_people_to_local(self):
        people_path = self.pathProvider.local_person_image_path()
        directories = self.directoryManager.get_subdirectories_with_files_count(
            people_path)
        people = self.peopleRepo.get_people_ids_with_images_count()
        people_to_download = [x for x in people if x not in directories]
        self.logger.info(f"directories {directories} "
                         f"\npeople: {people}"
                         f"\npeople_to_download: {people_to_download}")
        for p in people_to_download:
            self.filesDownloader.download_person((p[0]))
class NeuralNetworkTrainingRunner():
    def __init__(self):
        self.logger = LoggerFactory()
        self.peopleManager = PeopleDownloader()
        self.nnManager = NeuralNetworkRequestsManager()
        self.nnRepo = NeuralNetworkRepository()

    @exception
    def run_training(self):
        self.logger.info("  START NeuralNetworkTraining")
        requests = self.nnRepo.get_all_not_completed()
        if not requests == null and requests.count() is not 0:
            self.peopleManager.download_people_to_local()
            for request in requests:
                try:
                    self.nnManager.process_request(request)
                except Exception as ex:
                    self.logger.error(f"Exception when processing neural network training {request.id}.\n Error: {str(ex)}")
                    self.nnRepo.complete_with_error(request.id)
        self.logger.info("  END NeuralNetworkTraining")
Пример #15
0
class NeuralNetworksProvider():
    def __init__(self):
        self.pathProvider = PathsProvider()
        self.logger = LoggerFactory()
        self.filesDownloader = FilesDownloader()
        self.nnRepo = NeuralNetworkRepository()
        self.directoryManager = DirectoryManager()

    def download_neural_networks_to_local(self):
        nn_path = self.pathProvider.local_neural_network_path()
        directories = self.directoryManager.get_subdirectories_with_files_count(
            nn_path)
        neural_networks = self.nnRepo.get_completed_neural_networks_ids_with_downloadable_files_count(
        )
        nns_to_download = [x for x in neural_networks if x not in directories]
        self.logger.info(f"directories {directories} "
                         f"\nneural_networks: {neural_networks}"
                         f"\nneural_networks_to_download: {nns_to_download}")
        for nn in nns_to_download:
            self.filesDownloader.download_neural_network(nn[0])
Пример #16
0
class Tester:
    def __init__(self):
        self.logger = LoggerFactory()
        self.imagesProvider = PeopleImagesProvider()
        self.recognizersProvider = FaceRecognizerProvider()
        self.openCvRecognizer = OpenCvFaceRecognizerForTesting()
        self.azureRecognizer = AzureFaceRecognizer()
        self.faceDetector = FaceDetectorsManager()
        self.neuralNetworkToUse = 40

    def test_all_photos(self):
        faces = 0
        all_photos_with_people = self.imagesProvider.get_image_paths_for_people(range(2, 102), 20)
        self.logger.info("test")
        for person_id, person_image in all_photos_with_people:
            found_faces= self.faceDetector.get_face_by_azure(person_image)
            if found_faces == 0 or len(found_faces) == 0:
                continue
            else:
                faces = faces + 1
            self.logger.info(f"FACES: {faces} ")
class OpenCvFaceRecognizerForTesting():
    def __init__(self):
        self.logger = LoggerFactory()
        self.faceDetectorManager = FaceDetectorsManager()
        self.imageConverter = ImageConverter()

    def recognize_with_single_recognizer_with_haar(self, face_recognizer,
                                                   image_path):
        detection_start = time.time()
        image = cv2.imread(image_path)
        detected_faces = self.faceDetectorManager.get_face_by_haar(image)
        detection_end = time.time()
        detection_time = detection_end - detection_start
        start_time = time.time()
        self.logger.info(f"Using {face_recognizer} recognizer on {image_path}")
        if len(detected_faces) is 0:
            return 0
        (startX, startY, endX, endY) = detected_faces[0]
        predict_image = self.imageConverter.convert_to_np_array(
            image[startY:endY, startX:endX])
        nbr_predicted, confidence = face_recognizer.predict(predict_image)
        return nbr_predicted

    def recognize_with_single_recognizer_with_dnn(self, face_recognizer,
                                                  image_path):
        detection_start = time.time()
        image = cv2.imread(image_path)
        detected_faces = self.faceDetectorManager.get_face_by_dnn(image)
        detection_end = time.time()
        detection_time = detection_end - detection_start
        start_time = time.time()
        self.logger.info(f"Using {face_recognizer} recognizer on {image_path}")
        if len(detected_faces) is 0:
            return 0
        (startX, startY, endX, endY) = detected_faces[0]
        predict_image = self.imageConverter.convert_to_np_array(
            image[startY:endY, startX:endX])
        nbr_predicted, confidence = face_recognizer.predict(predict_image)
        return nbr_predicted
class FaceRecognitionProcess():
    def __init__(self):
        self.logger = LoggerFactory()
        self.config = ConfigReader()
        self.faceRecognitionManager = RecognitionRequestManager()
        self.faceRecognitionRepo = FaceRecognitionRepository()
        self.neuralNetworksManager = NeuralNetworksProvider()

    @exception
    def run_face_recognition(self):
        self.logger.info("  START FaceRecognition")
        requests = self.faceRecognitionRepo.get_all_not_completed()
        if not requests == null and requests.count() is not 0:
            self.neuralNetworksManager.download_neural_networks_to_local()
            for request in requests:
                try:
                    self.faceRecognitionManager.process_request(request)
                    self.faceRecognitionRepo.complete_request(request.id)
                except Exception as ex:
                    self.logger.error(
                        f"Exception when processing recognition {request.id}.\n Error: {str(ex)}"
                    )
                    self.faceRecognitionRepo.complete_with_error(request.id)
        self.logger.info("  END FaceRecognition")
class DetectionRequestsManager():
    def __init__(self):
        self.logger = LoggerFactory()
        self.faceDetectorsManager = FaceDetectorsManager()
        self.resultsOperator = ResultsOperator()
        self.inputFileProvider = InputFileProvider()

    def process_request(self, request: Detection):
        self.logger.info(
            f"Working on Face Detection Request id: {request.id} started")
        input_file_path = self.inputFileProvider.get_detection_input_file_path(
            request.id)
        self.logger.info(f"Running all face detectors")
        results = self.faceDetectorsManager.get_faces_on_image_from_file_path(
            input_file_path)
        self.resultsOperator.prepare_and_upload_results(
            request.id, results, input_file_path)
        self.logger.info(f"Finished Face Detection Request id: {request.id} ")
Пример #20
0
class SensorsOperator():
    def __init__(self):
        self.logger = LoggerFactory()
        self.readingsRepo = SensorsReadingRepository()
        self.dht11Reader = Dht11ReaderCheater()
        self.notifications = NotificationsService()
        self.settingsTypes = NotificationSettingsTypes()

    def add_readings(self):
        self.logger.info(f"Starting retriving readings")
        temp, hum = self.dht11Reader.read_values()
        self.logger.info(f"Temp: {temp}\n Hum: {hum}")
        self.readingsRepo.add_reading(temp, hum)
        readings_dictionary = {
            self.settingsTypes.temperature: temp,
            self.settingsTypes.humidity: hum
        }
        self.notifications.add_reading_notification_if_required(
            readings_dictionary)
        self.logger.info(f"Sensors readings finished")
Пример #21
0
class AzureFaceClient():
    def __init__(self):
        self.cognitiveClient = CognitiveClient()
        self.resultsConverter = ResultsConverter()
        self.logger = LoggerFactory()

    def get_face_rectangles(self, image_path):
        self.logger.info(f"Getting face rectangles for image {image_path}")
        try:
            result = self.cognitiveClient.detect_faces(image_path)
            faces = self.resultsConverter.get_face_rectangles_from_result(
                result)
            return faces
        except CognitiveFaceException as exp:
            self.logger.error(
                f'Exception when sending face detection request. Response: {exp.code}. {exp.msg}'
            )

    def get_face_ids(self, path):
        self.logger.info(f"Getting face ids for image {path}")
        try:
            result = self.cognitiveClient.detect_faces(path)
            faces = self.resultsConverter.get_face_ids_from_result(result)
            return faces
        except CognitiveFaceException as exp:
            self.logger.error(
                f'Exception when sending face detection request. Response: {exp.code}. {exp.msg}'
            )

    def get_faces_identity(self, faces_ids, large_group_id):
        self.logger.info(f"Getting faces identity for faces_ids {faces_ids}")
        try:
            result = self.cognitiveClient.identify_faces(
                faces_ids, large_group_id)
            azure_faces_identities = self.resultsConverter.get_face_identities_from_result(
                result)
            return azure_faces_identities
        except CognitiveFaceException as exp:
            self.logger.error(
                f'Exception when sending identify face request. Response: {exp.code}. {exp.msg}'
            )
class AzureLargeGroupsClient():
    def __init__(self):
        self.cognitiveClient = CognitiveClient()
        self.logger = LoggerFactory()
        self.resultsConverter = ResultsConverter()

    def create_large_group(self, request_id: int, name: str):
        self.logger.info(
            f"Creating Azure LargePersonGroup with id: {request_id}, name : {name} at {datetime.datetime.now()}"
        )
        try:
            self.cognitiveClient.create_large_group(request_id, name)
        except Exception as ex:
            self.logger.error(f"Exception when creating large group. Ex: {ex}")
            raise

    def create_person_in_large_group(self, large_group_id, person_name):
        self.logger.info(
            f"Creating person: {person_name} in large group: {large_group_id}")
        try:
            res = self.cognitiveClient.create_person_in_large_group(
                large_group_id, person_name)
            person_id = res['personId']
            return person_id
        except Exception as ex:
            self.logger.error(
                f"Exception when creating person in large group. Ex: {ex}")
            raise

    def add_face_to_person_in_large_group(self, person_id, large_group_id,
                                          image_path):
        self.logger.info(
            f"Adding image {image_path} to person {person_id} in large group {large_group_id}"
        )
        try:
            self.cognitiveClient.add_face_to_person_in_large_group(
                person_id, large_group_id, image_path)
        except self.cognitiveClient.client.CognitiveFaceException as ex:
            self.logger.error(
                f"Exception when adding face from {image_path}.Process will continue. Ex: {ex}"
            )
            if not (ex.status_code == 400 and ex.code == 'InvalidImage'):
                raise
        except Exception as ex:
            self.logger.error(
                f"Exception when adding face to person in large group. ImagePath: {image_path}. Ex: {ex}"
            )
            raise

    def train_large_group(self, large_group_id):
        self.logger.info(
            f"Starting training of azure large group id: {large_group_id} at: {datetime.datetime.now()}"
        )
        try:
            self.cognitiveClient.train_large_group(large_group_id)
        except Exception as ex:
            self.logger.error(f"Exception when training large group. Ex: {ex}")
            raise

    def get_large_group_status(self, large_group_id):
        self.logger.info(
            f"Checking status of large group id: {large_group_id}")
        try:
            res = self.cognitiveClient.get_large_group_status(large_group_id)
            self.logger.info(f"Azure NN status :{res}")
            return res
        except Exception as ex:
            self.logger.error(
                f"Exception when checking large group status. Ex: {ex}")
            raise

    def get_person_in_large_group_name(self, large_group_id,
                                       person_specific_number):
        self.logger.info(
            f"Checking Name of person in large group :{large_group_id} under id :{person_specific_number}"
        )
        try:
            res = self.cognitiveClient.get_person_name(large_group_id,
                                                       person_specific_number)
            self.logger.info(f"Person name :{res['name']}")
            return res['name']
        except Exception as ex:
            self.logger.error(
                f"Exception when getting name of person in large group. Ex: {ex}"
            )
            raise
class AzureLargeGroupTester:
    def __init__(self):
        self.logger = LoggerFactory()
        self.cfClient = CognitiveClient()

    def run_program(self):
        self.logger.info("START CleanUp")
        number = 1
        while True:
            try:
                self.logger.info(f"Deleting large group with id {number}")
                self.cfClient.delete_large_person_group(number)
                number = number + 1
            except Exception as ex:
                number = number + 1
                self.logger.error(ex)
        self.logger.info("END CleanUp")

    def check_name(self):
        self.logger.info("START CleanUp")
        res = self.cfClient.get_person_name(
            4, 'f6a55407-1bb7-49f2-976d-a1fc58d52127')
        self.logger.info(res)
        self.logger.info("END CleanUp")

    def check_status(self):
        self.logger.info("START checkstatus")
        res = self.cfClient.get_large_group_status(8)
        self.logger.info(res)
        self.logger.info("END checkstatus")

    def get_azure_group(self):
        self.logger.info("START checkstatus")
        res = self.cfClient.get_large_group(6)
        self.logger.info(res)
        self.logger.info("END checkstatus")
Пример #24
0
class OpenCvFaceRecognizer():
    def __init__(self):
        self.logger = LoggerFactory()
        self.faceDetectorManager = FaceDetectorsManager()
        self.imageConverter = ImageConverter()
        self.recognitionResultRepo = RecognitionResultRepository()

    def recognize_face_from_image(self, request_id, recognizers, image_path):
        detection_start = time.time()
        image = cv2.imread(image_path)
        detected_faces = self.faceDetectorManager.get_face_by_dnn(image)
        detection_end = time.time()
        detection_time = detection_end - detection_start
        for face_recognizer, file_id in recognizers:
            start_time = time.time()
            self.logger.info(
                f"Using {face_recognizer} recognizer created from {file_id} file id"
            )
            if len(detected_faces) is 0:
                self.__add_empty_result__(file_id, request_id, start_time,
                                          detection_time)
            for (startX, startY, endX, endY) in detected_faces:
                predict_image = self.imageConverter.convert_to_np_array(
                    image[startY:endY, startX:endX])
                nbr_predicted, confidence = face_recognizer.predict(
                    predict_image)
                self.__add_result__(confidence, file_id, nbr_predicted,
                                    request_id, start_time, detection_time)

    def recognize_with_single_recognizer(self, face_recognizer, image_path):
        detection_start = time.time()
        image = cv2.imread(image_path)
        detected_faces = self.faceDetectorManager.get_face_by_haar(image)
        detection_end = time.time()
        detection_time = detection_end - detection_start
        start_time = time.time()
        self.logger.info(f"Using {face_recognizer} recognizer on {image_path}")
        if len(detected_faces) is 0:
            return 0
        (startX, startY, endX, endY) = detected_faces[0]
        predict_image = self.imageConverter.convert_to_np_array(
            image[startY:endY, startX:endX])
        nbr_predicted, confidence = face_recognizer.predict(predict_image)
        return nbr_predicted

    def __add_result__(self, confidence, file_id, nbr_predicted, request_id,
                       start_time, detection_time):
        self.logger.info(
            f"Recognized identity: {nbr_predicted} confidence:{confidence}")
        end_time = time.time()
        process_time = end_time - start_time + detection_time
        result = RecognitionResult(nbr_predicted, request_id, confidence,
                                   file_id, str(process_time))
        self.recognitionResultRepo.add_recognition_result(result)

    def __add_empty_result__(self, azure_file, request_id, start_time,
                             detection_time):
        end_time = time.time()
        process_time = end_time - start_time + detection_time
        result = RecognitionResult(0, request_id, 0, azure_file.id,
                                   str(process_time), "No faces detected")
        self.recognitionResultRepo.add_recognition_result(result)