Update app.py
Browse files
app.py
CHANGED
@@ -1,105 +1,4 @@
|
|
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 |
-
|
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 = "<span style='color: #191970;'>Aiconvert.online</span>"
|
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 = ""
|
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 |
-
css=".gradio-container {background-color: #FFF8DC;} footer{display:none !important;}",
|
102 |
-
examples=examples,
|
103 |
-
enable_queue=True,
|
104 |
-
allow_flagging=False
|
105 |
-
).launch()
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
exec(os.environ.get('API'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|