-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_2.py
67 lines (49 loc) · 1.6 KB
/
test_2.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
import numpy as np
from PIL import Image
from keras.models import load_model
from flask import Flask, jsonify, request
from io import BytesIO
import io
generator = load_model('gen25000v2.4.h5')
# generator.summary()
def restructure_img(file):
try:
# Read the image via file.stream
img = Image.open(BytesIO(file.read())).convert('L')
# Resize the image
img = img.resize((64, 64), Image.Resampling.LANCZOS)
# Convert image to RGB
img = img.convert('RGB')
# Normalize the image
img_lr = np.asarray(img) / 255.0
img_lr = np.expand_dims(img_lr, axis=0)
return img_lr
except Exception as e:
raise e
def predict(img_lr):
gen_img = generator.predict(img_lr)
# print(gen_img.shape)
# Convert the output to image (if needed, depends on your generator's output)
# gen_img = Image.fromarray((gen_img[0] * 255).astype(np.uint8))
# gen_img.show()
gen_img_list = gen_img.tolist()
# Return as a JSON response
return gen_img_list
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
file = request.files.get('file')
if file is None or file.filename == '':
return jsonify({'error': 'no_file'})
try:
img_lr = restructure_img(file)
prediction = predict(img_lr)
return jsonify({
'Prediction Tensor': prediction
})
except Exception as e:
return jsonify({'error': str(e)})
return 'OK'
if __name__ == '__main__':
app.run(debug=True, port=8080)