File size: 2,953 Bytes
bd72a39
1e4d453
b0c5c57
13090e3
1e4d453
 
13090e3
 
 
 
 
914a2f2
13090e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b398cd4
13090e3
 
 
 
 
 
 
 
 
 
 
 
 
 
986d686
 
 
b398cd4
13090e3
 
 
 
 
 
 
 
 
 
 
 
 
 
986d686
 
 
b398cd4
13090e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675f1d6
13090e3
 
 
 
 
 
 
 
 
 
 
 
6620248
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import cv2
import torch
import random
import numpy as np
import gradio as gr
from util import util
from util.img2pixl import pixL
from data import create_dataset
from models import create_model
from options.test_options import TestOptions

opt = TestOptions().parse()
opt.num_threads = 0
opt.batch_size = 1
opt.display_id = -1
opt.no_dropout = True

model = create_model(opt)
model.setup(opt)

num_inferences = 0

def preprocess(image):

  im_type = None
  imgH, imgW = image.shape[:2]
  aspect_ratio = imgW / imgH


  if 0.75 <= aspect_ratio <= 1.75:

    image = cv2.resize(image, (512, 512))
    image = pixL().toThePixL(image,6,False)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = np.asarray([image])
    image = np.transpose(image, (0, 3, 1, 2))

    image = inference(image)

    return image

  elif 1.75 <= aspect_ratio: # upper boundary

    image = cv2.resize(image, (1024, 512))
    middlePoint = image.shape[1] // 2
    half_1 = image[:,:middlePoint]
    half_2 = image[:,middlePoint:]
    images = [half_1,half_2]
      
    for image in images:
      image = pixL().toThePixL(image,6,False)
      image = np.asarray([image])
      image = np.transpose(image, (0, 3, 1, 2))
      image = inference(image)
    
    image = cv2.hconcat([images[0], images[1]])
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    return image

  elif 0.00 <= aspect_ratio <= 0.75:

    image = cv2.resize(image, (512, 1024))
    middlePoint = image.shape[0] // 2
    half_1 = image[:middlePoint,:]
    half_2 = image[middlePoint:,:]
    images = [half_1,half_2]
      
    for image in images:
      image = pixL().toThePixL(image,6,False)
      image = np.asarray([image])
      image = np.transpose(image, (0, 3, 1, 2))
      image = inference(image)
    
    image = cv2.vconcat([images[0], images[1]])
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    return image

def postprocess(image):
  image = util.tensor2im(image)
  return image

def inference(image):
  global model

  data = {"A": None, "A_paths": None}
  data['A'] = torch.FloatTensor(image)

  model.set_input(data)
  model.test()

  image = model.get_current_visuals()['fake']

  return image

def pixera_CYCLEGAN(image):
  global num_inferences
  
  image = preprocess(image)

  image = postprocess(image)

  num_inferences += 1
  print(num_inferences)
  
  return image

title_ = "Pixera: Create your own Pixel Art"
description_ = "."

examples_path = f"{os.getcwd()}/imgs"
examples_ = os.listdir(examples_path)
random.shuffle(examples_)
examples_ = [[f"{examples_path}/{example}"] for example in examples_]

 
demo = gr.Interface(pixera_CYCLEGAN, inputs = [gr.Image(show_label= False)],
                                     outputs = [gr.Image(show_label= False)], 
                                     examples = examples_,
                                     title = title_,
                                     description= description_)
demo.launch()