-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
93 lines (47 loc) · 1.89 KB
/
App.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, render_template, request, flash
from werkzeug.utils import secure_filename
import cv2
import numpy as np
import io
from PIL import Image
import base64
from Helpers import *
app = Flask(__name__)
app.secret_key = "secret key"
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_image():
images = []
for file in request.files.getlist("file[]"):
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filestr = file.read()
npimg = np.frombuffer(filestr, np.uint8)
image = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
ratio = image.shape[0] / 500.0
orig = image.copy()
image = Helpers.resize(image, height = 500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = cv2.Laplacian(gray, cv2.CV_64F).var()
result = "Not Blurry"
if fm < 100:
result = "Blurry"
sharpness_value = "{:.0f}".format(fm)
message = [result,sharpness_value]
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
file_object = io.BytesIO()
img= Image.fromarray(Helpers.resize(img,width=500))
img.save(file_object, 'PNG')
base64img = "data:image/png;base64,"+base64.b64encode(file_object.getvalue()).decode('ascii')
images.append([message,base64img])
return render_template('upload.html', images=images )
if __name__ == "__main__":
app.run(debug=True)