NotFungibleIO commited on
Commit
fdd734a
β€’
1 Parent(s): 6a896e3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import cv2
4
+ import gradio as gr
5
+ import torch
6
+ from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
+ from gfpgan.utils import GFPGANer
8
+ from realesrgan.utils import RealESRGANer
9
+
10
+ os.system("pip freeze")
11
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
12
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
13
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
14
+
15
+ torch.hub.download_url_to_file(
16
+ 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg',
17
+ 'lincoln.jpg')
18
+ torch.hub.download_url_to_file(
19
+ 'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg',
20
+ 'AI-generate.jpg')
21
+ torch.hub.download_url_to_file(
22
+ 'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg',
23
+ 'Blake_Lively.jpg')
24
+ torch.hub.download_url_to_file(
25
+ 'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png',
26
+ '10045.png')
27
+
28
+ # background enhancer with RealESRGAN
29
+ model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
30
+ model_path = 'realesr-general-x4v3.pth'
31
+ half = True if torch.cuda.is_available() else False
32
+ upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
33
+
34
+ # Use GFPGAN for face enhancement
35
+ face_enhancer_v3 = GFPGANer(
36
+ model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
37
+ face_enhancer_v2 = GFPGANer(
38
+ model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
39
+ os.makedirs('output', exist_ok=True)
40
+
41
+
42
+ def inference(img, version, scale):
43
+ print(img, version, scale)
44
+ try:
45
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
46
+ if len(img.shape) == 3 and img.shape[2] == 4:
47
+ img_mode = 'RGBA'
48
+ else:
49
+ img_mode = None
50
+
51
+ h, w = img.shape[0:2]
52
+ if h < 300:
53
+ img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
54
+
55
+ if version == 'v1.2':
56
+ face_enhancer = face_enhancer_v2
57
+ else:
58
+ face_enhancer = face_enhancer_v3
59
+ try:
60
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
61
+ except RuntimeError as error:
62
+ print('Error', error)
63
+ else:
64
+ extension = 'png'
65
+
66
+ try:
67
+ if scale != 2:
68
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
69
+ h, w = img.shape[0:2]
70
+ output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
71
+ except Exception as error:
72
+ print('wrong scale input.', error)
73
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
74
+ extension = 'png'
75
+ else:
76
+ extension = 'jpg'
77
+ save_path = f'output/out.{extension}'
78
+ cv2.imwrite(save_path, output)
79
+
80
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
81
+ return output, save_path
82
+ except Exception as error:
83
+ print('global exception', error)
84
+ return None, None
85
+
86
+
87
+ title = "GFPGAN: Practical Face Restoration Algorithm"
88
+ description = r"""Gradio demo for <a href='https://github.com/TencentARC/GFPGAN' target='_blank'><b>GFPGAN: Towards Real-World Blind Face Restoration with Generative Facial Prior</b></a>.<br>
89
+ It can be used to restore your **old photos** or improve **AI-generated faces**.<br>
90
+ To use it, simply upload your image.<br>
91
+ If GFPGAN is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo</a> and recommend it to your friends 😊
92
+ """
93
+ article = r"""
94
+
95
+ [![download](https://img.shields.io/github/downloads/TencentARC/GFPGAN/total.svg)](https://github.com/TencentARC/GFPGAN/releases)
96
+ [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social)](https://github.com/TencentARC/GFPGAN)
97
+ [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2101.04061)
98
+
99
+ If you have any question, please email πŸ“§ `xintao.wang@outlook.com` or `xintaowang@tencent.com`.
100
+
101
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_GFPGAN' alt='visitor badge'></center>
102
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=Gradio_Xintao_GFPGAN' alt='visitor badge'></center>
103
+ """
104
+ gr.Interface(
105
+ inference, [
106
+ gr.inputs.Image(type="filepath", label="Input"),
107
+ gr.inputs.Radio(['v1.2', 'v1.3'], type="value", default='v1.3', label='GFPGAN version'),
108
+ gr.inputs.Number(label="Rescaling factor", default=2)
109
+ ], [
110
+ gr.outputs.Image(type="numpy", label="Output (The whole image)"),
111
+ gr.outputs.File(label="Download the output image")
112
+ ],
113
+ title=title,
114
+ description=description,
115
+ article=article,
116
+ examples=[['AI-generate.jpg', 'v1.3', 2], ['lincoln.jpg', 'v1.3', 2], ['Blake_Lively.jpg', 'v1.3', 2],
117
+ ['10045.png', 'v1.3', 2]]).launch()