Aidemy 2020/10/4
Hello, it is Yope! I am a liberal arts student, but I was interested in the possibilities of AI, so I went to the AI-specialized school "Aidemy" to study. I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This time, I will post an introduction to Flask memo. Nice to meet you.
-Flask is a Python web application framework. Similar to Rails in Ruby. -Use Flask as follows.
#Import Flask package
from flask import Flask
#Instantiate Flask class
app=Flask(__name__)
#Define the function when accessing the URL
@app.route('/')
def hello_world():
return "Hello World"
#Only execute when the code is executed directly.
if __name__=='__main__':
app.run()
[email protected] ("URL") __ is to execute the function defined after it when the URL is accessed. -The name will be described later.
-Name is a variable that is automatically defined for each file, and __the file name is stored. __ -In addition, __main is stored when the file is directly executed (when the file is executed by a command, etc.). __ (So, if you set name =='main', it will be the condition when the file is executed directly)
@app.route('/')
def hello_world():
return render_template('index.html')
-When accessing the URL of @ app.route () with __render_template ('HTML file') __, HTML can be reflected. -The HTML file at this time must be put in the templates folder.
#First, list the classes to be classified
classes = ["0","1","2","3","4","5","6","7","8","9","10"]
#Specifying the image size
image_size = 28
#Specify the folder to save the uploaded image and specify the extension to allow uploading
UPLOAD_FOLDER = "uploads"
ALLOWED_EXTENSIONS = set(['png','jpg','jpeg'])
#Instantiation of Flask class
app = Flask(__name__)
#Judgment of extension of uploaded image
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#Model loading
model = load_model('./model.h5')
-The extension judgments "'.' In filename" and "filename.rsplit ('.', 1) [1] .lower () in ALLOWED_EXTENSIONS" are conditional expressions. The result of the correctness judgment of this is returned by return.
-".' In filename" has a "." In the file name, or "filename.rsplit ('.', 1) [1] .lower () in ALLOWED_EXTENSIONS" has a "." After the file name. Indicates whether it corresponds to any of "ALLOWED_EXTENSIONS". At this time, __ "rsplit" __ means that the character string is separated from the back.
-On the HTML side, I explained "・ Input form: __ \