기본 콘텐츠로 건너뛰기

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

Flask.py #1 설치해보자

Flask.py #1 설치해보자 Flask는 python의 프레임워크라고 할 수 있습니다. Flask를 설치해보자! # 리눅스 -- 접기 이러면 끝 - # pip install flask $ sudo -s 리눅스 같은 경우는 이미 파이썬이 설치가 되어 있을겁니다. # 리눅스 -- 접기 # 맥 -- 접기 이러면 끝 - $ sudo pip install flask 설치가 끝낫으면 Terminal.app 을 열어줍시다. 로 접속하여 파이썬을 설치해 줍니다. [ https://www.python.org/ ] # 맥 -- 접기 # 윈도우 -- 접기 [ https://www.python.org/ ] 로 접속하여 파이썬을 설치해 줍니다. 설치가 끝났으면 [ 고급설정 ] - [ 환경변수 ] - [ 시스템 변수 ] 에서 Path를 찾아줍니다. Path를 찾으셧다면 편집을 누르고 파이썬의 설치 경로와 파이썬 폴더 내의 Script 폴더를 입력해 줍시다. 환경변수까지 끝나셧다면 cmd 창을 키시고 pip install flask 이러면 flask 설치가 끝나게 됩니당 from http://chocoweb.tistory.com/14 by ccl(A) rewrite - 2020-03-07 13:54:54