如何将我上传的图像从 html 页面传递到烧瓶?

我需要将我上传的图像传递给烧瓶。 html 页面上传了图像,但我无法将该图像作为请求传输到烧瓶。 我用它来进行图像预测,但无法获取图像。


这是我的烧瓶代码


import numpy as np

from flask import Flask, request, jsonify, render_template

import numpy

from PIL import Image

from keras.models import load_model

app = Flask(__name__)


model = load_model('traffic_classifier.h5')

classes = { 1:'Speed limit (20km/h)',

            2:'Speed limit (30km/h)',      

            3:'Speed limit (50km/h)',       

            4:'Speed limit (60km/h)',      

            5:'Speed limit (70km/h)',    

            6:'Speed limit (80km/h)',      

            7:'End of speed limit (80km/h)',     

           8:'Speed limit (100km/h)',    

            9:'Speed limit (120km/h)',     

           10:'No passing',   

           11:'No passing veh over 3.5 tons',     

           12:'Right-of-way at intersection',     

           13:'Priority road',    

           14:'Yield',     

           15:'Stop',       

           16:'No vehicles',       

           17:'Veh > 3.5 tons prohibited',       

           18:'No entry',       

           19:'General caution',     

           20:'Dangerous curve left',      

           21:'Dangerous curve right',   

           22:'Double curve',      

           23:'Bumpy road',     

           24:'Slippery road',       

           25:'Road narrows on the right',  

           26:'Road work',    

           27:'Traffic signals',      

           28:'Pedestrians',     

           29:'Children crossing',     

           30:'Bicycles crossing',       

           31:'Beware of ice/snow',

           32:'Wild animals crossing',      

           33:'End speed + passing limits',      

           34:'Turn right ahead',     

           35:'Turn left ahead',       

           36:'Ahead only',      

           37:'Go straight or right',      

           38:'Go straight or left',      

           39:'Keep right',     

           40:'Keep left',      

           41:'Roundabout mandatory',     

           42:'End of no passing',      

           43:'End no passing veh > 3.5 tons' }




元芳怎么了
浏览 66回答 1
1回答

慕神8447489

<form method="post" action="{{ url_for('') }}" enctype="multipart/form-data">def upload_image():&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ALLOWED_EXTENSIONS = [".png", ".jpg", ".jpeg", ".gif"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = request.files['image']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if file and any(split_filename(file.filename)[1] == s for s in ALLOWED_EXTENSIONS):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; folder = app.config['UPLOAD_FOLDER']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathName = app.config['IMAGE_PATH'] + datetime.utcnow().strftime(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '%Y\\%m\\')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not os.path.exists(os.path.join(folder + pathName)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.makedirs(folder + pathName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename = str(uuid.uuid4()) + split_filename(file.filename)[1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file.save(os.path.join(folder + pathName, filename))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = pathlib.PureWindowsPath(pathName + filename).as_posix()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return url_for('main.get_file', path=path, _external=True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'Please Choose PNG, JPG, JPEG, GIF Image, Not ' +&nbsp; split_filename(file.filename)[1], 404&nbsp; &nbsp; except Exception as error:&nbsp; &nbsp; &nbsp; &nbsp; return error.__str__()@mn.route('/file/<path:path>', methods=['GET'])def get_file(path):&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; return send_file(os.path.join(app.config['UPLOAD_FOLDER'], path))&nbsp; &nbsp; except :&nbsp; &nbsp; &nbsp; &nbsp; return send_file(os.path.join(app.config['UPLOAD_FOLDER'], '404.png'))这是我的代码
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5