0x90e commited on
Commit
5e08d25
·
1 Parent(s): 8a2560d

better image file handling

Browse files
Files changed (4) hide show
  1. ESRGANer.py +2 -0
  2. inference.py +4 -5
  3. inference_manga_v2.py +3 -4
  4. process_image.py +7 -16
ESRGANer.py CHANGED
@@ -103,6 +103,8 @@ class ESRGANer():
103
  tile_idx = y * tiles_x + x + 1
104
  input_tile = self.img[:, :, input_start_y_pad:input_end_y_pad, input_start_x_pad:input_end_x_pad]
105
 
 
 
106
  # upscale tile
107
  try:
108
  with torch.no_grad():
 
103
  tile_idx = y * tiles_x + x + 1
104
  input_tile = self.img[:, :, input_start_y_pad:input_end_y_pad, input_start_x_pad:input_end_x_pad]
105
 
106
+ print("Image processing started...")
107
+
108
  # upscale tile
109
  try:
110
  with torch.no_grad():
inference.py CHANGED
@@ -13,7 +13,7 @@ def is_cuda():
13
  else:
14
  return False
15
 
16
- model_type = sys.argv[3]
17
 
18
  if model_type == "Anime":
19
  model_path = "models/4x-AnimeSharp.pth"
@@ -22,8 +22,7 @@ if model_type == "Photo":
22
  else:
23
  model_path = "models/4x-UniScaleV2_Sharp.pth"
24
 
25
- img_path = sys.argv[1]
26
- output_dir = sys.argv[2]
27
  device = torch.device('cuda' if is_cuda() else 'cpu')
28
 
29
  if model_type != "Photo":
@@ -45,7 +44,7 @@ for k, v in model.named_parameters():
45
  model = model.to(device)
46
 
47
  # Read image
48
- img = cv2.imread(img_path, cv2.IMREAD_COLOR)
49
  img = img * 1.0 / 255
50
  img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
51
  img_LR = img.unsqueeze(0)
@@ -57,4 +56,4 @@ output = upsampler.enhance(img_LR)
57
  output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
58
  output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
59
  output = (output * 255.0).round()
60
- cv2.imwrite(output_dir, output, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
 
13
  else:
14
  return False
15
 
16
+ model_type = sys.argv[2]
17
 
18
  if model_type == "Anime":
19
  model_path = "models/4x-AnimeSharp.pth"
 
22
  else:
23
  model_path = "models/4x-UniScaleV2_Sharp.pth"
24
 
25
+ OUTPUT_PATH = sys.argv[1]
 
26
  device = torch.device('cuda' if is_cuda() else 'cpu')
27
 
28
  if model_type != "Photo":
 
44
  model = model.to(device)
45
 
46
  # Read image
47
+ img = cv2.imread(OUTPUT_PATH, cv2.IMREAD_COLOR)
48
  img = img * 1.0 / 255
49
  img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
50
  img_LR = img.unsqueeze(0)
 
56
  output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
57
  output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
58
  output = (output * 255.0).round()
59
+ cv2.imwrite(OUTPUT_PATH, output, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
inference_manga_v2.py CHANGED
@@ -12,8 +12,7 @@ def is_cuda():
12
  return False
13
 
14
  model_path = 'models/4x_eula_digimanga_bw_v2_nc1_307k.pth'
15
- img_path = sys.argv[1]
16
- output_dir = sys.argv[2]
17
  device = torch.device('cuda' if is_cuda() else 'cpu')
18
 
19
  model = arch.RRDB_Net(1, 1, 64, 23, gc=32, upscale=4, norm_type=None, act_type='leakyrelu', mode='CNA', res_scale=1, upsample_mode='upconv')
@@ -32,7 +31,7 @@ for k, v in model.named_parameters():
32
  model = model.to(device)
33
 
34
  # Read image
35
- img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
36
  img = img * 1.0 / 255
37
  img = torch.from_numpy(img[np.newaxis, :, :]).float()
38
  img_LR = img.unsqueeze(0)
@@ -44,4 +43,4 @@ output = upsampler.enhance(img_LR)
44
  output = output.squeeze(dim=0).float().cpu().clamp_(0, 1).numpy()
45
  output = np.transpose(output, (1, 2, 0))
46
  output = (output * 255.0).round()
47
- cv2.imwrite(output_dir, output, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
 
12
  return False
13
 
14
  model_path = 'models/4x_eula_digimanga_bw_v2_nc1_307k.pth'
15
+ OUTPUT_PATH = sys.argv[1]
 
16
  device = torch.device('cuda' if is_cuda() else 'cpu')
17
 
18
  model = arch.RRDB_Net(1, 1, 64, 23, gc=32, upscale=4, norm_type=None, act_type='leakyrelu', mode='CNA', res_scale=1, upsample_mode='upconv')
 
31
  model = model.to(device)
32
 
33
  # Read image
34
+ img = cv2.imread(OUTPUT_PATH, cv2.IMREAD_GRAYSCALE)
35
  img = img * 1.0 / 255
36
  img = torch.from_numpy(img[np.newaxis, :, :]).float()
37
  img_LR = img.unsqueeze(0)
 
43
  output = output.squeeze(dim=0).float().cpu().clamp_(0, 1).numpy()
44
  output = np.transpose(output, (1, 2, 0))
45
  output = (output * 255.0).round()
46
+ cv2.imwrite(OUTPUT_PATH, output, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
process_image.py CHANGED
@@ -1,9 +1,9 @@
1
  import os
2
  import gradio as gr
3
  from run_cmd import run_cmd
4
- from random import randint
5
  from PIL import Image
6
  import tempfile
 
7
 
8
  temp_path = tempfile.gettempdir()
9
 
@@ -11,29 +11,20 @@ def inference(img, size, type):
11
  if not img:
12
  raise Exception("No image!")
13
 
14
- _id = randint(1, 10000)
15
- INPUT_DIR = os.path.join(temp_path, f"input_image{str(_id)}")
16
- OUTPUT_DIR = os.path.join(temp_path, f"output_image{str(_id)}")
17
- img_in_path = os.path.join(INPUT_DIR, "input.jpg")
18
- img_out_path = os.path.join(OUTPUT_DIR, f"output_{size}.png")
19
- run_cmd(f"mkdir {INPUT_DIR}")
20
- run_cmd(f"mkdir {OUTPUT_DIR}")
21
 
22
- img.save(img_in_path, "PNG")
23
 
24
  if type == "Manga":
25
- run_cmd(f"python inference_manga_v2.py {img_in_path} {img_out_path}")
26
  else:
27
- run_cmd(f"python inference.py {img_in_path} {img_out_path} {type}")
28
 
29
- img_out = Image.open(img_out_path)
30
 
31
  if size == "x2":
32
  img_out = img_out.resize((img_out.width // 2, img_out.height // 2), resample=Image.BICUBIC)
33
 
34
- # Remove input and output image
35
- run_cmd(f"rm -rf {INPUT_DIR}")
36
-
37
  img_out.thumbnail((600, 600), Image.ANTIALIAS)
38
 
39
- return img_out, gr.File.update(value=img_out_path, visible=True)
 
1
  import os
2
  import gradio as gr
3
  from run_cmd import run_cmd
 
4
  from PIL import Image
5
  import tempfile
6
+ import uuid
7
 
8
  temp_path = tempfile.gettempdir()
9
 
 
11
  if not img:
12
  raise Exception("No image!")
13
 
14
+ OUTPUT_PATH = os.path.join(temp_path, f"{str(uuid.uuid4())[0:12]}_{size}.png")
 
 
 
 
 
 
15
 
16
+ img.save(OUTPUT_PATH)
17
 
18
  if type == "Manga":
19
+ run_cmd(f"python inference_manga_v2.py {OUTPUT_PATH}")
20
  else:
21
+ run_cmd(f"python inference.py {OUTPUT_PATH} {type}")
22
 
23
+ img_out = Image.open(OUTPUT_PATH)
24
 
25
  if size == "x2":
26
  img_out = img_out.resize((img_out.width // 2, img_out.height // 2), resample=Image.BICUBIC)
27
 
 
 
 
28
  img_out.thumbnail((600, 600), Image.ANTIALIAS)
29
 
30
+ return img_out, gr.File.update(value=OUTPUT_PATH, visible=True)