python
1import os 2from flask import Flask, request, redirect, url_for,flash 3from werkzeug.utils import secure_filename 4 5from keras.models import Sequential, load_model 6import keras,sys 7import numpy as np 8from PIL import Image 9 10classes = ["matsumoto_hitoshi","nakazima_ishireri"] 11num_classes = len(classes) 12image_size = 50 13 14UPLOAD_FOLDER = './uploads' 15ALLOWED_EXTENSIONS = set(['png', 'jpg', 'gif']) 16 17app = Flask(__name__) 18app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 19app.secret_key = "super secret key" 20 21 22 23def allowed_file(filename): 24 return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS 25 26@app.route('/', methods=['GET', 'POST']) 27def upload_file(): 28 if request.method == 'POST': 29 if 'file' not in request.files: 30 flash('ファイルがありません') 31 return redirect(request.url) 32 file = request.files['file'] 33 if file.filename == '': 34 flash('ファイルがありません') 35 return redirect(request.url) 36 if file and allowed_file(file.filename): 37 filename = secure_filename(file.filename) 38 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 39 filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) 40 41 model = load_model('./face_cnn.h5') 42 43 image = Image.open(filepath) 44 image = image.convert('RGB') 45 image = image.resize((image_size, image_size)) 46 data = np.asarray(image) 47 X = [] 48 X.append(data) 49 X = np.array(X) 50 51 result = model.predict([X])[0] 52 predicted = result.argmax() 53 percentage = int(result[predicted] * 100) 54 55 return "ラベル: " + classes[predicted] + ", 確率:"+ str(percentage) + " %" 56 57 58 #return redirect(url_for('uploaded_file', filename=filename)) 59 return ''' 60 <!doctype html> 61 <html> 62 <head> 63 <meta charset="UTF-8"> 64 <title>ファイルをアップロードして判定しよう</title></head> 65 <body> 66 <h1>ファイルをアップロードして判定しよう!</h1> 67 <form method = post enctype = multipart/form-data> 68 <p><input type=file name=file> 69 <input type=submit value=Upload> 70 </form> 71 </body> 72 </html> 73 ''' 74 75from flask import send_from_directory 76 77@app.route('/uploads/<filename>') 78def uploaded_file(filename): 79 return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
Using TensorFlow backend. C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) C:\Users\my174\anaconda3\envs\kaohanbetsu_for_exam\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)])
実行環境は
windowsos
vscode
python 3.7.8です
どうやって実行していますか?
app.run() がないので、wsgiサーバー(gunicornとか)から呼び出すなどしないといけないです。
回答1件
あなたの回答
tips
プレビュー