abiabidali commited on
Commit
0abd017
·
verified ·
1 Parent(s): f899183

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -26
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import torch
2
- from transformers import BlipProcessor, BlipForConditionalGeneration
3
  from PIL import Image
4
  from RealESRGAN import RealESRGAN
5
  import gradio as gr
@@ -28,10 +27,6 @@ model2 = load_model(2)
28
  model4 = load_model(4)
29
  model8 = load_model(8)
30
 
31
- # Initialize BLIP processor and model for image captioning
32
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
33
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
34
-
35
  # Enhance the image using the specified scale
36
  def enhance_image(image, scale):
37
  try:
@@ -54,13 +49,6 @@ def enhance_image(image, scale):
54
  print(f"Error enhancing image: {e}")
55
  return image
56
 
57
- # Generate captions for the images using BLIP
58
- def generate_caption(image):
59
- inputs = processor(images=image, return_tensors="pt").to(device)
60
- output_ids = model.generate(**inputs)
61
- caption = processor.decode(output_ids[0], skip_special_tokens=True)
62
- return caption
63
-
64
  # Adjust the DPI of the image
65
  def muda_dpi(input_image, dpi):
66
  dpi_tuple = (dpi, dpi)
@@ -79,11 +67,10 @@ def resize_image(input_image, width, height):
79
  temp_file.close()
80
  return Image.open(temp_file.name)
81
 
82
- # Process the images: enhance, adjust DPI, resize, caption, and save
83
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
84
  processed_images = []
85
  file_paths = []
86
- captions = []
87
 
88
  for i, image_file in enumerate(image_files):
89
  input_image = np.array(Image.open(image_file).convert('RGB'))
@@ -98,10 +85,6 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
98
  if resize:
99
  original_image = resize_image(np.array(original_image), width, height)
100
 
101
- # Generate a caption for the image
102
- caption = generate_caption(original_image)
103
- captions.append(caption)
104
-
105
  # Create a custom filename
106
  custom_filename = f"Image_Captioning_with_BLIP_{i+1}.jpg"
107
 
@@ -116,13 +99,13 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
116
  processed_images.append(original_image)
117
  file_paths.append(final_path)
118
 
119
- return processed_images, file_paths, captions
120
 
121
  # Gradio interface setup
122
  iface = gr.Interface(
123
  fn=process_images,
124
  inputs=[
125
- gr.Files(label="Upload Image Files"),
126
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
127
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
128
  gr.Checkbox(label="Adjust DPI"),
@@ -132,15 +115,13 @@ iface = gr.Interface(
132
  gr.Number(label="Height", value=512)
133
  ],
134
  outputs=[
135
- gr.Gallery(label="Final Images"),
136
- gr.Files(label="Download Final Images"),
137
- gr.Textbox(label="Image Captions")
138
  ],
139
- title="Multi-Image Enhancer with Captioning",
140
- description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, generate captions, and download the final results."
141
  )
142
 
143
  # Launch the Gradio interface
144
  iface.launch(debug=True)
145
 
146
-
 
1
  import torch
 
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
4
  import gradio as gr
 
27
  model4 = load_model(4)
28
  model8 = load_model(8)
29
 
 
 
 
 
30
  # Enhance the image using the specified scale
31
  def enhance_image(image, scale):
32
  try:
 
49
  print(f"Error enhancing image: {e}")
50
  return image
51
 
 
 
 
 
 
 
 
52
  # Adjust the DPI of the image
53
  def muda_dpi(input_image, dpi):
54
  dpi_tuple = (dpi, dpi)
 
67
  temp_file.close()
68
  return Image.open(temp_file.name)
69
 
70
+ # Process the images: enhance, adjust DPI, resize, and save
71
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
72
  processed_images = []
73
  file_paths = []
 
74
 
75
  for i, image_file in enumerate(image_files):
76
  input_image = np.array(Image.open(image_file).convert('RGB'))
 
85
  if resize:
86
  original_image = resize_image(np.array(original_image), width, height)
87
 
 
 
 
 
88
  # Create a custom filename
89
  custom_filename = f"Image_Captioning_with_BLIP_{i+1}.jpg"
90
 
 
99
  processed_images.append(original_image)
100
  file_paths.append(final_path)
101
 
102
+ return processed_images, file_paths
103
 
104
  # Gradio interface setup
105
  iface = gr.Interface(
106
  fn=process_images,
107
  inputs=[
108
+ gr.Files(label="Upload Image Files"), # Use gr.Files for multiple file uploads
109
  gr.Checkbox(label="Enhance Images (ESRGAN)"),
110
  gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model'),
111
  gr.Checkbox(label="Adjust DPI"),
 
115
  gr.Number(label="Height", value=512)
116
  ],
117
  outputs=[
118
+ gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
119
+ gr.Files(label="Download Final Images")
 
120
  ],
121
+ title="Multi-Image Enhancer",
122
+ description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results."
123
  )
124
 
125
  # Launch the Gradio interface
126
  iface.launch(debug=True)
127