def generate(model, input_args): encoder, decoder, latent_avg, manipulator = model original = input_args['original'] input_size = [1024, 1024] output = original.copy() cropped, n_faces, quad = face_detection.align(original) for k, v in editor.edits.items(): layer_index, channel_index, sense = v conv_name = editor.idx_dict[layer_index] manipulator.edits[conv_name][channel_index] = input_args[k]*sense for i in range(n_faces): # We already did the first one above if i > 0: cropped, _, quad = face_detection.align(original, face_index=i) transformed_crop = editor.run(encoder, decoder, latent_avg, cropped) output = face_detection.composite_images(quad, transformed_crop, output) return output
def test_face_detection_multi_face(): image_in = Image.open("test_data/two-face.jpg") aligned_image_1, n_faces_1, quad_1 = face_detection.align(image_in, face_index=0, output_size=256) aligned_image_2, n_faces_2, quad_2 = face_detection.align(image_in, face_index=1, output_size=256) assert aligned_image_1.size == aligned_image_2.size == (256, 256) assert n_faces_1 == n_faces_2 == 2 assert (quad_1 != quad_2).all()
def test_composite_different_face(): """Don't get the same image back""" image_in = Image.open("test_data/two-face.jpg") output = image_in.copy() aligned_image_1, n_faces_1, quad_1 = face_detection.align(image_in, face_index=0, output_size=1024) aligned_image_2, n_faces_2, quad_2 = face_detection.align(image_in, face_index=1, output_size=1024) composited = face_detection.composite_images(quad_1, aligned_image_2, output) composited.thumbnail((128, 128)) image_in.thumbnail((128, 128)) assert not np.allclose(np.array(composited), np.array(image_in), atol=100)
def test_face_detection_one_face(): image_in = Image.open("test_data/face-ok.jpg") aligned_image, n_faces, quad = face_detection.align(image_in, face_index=0, output_size=256) assert aligned_image.size == (256, 256) assert n_faces == 1 assert quad.shape == (4, 2)
def test_composite(): """Get the orginal back when compositing the same face""" image_in = Image.open("test_data/face-ok.jpg") output = image_in.copy() aligned_image, n_faces, quad = face_detection.align(image_in, face_index=0, output_size=1024) composited = face_detection.composite_images(quad, aligned_image, output) composited.thumbnail((128, 128)) image_in.thumbnail((128, 128)) assert np.allclose(np.array(composited), np.array(image_in), atol=100)