-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
37 lines (30 loc) · 1.33 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
import model as ml
import tensorflow as tf
import numpy as np
import rps_battle
import os
model = tf.keras.models.load_model('MobileNet_Colab.h5')
class_names = {0: 'paper', 1: 'rock', 2: 'scissors'}
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("index.html")
if request.method == "POST":
f = request.files['imageFile']
if secure_filename(f.filename) == '' or secure_filename(f.filename)[-4:] not in ['.jpg', '.png']:
return render_template("index.html", error=404)
file_path = 'static/images/' + secure_filename(f.filename)
f.save(file_path)
probability = ml.rps_predict(file_path, model)[0]
if max(probability) < 0.7:
return render_template("index.html", unknown=True)
user_str = class_names[np.argmax(probability)]
result, com_path, com = rps_battle.checkWin(np.argmax(probability))
com_str = class_names[com]
return render_template("index.html", img=file_path, ret_=user_str, com=com_str, com_path=com_path,
result=result)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))