|
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 |
|
|
|
|
|
myapp = Flask(__name__) |
|
CORS(myapp) |
|
|
|
@myapp.route('/') |
|
def home(): |
|
return "Welcome to the Image Denoiser!" |
|
|
|
@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() |
|
img = Image.open(io.BytesIO(input_image)) |
|
|
|
|
|
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) |
|
|
|
|
|
denoised_img = cv2.fastNlMeansDenoising(img_cv, None, h=10, templateWindowSize=7, searchWindowSize=21) |
|
|
|
|
|
denoised_img_pil = Image.fromarray(cv2.cvtColor(denoised_img, cv2.COLOR_BGR2RGB)) |
|
|
|
|
|
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') |
|
|
|
|
|
if __name__ == "__main__": |
|
myapp.run(host='0.0.0.0', port=7860) |