AItool commited on
Commit
8c3cf7f
·
verified ·
1 Parent(s): e756496

Delete app_enhance.py

Browse files
Files changed (1) hide show
  1. app_enhance.py +0 -127
app_enhance.py DELETED
@@ -1,127 +0,0 @@
1
- import os
2
- import subprocess
3
- import spaces
4
- import torch
5
- import cv2
6
- import uuid
7
- import gradio as gr
8
- import numpy as np
9
-
10
- from PIL import Image
11
- from basicsr.archs.srvgg_arch import SRVGGNetCompact
12
- from gfpgan.utils import GFPGANer
13
- from realesrgan.utils import RealESRGANer
14
-
15
- def runcmd(cmd, verbose = False):
16
-
17
- process = subprocess.Popen(
18
- cmd,
19
- stdout = subprocess.PIPE,
20
- stderr = subprocess.PIPE,
21
- text = True,
22
- shell = True
23
- )
24
- std_out, std_err = process.communicate()
25
- if verbose:
26
- print(std_out.strip(), std_err)
27
- pass
28
-
29
- if not os.path.exists('GFPGANv1.4.pth'):
30
- runcmd("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
31
- if not os.path.exists('realesr-general-x4v3.pth'):
32
- runcmd("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
33
-
34
- model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
35
- model_path = 'realesr-general-x4v3.pth'
36
- half = True if torch.cuda.is_available() else "cpu" #device = "cuda" if torch.cuda.is_available() else "cpu" #model.to(device)
37
- upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
38
-
39
-
40
- @spaces.GPU(duration=15)
41
- def enhance_image(
42
- input_image: Image,
43
- scale: int,
44
- enhance_mode: str,
45
- ):
46
- only_face = enhance_mode == "Only Face Enhance"
47
- if enhance_mode == "Only Face Enhance":
48
- face_enhancer = GFPGANer(model_path='GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2)
49
- elif enhance_mode == "Only Image Enhance":
50
- face_enhancer = None
51
- else:
52
- face_enhancer = GFPGANer(model_path='GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
53
-
54
- img = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR)
55
-
56
- h, w = img.shape[0:2]
57
- if h < 300:
58
- img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
59
-
60
- if face_enhancer is not None:
61
- _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=only_face, paste_back=True)
62
- else:
63
- output, _ = upsampler.enhance(img, outscale=scale)
64
-
65
- # if scale != 2:
66
- # interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
67
- # h, w = img.shape[0:2]
68
- # output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
69
-
70
- h, w = output.shape[0:2]
71
- max_size = 3480
72
- if h > max_size:
73
- w = int(w * max_size / h)
74
- h = max_size
75
-
76
- if w > max_size:
77
- h = int(h * max_size / w)
78
- w = max_size
79
-
80
- output = cv2.resize(output, (w, h), interpolation=cv2.INTER_LANCZOS4)
81
-
82
- enhanced_image = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
83
- tmpPrefix = "/tmp/gradio/"
84
-
85
- extension = 'png'
86
-
87
- targetDir = f"{tmpPrefix}output/"
88
- if not os.path.exists(targetDir):
89
- os.makedirs(targetDir)
90
-
91
- enhanced_path = f"{targetDir}{uuid.uuid4()}.{extension}"
92
- enhanced_image.save(enhanced_path, quality=100)
93
-
94
- return enhanced_image, enhanced_path
95
-
96
-
97
- def create_demo() -> gr.Blocks:
98
- with gr.Blocks() as demo:
99
- with gr.Row():
100
- with gr.Column():
101
- scale = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="Scale")
102
- with gr.Column():
103
- enhance_mode = gr.Dropdown(
104
- label="Enhance Mode",
105
- choices=[
106
- "Only Face Enhance",
107
- "Only Image Enhance",
108
- "Face Enhance + Image Enhance",
109
- ],
110
- value="Face Enhance + Image Enhance",
111
- )
112
- g_btn = gr.Button("Enhance Image")
113
- with gr.Row():
114
- with gr.Column():
115
- input_image = gr.Image(label="Input Image", type="pil")
116
- with gr.Column():
117
- output_image = gr.Image(label="Enhanced Image", type="pil", interactive=False)
118
- enhance_image_path = gr.File(label="Download the Enhanced Image", interactive=False)
119
-
120
-
121
- g_btn.click(
122
- fn=enhance_image,
123
- inputs=[input_image, scale, enhance_mode],
124
- outputs=[output_image, enhance_image_path],
125
- )
126
-
127
- return demo