forked from kay-cottage/API_CNN_Cell_Identify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_Server.py
More file actions
94 lines (70 loc) · 2.53 KB
/
API_Server.py
File metadata and controls
94 lines (70 loc) · 2.53 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# coding:utf-8
import time
from tensorflow.keras.models import load_model
import matplotlib.image as processimage
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from flask import Flask
import os
import json
from flask import request
app = Flask(__name__)
app.config['DEBUG'] = True
class Prediction(object):
def __init__(self, ModelFile, PredictFile, CellType, Width=100, Height=100):
self.modelfile = ModelFile
self.predict_file = PredictFile
self.Width = Width
self.Height = Height
self.CellType = CellType
def Predict(self):
model = load_model(self.modelfile)
img_open = Image.open(self.predict_file)
conv_RGB = img_open.convert('RGB')
new_img = conv_RGB.resize((self.Width, self.Height), Image.BILINEAR)
new_img.save(self.predict_file)
#print('Image Processed')
image = processimage.imread(self.predict_file)
image_to_array = np.array(image) / 255.0
image_to_array = image_to_array.reshape(-1, 100, 100, 3)
#print('Image reshaped')
prediction = model.predict(image_to_array)
print(prediction)
Final_prediction = [result.argmax() for result in prediction][0]
print(Final_prediction)
#生成预测结果json文件
CellType = ['嗜酸性粒细胞', '嗜碱性粒细胞', '中性粒细胞', '空']
probability = []
count = 0
for i in prediction[0]:
percentage = '%.2f%%' % (i * 100)
probability.append(percentage)
count += 1
print(probability)
global dic2
dic2 = dict(zip(CellType, probability))
print(dic2)
return json.dumps(dic2)
def ShowPredImg(self):
image = processimage.imread(self.predict_file)
plt.imshow(image)
plt.show()
def main():
CellType = ['嗜酸性粒细胞', '嗜碱性粒细胞', '中性粒细胞', '空']
Pred = Prediction(PredictFile='2.jpg', ModelFile='cellfinder.h5', Width=100, Height=100, CellType=CellType)
Pred.Predict()
#设置路由
@app.route("/",methods = ["POST"])
def upload():
file_obj = request.files.get("pic")
if file_obj is None:
return "上传失败"
file_obj.save("2.jpg")
print("上传成功")
main()
os.remove("2.jpg")
return json.dumps((dic2))
#启动路由
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)