Geek7 commited on
Commit
af2d09d
·
verified ·
1 Parent(s): 82f07de

Create myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +42 -0
myapp.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request, send_file
2
+ from flask_cors import CORS
3
+ from PIL import Image
4
+ import io
5
+ import numpy as np
6
+ import cv2
7
+
8
+ # Initialize the Flask app
9
+ myapp = Flask(__name__)
10
+ CORS(myapp) # Enable CORS if needed
11
+
12
+ @myapp.route('/')
13
+ def home():
14
+ return "Welcome to the Image Denoiser!" # Basic home response
15
+
16
+ @myapp.route('/denoise', methods=['POST'])
17
+ def denoise_image():
18
+ if 'image' not in request.files:
19
+ return jsonify({"error": "No image provided"}), 400
20
+
21
+ input_image = request.files['image'].read() # Read the uploaded image
22
+ img = Image.open(io.BytesIO(input_image))
23
+
24
+ # Convert to OpenCV format
25
+ img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
26
+
27
+ # Apply fastNlMeansDenoising
28
+ denoised_img = cv2.fastNlMeansDenoising(img_cv, None, h=10, templateWindowSize=7, searchWindowSize=21)
29
+
30
+ # Convert back to PIL format
31
+ denoised_img_pil = Image.fromarray(cv2.cvtColor(denoised_img, cv2.COLOR_BGR2RGB))
32
+
33
+ # Save to bytes
34
+ img_byte_arr = io.BytesIO()
35
+ denoised_img_pil.save(img_byte_arr, format='PNG')
36
+ img_byte_arr.seek(0)
37
+
38
+ return send_file(img_byte_arr, mimetype='image/png')
39
+
40
+ # Add this block to make sure your app runs when called
41
+ if __name__ == "__main__":
42
+ myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing