code HTML
โค้ด: เลือกทั้งหมด
{% extends "layout.html" %}
{% block title %}
<title>Test ML</title>
{% endblock %}
{% block content %}
<div class="parent">
<header>
<h1>Predict an image</h1>
</header>
<p>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</p>
{% if filename %}
<main>
<div>
<img src="{{ url_for('display_image', filename=filename) }}">
</div>
{% endif %}
<form action="http://127.0.0.1:8000/submitform" method="post" onsubmit="onSubmit()" enctype="multipart/form-data">
<div class="flex-container">
<select class="form-select" name="model" id="model" aria-label="select a model">
<option value="1" selected>1st model 224*224</option>
<option value="2">2nd model 90*96</option>
</select>
</div>
<div class="flex-container">
<input type="file" class="form-control" id="file" name="file" required>
<button type="submit" class="btn btn-primary mb-3">Predict</button>
</div>
</form>
<hr>
<div>
{% if gender_prediction %}
<h3>This's a picture of a {{gender_prediction}}.</h3>
<h4>It was processed by {{model}}</h4>
{% else %}
<h3>Nothing's here</h3>
{% endif %}
</div>
</div>
</main>
<script>
function onSubmit() {
try {
window.location.replace("http://127.0.0.1:5000/");
}
catch(error) {
console.error(error);
}
}
</script>
โค้ด: เลือกทั้งหมด
from flask import Flask, url_for, render_template, request
import os
from flask.helpers import flash
from werkzeug.utils import redirect, secure_filename
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow import keras
import requests
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads/'
app.secret_key = 'random string'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
# there're type of file that allowed types
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
last_file_path = ''
loaded_model = keras.models.load_model('../model/gender_classification_model224-224.h5')
prediction = ''
# return the tuple of prediction
# the first index's gender
# the second index's probability
def get_classes(data):
global loaded_model
prob = loaded_model.predict(data)
if prob[0][0] < 0.5:
return 'woman', 1 - prob[0][0]
else:
return 'man', prob[0][0]
# this function check the type of file
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# http://127.0.0.1:5000/
@app.route('/')
def index(): # just render the index.html
url = 'http://127.0.0.1:8000/submitform'
response = requests.get(url)
if response.status_code == 200:
print(response)
print(type(response))
return render_template('index.html')
# Flask section
# http://127.0.0.1:8000/predict
# @app.route('/', methods = ['post'])
# def predict_page():
# url = 'http://127.0.0.1:8000/submitform'
# response = requests.get(url)
# print(type(response))
# print(response)
# if response.status_code == 200:
# pass
# return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True) # debug mode is on
โค้ด: เลือกทั้งหมด
from fastapi import FastAPI, Body, Request, File, UploadFile, Form
import uvicorn
from pathlib import Path
api = FastAPI() # Init FastAPI Ap
# Flask section
# http://127.0.0.1:8000/predict
# @flask_app.get('/')
# def predict_page():
# url = 'http://127.0.0.1:8000/submitform'
# response = requests.get(url)
# print(type(response))
# print(response)
# if response.status_code == 200:
# pass
# return render_template('index.html')
# http://127.0.0.1:8000/predict
# @flask_app.get('/predicted')
# def predict_page_result():
# url = 'http://127.0.0.1:8000/submitform'
# response = requests.get(url)
# print(response)
# FastAPI section
# http://127.0.0.1:8000/submitform
@api.post('/submitform')
async def handle_form(model: str = Form(...), file: UploadFile = File(...)):
return {'model': model, 'file': file}
if __name__ == '__main__':
uvicorn.run(api, host='127.0.0.1', port=8000, debug=True)