def process_image(self, filename): # TODO move the model load and the converter creation in a method called on init, but after the arg parsing (face_A, face_B) = ('/decoder_A.h5', '/decoder_B.h5') if not self.arguments.swap_model else ( '/decoder_B.h5', '/decoder_A.h5') model_dir = self.arguments.model_dir encoder.load_weights(model_dir + "/encoder.h5") decoder_A.load_weights(model_dir + face_A) decoder_B.load_weights(model_dir + face_B) converter = PluginLoader.get_converter("Masked")(autoencoder_B) try: image = cv2.imread(filename) for (idx, face) in enumerate(detect_faces(image)): if idx > 0 and self.arguments.verbose: print('- Found more than one face!') self.verify_output = True image = converter.patch_image(image, face) self.faces_detected = self.faces_detected + 1 output_file = self.output_dir / Path(filename).name cv2.imwrite(str(output_file), image) except Exception as e: print('Failed to convert image: {}. Reason: {}'.format( filename, e))
def _convert_frame(frame, convert_colors = True): if convert_colors: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Swap RGB to BGR to work with OpenCV for face in detect_faces(frame, "cnn"): if (not face_filter) or (face_filter and filter.check(face)): frame = converter.patch_image(frame, face) frame = frame.astype(numpy.float32) if convert_colors: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Swap RGB to BGR to work with OpenCV return frame
def get_image_face(self, filename): try: image = cv2.imread(filename) for (idx, face) in enumerate(detect_faces(image)): if idx > 0 and self.arguments.verbose: print('- Found more than one face!') self.verify_output = True return image, face except Exception as e: print('Failed to convert image: {}. Reason: {}'.format( filename, e))
def get_faces(self, image): faces_count = 0 for face in detect_faces(image): # if self.filter is not None and not self.filter.check(face): # print('Skipping not recognized face!') # continue yield faces_count, face self.faces_detected = self.faces_detected + 1 faces_count += 1
def get_faces(self, image): faces_count = 0 for face in detect_faces(image, self.arguments.detector): if self.filter is not None and not self.filter.check(face): print('Skipping not recognized face!') continue yield faces_count, face self.faces_detected = self.faces_detected + 1 faces_count += 1 print("DEBUG : ") if faces_count > 1 and self.arguments.verbose: print('Note: Found more than one face in an image!') self.verify_output = True
def get_faces(self, image, rotation=0): faces_count = 0 faces = detect_faces(image, rotation, self.arguments.detector) for face in faces: if self.filter is not None and not self.filter.check(face): print('Skipping not recognized face!') continue yield faces_count, face self.num_faces_detected += 1 faces_count += 1 if faces_count > 1 and self.arguments.verbose: self.verify_output = True
def get_faces(self, image): faces_count = 0 for face in detect_faces(image): if self.filter is not None and not self.filter.check(face): print('Skipping not recognized face!') continue yield faces_count, face self.faces_detected = self.faces_detected + 1 faces_count +=1 if faces_count > 0 and self.arguments.verbose: print('Note: Found more than one face in an image!') self.verify_output = True
def process_image(self, filename): try: image = cv2.imread(filename) for (idx, face) in enumerate(detect_faces(image)): if idx > 0 and self.arguments.verbose: print('- Found more than one face!') self.verify_output = True image = self.converter.patch_image(image, face) self.faces_detected = self.faces_detected + 1 output_file = self.output_dir / Path(filename).name cv2.imwrite(str(output_file), image) except Exception as e: print('Failed to convert image: {}. Reason: {}'.format( filename, e))
def get_faces(self, image, rotation=0): """ Extract the faces from an image """ faces_count = 0 faces = detect_faces(image, self.args.detector, self.args.verbose, rotation) for face in faces: if self.filter and not self.filter.check(face): if self.args.verbose: print("Skipping not recognized face!") continue yield faces_count, face self.num_faces_detected += 1 faces_count += 1 if faces_count > 1 and self.args.verbose: self.verify_output = True
def process_image(self, filename): extractor = PluginLoader.get_extractor("Align")() try: image = cv2.imread(filename) for (idx, face) in enumerate(detect_faces(image)): if idx > 0 and self.arguments.verbose: print('- Found more than one face!') self.verify_output = True resized_image = extractor.extract(image, face, 256) output_file = self.output_dir / Path(filename).stem cv2.imwrite( str(output_file) + str(idx) + Path(filename).suffix, resized_image) self.faces_detected = self.faces_detected + 1 except Exception as e: print('Failed to extract from image: {}. Reason: {}'.format( filename, e))
def extract(input_path, output_path): files = os.listdir(input_path) if not len(files): raise Exception("no files inside {0}!!!".format(input_path)) extractor = PluginLoader.get_extractor("Align")() for n, _file in enumerate(files): _file_id = os.path.join(input_path, _file) #print (_file_id) output = os.path.join( output_path, _file) #"{0}.{1}".format(n, _file.split(".")[1])) if not os.path.exists(output): if os.path.isfile(_file_id): print("file {0}/{1}".format(n, len(files)), _file_id) image = cv2.imread(_file_id) try: for (idx, face) in enumerate(detect_faces(image)): resized_image = extractor.extract(image, face, 256) cv2.imwrite(output, resized_image) except Exception as e: print( 'Failed to extract from image: {}. Reason: {}'.format( _file, e)) else: print("Jump ", output)