[RISE] 10주차 활동내용

세휘·2021년 5월 9일
0

RISE 프로젝트

목록 보기
8/12

Flask 웹 서버에서 저장한 모델 불러오기

import flask
from flask import Flask, request, render_template
import numpy as np
from scipy import misc
from keras.models import load_model

app = Flask(__name__)

# 메인 페이지 라우팅
@app.route("/")
@app.route("/index")
def index():
    return flask.render_template('index.html')

# 데이터 예측 처리
@app.route("/predict", methods=['POST'])
def make_predecion():
    if request.method == 'POST':
        # 업로드 파일 처리 분기
        file = request.files['image']
        if not file:
            return render_template('index.html', label="No Files")

        # 이미지 픽셀 정보 읽기
        # 알파 채널 값 제거 후 1차원 Reshape
        img = misc.imread(file)
        img = img[:, :, :3]
        img = img.reshape(1, -1)

        # 입력 받은 이미지 예측
        prediction = model. predict(img)

        label = str(np.squeeze(prediction))

        # 결과 리턴
        return render_template('index.html', label=label)

if __name__ == '__main__':
    # 모델 로드
    model = load_model('my_model1.h5')
    app.run(host='0.0.0.0', port=8000, debug=True)

0개의 댓글