abiabidali commited on
Commit
5a91dbf
·
verified ·
1 Parent(s): e13ad87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -42
app.py CHANGED
@@ -1,18 +1,14 @@
1
  import torch
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
4
- from transformers import BlipProcessor, BlipForConditionalGeneration # Example for Hugging Face model
5
  import gradio as gr
6
  import numpy as np
7
- import io
8
- import zipfile
9
- import os
10
  import time
 
11
 
12
- # Set the device to CUDA if available, otherwise CPU
13
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14
 
15
- # Load the RealESRGAN models for enhancement
16
  def load_model(scale):
17
  model = RealESRGAN(device, scale=scale)
18
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
@@ -21,72 +17,101 @@ def load_model(scale):
21
  print(f"Weights for scale {scale} loaded successfully.")
22
  except Exception as e:
23
  print(f"Error loading weights for scale {scale}: {e}")
 
24
  return model
25
 
26
  model2 = load_model(2)
27
  model4 = load_model(4)
28
  model8 = load_model(8)
29
 
30
- # Load Hugging Face model and processor for image description
31
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
32
- caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
33
-
34
  def enhance_image(image, scale):
35
  try:
 
 
36
  image_np = np.array(image.convert('RGB'))
37
- model = model2 if scale == '2x' else model4 if scale == '4x' else model8
38
- result = model.predict(image_np)
39
- return Image.fromarray(np.uint8(result))
 
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
  print(f"Error enhancing image: {e}")
42
  return image
43
 
44
- def describe_image(image):
45
- inputs = processor(image, return_tensors="pt").to(device)
46
- generated_ids = caption_model.generate(**inputs)
47
- description = processor.decode(generated_ids[0], skip_special_tokens=True)
48
- return description
 
 
49
 
50
- def process_images(image_files, enhance, scale, generate_description):
 
 
 
 
 
 
 
 
51
  processed_images = []
52
- descriptions = []
53
- zip_buffer = io.BytesIO()
54
 
55
  for image_file in image_files:
56
- image = Image.open(image_file).convert('RGB')
 
57
 
58
  if enhance:
59
- image = enhance_image(image, scale)
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- if generate_description:
62
- description = describe_image(image)
63
- descriptions.append(description)
64
 
65
- # Save enhanced image to ZIP in-memory buffer
66
- buffer = io.BytesIO()
67
- image.save(buffer, format='JPEG')
68
- processed_images.append(Image.open(io.BytesIO(buffer.getvalue())))
69
- with zipfile.ZipFile(zip_buffer, 'a') as zipf:
70
- zipf.writestr(os.path.basename(image_file.name), buffer.getvalue())
71
 
72
- zip_buffer.seek(0)
73
- return processed_images, zip_buffer, descriptions
74
 
75
  iface = gr.Interface(
76
  fn=process_images,
77
  inputs=[
78
- gr.Files(label="Upload Image Files"),
79
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
80
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
81
- gr.Checkbox(label="Generate Image Descriptions")
 
 
 
 
82
  ],
83
  outputs=[
84
- gr.Gallery(label="Enhanced Images"),
85
- gr.File(label="Download Enhanced Images (ZIP)"),
86
- gr.Textbox(label="Generated Descriptions", lines=5)
87
  ],
88
- title="Image Enhancer with Description Generator",
89
- description="Upload multiple images, enhance using AI, generate descriptions using Hugging Face, and download results as a ZIP file."
90
  )
91
 
92
- iface.launch()
 
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
+ import os
9
 
 
10
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
 
 
12
  def load_model(scale):
13
  model = RealESRGAN(device, scale=scale)
14
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
 
17
  print(f"Weights for scale {scale} loaded successfully.")
18
  except Exception as e:
19
  print(f"Error loading weights for scale {scale}: {e}")
20
+ model.load_weights(weights_path, download=False)
21
  return model
22
 
23
  model2 = load_model(2)
24
  model4 = load_model(4)
25
  model8 = load_model(8)
26
 
 
 
 
 
27
  def enhance_image(image, scale):
28
  try:
29
+ print(f"Enhancing image with scale {scale}...")
30
+ start_time = time.time()
31
  image_np = np.array(image.convert('RGB'))
32
+ print(f"Image converted to numpy array: shape {image_np.shape}, dtype {image_np.dtype}")
33
+
34
+ if scale == '2x':
35
+ result = model2.predict(image_np)
36
+ elif scale == '4x':
37
+ result = model4.predict(image_np)
38
+ else:
39
+ result = model8.predict(image_np)
40
+
41
+ enhanced_image = Image.fromarray(np.uint8(result))
42
+ print(f"Image enhanced in {time.time() - start_time:.2f} seconds")
43
+ return enhanced_image
44
  except Exception as e:
45
  print(f"Error enhancing image: {e}")
46
  return image
47
 
48
+ def muda_dpi(input_image, dpi):
49
+ dpi_tuple = (dpi, dpi)
50
+ image = Image.fromarray(input_image.astype('uint8'), 'RGB')
51
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
52
+ image.save(temp_file, format='JPEG', dpi=dpi_tuple)
53
+ temp_file.close()
54
+ return Image.open(temp_file.name)
55
 
56
+ def resize_image(input_image, width, height):
57
+ image = Image.fromarray(input_image.astype('uint8'), 'RGB')
58
+ resized_image = image.resize((width, height))
59
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
60
+ resized_image.save(temp_file, format='JPEG')
61
+ temp_file.close()
62
+ return Image.open(temp_file.name)
63
+
64
+ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
65
  processed_images = []
66
+ file_paths = []
 
67
 
68
  for image_file in image_files:
69
+ input_image = np.array(Image.open(image_file).convert('RGB'))
70
+ original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
71
 
72
  if enhance:
73
+ original_image = enhance_image(original_image, scale)
74
+
75
+ if adjust_dpi:
76
+ original_image = muda_dpi(np.array(original_image), dpi)
77
+
78
+ if resize:
79
+ original_image = resize_image(np.array(original_image), width, height)
80
+
81
+ # Sanitize the base filename
82
+ base_name = os.path.basename(image_file.name)
83
+ file_name, _ = os.path.splitext(base_name)
84
+
85
+ # Remove any characters that aren't alphanumeric, spaces, underscores, or hyphens
86
+ file_name = ''.join(e for e in file_name if e.isalnum() or e in (' ', '_', '-')).strip().replace(' ', '_')
87
 
88
+ # Create a final file path without unnecessary suffixes
89
+ output_path = os.path.join(tempfile.gettempdir(), f"{file_name}.jpg")
90
+ original_image.save(output_path, format='JPEG')
91
 
92
+ processed_images.append(original_image)
93
+ file_paths.append(output_path)
 
 
 
 
94
 
95
+ return processed_images, file_paths
 
96
 
97
  iface = gr.Interface(
98
  fn=process_images,
99
  inputs=[
100
+ gr.Files(label="Upload Image Files"), # Use gr.Files for multiple file uploads
101
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
102
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
103
+ gr.Checkbox(label="Adjust DPI"),
104
+ gr.Number(label="DPI", value=300),
105
+ gr.Checkbox(label="Resize"),
106
+ gr.Number(label="Width", value=512),
107
+ gr.Number(label="Height", value=512)
108
  ],
109
  outputs=[
110
+ gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
111
+ gr.Files(label="Download Final Images")
 
112
  ],
113
+ title="Multi-Image Enhancer",
114
+ description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results."
115
  )
116
 
117
+ iface.launch(debug=True)