from flask import Flask, jsonify, request, send_file from flask_cors import CORS from PIL import Image import io import numpy as np import cv2 # Initialize the Flask app myapp = Flask(__name__) CORS(myapp) # Enable CORS if needed @myapp.route('/') def home(): return "Welcome to the Image Denoiser!" # Basic home response @myapp.route('/denoise', methods=['POST']) def denoise_image(): if 'image' not in request.files: return jsonify({"error": "No image provided"}), 400 input_image = request.files['image'].read() # Read the uploaded image img = Image.open(io.BytesIO(input_image)) # Convert to OpenCV format img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # Apply fastNlMeansDenoising denoised_img = cv2.fastNlMeansDenoising(img_cv, None, h=10, templateWindowSize=7, searchWindowSize=21) # Convert back to PIL format denoised_img_pil = Image.fromarray(cv2.cvtColor(denoised_img, cv2.COLOR_BGR2RGB)) # Save to bytes img_byte_arr = io.BytesIO() denoised_img_pil.save(img_byte_arr, format='PNG') img_byte_arr.seek(0) return send_file(img_byte_arr, mimetype='image/png') # Add this block to make sure your app runs when called if __name__ == "__main__": myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing