Doron Adler
commited on
Commit
•
0e3fe1f
1
Parent(s):
958a8ec
Style a face to look more "Vintage"
Browse files- .gitattributes +2 -0
- Example00001.jpg +0 -0
- Example00002.jpg +0 -0
- Example00003.jpg +0 -0
- Example00004.jpg +0 -0
- Example00005.jpg +0 -0
- Example00006.jpg +0 -0
- README.md +30 -2
- Sample00001.jpg +0 -0
- Sample00002.jpg +0 -0
- Sample00003.jpg +0 -0
- Sample00004.jpg +0 -0
- Sample00005.jpg +0 -0
- Sample00006.jpg +0 -0
- app.py +104 -0
- face_detection.py +140 -0
- ffhqu2vintage512_pix2pixHD_v1E11-inp2inst-simp.onnx +3 -0
- requirements.txt +7 -0
- shape_predictor_5_face_landmarks.dat +3 -0
.gitattributes
CHANGED
@@ -25,3 +25,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
ffhqu2vintage512_pix2pixHD_v1E11-inp2inst-simp.onnx filter=lfs diff=lfs merge=lfs -text
|
29 |
+
shape_predictor_5_face_landmarks.dat filter=lfs diff=lfs merge=lfs -text
|
Example00001.jpg
ADDED
Example00002.jpg
ADDED
Example00003.jpg
ADDED
Example00004.jpg
ADDED
Example00005.jpg
ADDED
Example00006.jpg
ADDED
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
@@ -9,3 +9,31 @@ pinned: false
|
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Vintage Style
|
3 |
+
emoji: 💁🏼♀️
|
4 |
colorFrom: pink
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
|
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
12 |
+
|
13 |
+
# Configuration
|
14 |
+
|
15 |
+
`title`: _string_
|
16 |
+
Display title for the Space
|
17 |
+
|
18 |
+
`emoji`: _string_
|
19 |
+
Space emoji (emoji-only character allowed)
|
20 |
+
|
21 |
+
`colorFrom`: _string_
|
22 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
23 |
+
|
24 |
+
`colorTo`: _string_
|
25 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
26 |
+
|
27 |
+
`sdk`: _string_
|
28 |
+
Can be either `gradio` or `streamlit`
|
29 |
+
|
30 |
+
`sdk_version` : _string_
|
31 |
+
Only applicable for `streamlit` SDK.
|
32 |
+
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
33 |
+
|
34 |
+
`app_file`: _string_
|
35 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
36 |
+
Path is relative to the root of the repository.
|
37 |
+
|
38 |
+
`pinned`: _boolean_
|
39 |
+
Whether the Space stays on top of your list.
|
Sample00001.jpg
ADDED
Sample00002.jpg
ADDED
Sample00003.jpg
ADDED
Sample00004.jpg
ADDED
Sample00005.jpg
ADDED
Sample00006.jpg
ADDED
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system("pip install --upgrade pip")
|
3 |
+
os.system("pip install gradio==2.5.3")
|
4 |
+
import onnxruntime as rt
|
5 |
+
import sys
|
6 |
+
import PIL
|
7 |
+
from PIL import Image, ImageOps, ImageFile
|
8 |
+
import numpy as np
|
9 |
+
from pathlib import Path
|
10 |
+
import collections
|
11 |
+
from typing import Union, List
|
12 |
+
import scipy.ndimage
|
13 |
+
import requests
|
14 |
+
|
15 |
+
MODEL_FILE = "ffhqu2vintage512_pix2pixHD_v1E11-inp2inst-simp.onnx"
|
16 |
+
so = rt.SessionOptions()
|
17 |
+
so.inter_op_num_threads = 4
|
18 |
+
so.intra_op_num_threads = 4
|
19 |
+
session = rt.InferenceSession(MODEL_FILE, sess_options=so)
|
20 |
+
input_name = session.get_inputs()[0].name
|
21 |
+
print("input_name = " + str(input_name))
|
22 |
+
output_name = session.get_outputs()[0].name
|
23 |
+
print("output_name = " + str(output_name))
|
24 |
+
|
25 |
+
import os
|
26 |
+
os.system("pip install dlib")
|
27 |
+
import face_detection
|
28 |
+
|
29 |
+
def array_to_image(array_in):
|
30 |
+
array_in = np.squeeze(255*(array_in + 1)/2)
|
31 |
+
array_in = np.transpose(array_in, (1, 2, 0))
|
32 |
+
im = Image.fromarray(array_in.astype(np.uint8))
|
33 |
+
return im
|
34 |
+
|
35 |
+
def image_as_array(image_in):
|
36 |
+
im_array = np.array(image_in, np.float32)
|
37 |
+
im_array = (im_array/255)*2 - 1
|
38 |
+
im_array = np.transpose(im_array, (2, 0, 1))
|
39 |
+
im_array = np.expand_dims(im_array, 0)
|
40 |
+
return im_array
|
41 |
+
|
42 |
+
def find_aligned_face(image_in, size=512):
|
43 |
+
aligned_image, n_faces, quad = face_detection.align(image_in, face_index=0, output_size=size)
|
44 |
+
return aligned_image, n_faces, quad
|
45 |
+
|
46 |
+
def align_first_face(image_in, size=512):
|
47 |
+
aligned_image, n_faces, quad = find_aligned_face(image_in,size=size)
|
48 |
+
if n_faces == 0:
|
49 |
+
try:
|
50 |
+
image_in = ImageOps.exif_transpose(image_in)
|
51 |
+
except:
|
52 |
+
print("exif problem, not rotating")
|
53 |
+
image_in = image_in.resize((size, size))
|
54 |
+
im_array = image_as_array(image_in)
|
55 |
+
else:
|
56 |
+
im_array = image_as_array(aligned_image)
|
57 |
+
|
58 |
+
return im_array
|
59 |
+
|
60 |
+
def img_concat_h(im1, im2):
|
61 |
+
dst = Image.new('RGB', (im1.width + im2.width, im1.height))
|
62 |
+
dst.paste(im1, (0, 0))
|
63 |
+
dst.paste(im2, (im1.width, 0))
|
64 |
+
return dst
|
65 |
+
|
66 |
+
import gradio as gr
|
67 |
+
|
68 |
+
def face2vintage(
|
69 |
+
img: Image.Image,
|
70 |
+
size: int
|
71 |
+
) -> Image.Image:
|
72 |
+
|
73 |
+
aligned_img = align_first_face(img)
|
74 |
+
if aligned_img is None:
|
75 |
+
output=None
|
76 |
+
else:
|
77 |
+
output = session.run([output_name], {input_name: aligned_img})[0]
|
78 |
+
output = array_to_image(output)
|
79 |
+
aligned_img = array_to_image(aligned_img).resize((output.width, output.height))
|
80 |
+
output = img_concat_h(aligned_img, output)
|
81 |
+
|
82 |
+
return output
|
83 |
+
|
84 |
+
def inference(img):
|
85 |
+
out = face2vintage(img, 512)
|
86 |
+
return out
|
87 |
+
|
88 |
+
|
89 |
+
title = "Vintage style Pix2PixHD"
|
90 |
+
description = "Style a face to look more \"Vintage\". Upload an image with a face, or click on one of the examples below. If a face could not be detected, an image will still be created."
|
91 |
+
article = "<hr><p style='text-align: center'>See the <a href='https://github.com/justinpinkney/pix2pixHD' target='_blank'>Github Repo</a></p><p style='text-align: center'>samples: <img src='https://hf.space/gradioiframe/Norod78/VintageStyle/file/Sample00001.jpg' alt='Sample00001'/><img src='https://hf.space/gradioiframe/Norod78/VintageStyle/file/Sample00002.jpg' alt='Sample00002'/><img src='https://hf.space/gradioiframe/Norod78/VintageStyle/file/Sample00003.jpg' alt='Sample00003'/><img src='https://hf.space/gradioiframe/Norod78/VintageStyle/file/Sample00004.jpg' alt='Sample00004'/><img src='https://hf.space/gradioiframe/Norod78/VintageStyle/file/Sample00005.jpg' alt='Sample00005'/></p><p>The \"Vintage Style\" Pix2PixHD model was trained by <a href='https://linktr.ee/Norod78' target='_blank'>Doron Adler</a></p>"
|
92 |
+
|
93 |
+
examples=[['Example00001.jpg'],['Example00002.jpg'],['Example00003.jpg'],['Example00004.jpg'],['Example00005.jpg'], ['Example00006.jpg']]
|
94 |
+
gr.Interface(
|
95 |
+
inference,
|
96 |
+
gr.inputs.Image(type="pil", label="Input"),
|
97 |
+
gr.outputs.Image(type="pil", label="Output"),
|
98 |
+
title=title,
|
99 |
+
description=description,
|
100 |
+
article=article,
|
101 |
+
examples=examples,
|
102 |
+
enable_queue=True,
|
103 |
+
allow_flagging=False
|
104 |
+
).launch()
|
face_detection.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Justin Pinkney
|
2 |
+
|
3 |
+
import dlib
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
from PIL import ImageOps
|
8 |
+
from scipy.ndimage import gaussian_filter
|
9 |
+
import cv2
|
10 |
+
|
11 |
+
|
12 |
+
MODEL_PATH = "shape_predictor_5_face_landmarks.dat"
|
13 |
+
detector = dlib.get_frontal_face_detector()
|
14 |
+
|
15 |
+
|
16 |
+
def align(image_in, face_index=0, output_size=256):
|
17 |
+
try:
|
18 |
+
image_in = ImageOps.exif_transpose(image_in)
|
19 |
+
except:
|
20 |
+
print("exif problem, not rotating")
|
21 |
+
|
22 |
+
landmarks = list(get_landmarks(image_in))
|
23 |
+
n_faces = len(landmarks)
|
24 |
+
face_index = min(n_faces-1, face_index)
|
25 |
+
if n_faces == 0:
|
26 |
+
aligned_image = image_in
|
27 |
+
quad = None
|
28 |
+
else:
|
29 |
+
aligned_image, quad = image_align(image_in, landmarks[face_index], output_size=output_size)
|
30 |
+
|
31 |
+
return aligned_image, n_faces, quad
|
32 |
+
|
33 |
+
|
34 |
+
def composite_images(quad, img, output):
|
35 |
+
"""Composite an image into and output canvas according to transformed co-ords"""
|
36 |
+
output = output.convert("RGBA")
|
37 |
+
img = img.convert("RGBA")
|
38 |
+
input_size = img.size
|
39 |
+
src = np.array(((0, 0), (0, input_size[1]), input_size, (input_size[0], 0)), dtype=np.float32)
|
40 |
+
dst = np.float32(quad)
|
41 |
+
mtx = cv2.getPerspectiveTransform(dst, src)
|
42 |
+
img = img.transform(output.size, Image.PERSPECTIVE, mtx.flatten(), Image.BILINEAR)
|
43 |
+
output.alpha_composite(img)
|
44 |
+
|
45 |
+
return output.convert("RGB")
|
46 |
+
|
47 |
+
|
48 |
+
def get_landmarks(image):
|
49 |
+
"""Get landmarks from PIL image"""
|
50 |
+
shape_predictor = dlib.shape_predictor(MODEL_PATH)
|
51 |
+
|
52 |
+
max_size = max(image.size)
|
53 |
+
reduction_scale = int(max_size/512)
|
54 |
+
if reduction_scale == 0:
|
55 |
+
reduction_scale = 1
|
56 |
+
downscaled = image.reduce(reduction_scale)
|
57 |
+
img = np.array(downscaled)
|
58 |
+
detections = detector(img, 0)
|
59 |
+
|
60 |
+
for detection in detections:
|
61 |
+
try:
|
62 |
+
face_landmarks = [(reduction_scale*item.x, reduction_scale*item.y) for item in shape_predictor(img, detection).parts()]
|
63 |
+
yield face_landmarks
|
64 |
+
except Exception as e:
|
65 |
+
print(e)
|
66 |
+
|
67 |
+
|
68 |
+
def image_align(src_img, face_landmarks, output_size=512, transform_size=2048, enable_padding=True, x_scale=1, y_scale=1, em_scale=0.1, alpha=False):
|
69 |
+
# Align function modified from ffhq-dataset
|
70 |
+
# See https://github.com/NVlabs/ffhq-dataset for license
|
71 |
+
|
72 |
+
lm = np.array(face_landmarks)
|
73 |
+
lm_eye_left = lm[2:3] # left-clockwise
|
74 |
+
lm_eye_right = lm[0:1] # left-clockwise
|
75 |
+
|
76 |
+
# Calculate auxiliary vectors.
|
77 |
+
eye_left = np.mean(lm_eye_left, axis=0)
|
78 |
+
eye_right = np.mean(lm_eye_right, axis=0)
|
79 |
+
eye_avg = (eye_left + eye_right) * 0.5
|
80 |
+
eye_to_eye = 0.71*(eye_right - eye_left)
|
81 |
+
mouth_avg = lm[4]
|
82 |
+
eye_to_mouth = 1.35*(mouth_avg - eye_avg)
|
83 |
+
|
84 |
+
# Choose oriented crop rectangle.
|
85 |
+
x = eye_to_eye.copy()
|
86 |
+
x /= np.hypot(*x)
|
87 |
+
x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)
|
88 |
+
x *= x_scale
|
89 |
+
y = np.flipud(x) * [-y_scale, y_scale]
|
90 |
+
c = eye_avg + eye_to_mouth * em_scale
|
91 |
+
quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])
|
92 |
+
quad_orig = quad.copy()
|
93 |
+
qsize = np.hypot(*x) * 2
|
94 |
+
|
95 |
+
img = src_img.convert('RGBA').convert('RGB')
|
96 |
+
|
97 |
+
# Shrink.
|
98 |
+
shrink = int(np.floor(qsize / output_size * 0.5))
|
99 |
+
if shrink > 1:
|
100 |
+
rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))
|
101 |
+
img = img.resize(rsize, Image.ANTIALIAS)
|
102 |
+
quad /= shrink
|
103 |
+
qsize /= shrink
|
104 |
+
|
105 |
+
# Crop.
|
106 |
+
border = max(int(np.rint(qsize * 0.1)), 3)
|
107 |
+
crop = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
|
108 |
+
crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]), min(crop[3] + border, img.size[1]))
|
109 |
+
if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:
|
110 |
+
img = img.crop(crop)
|
111 |
+
quad -= crop[0:2]
|
112 |
+
|
113 |
+
# Pad.
|
114 |
+
pad = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
|
115 |
+
pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0), max(pad[3] - img.size[1] + border, 0))
|
116 |
+
if enable_padding and max(pad) > border - 4:
|
117 |
+
pad = np.maximum(pad, int(np.rint(qsize * 0.3)))
|
118 |
+
img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')
|
119 |
+
h, w, _ = img.shape
|
120 |
+
y, x, _ = np.ogrid[:h, :w, :1]
|
121 |
+
mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w-1-x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h-1-y) / pad[3]))
|
122 |
+
blur = qsize * 0.02
|
123 |
+
img += (gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)
|
124 |
+
img += (np.median(img, axis=(0,1)) - img) * np.clip(mask, 0.0, 1.0)
|
125 |
+
img = np.uint8(np.clip(np.rint(img), 0, 255))
|
126 |
+
if alpha:
|
127 |
+
mask = 1-np.clip(3.0 * mask, 0.0, 1.0)
|
128 |
+
mask = np.uint8(np.clip(np.rint(mask*255), 0, 255))
|
129 |
+
img = np.concatenate((img, mask), axis=2)
|
130 |
+
img = Image.fromarray(img, 'RGBA')
|
131 |
+
else:
|
132 |
+
img = Image.fromarray(img, 'RGB')
|
133 |
+
quad += pad[:2]
|
134 |
+
|
135 |
+
# Transform.
|
136 |
+
img = img.transform((transform_size, transform_size), Image.QUAD, (quad + 0.5).flatten(), Image.BILINEAR)
|
137 |
+
if output_size < transform_size:
|
138 |
+
img = img.resize((output_size, output_size), Image.ANTIALIAS)
|
139 |
+
|
140 |
+
return img, quad_orig
|
ffhqu2vintage512_pix2pixHD_v1E11-inp2inst-simp.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bdecf0aab26bb6915f0b8eb04b0778e30f20e5afe6f4477d8e0705c9bd347daa
|
3 |
+
size 729804845
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
opencv-python-headless
|
3 |
+
Pillow
|
4 |
+
scikit-image
|
5 |
+
onnxruntime
|
6 |
+
scipy
|
7 |
+
cmake
|
shape_predictor_5_face_landmarks.dat
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c4b1e9804792707d3a405c2c16a80a20269e6675021f64a41d30fffafbc41888
|
3 |
+
size 9150489
|