Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from RealESRGAN import RealESRGAN
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import tempfile
|
7 |
+
import time
|
8 |
+
|
9 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
10 |
+
|
11 |
+
def load_model(scale):
|
12 |
+
model = RealESRGAN(device, scale=scale)
|
13 |
+
weights_path = f'weights/RealESRGAN_x{scale}.pth'
|
14 |
+
try:
|
15 |
+
model.load_weights(weights_path, download=True)
|
16 |
+
print(f"Weights for scale {scale} loaded successfully.")
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error loading weights for scale {scale}: {e}")
|
19 |
+
model.load_weights(weights_path, download=False)
|
20 |
+
return model
|
21 |
+
|
22 |
+
model2 = load_model(2)
|
23 |
+
model4 = load_model(4)
|
24 |
+
model8 = load_model(8)
|
25 |
+
|
26 |
+
def enhance_image(image, scale):
|
27 |
+
try:
|
28 |
+
print(f"Enhancing image with scale {scale}...")
|
29 |
+
start_time = time.time()
|
30 |
+
image_np = np.array(image.convert('RGB'))
|
31 |
+
print(f"Image converted to numpy array: shape {image_np.shape}, dtype {image_np.dtype}")
|
32 |
+
|
33 |
+
if scale == '2x':
|
34 |
+
result = model2.predict(image_np)
|
35 |
+
elif scale == '4x':
|
36 |
+
result = model4.predict(image_np)
|
37 |
+
else:
|
38 |
+
result = model8.predict(image_np)
|
39 |
+
|
40 |
+
enhanced_image = Image.fromarray(np.uint8(result))
|
41 |
+
print(f"Image enhanced in {time.time() - start_time:.2f} seconds")
|
42 |
+
return enhanced_image
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Error enhancing image: {e}")
|
45 |
+
return image
|
46 |
+
|
47 |
+
def muda_dpi(input_image, dpi):
|
48 |
+
dpi_tuple = (dpi, dpi)
|
49 |
+
image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
50 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
51 |
+
image.save(temp_file, format='PNG', dpi=dpi_tuple)
|
52 |
+
temp_file.close()
|
53 |
+
return Image.open(temp_file.name)
|
54 |
+
|
55 |
+
def resize_image(input_image, width, height):
|
56 |
+
image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
57 |
+
resized_image = image.resize((width, height))
|
58 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
59 |
+
resized_image.save(temp_file, format='PNG')
|
60 |
+
temp_file.close()
|
61 |
+
return Image.open(temp_file.name)
|
62 |
+
|
63 |
+
def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
|
64 |
+
processed_images = []
|
65 |
+
file_paths = []
|
66 |
+
|
67 |
+
for image_file in image_files:
|
68 |
+
input_image = np.array(Image.open(image_file).convert('RGB'))
|
69 |
+
original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
70 |
+
|
71 |
+
if enhance:
|
72 |
+
original_image = enhance_image(original_image, scale)
|
73 |
+
|
74 |
+
if adjust_dpi:
|
75 |
+
original_image = muda_dpi(np.array(original_image), dpi)
|
76 |
+
|
77 |
+
if resize:
|
78 |
+
original_image = resize_image(np.array(original_image), width, height)
|
79 |
+
|
80 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
81 |
+
original_image.save(temp_file.name)
|
82 |
+
processed_images.append(original_image)
|
83 |
+
file_paths.append(temp_file.name)
|
84 |
+
|
85 |
+
return processed_images, file_paths
|
86 |
+
|
87 |
+
iface = gr.Interface(
|
88 |
+
fn=process_images,
|
89 |
+
inputs=[
|
90 |
+
gr.Files(label="Upload Image Files"), # Use gr.Files for multiple file uploads
|
91 |
+
gr.Checkbox(label="Enhance Images (ESRGAN)"),
|
92 |
+
gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
|
93 |
+
gr.Checkbox(label="Adjust DPI"),
|
94 |
+
gr.Number(label="DPI", value=300),
|
95 |
+
gr.Checkbox(label="Resize"),
|
96 |
+
gr.Number(label="Width", value=512),
|
97 |
+
gr.Number(label="Height", value=512)
|
98 |
+
],
|
99 |
+
outputs=[
|
100 |
+
gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
|
101 |
+
gr.Files(label="Download Final Images")
|
102 |
+
],
|
103 |
+
title="Multi-Image Enhancer",
|
104 |
+
description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results."
|
105 |
+
)
|
106 |
+
|
107 |
+
iface.launch(debug=True)
|