기본 콘텐츠로 건너뛰기

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)

댓글

이 블로그의 인기 게시물

외래어 정리

외래어 정리 [A] acacia 아카시아 academic 아카데믹 academy 아카데미 acanthus 아칸서스 accelerator 액셀러레이터 accent 악센트 acceptor 억셉터 access 액세스 accessory 액세서리 accordion 아코디언 ace 에이스 acetate 아세테이트 acetaldehyde 아세트알데히드 acetic acid 아세트(산) acetone 아세톤 acetyl 아세틸 acetylene 아세틸렌 Achilles tendon 아킬레스(건) acre 에이커 acrylic acid 아크릴(산) action 액션 active 액티브 acyl 아실 AD 에이디 adagio 아다지오 adapter 어댑터 ad + balloon 애드벌룬 address 어드레스 adenine 아데닌 adrenaline 아드레날린 advantage 어드밴티지 aerobic dance 에어로빅 댄스 aerofoil 에어로포일 aerosol 에어로졸 afghan 아프간 [편물] after + service 애프터서비스 agape 아가페 Ainu 아이누 air conditioner 에어컨(디셔너) airspray 에어스프레이 album 앨범 albumin 알부민 alcohol 알코올 aldehyde 알데히드 ALGOL 알골 algorism 알고리즘 alibi 알리바이 alkali 알칼리 alkaloid 알칼로이드 Allah 알라 allegory 알레고리 allegretto 알레그레토 allegro 알레그로 alleluia 알렐루야 Allergie 알레르기 alligator 앨리게이터 all-in-one 올인원 almond 아몬드 aloha 'oe 알로하 오에 Alpenhorn 알펜호른 alpha 알파 alphabet 알파벳 ...

Flask 15. HTTP와 RESTful API

Flask 15. HTTP와 RESTful API 프론트엔드와 백엔드가 분리가 되었다. 그러나 분리가 되면 서로 데이터를 주고받기 위한 프로토콜이 있어야 한다. 이 방법이 HTTP를 사용하는 RESTful API이다. https://docs.microsoft.com/ko-kr/azure/architecture/best-practices/api-design from http://ohdowon064.tistory.com/126 by ccl(A) rewrite - 2020-03-11 16:54:10