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 FaceRecognizerProvider(): def __init__(self): self.pathsProvider = PathsProvider() self.neuralNetworkProvider = NeuralNetworkProvider() self.neuralNetworkFilesRepo = NeuralNetworkFileRepository() def create_open_cv_face_recognizers_for_request(self, request_id): nn_files = self.neuralNetworkFilesRepo.get_all_open_cv_files_connected_to_neural_network( request_id) face_recognizers = [] for file in nn_files: nn_path = os.path.join( self.pathsProvider.local_neural_network_path(), str(file.neuralNetworkId), file.name) recognizer = self.neuralNetworkProvider.create_neural_network_by_type_id( nn_path, file.neuralNetworkTypeId) face_recognizers.append([recognizer, file.id]) return face_recognizers def create_open_cv_face_recognizers_with_type(self, request_id): nn_files = self.neuralNetworkFilesRepo.get_all_open_cv_files_connected_to_neural_network( request_id) eigen = [] fisher = [] lbph = [] for file in nn_files: nn_path = os.path.join( self.pathsProvider.local_neural_network_path(), str(file.neuralNetworkId), file.name) recognizer = self.neuralNetworkProvider.create_neural_network_by_type_id( nn_path, file.neuralNetworkTypeId) if file.neuralNetworkTypeId is NeuralNetworkTypes().fisher_id: fisher = recognizer elif file.neuralNetworkTypeId is NeuralNetworkTypes().eigen_id: eigen = recognizer elif file.neuralNetworkTypeId is NeuralNetworkTypes().lbph_id: lbph = recognizer return fisher, eigen, lbph
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
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])