Ahsen Khaliq commited on
Commit
3afae8b
1 Parent(s): f25bddf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -22
app.py CHANGED
@@ -19,34 +19,56 @@ import torch
19
  from basicsr.utils import imwrite
20
  from gfpgan import GFPGANer
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  os.system("pip install gfpgan")
23
  os.system("pip freeze")
24
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth -P .")
25
 
26
 
27
- def run_cmd(command):
28
- try:
29
- print(command)
30
- call(command, shell=True)
31
- except KeyboardInterrupt:
32
- print("Process interrupted")
33
- sys.exit(1)
34
-
35
  def inference(img):
36
- _id = randint(1, 10000)
37
- INPUT_DIR = "/tmp/input_image" + str(_id) + "/"
38
- OUTPUT_DIR = "/tmp/output_image" + str(_id) + "/"
39
- run_cmd("rm -rf " + INPUT_DIR)
40
- run_cmd("rm -rf " + OUTPUT_DIR)
41
- run_cmd("mkdir " + INPUT_DIR)
42
- run_cmd("mkdir " + OUTPUT_DIR)
43
- basewidth = 256
44
- wpercent = (basewidth/float(img.size[0]))
45
- hsize = int((float(img.size[1])*float(wpercent)))
46
- img = img.resize((basewidth,hsize), Image.ANTIALIAS)
47
- img.save(INPUT_DIR + "1.jpg", "JPEG")
48
- run_cmd("python inference_gfpgan.py --upscale 2 --test_path "+ INPUT_DIR + " --save_root " + OUTPUT_DIR + " --paste_back")
49
- return os.path.join(OUTPUT_DIR, "1_00.png")
50
 
51
  title = "GFP-GAN"
52
  description = "Gradio demo for GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once"
 
19
  from basicsr.utils import imwrite
20
  from gfpgan import GFPGANer
21
 
22
+ # background upsampler
23
+ if args.bg_upsampler == 'realesrgan':
24
+ if not torch.cuda.is_available(): # CPU
25
+ import warnings
26
+ warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '
27
+ 'If you really want to use it, please modify the corresponding codes.')
28
+ bg_upsampler = None
29
+ else:
30
+ from basicsr.archs.rrdbnet_arch import RRDBNet
31
+ from realesrgan import RealESRGANer
32
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
33
+ bg_upsampler = RealESRGANer(
34
+ scale=2,
35
+ model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
36
+ model=model,
37
+ tile=args.bg_tile,
38
+ tile_pad=10,
39
+ pre_pad=0,
40
+ half=True) # need to set False in CPU mode
41
+ else:
42
+ bg_upsampler = None
43
+
44
+ # set up GFPGAN restorer
45
+ restorer = GFPGANer(
46
+ model_path=args.model_path,
47
+ upscale=args.upscale,
48
+ arch=args.arch,
49
+ channel_multiplier=args.channel,
50
+ bg_upsampler=bg_upsampler)
51
+ img_list = sorted(glob.glob(os.path.join(args.test_path, '*')))
52
+
53
+
54
+
55
  os.system("pip install gfpgan")
56
  os.system("pip freeze")
57
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth -P .")
58
 
59
 
 
 
 
 
 
 
 
 
60
  def inference(img):
61
+ input_img = cv2.imread(img, cv2.IMREAD_COLOR)
62
+ # restore faces and background if necessary
63
+ cropped_faces, restored_faces, restored_img = restorer.enhance(
64
+ input_img, has_aligned=args.aligned, only_center_face=args.only_center_face, paste_back=args.paste_back)
65
+ # save faces
66
+ # save cropped face
67
+
68
+ # save comparison image
69
+ cmp_img = np.concatenate((cropped_face, restored_face), axis=1)
70
+
71
+ return Image.fromarray(cmp_img)
 
 
 
72
 
73
  title = "GFP-GAN"
74
  description = "Gradio demo for GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once"