딥러닝 머신러닝
파이썬 flask에서 텐서플로 숫자 맞추기 웹서비스 해보기
혼앱사
2023. 6. 20. 08:05
반응형
앞으로 진행할 서비스를 테스트 해보았다. 마이봇에서 다양한 ai 컨텐츠를 적용하기 위해
백엔드에서 만들어진 모델을 파이썬 환경에 API로 json 형태로 REST 방식으로 전달할 계획이다.
만들어본 flask 백엔드 소스
import json
from flask import Flask, jsonify, request
from flask_cors import CORS
import keras
import base64
from io import BytesIO
from PIL import Image
import numpy as np
import cv2
app = Flask(__name__)
CORS(app)
# MNIST 학습 모델 https://www.tensorflow.org/tutorials/quickstart/beginner?hl=ko
@app.route('/predict', methods=['POST'])
def test_get():
data = request.json
#print(data['image'])
content = data['image'].split(';')[1]
#print(request)
image_encoded = content.split(',')[1]
body = base64.decodebytes(image_encoded.encode('utf-8'))
fh = open("imageToSave.png", "wb") #이미지 파일로 떨거봄 /*서비스 할때 삭제*/
fh.write(body)
fh.close()
# 들어온이미지를 28 28 정사각형 사이즈로 변화
img = np.array(Image.open(BytesIO(body)).convert("L"))
image = cv2.resize(img, (28,28))/255.0
image = np.reshape(image, (1, (28,28)[0], (28,28)[1]))
# 이미 만들어지 모델을 가져옴 (한습한 데이터)
new_model = keras.models.load_model('./tf-model.h5')
predictions=new_model.predict(image)
print("1",np.argmax(predictions[0])) #가장 근사한 값을 나열한것중 첫벗째
print("2",np.argmax(predictions[:10],axis=1))
print("3",predictions[0])
result =int(np.argmax(predictions[0]))
#return rDate
return jsonify({
"greeting": ["hello", "world"],
"result": result
})
if __name__ == '__main__':
app.run(host='localhost', port=5000, threaded=False)
flask 실행 모습
데이터를 받아서 처리하는쪽
$.ajax({
type:'POST',
url:"http://localhost:5000/predict",
dataType:'json',
contentType: "application/json; charset=utf-8",
data:SENDDATA,
success:function(responce){
console.log(responce);
$("#answer").text(responce["result"])
},
error: function (response) {
// alert the error if any error occured
alert("this one is error");
alert(response["responseJSON"]["error"]);
},
})
https://github.com/bastokr/st_tanserflow/tree/main/trainflask1
GitHub - bastokr/st_tanserflow
Contribute to bastokr/st_tanserflow development by creating an account on GitHub.
github.com
728x90
반응형