Jonny001 commited on
Commit
14f7cf7
1 Parent(s): 187a496

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"),
84
+ gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Select Resolution model'),
85
+ gr.Checkbox(label="Apply DPI"),
86
+ gr.Number(label="DPI", value=300),
87
+ gr.Checkbox(label="Apply 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="Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding.",
97
+ theme="NoCrypt/miku"
98
+ ]
99
+ )
100
+
101
+ iface.launch(debug=True)