기본 콘텐츠로 건너뛰기

Flask(플라스크) - 머신러닝, 딥러닝 웹 서비스 예제 소스 및 개념 설명(2)

Flask(플라스크) - 머신러닝, 딥러닝 웹 서비스 예제 소스 및 개념 설명(2)

import flask

from flask import Flask, request, render_template

from sklearn.externals import joblib

import numpy as np

from scipy import misc

from ml.model import export_model

from flask_restful import Resource, Api

app = Flask(__name__)

api = Api(app)

# 메인 페이지 라우팅

@app.route( "/" )

@app.route( "/index" )

def index():

return flask.render_template( 'index.html' )

# 데이터 예측 처리

@app.route( '/predict' , methods = [ 'POST' ])

def make_prediction():

if request.method = = 'POST' :

# 업로드 파일 처리 분기

file = request.files[ 'image' ]

if not file : return render_template( 'index.html' , ml_label = "No Files" )

# 이미지 픽셀 정보 읽기

# 알파 채널 값 제거 후 1차원 Reshape

img = misc.imread( file )

img = img[:, :, : 3 ]

img = img.reshape( 1 , - 1 )

# 입력 받은 이미지 예측

prediction = model.predict(img)

# 예측 값을 1차원 배열로부터 확인 가능한 문자열로 변환

label = str (np.squeeze(prediction))

# 숫자가 10일 경우 0으로 처리

if label = = '10' : label = '0'

# 결과 리턴

return render_template( 'index.html' , ml_label = label)

# 데이터 모델 재학습

@app.route( '/retrain' , methods = [ 'POST' ])

def make_model():

if request.method = = 'POST' :

# 모델 재 생성

export_model( 'R' )

return render_template( 'index.html' , md_label = '모델 재생성 완료' )

# 데이터 모델 재학습(RestApi)

class RestMl(Resource):

def get(self):

export_model( 'R' )

return { 'result' : True, 'modelName' : 'model.pkl' }

# Rest 등록

api.add_resource(RestMl, '/retrainModel' )

if __name__ = = '__main__' :

# 모델 로드

# ml/model.py 선 실행 후 생성

model = joblib.load( './model/model.pkl' )

# Flask 서비스 스타트

from http://niceman.tistory.com/193 by ccl(S)

댓글

이 블로그의 인기 게시물

Flask 13. pythonanywhere에 배포하기

Flask 13. pythonanywhere에 배포하기 Login: PythonAnywhere It's always a pleasure to hear from you! Ask us a question, or tell us what you love or hate about PythonAnywhere. We'll get back to you over email ASAP. Sorry, there was an error connecting to the server. Please try again in a few moments... www.pythonanywhere.com from http://ohdowon064.tistory.com/124 by ccl(A) rewrite - 2020-03-11 15:54:10

[ubuntu] FLASK_APP

[ubuntu] FLASK_APP Development/Debugging 🐞 FLASK_ENV=development FLASK_APP = app.py flask run zsh: command not found: FLASK_APP ✔️ FLASK_ENV=development FLASK_APP=app.py flask run 띄어쓰기를 해서 저런 오류를 출력할수도 있구나 😲 참고 : 108p에서 FLASK가 FKAS로 오타나있다. from http://hee-stories.tistory.com/18 by ccl(A) rewrite - 2020-03-24 17:20:11