xp3857 commited on
Commit
7ffed14
·
1 Parent(s): 3f69792

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -65
app.py CHANGED
@@ -10,24 +10,15 @@ from skimage import color
10
  import torchvision.transforms as transforms
11
  from PIL import Image
12
  import torch
13
- import uuid
14
  import dlib
15
- uid = uuid.uuid4()
16
-
17
  #os.system("pip install dlib")
18
  os.system('bash setup.sh')
19
 
20
- def run_im(inp):
21
- outp=run(inp)
22
- return outp
23
-
24
-
25
  def lab2rgb(L, AB):
26
  """Convert an Lab tensor image to a RGB numpy output
27
  Parameters:
28
  L (1-channel tensor array): L channel images (range: [-1, 1], torch tensor array)
29
  AB (2-channel tensor array): ab channel images (range: [-1, 1], torch tensor array)
30
-
31
  Returns:
32
  rgb (RGB numpy image): rgb output images (range: [0, 255], numpy array)
33
  """
@@ -74,30 +65,35 @@ def inferRestoration(img, model_name):
74
  result = transforms.ToPILImage()(result)
75
  return result
76
 
77
- def inferColorization(img):
78
- model_name = "Deoldify"
79
- model = torch.hub.load('manhkhanhad/ImageRestorationInfer', 'DeOldifyColorization')
80
- transform_list = [
81
- transforms.ToTensor(),
82
- transforms.Normalize((0.5,), (0.5,))
83
- ]
84
- transform = transforms.Compose(transform_list)
85
- #a = transforms.ToTensor()(a)
86
- img = img.convert('L')
87
- img = transform(img)
88
- img = torch.unsqueeze(img, 0)
89
- result = model(img)
90
-
91
- result = result[0].detach()
92
- result = (result +1)/2.0
93
-
94
- #img = transforms.Grayscale(3)(img)
95
- #img = transforms.ToTensor()(img)
96
- #img = torch.unsqueeze(img, 0)
97
- #result = model(img)
98
- #result = torch.clip(result, min=0, max=1)
99
- image_pil = transforms.ToPILImage()(result)
100
- return image_pil
 
 
 
 
 
101
 
102
  transform_seq = get_transform(model_name)
103
  img = transform_seq(img)
@@ -131,42 +127,35 @@ def run_cmd(command):
131
  print("Process interrupted")
132
  sys.exit(1)
133
 
134
- def run(image):
135
-
136
- if os.path.isdir("Temp"):
137
- shutil.rmtree("Temp")
138
-
139
- os.makedirs("Temp")
140
- os.makedirs("Temp/input")
141
- print(type(image))
142
- cv2.imwrite("Temp/input/input_img.png", image)
143
-
144
- command = ("python run.py --input_folder "
145
- + "Temp/input"
146
- + " --output_folder "
147
- + "Temp"
148
- + " --GPU "
149
- + "-1"
150
- + " --with_scratch")
151
- run_cmd(command)
152
- print(f"Temp/final_output: {os.listdir(path='Temp/final_output')}")
153
- print(f"Temp: {os.listdir(path='Temp')}")
154
- print(f"Temp: {os.listdir(path='Temp/input')}")
155
- print(f"Temp: {os.listdir(path='Temp/stage_1_restore_output')}")
156
- print(f"Temp: {os.listdir(path='Temp/stage_2_detection_output')}")
157
- print(f"Temp: {os.listdir(path='Temp/stage_3_face_output')}")
158
- print(f"Temp: {os.listdir(path='Temp/final_output')}")
159
- print(f"Save: {os.listdir(path='save')}")
160
 
161
- result_restoration = Image.open("Temp/final_output/input_img.png")
162
- shutil.rmtree("Temp")
 
 
 
 
 
 
163
 
 
 
164
 
165
- result_colorization = inferColorization(result_restoration)
 
 
 
 
166
 
167
  return result_colorization
168
- def load_im(url):
169
- return url
170
 
171
 
172
  with gr.Blocks() as app:
@@ -174,8 +163,12 @@ with gr.Blocks() as app:
174
  gr.Column()
175
  with gr.Column():
176
  im = gr.Image(label="Input Image")
 
 
 
 
177
  im_btn=gr.Button(label="Restore")
178
  out_im = gr.Image(label="Restored Image")
179
  gr.Column()
180
- im_btn.click(run,im,out_im)
181
  app.queue(concurrency_count=100).launch(show_api=False)
 
10
  import torchvision.transforms as transforms
11
  from PIL import Image
12
  import torch
 
13
  import dlib
 
 
14
  #os.system("pip install dlib")
15
  os.system('bash setup.sh')
16
 
 
 
 
 
 
17
  def lab2rgb(L, AB):
18
  """Convert an Lab tensor image to a RGB numpy output
19
  Parameters:
20
  L (1-channel tensor array): L channel images (range: [-1, 1], torch tensor array)
21
  AB (2-channel tensor array): ab channel images (range: [-1, 1], torch tensor array)
 
22
  Returns:
23
  rgb (RGB numpy image): rgb output images (range: [0, 255], numpy array)
24
  """
 
65
  result = transforms.ToPILImage()(result)
66
  return result
67
 
68
+ def inferColorization(img,model_name):
69
+ #print(model_name)
70
+ if model_name == "Pix2Pix Resnet 9block":
71
+ model = torch.hub.load('manhkhanhad/ImageRestorationInfer', 'pix2pixColorization_resnet9b')
72
+ elif model_name == "Pix2Pix Unet 256":
73
+ model = torch.hub.load('manhkhanhad/ImageRestorationInfer', 'pix2pixColorization_unet256')
74
+ elif model_name == "Deoldify":
75
+ model = torch.hub.load('manhkhanhad/ImageRestorationInfer', 'DeOldifyColorization')
76
+ transform_list = [
77
+ transforms.ToTensor(),
78
+ transforms.Normalize((0.5,), (0.5,))
79
+ ]
80
+ transform = transforms.Compose(transform_list)
81
+ #a = transforms.ToTensor()(a)
82
+ img = img.convert('L')
83
+ img = transform(img)
84
+ img = torch.unsqueeze(img, 0)
85
+ result = model(img)
86
+
87
+ result = result[0].detach()
88
+ result = (result +1)/2.0
89
+
90
+ #img = transforms.Grayscale(3)(img)
91
+ #img = transforms.ToTensor()(img)
92
+ #img = torch.unsqueeze(img, 0)
93
+ #result = model(img)
94
+ #result = torch.clip(result, min=0, max=1)
95
+ image_pil = transforms.ToPILImage()(result)
96
+ return image_pil
97
 
98
  transform_seq = get_transform(model_name)
99
  img = transform_seq(img)
 
127
  print("Process interrupted")
128
  sys.exit(1)
129
 
130
+ def run(image,Restoration_mode, Colorizaition_mode):
131
+ if Restoration_mode == "BOPBTL":
132
+ if os.path.isdir("Temp"):
133
+ shutil.rmtree("Temp")
134
+
135
+ os.makedirs("Temp")
136
+ os.makedirs("Temp/input")
137
+ print(type(image))
138
+ cv2.imwrite("Temp/input/input_img.png", image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ command = ("python run.py --input_folder "
141
+ + "Temp/input"
142
+ + " --output_folder "
143
+ + "Temp"
144
+ + " --GPU "
145
+ + "-1"
146
+ + " --with_scratch")
147
+ run_cmd(command)
148
 
149
+ result_restoration = Image.open("Temp/final_output/input_img.png")
150
+ shutil.rmtree("Temp")
151
 
152
+ elif Restoration_mode == "Pix2Pix":
153
+ result_restoration = inferRestoration(image, Restoration_mode)
154
+ print("Restoration_mode",Restoration_mode)
155
+
156
+ result_colorization = inferColorization(result_restoration,Colorizaition_mode)
157
 
158
  return result_colorization
 
 
159
 
160
 
161
  with gr.Blocks() as app:
 
163
  gr.Column()
164
  with gr.Column():
165
  im = gr.Image(label="Input Image")
166
+ with gr.Row():
167
+ rad1 = gr.Radio(["BOPBTL", "Pix2Pix"])
168
+ rad2 = gr.Radio(["Deoldify", "Pix2Pix Resnet 9block","Pix2Pix Unet 256"])
169
+
170
  im_btn=gr.Button(label="Restore")
171
  out_im = gr.Image(label="Restored Image")
172
  gr.Column()
173
+ im_btn.click(run,[im,rad1,rad2],out_im)
174
  app.queue(concurrency_count=100).launch(show_api=False)