Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,8 @@ from flask import Flask, request, send_file, jsonify
|
|
5 |
from rembg import remove
|
6 |
from PIL import Image, ImageEnhance, ImageFilter
|
7 |
import io
|
|
|
|
|
8 |
|
9 |
# --- Create the Flask App ---
|
10 |
app = Flask(__name__)
|
@@ -173,6 +175,70 @@ def enhance_image_api():
|
|
173 |
|
174 |
return jsonify({"error": "An unknown error occurred"}), 500
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
# --- Run the App ---
|
177 |
if __name__ == '__main__':
|
178 |
# For local testing, ensure the environment variable is set.
|
|
|
5 |
from rembg import remove
|
6 |
from PIL import Image, ImageEnhance, ImageFilter
|
7 |
import io
|
8 |
+
import cv2
|
9 |
+
import numpy as np
|
10 |
|
11 |
# --- Create the Flask App ---
|
12 |
app = Flask(__name__)
|
|
|
175 |
|
176 |
return jsonify({"error": "An unknown error occurred"}), 500
|
177 |
|
178 |
+
# --- POWERFUL AI IMAGE ENHANCER API ENDPOINT ---
|
179 |
+
|
180 |
+
@app.route('/auto-enhance-image', methods=['POST'])
|
181 |
+
def auto_enhance_image_api():
|
182 |
+
# 1. --- API Key Authentication ---
|
183 |
+
api_key_header = request.headers.get('x-api-key')
|
184 |
+
if not api_key_header or api_key_header != API_KEY:
|
185 |
+
return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
|
186 |
+
|
187 |
+
# 2. --- File Check ---
|
188 |
+
if 'file' not in request.files:
|
189 |
+
return jsonify({"error": "No file part in the request"}), 400
|
190 |
+
|
191 |
+
file = request.files['file']
|
192 |
+
if file.filename == '':
|
193 |
+
return jsonify({"error": "No selected file"}), 400
|
194 |
+
|
195 |
+
# 3. --- Professional Image Enhancement with OpenCV ---
|
196 |
+
if file:
|
197 |
+
try:
|
198 |
+
# Image ko bytes se parh kar ek NumPy array mein convert karna
|
199 |
+
filestr = file.read()
|
200 |
+
npimg = np.frombuffer(filestr, np.uint8)
|
201 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
|
202 |
+
|
203 |
+
# --- Enhancement Step 1: CLAHE for Perfect Contrast ---
|
204 |
+
# Image ko LAB color space mein convert karna
|
205 |
+
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
206 |
+
l, a, b = cv2.split(lab)
|
207 |
+
# Lightness (L) channel per CLAHE apply karna
|
208 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
209 |
+
cl = clahe.apply(l)
|
210 |
+
# Channels ko wapas milana
|
211 |
+
limg = cv2.merge((cl, a, b))
|
212 |
+
# Image ko wapas BGR color space mein convert karna
|
213 |
+
img_clahe = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
|
214 |
+
|
215 |
+
# --- Enhancement Step 2: Unsharp Masking for Natural Sharpness ---
|
216 |
+
# Image ko thoda blur karna
|
217 |
+
gaussian_blur = cv2.GaussianBlur(img_clahe, (0, 0), 3.0)
|
218 |
+
# Original image mein sharpness add karna
|
219 |
+
# Is se details bohot behtar ho jaati hain
|
220 |
+
img_enhanced = cv2.addWeighted(img_clahe, 1.5, gaussian_blur, -0.5, 0)
|
221 |
+
|
222 |
+
# --- Final Result ---
|
223 |
+
# Processed image (NumPy array) ko wapas PNG bytes mein convert karna
|
224 |
+
is_success, buffer = cv2.imencode(".png", img_enhanced)
|
225 |
+
if not is_success:
|
226 |
+
raise Exception("Could not encode enhanced image.")
|
227 |
+
|
228 |
+
output_image_bytes = buffer.tobytes()
|
229 |
+
|
230 |
+
# 4. --- Send the Response ---
|
231 |
+
return send_file(
|
232 |
+
io.BytesIO(output_image_bytes),
|
233 |
+
mimetype='image/png',
|
234 |
+
as_attachment=True,
|
235 |
+
download_name='auto_enhanced_image.png'
|
236 |
+
)
|
237 |
+
except Exception as e:
|
238 |
+
return jsonify({"error": "Failed to enhance image", "details": str(e)}), 500
|
239 |
+
|
240 |
+
return jsonify({"error": "An unknown error occurred"}), 500
|
241 |
+
|
242 |
# --- Run the App ---
|
243 |
if __name__ == '__main__':
|
244 |
# For local testing, ensure the environment variable is set.
|