syedaoon commited on
Commit
eae91d6
Β·
verified Β·
1 Parent(s): 3b2db10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -128
app.py CHANGED
@@ -14,9 +14,10 @@ HTML_TEMPLATE = """
14
  <!DOCTYPE html>
15
  <html>
16
  <head>
17
- <title>ZeroIG Enhancement</title>
18
  <style>
19
  body { font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; }
 
20
  .container { text-align: center; }
21
  .upload-area { border: 2px dashed #ccc; padding: 40px; margin: 20px 0; border-radius: 10px; }
22
  .result { margin-top: 20px; }
@@ -29,14 +30,23 @@ HTML_TEMPLATE = """
29
  </head>
30
  <body>
31
  <div class="container">
32
- <h1>🌟 ZeroIG: Zero-Shot Low-Light Enhancement</h1>
33
- <p><strong>CVPR 2024</strong> - Upload a low-light image for professional enhancement!</p>
 
 
 
 
 
 
 
 
 
34
 
35
  <form method="post" enctype="multipart/form-data">
36
  <div class="upload-area">
37
  <input type="file" name="image" accept="image/*" required>
38
  <br><br>
39
- <button type="submit" style="padding: 10px 20px; font-size: 16px;">πŸš€ Enhance with ZeroIG</button>
40
  </div>
41
  </form>
42
 
@@ -53,136 +63,145 @@ HTML_TEMPLATE = """
53
  <h3>Results:</h3>
54
  <div class="comparison">
55
  <div class="image-container">
56
- <h4>Original (Low-light)</h4>
57
  <img src="data:image/png;base64,{{ original_image }}" alt="Original">
58
  </div>
59
  <div class="image-container">
60
- <h4>ZeroIG Enhanced</h4>
61
  <img src="data:image/png;base64,{{ result_image }}" alt="Enhanced">
62
- <br><br>
63
- <a href="data:image/png;base64,{{ result_image }}" download="zeroig_enhanced.png"
64
- style="background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
65
- πŸ“₯ Download Enhanced Image
66
- </a>
67
  </div>
68
  </div>
69
  </div>
70
  {% endif %}
71
-
72
- <div style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-radius: 10px;">
73
- <h3>About ZeroIG</h3>
74
- <p>Zero-shot illumination-guided joint denoising and adaptive enhancement for low-light images.</p>
75
- <p><strong>Features:</strong> No training data required β€’ Joint denoising & enhancement β€’ Prevents over-enhancement</p>
76
- <p>
77
- πŸ“„ <a href="https://openaccess.thecvf.com/content/CVPR2024/papers/Shi_ZERO-IG_Zero-Shot_Illumination-Guided_Joint_Denoising_and_Adaptive_Enhancement_for_Low-Light_CVPR_2024_paper.pdf">Research Paper</a> |
78
- πŸ’» <a href="https://github.com/Doyle59217/ZeroIG">Source Code</a>
79
- </p>
80
- </div>
81
  </div>
82
  </body>
83
  </html>
84
  """
85
 
86
- class ZeroIGProcessor:
87
- def __init__(self):
88
- self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
89
- self.model = self.load_model()
90
- print(f"ZeroIG initialized on {self.device}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- def load_model(self):
 
 
 
 
 
 
93
  try:
94
- # Import your uploaded ZeroIG files
95
- from model import Finetunemodel, Network
96
-
97
- # Try to load trained weights
98
- model_path = "./weights/model.pt"
99
- if os.path.exists(model_path):
100
- print(f"Found model weights at {model_path}")
101
- model = Finetunemodel(model_path)
102
- print("βœ… Loaded ZeroIG Finetunemodel with trained weights")
103
- else:
104
- print("No trained weights found, using Network with random initialization")
105
- model = Network()
106
- print("⚠️ Using ZeroIG Network with random weights")
107
 
108
- model.to(self.device)
109
- model.eval()
110
- print(f"Model moved to {self.device}")
111
- return model
112
 
113
- except ImportError as e:
114
- print(f"❌ Could not import ZeroIG modules: {e}")
115
- print("Make sure you have uploaded: model.py, loss.py, utils.py")
116
- return None
 
 
 
 
 
 
 
117
  except Exception as e:
118
- print(f"❌ Could not load ZeroIG model: {e}")
119
- return None
120
-
121
- def enhance_image(self, image):
122
- """Enhance image using your ZeroIG model"""
123
  try:
124
- if self.model is None:
125
- return self.simple_enhance(image), "❌ ZeroIG model not available - using simple enhancement"
126
-
127
- # Resize if too large to prevent memory issues
128
- original_size = image.size
129
- max_size = 800 # Adjust based on your needs
130
- if max(image.size) > max_size:
131
- ratio = max_size / max(image.size)
132
- new_size = tuple(int(dim * ratio) for dim in image.size)
133
- image = image.resize(new_size, Image.Resampling.LANCZOS)
134
- print(f"Resized image from {original_size} to {image.size}")
135
-
136
- # Convert to tensor
137
- transform = transforms.ToTensor()
138
- input_tensor = transform(image).unsqueeze(0).to(self.device)
139
- print(f"Input tensor shape: {input_tensor.shape}")
140
-
141
- # Run your ZeroIG model
142
- with torch.no_grad():
143
- if hasattr(self.model, 'enhance') and hasattr(self.model, 'denoise_1'):
144
- # Finetunemodel - returns (enhanced, denoised)
145
- enhanced, denoised = self.model(input_tensor)
146
- result_tensor = denoised # Use denoised output
147
- status = "βœ… Enhanced with ZeroIG Finetunemodel"
148
- print("Used Finetunemodel")
149
- else:
150
- # Network model - returns multiple outputs
151
- outputs = self.model(input_tensor)
152
- result_tensor = outputs[13] # H3 is the final denoised result
153
- status = "βœ… Enhanced with ZeroIG Network model"
154
- print("Used Network model")
155
-
156
- # Convert back to PIL
157
- result_tensor = result_tensor.squeeze(0).cpu().clamp(0, 1)
158
- enhanced_image = transforms.ToPILImage()(result_tensor)
159
- print(f"Output image size: {enhanced_image.size}")
160
-
161
- # Resize back to original size if needed
162
- if enhanced_image.size != original_size and original_size != image.size:
163
- enhanced_image = enhanced_image.resize(original_size, Image.Resampling.LANCZOS)
164
- print(f"Resized back to original size: {enhanced_image.size}")
165
-
166
- return enhanced_image, status
167
 
 
 
 
168
  except Exception as e:
169
- print(f"ZeroIG enhancement error: {e}")
170
- import traceback
171
- traceback.print_exc()
172
- return self.simple_enhance(image), f"⚠️ ZeroIG failed, using simple enhancement: {str(e)}"
173
 
174
- def simple_enhance(self, image):
175
- """Fallback simple enhancement"""
176
- arr = np.array(image).astype(np.float32)
177
- enhanced = np.clip(arr * 1.8, 0, 255).astype(np.uint8)
178
- return Image.fromarray(enhanced)
179
 
180
- # Initialize ZeroIG processor
181
- print("πŸš€ Loading ZeroIG processor...")
182
- zeroig = ZeroIGProcessor()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  def image_to_base64(image):
185
- """Convert PIL image to base64 string"""
186
  img_buffer = io.BytesIO()
187
  image.save(img_buffer, format='PNG')
188
  img_str = base64.b64encode(img_buffer.getvalue()).decode()
@@ -199,34 +218,56 @@ def index():
199
  try:
200
  file = request.files['image']
201
  if file:
202
- print(f"Processing uploaded image: {file.filename}")
203
 
204
- # Open and process image
205
  image = Image.open(file.stream).convert('RGB')
206
- print(f"Image size: {image.size}")
207
-
208
- # Store original for comparison
209
  original_image = image_to_base64(image)
210
 
211
- # Enhance with your ZeroIG model
212
- enhanced_image, enhancement_status = zeroig.enhance_image(image)
213
-
214
- # Convert result to base64
215
- result_image = image_to_base64(enhanced_image)
216
- status = enhancement_status
217
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  except Exception as e:
219
- error = f"Error processing image: {str(e)}"
220
  print(f"Error: {e}")
221
- import traceback
222
- traceback.print_exc()
223
 
224
- return render_template_string(HTML_TEMPLATE,
 
 
225
  original_image=original_image,
226
- result_image=result_image,
227
- status=status,
228
  error=error)
229
 
230
  if __name__ == '__main__':
231
- print("πŸš€ Starting ZeroIG Flask app...")
232
  app.run(host='0.0.0.0', port=7860)
 
14
  <!DOCTYPE html>
15
  <html>
16
  <head>
17
+ <title>ZeroIG Enhancement - Debug</title>
18
  <style>
19
  body { font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; }
20
+ .debug { background: #f0f0f0; padding: 15px; margin: 10px 0; border-radius: 5px; font-family: monospace; }
21
  .container { text-align: center; }
22
  .upload-area { border: 2px dashed #ccc; padding: 40px; margin: 20px 0; border-radius: 10px; }
23
  .result { margin-top: 20px; }
 
30
  </head>
31
  <body>
32
  <div class="container">
33
+ <h1>πŸ”§ ZeroIG Debug Mode</h1>
34
+
35
+ <div class="debug">
36
+ <h3>πŸ“ File Check:</h3>
37
+ {{ file_status }}
38
+ </div>
39
+
40
+ <div class="debug">
41
+ <h3>🐍 Import Status:</h3>
42
+ {{ import_status }}
43
+ </div>
44
 
45
  <form method="post" enctype="multipart/form-data">
46
  <div class="upload-area">
47
  <input type="file" name="image" accept="image/*" required>
48
  <br><br>
49
+ <button type="submit" style="padding: 10px 20px; font-size: 16px;">πŸ§ͺ Test Enhancement</button>
50
  </div>
51
  </form>
52
 
 
63
  <h3>Results:</h3>
64
  <div class="comparison">
65
  <div class="image-container">
66
+ <h4>Original</h4>
67
  <img src="data:image/png;base64,{{ original_image }}" alt="Original">
68
  </div>
69
  <div class="image-container">
70
+ <h4>Enhanced</h4>
71
  <img src="data:image/png;base64,{{ result_image }}" alt="Enhanced">
 
 
 
 
 
72
  </div>
73
  </div>
74
  </div>
75
  {% endif %}
 
 
 
 
 
 
 
 
 
 
76
  </div>
77
  </body>
78
  </html>
79
  """
80
 
81
+ def check_files():
82
+ """Check what files are available"""
83
+ status = []
84
+
85
+ # Check current directory files
86
+ files_in_dir = os.listdir('.')
87
+ status.append(f"Files in current directory: {files_in_dir}")
88
+
89
+ # Check specific ZeroIG files
90
+ required_files = ['model.py', 'loss.py', 'utils.py']
91
+ for file in required_files:
92
+ if os.path.exists(file):
93
+ status.append(f"βœ… {file} - EXISTS")
94
+ # Check file size
95
+ size = os.path.getsize(file)
96
+ status.append(f" Size: {size} bytes")
97
+ else:
98
+ status.append(f"❌ {file} - MISSING")
99
+
100
+ # Check weights directory
101
+ if os.path.exists('weights'):
102
+ weights_files = os.listdir('weights')
103
+ status.append(f"πŸ“ weights/ directory: {weights_files}")
104
+ else:
105
+ status.append("πŸ“ weights/ directory: NOT FOUND")
106
+
107
+ return "<br>".join(status)
108
+
109
+ def check_imports():
110
+ """Try importing ZeroIG modules and report results"""
111
+ status = []
112
 
113
+ try:
114
+ # Add current directory to path
115
+ import sys
116
+ sys.path.insert(0, '.')
117
+ status.append("βœ… Added current directory to Python path")
118
+
119
+ # Try importing each module
120
  try:
121
+ import model
122
+ status.append("βœ… Successfully imported 'model' module")
 
 
 
 
 
 
 
 
 
 
 
123
 
124
+ # Check what's in the model module
125
+ model_contents = dir(model)
126
+ status.append(f" model module contents: {model_contents}")
 
127
 
128
+ # Try to access specific classes
129
+ if hasattr(model, 'Network'):
130
+ status.append("βœ… Found Network class")
131
+ else:
132
+ status.append("❌ Network class not found")
133
+
134
+ if hasattr(model, 'Finetunemodel'):
135
+ status.append("βœ… Found Finetunemodel class")
136
+ else:
137
+ status.append("❌ Finetunemodel class not found")
138
+
139
  except Exception as e:
140
+ status.append(f"❌ Failed to import model: {e}")
141
+
 
 
 
142
  try:
143
+ import loss
144
+ status.append("βœ… Successfully imported 'loss' module")
145
+ except Exception as e:
146
+ status.append(f"❌ Failed to import loss: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
+ try:
149
+ import utils
150
+ status.append("βœ… Successfully imported 'utils' module")
151
  except Exception as e:
152
+ status.append(f"❌ Failed to import utils: {e}")
153
+
154
+ except Exception as e:
155
+ status.append(f"❌ General import error: {e}")
156
 
157
+ return "<br>".join(status)
 
 
 
 
158
 
159
+ def try_create_model():
160
+ """Try to create ZeroIG model and report what happens"""
161
+ try:
162
+ sys.path.insert(0, '.')
163
+
164
+ from model import Network, Finetunemodel
165
+
166
+ # Try creating Network
167
+ network = Network()
168
+ print("βœ… Successfully created Network model")
169
+
170
+ # Try creating Finetunemodel (this will fail without weights, but should show the error)
171
+ try:
172
+ finetuned = Finetunemodel("./weights/model.pt")
173
+ print("βœ… Successfully created Finetunemodel")
174
+ return finetuned, "Finetunemodel"
175
+ except Exception as e:
176
+ print(f"⚠️ Finetunemodel failed (expected without weights): {e}")
177
+ print("βœ… Using Network model instead")
178
+ return network, "Network"
179
+
180
+ except Exception as e:
181
+ print(f"❌ Model creation failed: {e}")
182
+ import traceback
183
+ traceback.print_exc()
184
+ return None, f"Failed: {e}"
185
+
186
+ # Try to load model on startup
187
+ print("πŸš€ Starting ZeroIG debug app...")
188
+ print("πŸ“ Checking files...")
189
+ file_check = check_files()
190
+ print("🐍 Checking imports...")
191
+ import_check = check_imports()
192
+
193
+ # Try to create model
194
+ print("πŸ€– Trying to create model...")
195
+ model, model_status = try_create_model()
196
+
197
+ def simple_enhance(image):
198
+ """Fallback enhancement"""
199
+ arr = np.array(image).astype(np.float32)
200
+ enhanced = np.clip(arr * 1.5, 0, 255).astype(np.uint8)
201
+ return Image.fromarray(enhanced)
202
 
203
  def image_to_base64(image):
204
+ """Convert PIL image to base64"""
205
  img_buffer = io.BytesIO()
206
  image.save(img_buffer, format='PNG')
207
  img_str = base64.b64encode(img_buffer.getvalue()).decode()
 
218
  try:
219
  file = request.files['image']
220
  if file:
221
+ print(f"Processing: {file.filename}")
222
 
 
223
  image = Image.open(file.stream).convert('RGB')
 
 
 
224
  original_image = image_to_base64(image)
225
 
226
+ if model is not None:
227
+ # Try using the actual model
228
+ try:
229
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
230
+ model.to(device)
231
+ model.eval()
232
+
233
+ transform = transforms.ToTensor()
234
+ input_tensor = transform(image).unsqueeze(0).to(device)
235
+
236
+ with torch.no_grad():
237
+ if hasattr(model, 'enhance') and hasattr(model, 'denoise_1'):
238
+ enhanced, denoised = model(input_tensor)
239
+ result_tensor = denoised
240
+ else:
241
+ outputs = model(input_tensor)
242
+ result_tensor = outputs[13]
243
+
244
+ result_tensor = result_tensor.squeeze(0).cpu().clamp(0, 1)
245
+ enhanced_image = transforms.ToPILImage()(result_tensor)
246
+ result_image = image_to_base64(enhanced_image)
247
+ status = f"βœ… Used {model_status} model successfully!"
248
+
249
+ except Exception as e:
250
+ print(f"Model processing error: {e}")
251
+ enhanced_image = simple_enhance(image)
252
+ result_image = image_to_base64(enhanced_image)
253
+ status = f"⚠️ Model failed, used simple enhancement: {e}"
254
+ else:
255
+ enhanced_image = simple_enhance(image)
256
+ result_image = image_to_base64(enhanced_image)
257
+ status = "⚠️ No model available, used simple enhancement"
258
+
259
  except Exception as e:
260
+ error = f"Error: {e}"
261
  print(f"Error: {e}")
 
 
262
 
263
+ return render_template_string(HTML_TEMPLATE,
264
+ file_status=file_check,
265
+ import_status=import_check,
266
  original_image=original_image,
267
+ result_image=result_image,
268
+ status=status,
269
  error=error)
270
 
271
  if __name__ == '__main__':
272
+ print("πŸš€ Debug app ready!")
273
  app.run(host='0.0.0.0', port=7860)