File size: 3,062 Bytes
cc079b9
 
 
 
4fcd96b
cc079b9
 
 
 
 
 
 
4fcd96b
cc079b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33d2f51
cc079b9
4fcd96b
 
 
 
 
cc079b9
 
 
 
 
 
 
 
 
 
 
 
 
4fcd96b
 
 
 
4162d5c
359aac9
 
 
 
 
 
 
 
 
cc079b9
 
 
 
 
f69473d
4162d5c
 
f69473d
cc079b9
84a7577
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import tensorflow as tf
import numpy as np
from imageio.v2 import imread
import os, glob, cv2, shutil
from super_image import EdsrModel, ImageLoader
from PIL import Image
import gradio as gr

pb = 'dmt.pb'
style_dim = 8
img_size=256

model_scale = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=2)

def preprocess(img):
  return (img / 255. - 0.5) * 2

def deprocess(img):
  return (img + 1) / 2

def load_image(path):
  img = cv2.resize(imread(path), (img_size, img_size))
  img_ = np.expand_dims(preprocess(img), 0)
  return img / 255., img_

def inference(A,B):
  with tf.Graph().as_default():
      output_graph_def = tf.compat.v1.GraphDef()

      with open(pb, 'rb') as fr:
          output_graph_def.ParseFromString(fr.read())
          tf.import_graph_def(output_graph_def, name='')

      sess = tf.compat.v1.Session()
      sess.run(tf.compat.v1.global_variables_initializer())
      graph = tf.compat.v1.get_default_graph()
      Xs = graph.get_tensor_by_name('decoder_1/g:0')
      X = graph.get_tensor_by_name('X:0')
      Y = graph.get_tensor_by_name('Y:0')
  A_img, A_img_ = load_image(A)
  B_img, B_img_ = load_image(B)
  Xs_ = sess.run(Xs, feed_dict={X: A_img_, Y: B_img_})
  output = deprocess(Xs_)[0]
  output = np.array(np.array(output)*255,dtype=np.uint8)
  # output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)

  image = Image.fromarray(output)
  inputs = ImageLoader.load_image(image)
  preds = model_scale(inputs)
  ImageLoader.save_image(preds, 'output/scaled_2x.png')
  # return output



def makeupTransfer(arr1,arr2):
  print("-"*8)
  shutil.rmtree("input/")
  os.makedirs("input/")
  output1 = cv2.cvtColor(arr1, cv2.COLOR_BGR2RGB)
  output2 = cv2.cvtColor(arr2, cv2.COLOR_BGR2RGB)
  cv2.imwrite("input/original.png",output1)
  cv2.imwrite("input/ref.png",output2)
  no_makeup = "input/original.png"
  makeup = "input/ref.png"
  inference(no_makeup, makeup)
  img = cv2.imread("output/scaled_2x.png")
  return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  # return tr_out

examples = [['faces/no_makeup/xfsy_0226.png', 'faces/makeup/XMY-136.png'],
            ['faces/no_makeup/XYUH-006.png', 'faces/makeup/XMY-266.png'],
            ['faces/no_makeup/vSYYZ306.png', 'faces/makeup/vRX916.png'],
            ['faces/no_makeup/xfsy_0405.png', 'faces/makeup/vFG137.png'],
            ['faces/no_makeup/xfsy_0055.png', 'faces/makeup/vFG756.png'],
            ['faces/no_makeup/xfsy_0521.png', 'faces/makeup/XMY-074.png'],
            ['faces/no_makeup/vSYYZ639.png', 'faces/makeup/vFG112.png'],
            ['faces/no_makeup/vSYYZ429.png', 'faces/makeup/XMY-014.png'],
            ['faces/no_makeup/xfsy_0068.png', 'faces/makeup/vFG56.png']]

app = gr.Interface(fn=makeupTransfer,
             inputs=[gr.Image(label="Reference Image",type='numpy'),
                     gr.Image(label="Makeup Image",type='numpy')],
             outputs=gr.Image(label="Makeup Transfer Image",type='numpy'),
             title="MakeUp Transfer APP",
             examples=examples,
             cache_examples=True
             )

app.launch(share=True)