-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
103 lines (81 loc) · 3.02 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
94
95
96
97
98
99
100
101
102
103
# use fast api taking string as parameter from a get function and converting it to image and then to numpy array and then to a dataframe and then to a prediction and then to a json file and then to a string and then to a response
from flask import Flask, json, request
from tensorflow.keras.models import load_model
from flask_cors import CORS, cross_origin
import numpy as np
import pandas as pd
import cv2
import pickle
import base64
import os
from io import BytesIO
from PIL import Image
from typing import List
from pydantic import BaseModel
import tensorflow as tf
app = Flask(__name__)
cors = CORS(app)
cors = CORS(app, resource={
r"/*":{
"origins":"*"
}
})
app.config['CORS_HEADERS'] = 'Content-Type'
######################################################################
######################################################################
# json_path = './braintumormodel.json'
# weights_path = './braintumor.h5'
# if not os.path.exists(json_path):
# raise FileNotFoundError(f"JSON file not found: {json_path}")
# if not os.path.exists(weights_path):
# raise FileNotFoundError(f"H5 file not found: {weights_path}")
# # Load JSON and create model
# with open(json_path, 'r') as json_file:
# loaded_model_json = json_file.read()
# # Check if JSON content is not empty
# if not loaded_model_json:
# raise ValueError("The JSON file is empty or could not be read properly.")
# # Create the model from JSON
# loaded_model = model_from_json(loaded_model_json)
# # Load weights into the new model
# loaded_model.load_weights(weights_path)
# print("Model loaded successfully.")
####################################################################
weights_path = './braintumorfile.h5'
loaded_model = load_model(weights_path)
print("Model loaded successfully.")
######################################################################
def get_cv2_image_from_base64_string(b64str):
encoded_data = b64str.split(',')[1]
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return img
def get_image_from_base64_string(b64str):
encoded_data = b64str.split(',')[1]
image_data = BytesIO(base64.b64decode(encoded_data))
img = Image.open(image_data)
return img
@app.route('/',methods=['GET'])
def home():
return "Hello World"
labels = ['glioma_tumor','meningioma_tumor','no_tumor','pituitary_tumor']
@app.route("/predict", methods=['POST'])
def read_root():
data = request.get_json()
predict_img = []
for item in data['image']:
image = get_cv2_image_from_base64_string(item)
image = cv2.resize(image,(150,150))
predict_img.append(image)
img_array = np.array(predict_img)
img_array = img_array.reshape(1,150,150,3)
prediction = loaded_model.predict(img_array)
result = prediction.argmax()
# make the probablity frtom prediction
# print(prediction[:,1])
# print(result)
print(result)
print(labels[result])
return {"result":labels[result]}
if __name__ == '__main__':
app.run(port=5000)