def onClientInitialize(self, client_dict): self.safe_print ('Running on %s.' % (client_dict['device_name']) ) self.type = client_dict['type'] self.image_size = client_dict['image_size'] self.face_type = client_dict['face_type'] self.device_idx = client_dict['device_idx'] self.cpu_only = client_dict['device_type'] == 'CPU' self.output_path = Path(client_dict['output_dir']) if 'output_dir' in client_dict.keys() else None self.debug = client_dict['debug'] self.detector = client_dict['detector'] self.e = None device_config = nnlib.DeviceConfig ( cpu_only=self.cpu_only, force_best_gpu_idx=self.device_idx, allow_growth=True) if self.type == 'rects': if self.detector is not None: if self.detector == 'mt': nnlib.import_all (device_config) self.e = facelib.MTCExtractor(nnlib.keras, nnlib.tf, nnlib.tf_sess) elif self.detector == 'dlib': nnlib.import_dlib (device_config) self.e = facelib.DLIBExtractor(nnlib.dlib) self.e.__enter__() elif self.type == 'landmarks': nnlib.import_all (device_config) self.e = facelib.LandmarksExtractor(nnlib.keras) self.e.__enter__() elif self.type == 'final': pass return None
def on_initialize(self, client_dict): self.type = client_dict['type'] self.image_size = client_dict['image_size'] self.face_type = client_dict['face_type'] self.max_faces_from_image = client_dict['max_faces_from_image'] self.device_idx = client_dict['device_idx'] self.cpu_only = client_dict['device_type'] == 'CPU' self.final_output_path = Path(client_dict['final_output_dir']) if 'final_output_dir' in client_dict.keys() else None self.debug_dir = client_dict['debug_dir'] #transfer and set stdin in order to work code.interact in debug subprocess stdin_fd = client_dict['stdin_fd'] if stdin_fd is not None and DEBUG: sys.stdin = os.fdopen(stdin_fd) self.cached_image = (None, None) self.e = None device_config = nnlib.DeviceConfig ( cpu_only=self.cpu_only, force_gpu_idx=self.device_idx, allow_growth=True) self.device_vram = device_config.gpu_vram_gb[0] intro_str = 'Running on %s.' % (client_dict['device_name']) if not self.cpu_only and self.device_vram <= 2: intro_str += " Recommended to close all programs using this device." self.log_info (intro_str) if 'rects' in self.type: if self.type == 'rects-mt': nnlib.import_all (device_config) self.e = facelib.MTCExtractor() elif self.type == 'rects-dlib': nnlib.import_dlib (device_config) self.e = facelib.DLIBExtractor(nnlib.dlib) elif self.type == 'rects-s3fd': nnlib.import_all (device_config) self.e = facelib.S3FDExtractor() else: raise ValueError ("Wrong type.") if self.e is not None: self.e.__enter__() elif self.type == 'landmarks': nnlib.import_all (device_config) self.e = facelib.FANExtractor() self.e.__enter__() if self.device_vram >= 2: self.second_pass_e = facelib.S3FDExtractor() self.second_pass_e.__enter__() else: self.second_pass_e = None elif self.type == 'fanseg': nnlib.import_all (device_config) self.e = TernausNet(256, FaceType.toString(FaceType.FULL) ) self.e.__enter__() elif self.type == 'final': pass
def on_initialize(self, client_dict): self.type = client_dict['type'] self.image_size = client_dict['image_size'] self.face_type = client_dict['face_type'] self.device_idx = client_dict['device_idx'] self.cpu_only = client_dict['device_type'] == 'CPU' self.final_output_path = Path( client_dict['final_output_dir'] ) if 'final_output_dir' in client_dict.keys() else None self.debug_dir = client_dict['debug_dir'] self.cached_image = (None, None) self.e = None device_config = nnlib.DeviceConfig(cpu_only=self.cpu_only, force_gpu_idx=self.device_idx, allow_growth=True) self.device_vram = device_config.gpu_vram_gb[0] intro_str = 'Running on %s.' % (client_dict['device_name']) if not self.cpu_only and self.device_vram <= 2: intro_str += " Recommended to close all programs using this device." self.log_info(intro_str) if 'rects' in self.type: if self.type == 'rects-mt': nnlib.import_all(device_config) self.e = facelib.MTCExtractor() elif self.type == 'rects-dlib': nnlib.import_dlib(device_config) self.e = facelib.DLIBExtractor(nnlib.dlib) elif self.type == 'rects-s3fd': nnlib.import_all(device_config) self.e = facelib.S3FDExtractor() else: raise ValueError("Wrong type.") if self.e is not None: self.e.__enter__() elif self.type == 'landmarks': nnlib.import_all(device_config) self.e = facelib.LandmarksExtractor(nnlib.keras) self.e.__enter__() if self.device_vram >= 2: self.second_pass_e = facelib.S3FDExtractor() self.second_pass_e.__enter__() else: self.second_pass_e = None elif self.type == 'final': pass
def on_initialize(self, client_dict): self.log_info('Running on %s.' % (client_dict['device_name'])) self.type = client_dict['type'] self.image_size = client_dict['image_size'] self.face_type = client_dict['face_type'] self.device_idx = client_dict['device_idx'] self.cpu_only = client_dict['device_type'] == 'CPU' self.output_path = Path( client_dict['output_dir']) if 'output_dir' in client_dict.keys( ) else None self.debug_dir = client_dict['debug_dir'] self.detector = client_dict['detector'] self.accurate_landmarks_extractor = client_dict[ 'accurate_landmarks_extractor'] self.cached_image = (None, None) self.e = None device_config = nnlib.DeviceConfig(cpu_only=self.cpu_only, force_gpu_idx=self.device_idx, allow_growth=True) if self.type == 'rects': if self.detector is not None: if self.detector == 'mt': nnlib.import_all(device_config) self.e = facelib.MTCExtractor() elif self.detector == 'dlib': nnlib.import_dlib(device_config) self.e = facelib.DLIBExtractor(nnlib.dlib) elif self.detector == 's3fd': nnlib.import_all(device_config) self.e = facelib.S3FDExtractor() else: raise ValueError("Wrong detector type.") if self.e is not None: self.e.__enter__() elif self.type == 'landmarks': nnlib.import_all(device_config) self.e = facelib.LandmarksExtractor(nnlib.keras) self.e.__enter__() if self.accurate_landmarks_extractor and device_config.gpu_vram_gb[ 0] >= 2: self.second_pass_e = facelib.S3FDExtractor() self.second_pass_e.__enter__() else: self.second_pass_e = None elif self.type == 'final': pass
def onClientInitialize(self, client_dict): self.safe_print('Running on %s.' % (client_dict['device_name'])) self.type = client_dict['type'] self.image_size = client_dict['image_size'] self.face_type = client_dict['face_type'] self.device_idx = client_dict['device_idx'] self.output_path = Path( client_dict['output_dir']) if 'output_dir' in client_dict.keys( ) else None self.debug = client_dict['debug'] self.detector = client_dict['detector'] self.keras = None self.tf = None self.tf_session = None self.e = None if self.type == 'rects': if self.detector is not None: if self.detector == 'mt': self.gpu_config = gpufmkmgr.GPUConfig( force_best_gpu_idx=self.device_idx, allow_growth=True) self.tf = gpufmkmgr.import_tf(self.gpu_config) self.tf_session = gpufmkmgr.get_tf_session() self.keras = gpufmkmgr.import_keras() self.e = facelib.MTCExtractor(self.keras, self.tf, self.tf_session) elif self.detector == 'dlib': self.dlib = gpufmkmgr.import_dlib(self.device_idx) self.e = facelib.DLIBExtractor(self.dlib) self.e.__enter__() elif self.type == 'landmarks': self.gpu_config = gpufmkmgr.GPUConfig( force_best_gpu_idx=self.device_idx, allow_growth=True) self.tf = gpufmkmgr.import_tf(self.gpu_config) self.tf_session = gpufmkmgr.get_tf_session() self.keras = gpufmkmgr.import_keras() self.e = facelib.LandmarksExtractor(self.keras) self.e.__enter__() elif self.type == 'final': pass return None
def extract_pass_process(sq, cq): e = None type = None device_idx = None debug = False output_path = None detector = None image_size = None face_type = None while True: obj = sq.get() obj_op = obj['op'] if obj_op == 'extract': data = obj['data'] filename_path = Path(data[0]) if not filename_path.exists(): cq.put({ 'op': 'error', 'close': False, 'is_file_not_found': True, 'data': obj['data'], 'message': 'Failed to extract %s, reason: file not found.' % (str(filename_path)) }) else: try: image = cv2.imread(str(filename_path)) if type == 'rects': rects = e.extract_from_bgr(image) cq.put({ 'op': 'extract_success', 'data': obj['data'], 'result': [str(filename_path), rects] }) elif type == 'landmarks': rects = data[1] landmarks = e.extract_from_bgr(image, rects) cq.put({ 'op': 'extract_success', 'data': obj['data'], 'result': [str(filename_path), landmarks] }) elif type == 'final': result = [] faces = data[1] if debug: debug_output_file = '{}_{}'.format( str( Path(str(output_path) + '_debug') / filename_path.stem), 'debug.png') debug_image = image.copy() for (face_idx, face) in enumerate(faces): rect = face[0] image_landmarks = np.array(face[1]) image_to_face_mat = facelib.LandmarksProcessor.get_transform_mat( image_landmarks, image_size, face_type) output_file = '{}_{}{}'.format( str(output_path / filename_path.stem), str(face_idx), '.png') if debug: facelib.LandmarksProcessor.draw_rect_landmarks( debug_image, rect, image_landmarks, image_size, face_type) face_image = cv2.warpAffine( image, image_to_face_mat, (image_size, image_size)) face_image_landmarks = facelib.LandmarksProcessor.transform_points( image_landmarks, image_to_face_mat) cv2.imwrite(output_file, face_image) a_png = AlignedPNG.load(output_file) d = { 'type': 'face', 'landmarks': face_image_landmarks.tolist(), 'yaw_value': facelib.LandmarksProcessor.calc_face_yaw( face_image_landmarks), 'pitch_value': facelib.LandmarksProcessor.calc_face_pitch( face_image_landmarks), 'source_filename': filename_path.name, 'source_rect': rect, 'source_landmarks': image_landmarks.tolist() } a_png.setFaceswapDictData(d) a_png.save(output_file) result.append(output_file) if debug: cv2.imwrite(debug_output_file, debug_image) cq.put({ 'op': 'extract_success', 'data': obj['data'], 'result': result }) except Exception as e: cq.put({ 'op': 'error', 'close': True, 'data': obj['data'], 'message': 'Failed to extract %s, reason: %s. \r\n%s' % (str(filename_path), str(e), traceback.format_exc()) }) break elif obj_op == 'init': try: type = obj['type'] image_size = obj['image_size'] face_type = obj['face_type'] device_idx = obj['device_idx'] output_path = Path( obj['output_dir']) if 'output_dir' in obj.keys() else None debug = obj['debug'] detector = obj['detector'] if type == 'rects': if detector is not None: if detector == 'mt': tf = gpufmkmgr.import_tf([device_idx], allow_growth=True) tf_session = gpufmkmgr.get_tf_session() keras = gpufmkmgr.import_keras() e = facelib.MTCExtractor(keras, tf, tf_session) elif detector == 'dlib': dlib = gpufmkmgr.import_dlib(device_idx) e = facelib.DLIBExtractor(dlib) e.__enter__() elif type == 'landmarks': gpufmkmgr.import_tf([device_idx], allow_growth=True) keras = gpufmkmgr.import_keras() e = facelib.LandmarksExtractor(keras) e.__enter__() elif type == 'final': pass cq.put({'op': 'init_ok'}) except Exception as e: cq.put({ 'op': 'error', 'close': True, 'message': 'Exception while initialization: %s' % (traceback.format_exc()) }) break if detector is not None and (type == 'rects' or type == 'landmarks'): e.__exit__()
def on_initialize(self, client_dict): self.type = client_dict["type"] self.image_size = client_dict["image_size"] self.face_type = client_dict["face_type"] self.device_idx = client_dict["device_idx"] self.cpu_only = client_dict["device_type"] == "CPU" self.final_output_path = (Path(client_dict["final_output_dir"]) if "final_output_dir" in client_dict.keys() else None) self.debug_dir = client_dict["debug_dir"] # transfer and set stdin in order to work code.interact in debug subprocess stdin_fd = client_dict["stdin_fd"] if stdin_fd is not None and DEBUG: sys.stdin = os.fdopen(stdin_fd) self.cached_image = (None, None) self.e = None device_config = nnlib.DeviceConfig(cpu_only=self.cpu_only, force_gpu_idx=self.device_idx, allow_growth=True) self.device_vram = device_config.gpu_vram_gb[0] intro_str = "Running on %s." % (client_dict["device_name"]) if not self.cpu_only and self.device_vram <= 2: intro_str += " Recommended to close all programs using this device." self.log_info(intro_str) if "rects" in self.type: if self.type == "rects-mt": nnlib.import_all(device_config) self.e = facelib.MTCExtractor() elif self.type == "rects-dlib": nnlib.import_dlib(device_config) self.e = facelib.DLIBExtractor(nnlib.dlib) elif self.type == "rects-s3fd": nnlib.import_all(device_config) self.e = facelib.S3FDExtractor() else: raise ValueError("Wrong type.") if self.e is not None: self.e.__enter__() elif self.type == "landmarks": nnlib.import_all(device_config) self.e = facelib.LandmarksExtractor(nnlib.keras) self.e.__enter__() if self.device_vram >= 2: self.second_pass_e = facelib.S3FDExtractor() self.second_pass_e.__enter__() else: self.second_pass_e = None elif self.type == "fanseg": nnlib.import_all(device_config) self.e = facelib.FANSegmentator( 256, FaceType.toString(FaceType.FULL)) self.e.__enter__() elif self.type == "final": pass