-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (58 loc) · 1.88 KB
/
app.py
File metadata and controls
79 lines (58 loc) · 1.88 KB
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
import tensorflow as tf
import numpy as np
from flask import Flask, render_template, request
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import tempfile
import base64
import io
from utils import *
def normalize(input_image):
input_image = tf.cast(input_image, tf.float32) / 255.0
return input_image
configureGPU()
model = tf.keras.models.load_model('./models/model.h5')
BATCH_SIZE = 1
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
SEED = 123
app = Flask(__name__)
@app.route('/',methods=['GET'])
def index_page():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
# Create temp dir for input image
temp_dir = tempfile.TemporaryDirectory()
# Get the input image
img_data = request.files['img']
img_byte_string = img_data.read()
img_array = np.frombuffer(img_byte_string, dtype = np.uint8)
# arr = cv2.imdecode(img_array, cv2.IMREAD_COLOR)[:,:,::-1]
arr = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
# Save it to a file
cv2.imwrite(temp_dir.name + "/image.jpg", arr)
# Get the prediction
DATA_PATH = temp_dir.name
ds = getDataset(DATA_PATH, BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, SEED)
ds = ds.map(normalize)
fig = predict(model, ds)
# Convert plot to image
img_buf = io.BytesIO()
fig.savefig(img_buf, format = 'JPEG')
img = Image.open(img_buf)
# Save the image to buffer
mem_bytes = io.BytesIO()
img.save(mem_bytes, 'JPEG')
# Process it to display
mem_bytes.seek(0)
img_base64 = base64.b64encode(mem_bytes.getvalue()).decode('ascii')
mime = "image/jpeg"
uri = "data:%s;base64,%s"%(mime, img_base64)
img_buf.close()
mem_bytes.close()
temp_dir.cleanup()
return render_template("index.html", image = uri)
if __name__ == '__main__':
app.run(host = "localhost", port = 8080)