-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlask Covid xray deploy.py
71 lines (56 loc) · 2.21 KB
/
Flask Covid xray deploy.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
from flask import Flask, request, jsonify, url_for, render_template
import uuid
import os
from tensorflow.keras.models import load_model
import numpy as np
from werkzeug import secure_filename
from PIL import Image, ImageFile
from io import BytesIO
from tensorflow.keras.preprocessing import image
import cv2
ALLOWED_EXTENSION =set(['txt', 'pdf', 'png','jpg','jpeg','gif'])
IMAGE_HEIGHT =256
IMAGE_WIDTH = 256
IMAGE_CHANNELS = 3
label_names = {0 : 'Covid-19 Positive', 1 : 'Healthy' , 2: 'Viral Pneumonia', 3 : 'Bacterial Pneumonia'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSION
app = Flask(__name__)
model = load_model("model.h5")
@app.route('/')
def index():
return render_template('ImageML.html')
@app.route('/api/image', methods=['POST'])
def upload_image():
if 'image' not in request.files:
return render_template('ImageML.html', prediction='No posted image. Should be attribute named image')
file = request.files['image']
if file.filename =='':
return render_template('ImageML.html', prediction = 'You did not select an image')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print("***"+filename)
x = []
ImageFile.LOAD_TRUNCATED_IMAGES = False
img = Image.open(BytesIO(file.read()))
img.load()
img = image.img_to_array(img)
if img.shape[2] == 3:
img = cv2.resize(img,(256,256))
img = img / 255
img = img.reshape(-1,256,256,3)
predict = model.predict(img)
predict = np.argmax(predict)
else:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
img = cv2.resize(img,(256,256))
img = img / 255
img = img.reshape(-1,256,256,3)
predict = model.predict(img)
predict = np.argmax(predict)
response = (label_names[predict])
return render_template('ImageML.html', prediction = '{}'.format(response))
else:
return render_template('ImageML.html', prediction = 'Invalid File extension')
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)