Spaces:
Running
Running
rmayormartins
commited on
Commit
•
2ecae98
1
Parent(s):
93ead80
Subindo arquivos
Browse files- README.md +14 -7
- app.py +102 -0
- gatuno.JPG +0 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,13 +1,20 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: ecl-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: image-enhancer
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: "4.12.0"
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
+
# Image Enhancer
|
13 |
+
|
14 |
+
Upload an image (.jpg, .png), enhance using AI (use ESRGAN), adjust DPI, resize and download the final result.
|
15 |
+
|
16 |
+
## Versão teste 1 (14/05)
|
17 |
+
|
18 |
+
- Ramon Mayor Martins
|
19 |
+
- E-mail: [rmayormartins@gmail.com](mailto:rmayormartins@gmail.com)
|
20 |
+
|
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_image(input_image, enhance, scale, adjust_dpi, dpi, resize, width, height):
|
64 |
+
original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
65 |
+
|
66 |
+
if enhance:
|
67 |
+
original_image = enhance_image(original_image, scale)
|
68 |
+
|
69 |
+
if adjust_dpi:
|
70 |
+
original_image = muda_dpi(np.array(original_image), dpi)
|
71 |
+
|
72 |
+
if resize:
|
73 |
+
original_image = resize_image(np.array(original_image), width, height)
|
74 |
+
|
75 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
76 |
+
original_image.save(temp_file.name)
|
77 |
+
return original_image, temp_file.name
|
78 |
+
|
79 |
+
iface = gr.Interface(
|
80 |
+
fn=process_image,
|
81 |
+
inputs=[
|
82 |
+
gr.Image(label="Upload"),
|
83 |
+
gr.Checkbox(label="Enhance Image (ESRGAN)"),
|
84 |
+
gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
|
85 |
+
gr.Checkbox(label="Adjust DPI"),
|
86 |
+
gr.Number(label="DPI", value=300),
|
87 |
+
gr.Checkbox(label="Resize"),
|
88 |
+
gr.Number(label="Width", value=512),
|
89 |
+
gr.Number(label="Height", value=512)
|
90 |
+
],
|
91 |
+
outputs=[
|
92 |
+
gr.Image(label="Final Image"),
|
93 |
+
gr.File(label="Download Final Image")
|
94 |
+
],
|
95 |
+
title="Image Enhancer",
|
96 |
+
description="Upload an image (.jpg, .png), enhance using AI, adjust DPI, resize and download the final result.",
|
97 |
+
examples=[
|
98 |
+
["gatuno.jpg"]
|
99 |
+
]
|
100 |
+
)
|
101 |
+
|
102 |
+
iface.launch(debug=True)
|
gatuno.JPG
ADDED
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
Pillow
|
3 |
+
numpy
|
4 |
+
RealESRGAN
|
5 |
+
gradio
|