Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,21 +40,15 @@ def download_file(url, dir_path, file_name):
|
|
40 |
print("Download complete.")
|
41 |
except Exception as e:
|
42 |
print(f"Error downloading {file_name}: {e}")
|
43 |
-
# In case wget is not available, you might need to use requests or urllib
|
44 |
-
# import requests
|
45 |
-
# with open(file_path, 'wb') as f:
|
46 |
-
# f.write(requests.get(url).content)
|
47 |
return file_path
|
48 |
|
49 |
# --- DOWNLOAD MODELS AND EXAMPLES ---
|
50 |
print("Checking for required files...")
|
51 |
-
# Models
|
52 |
models_dir = 'models'
|
53 |
download_file('https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth', models_dir, 'realesr-general-x4v3.pth')
|
54 |
download_file('https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth', models_dir, 'GFPGANv1.4.pth')
|
55 |
download_file('https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth', models_dir, 'RestoreFormer.pth')
|
56 |
|
57 |
-
# Example Images
|
58 |
examples_dir = 'examples'
|
59 |
example1_path = download_file('https://raw.githubusercontent.com/TencentARC/GFPGAN/master/inputs/whole_imgs/10045.png', examples_dir, 'example1.png')
|
60 |
example2_path = download_file('https://raw.githubusercontent.com/TencentARC/GFPGAN/master/inputs/whole_imgs/Blake_Lively.jpg', examples_dir, 'example2.jpg')
|
@@ -70,13 +64,13 @@ try:
|
|
70 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
71 |
half = torch.cuda.is_available()
|
72 |
bg_upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
73 |
-
print("Background Upsampler (Real-ESRGAN) loaded.")
|
74 |
except Exception as e:
|
75 |
print(f"Error loading background upsampler: {e}. The app may not work correctly.")
|
76 |
|
77 |
# --- CORE IMAGE PROCESSING FUNCTION ---
|
78 |
-
def upscale_image(img_path, version
|
79 |
-
"""Enhance an image using GFPGAN and Real-ESRGAN."""
|
80 |
if not img_path:
|
81 |
raise gr.Error("Please upload an image.")
|
82 |
if not bg_upsampler:
|
@@ -90,22 +84,15 @@ def upscale_image(img_path, version, scale):
|
|
90 |
|
91 |
face_enhancer = GFPGANer(
|
92 |
model_path=os.path.join(models_dir, f'{version}.pth'),
|
93 |
-
upscale=2, # GFPGAN
|
94 |
arch='RestoreFormer' if version == 'RestoreFormer' else 'clean',
|
95 |
channel_multiplier=2,
|
96 |
-
bg_upsampler=bg_upsampler
|
97 |
)
|
98 |
|
|
|
99 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
100 |
|
101 |
-
if scale != 2:
|
102 |
-
h, w = output.shape[0:2]
|
103 |
-
target_w, target_h = int(w * scale / 2), int(h * scale / 2)
|
104 |
-
if target_w > 8000 or target_h > 8000:
|
105 |
-
raise gr.Error(f"Target size is too large. Please choose a smaller scale.")
|
106 |
-
interpolation = cv2.INTER_LANCZOS4 if scale > 2 else cv2.INTER_AREA
|
107 |
-
output = cv2.resize(output, (target_w, target_h), interpolation=interpolation)
|
108 |
-
|
109 |
output_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
110 |
ext = 'png' if has_alpha else 'jpg'
|
111 |
|
@@ -121,7 +108,7 @@ def upscale_image(img_path, version, scale):
|
|
121 |
# --- GRADIO UI LAYOUT ---
|
122 |
with gr.Blocks(css=CSS_STYLING, theme=gr.themes.Base()) as demo:
|
123 |
gr.Markdown("<h1 id='main-title'>NeuraVision AI Image Upscaler</h1>", elem_id="main-title")
|
124 |
-
gr.Markdown("<p id='main-subtitle'>Enhance old, blurry, and low-resolution photos with AI.</p>", elem_id="main-subtitle")
|
125 |
|
126 |
with gr.Row(variant="panel"):
|
127 |
# LEFT COLUMN (INPUT & CONTROLS)
|
@@ -130,19 +117,14 @@ with gr.Blocks(css=CSS_STYLING, theme=gr.themes.Base()) as demo:
|
|
130 |
|
131 |
version = gr.Radio(
|
132 |
['GFPGANv1.4', 'RestoreFormer'], value='GFPGANv1.4',
|
133 |
-
label='AI Model', info="v1.4
|
134 |
-
)
|
135 |
-
|
136 |
-
scale = gr.Slider(
|
137 |
-
minimum=1, maximum=8, step=0.5, value=4,
|
138 |
-
label="Upscale Factor", info="How many times larger to make the image."
|
139 |
)
|
140 |
|
141 |
submit_btn = gr.Button("Enhance Image", variant="primary", elem_id="submit-button")
|
142 |
|
143 |
gr.Examples(
|
144 |
-
examples=[[example1_path, "RestoreFormer"
|
145 |
-
inputs=[input_image, version
|
146 |
label="Click an example to start"
|
147 |
)
|
148 |
|
@@ -154,7 +136,7 @@ with gr.Blocks(css=CSS_STYLING, theme=gr.themes.Base()) as demo:
|
|
154 |
# --- BUTTON & EVENT HANDLING ---
|
155 |
submit_btn.click(
|
156 |
fn=upscale_image,
|
157 |
-
inputs=[input_image, version
|
158 |
outputs=[output_image, download_button]
|
159 |
)
|
160 |
input_image.clear(lambda: (None, None), None, [output_image, download_button])
|
|
|
40 |
print("Download complete.")
|
41 |
except Exception as e:
|
42 |
print(f"Error downloading {file_name}: {e}")
|
|
|
|
|
|
|
|
|
43 |
return file_path
|
44 |
|
45 |
# --- DOWNLOAD MODELS AND EXAMPLES ---
|
46 |
print("Checking for required files...")
|
|
|
47 |
models_dir = 'models'
|
48 |
download_file('https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth', models_dir, 'realesr-general-x4v3.pth')
|
49 |
download_file('https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth', models_dir, 'GFPGANv1.4.pth')
|
50 |
download_file('https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth', models_dir, 'RestoreFormer.pth')
|
51 |
|
|
|
52 |
examples_dir = 'examples'
|
53 |
example1_path = download_file('https://raw.githubusercontent.com/TencentARC/GFPGAN/master/inputs/whole_imgs/10045.png', examples_dir, 'example1.png')
|
54 |
example2_path = download_file('https://raw.githubusercontent.com/TencentARC/GFPGAN/master/inputs/whole_imgs/Blake_Lively.jpg', examples_dir, 'example2.jpg')
|
|
|
64 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
65 |
half = torch.cuda.is_available()
|
66 |
bg_upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
67 |
+
print("Background Upsampler (Real-ESRGAN) loaded for 4x enhancement.")
|
68 |
except Exception as e:
|
69 |
print(f"Error loading background upsampler: {e}. The app may not work correctly.")
|
70 |
|
71 |
# --- CORE IMAGE PROCESSING FUNCTION ---
|
72 |
+
def upscale_image(img_path, version):
|
73 |
+
"""Enhance an image using GFPGAN and Real-ESRGAN with a fixed 4x upscale."""
|
74 |
if not img_path:
|
75 |
raise gr.Error("Please upload an image.")
|
76 |
if not bg_upsampler:
|
|
|
84 |
|
85 |
face_enhancer = GFPGANer(
|
86 |
model_path=os.path.join(models_dir, f'{version}.pth'),
|
87 |
+
upscale=2, # Native GFPGAN upscale factor
|
88 |
arch='RestoreFormer' if version == 'RestoreFormer' else 'clean',
|
89 |
channel_multiplier=2,
|
90 |
+
bg_upsampler=bg_upsampler # Real-ESRGAN used for 4x background
|
91 |
)
|
92 |
|
93 |
+
# This will produce a 4x enhanced image because the bg_upsampler is 4x
|
94 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
output_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
97 |
ext = 'png' if has_alpha else 'jpg'
|
98 |
|
|
|
108 |
# --- GRADIO UI LAYOUT ---
|
109 |
with gr.Blocks(css=CSS_STYLING, theme=gr.themes.Base()) as demo:
|
110 |
gr.Markdown("<h1 id='main-title'>NeuraVision AI Image Upscaler</h1>", elem_id="main-title")
|
111 |
+
gr.Markdown("<p id='main-subtitle'>Enhance old, blurry, and low-resolution photos with AI (Fixed 4x Upscale).</p>", elem_id="main-subtitle")
|
112 |
|
113 |
with gr.Row(variant="panel"):
|
114 |
# LEFT COLUMN (INPUT & CONTROLS)
|
|
|
117 |
|
118 |
version = gr.Radio(
|
119 |
['GFPGANv1.4', 'RestoreFormer'], value='GFPGANv1.4',
|
120 |
+
label='AI Model', info="v1.4 for general use. RestoreFormer for old photos."
|
|
|
|
|
|
|
|
|
|
|
121 |
)
|
122 |
|
123 |
submit_btn = gr.Button("Enhance Image", variant="primary", elem_id="submit-button")
|
124 |
|
125 |
gr.Examples(
|
126 |
+
examples=[[example1_path, "RestoreFormer"], [example2_path, "GFPGANv1.4"]],
|
127 |
+
inputs=[input_image, version],
|
128 |
label="Click an example to start"
|
129 |
)
|
130 |
|
|
|
136 |
# --- BUTTON & EVENT HANDLING ---
|
137 |
submit_btn.click(
|
138 |
fn=upscale_image,
|
139 |
+
inputs=[input_image, version],
|
140 |
outputs=[output_image, download_button]
|
141 |
)
|
142 |
input_image.clear(lambda: (None, None), None, [output_image, download_button])
|