abiabidali commited on
Commit
873696c
·
verified ·
1 Parent(s): cd4c224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -55
app.py CHANGED
@@ -1,5 +1,3 @@
1
-
2
-
3
  import torch
4
  from PIL import Image
5
  from RealESRGAN import RealESRGAN
@@ -8,7 +6,9 @@ import numpy as np
8
  import tempfile
9
  import time
10
  import os
11
- from transformers import pipeline # For Hugging Face image description generation
 
 
12
 
13
  # Check for GPU availability
14
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
@@ -36,11 +36,7 @@ description_generator = pipeline("image-to-text", model="nlpconnect/vit-gpt2-ima
36
  # Enhance image based on selected scale
37
  def enhance_image(image, scale):
38
  try:
39
- print(f"Enhancing image with scale {scale}...")
40
- start_time = time.time()
41
  image_np = np.array(image.convert('RGB'))
42
- print(f"Image converted to numpy array: shape {image_np.shape}, dtype {image_np.dtype}")
43
-
44
  if scale == '2x':
45
  result = model2.predict(image_np)
46
  elif scale == '4x':
@@ -48,25 +44,21 @@ def enhance_image(image, scale):
48
  else:
49
  result = model8.predict(image_np)
50
 
51
- enhanced_image = Image.fromarray(np.uint8(result))
52
- print(f"Image enhanced in {time.time() - start_time:.2f} seconds")
53
- return enhanced_image
54
  except Exception as e:
55
  print(f"Error enhancing image: {e}")
56
  return image
57
 
58
- # Generate image description using Hugging Face Transformers
59
  def generate_description(image):
60
  try:
61
- print("Generating description for the image...")
62
  description = description_generator(image)[0]['generated_text']
63
- print(f"Description generated: {description}")
64
  return description
65
  except Exception as e:
66
  print(f"Error generating description: {e}")
67
  return "Description unavailable."
68
 
69
- # Adjust DPI of an image
70
  def muda_dpi(input_image, dpi):
71
  dpi_tuple = (dpi, dpi)
72
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
@@ -75,7 +67,7 @@ def muda_dpi(input_image, dpi):
75
  temp_file.close()
76
  return Image.open(temp_file.name)
77
 
78
- # Resize an image to specified dimensions
79
  def resize_image(input_image, width, height):
80
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
81
  resized_image = image.resize((width, height))
@@ -84,50 +76,67 @@ def resize_image(input_image, width, height):
84
  temp_file.close()
85
  return Image.open(temp_file.name)
86
 
87
- # Process a list of images with various options
88
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
89
  processed_images = []
90
  file_paths = []
91
- descriptions = [] # List to store descriptions
92
-
93
- for image_file in image_files:
94
- input_image = np.array(Image.open(image_file).convert('RGB'))
95
- original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
96
-
97
- if enhance:
98
- original_image = enhance_image(original_image, scale)
99
-
100
- if adjust_dpi:
101
- original_image = muda_dpi(np.array(original_image), dpi)
 
 
 
 
 
 
 
 
 
102
 
103
- if resize:
104
- original_image = resize_image(np.array(original_image), width, height)
105
-
106
- # Generate description
107
- description = generate_description(original_image)
108
- descriptions.append(description)
109
-
110
- # Sanitize the base filename
111
- base_name = os.path.basename(image_file.name)
112
- file_name, _ = os.path.splitext(base_name)
113
-
114
- # Remove any characters that aren't alphanumeric, spaces, underscores, or hyphens
115
- file_name = ''.join(e for e in file_name if e.isalnum() or e in (' ', '_', '-')).strip().replace(' ', '_')
116
-
117
- # Create a final file path without unnecessary suffixes
118
- output_path = os.path.join(tempfile.gettempdir(), f"{file_name}.jpg")
119
- original_image.save(output_path, format='JPEG')
120
-
121
- processed_images.append(original_image)
122
- file_paths.append(output_path)
123
-
124
- return processed_images, file_paths, descriptions
 
 
 
 
 
 
 
 
125
 
126
- # Set up Gradio interface with share=True for public access
127
  iface = gr.Interface(
128
  fn=process_images,
129
  inputs=[
130
- gr.Files(label="Upload Image Files"), # Use gr.Files for multiple file uploads
131
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
132
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
133
  gr.Checkbox(label="Adjust DPI"),
@@ -137,12 +146,12 @@ iface = gr.Interface(
137
  gr.Number(label="Height", value=512)
138
  ],
139
  outputs=[
140
- gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
141
- gr.Files(label="Download Final Images"),
142
- gr.Textbox(label="Image Descriptions", lines=5) # Display generated descriptions
143
  ],
144
  title="Multi-Image Enhancer with Hugging Face Descriptions",
145
- description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, generate descriptions, and download the final results."
146
  )
147
 
148
  iface.launch(debug=True, share=True)
 
 
 
1
  import torch
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
 
6
  import tempfile
7
  import time
8
  import os
9
+ from transformers import pipeline
10
+ import csv
11
+ import zipfile
12
 
13
  # Check for GPU availability
14
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
36
  # Enhance image based on selected scale
37
  def enhance_image(image, scale):
38
  try:
 
 
39
  image_np = np.array(image.convert('RGB'))
 
 
40
  if scale == '2x':
41
  result = model2.predict(image_np)
42
  elif scale == '4x':
 
44
  else:
45
  result = model8.predict(image_np)
46
 
47
+ return Image.fromarray(np.uint8(result))
 
 
48
  except Exception as e:
49
  print(f"Error enhancing image: {e}")
50
  return image
51
 
52
+ # Generate image description
53
  def generate_description(image):
54
  try:
 
55
  description = description_generator(image)[0]['generated_text']
 
56
  return description
57
  except Exception as e:
58
  print(f"Error generating description: {e}")
59
  return "Description unavailable."
60
 
61
+ # Adjust DPI
62
  def muda_dpi(input_image, dpi):
63
  dpi_tuple = (dpi, dpi)
64
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
 
67
  temp_file.close()
68
  return Image.open(temp_file.name)
69
 
70
+ # Resize an image
71
  def resize_image(input_image, width, height):
72
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
73
  resized_image = image.resize((width, height))
 
76
  temp_file.close()
77
  return Image.open(temp_file.name)
78
 
79
+ # Process images and generate a ZIP file with images and CSV
80
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
81
  processed_images = []
82
  file_paths = []
83
+ descriptions = []
84
+
85
+ # Temporary CSV file path
86
+ csv_file_path = os.path.join(tempfile.gettempdir(), "image_descriptions.csv")
87
+ with open(csv_file_path, mode="w", newline="") as csv_file:
88
+ writer = csv.writer(csv_file)
89
+ writer.writerow(["Filename", "Title", "Keywords"])
90
+
91
+ for image_file in image_files:
92
+ input_image = np.array(Image.open(image_file).convert('RGB'))
93
+ original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
94
+
95
+ if enhance:
96
+ original_image = enhance_image(original_image, scale)
97
+
98
+ if adjust_dpi:
99
+ original_image = muda_dpi(np.array(original_image), dpi)
100
+
101
+ if resize:
102
+ original_image = resize_image(np.array(original_image), width, height)
103
 
104
+ # Generate description
105
+ description = generate_description(original_image)
106
+ title = description # Using description as the title
107
+ keywords = ", ".join(set(description.split()))[:45] # Limit to 45 unique words
108
+
109
+ # Clean the filename
110
+ base_name = os.path.basename(image_file.name)
111
+ file_name, _ = os.path.splitext(base_name)
112
+ file_name = ''.join(e for e in file_name if e.isalnum() or e in (' ', '_', '-')).strip().replace(' ', '_')
113
+
114
+ # Final image path
115
+ output_path = os.path.join(tempfile.gettempdir(), f"{file_name}.jpg")
116
+ original_image.save(output_path, format='JPEG')
117
+
118
+ # Write to CSV
119
+ writer.writerow([file_name, title, keywords])
120
+
121
+ # Collect image paths and descriptions
122
+ processed_images.append(original_image)
123
+ file_paths.append(output_path)
124
+ descriptions.append(description)
125
+
126
+ # Create a ZIP file with all images and CSV
127
+ zip_file_path = os.path.join(tempfile.gettempdir(), "processed_images.zip")
128
+ with zipfile.ZipFile(zip_file_path, 'w') as zipf:
129
+ for file_path in file_paths:
130
+ zipf.write(file_path, arcname=os.path.basename(file_path))
131
+ zipf.write(csv_file_path, arcname="image_descriptions.csv")
132
+
133
+ return processed_images, zip_file_path, descriptions
134
 
135
+ # Gradio interface
136
  iface = gr.Interface(
137
  fn=process_images,
138
  inputs=[
139
+ gr.Files(label="Upload Image Files"),
140
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
141
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
142
  gr.Checkbox(label="Adjust DPI"),
 
146
  gr.Number(label="Height", value=512)
147
  ],
148
  outputs=[
149
+ gr.Gallery(label="Final Images"),
150
+ gr.File(label="Download ZIP of Images and Descriptions"),
151
+ gr.Textbox(label="Image Descriptions", lines=5)
152
  ],
153
  title="Multi-Image Enhancer with Hugging Face Descriptions",
154
+ description="Upload multiple images, enhance, adjust DPI, resize, generate descriptions, and download the results and a ZIP archive."
155
  )
156
 
157
  iface.launch(debug=True, share=True)