tuan2308 commited on
Commit
68cea4c
·
verified ·
1 Parent(s): 052a51e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -14,7 +14,6 @@ from realesrgan.archs.srvgg_arch import SRVGGNetCompact
14
  # Global (CPU-only data; KHÔNG chạm CUDA ở đây)
15
  # --------------------
16
  last_file = None
17
- img_mode = "RGBA"
18
 
19
  DEVICE = "cpu" # set trong gpu_startup()
20
  USE_HALF = False # set trong gpu_startup()
@@ -49,28 +48,10 @@ def rnd_string(x):
49
  chars = "abcdefghijklmnopqrstuvwxyz_0123456789"
50
  return "".join(random.choice(chars) for _ in range(x))
51
 
52
- def has_transparency(img):
53
- if img.info.get("transparency", None) is not None:
54
- return True
55
- if img.mode == "P":
56
- transparent = img.info.get("transparency", -1)
57
- for _, index in img.getcolors():
58
- if index == transparent:
59
- return True
60
- elif img.mode == "RGBA":
61
- extrema = img.getextrema()
62
- if extrema[3][0] < 255:
63
- return True
64
- return False
65
-
66
  def image_properties(img):
67
- global img_mode
68
  if img:
69
- if has_transparency(img):
70
- img_mode = "RGBA"
71
- else:
72
- img_mode = "RGB"
73
- return f"Resolution: Width: {img.size[0]}, Height: {img.size[1]} | Color Mode: {img_mode}"
74
 
75
  def reset():
76
  global last_file
@@ -184,9 +165,21 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
184
 
185
  upsampler = get_upsampler(model_name, denoise_strength)
186
 
187
- # PIL -> cv2 BGRA
188
  cv_img = numpy.array(img)
189
- img_bgra = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  try:
192
  if face_enhance:
@@ -201,9 +194,15 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
201
  print('Error', error)
202
  return None
203
  else:
204
- extension = 'png' if img_mode == 'RGBA' else 'jpg'
205
- out_filename = f"output_{rnd_string(8)}.{extension}"
206
- cv2.imwrite(out_filename, output)
 
 
 
 
 
 
207
  global last_file
208
  last_file = out_filename
209
  return out_filename
@@ -237,7 +236,7 @@ def main():
237
  with gr.Group():
238
  input_image = gr.Image(label="Input Image", type="pil", image_mode="RGBA")
239
  input_image_properties = gr.Textbox(label="Image Properties", max_lines=1)
240
- output_image = gr.Image(label="Output Image", image_mode="RGBA")
241
  with gr.Row():
242
  reset_btn = gr.Button("Remove images")
243
  restore_btn = gr.Button("Upscale")
 
14
  # Global (CPU-only data; KHÔNG chạm CUDA ở đây)
15
  # --------------------
16
  last_file = None
 
17
 
18
  DEVICE = "cpu" # set trong gpu_startup()
19
  USE_HALF = False # set trong gpu_startup()
 
48
  chars = "abcdefghijklmnopqrstuvwxyz_0123456789"
49
  return "".join(random.choice(chars) for _ in range(x))
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def image_properties(img):
 
52
  if img:
53
+ # Chỉ báo thông tin trực tiếp từ ảnh, không kiểm tra alpha
54
+ return f"Resolution: Width: {img.size[0]}, Height: {img.size[1]} | Color Mode: {img.mode}"
 
 
 
55
 
56
  def reset():
57
  global last_file
 
165
 
166
  upsampler = get_upsampler(model_name, denoise_strength)
167
 
168
+ # PIL -> cv2 (giữ nguyên nếu có alpha; ta sẽ bỏ alpha trước khi lưu JPG)
169
  cv_img = numpy.array(img)
170
+ if cv_img.ndim == 3 and cv_img.shape[2] == 4:
171
+ # RGBA -> BGRA
172
+ img_bgra = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
173
+ elif cv_img.ndim == 3 and cv_img.shape[2] == 3:
174
+ # RGB -> BGR, rồi thêm alpha giả để pipeline cũ vẫn chạy nếu cần
175
+ bgr = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
176
+ alpha = numpy.full((bgr.shape[0], bgr.shape[1], 1), 255, dtype=bgr.dtype)
177
+ img_bgra = numpy.concatenate([bgr, alpha], axis=2)
178
+ else:
179
+ # 1-channel (L) -> BGR + alpha
180
+ bgr = cv2.cvtColor(cv_img, cv2.COLOR_GRAY2BGR)
181
+ alpha = numpy.full((bgr.shape[0], bgr.shape[1], 1), 255, dtype=bgr.dtype)
182
+ img_bgra = numpy.concatenate([bgr, alpha], axis=2)
183
 
184
  try:
185
  if face_enhance:
 
194
  print('Error', error)
195
  return None
196
  else:
197
+ out_filename = f"output_{rnd_string(8)}.jpg"
198
+ # Đảm bảo ảnh 3 kênh trước khi lưu JPG
199
+ if output.ndim == 3 and output.shape[2] == 4:
200
+ output_to_save = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)
201
+ elif output.ndim == 3 and output.shape[2] == 3:
202
+ output_to_save = output
203
+ else:
204
+ output_to_save = cv2.cvtColor(output, cv2.COLOR_GRAY2BGR)
205
+ cv2.imwrite(out_filename, output_to_save)
206
  global last_file
207
  last_file = out_filename
208
  return out_filename
 
236
  with gr.Group():
237
  input_image = gr.Image(label="Input Image", type="pil", image_mode="RGBA")
238
  input_image_properties = gr.Textbox(label="Image Properties", max_lines=1)
239
+ output_image = gr.Image(label="Output Image", image_mode="RGB")
240
  with gr.Row():
241
  reset_btn = gr.Button("Remove images")
242
  restore_btn = gr.Button("Upscale")