Python Flask文件上传失败,出现404错误

时间:2019-09-29 12:10:34

标签: python http flask keras

在遵循Flask文档/有关如何上传文件的教程时,我使用了以下代码:

main.py:

from flask import render_template, jsonify, Flask, redirect, url_for, request
from app import app
import random
import os
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np


@app.route('/')



@app.route('/upload')
def upload_file2():
   return render_template('index.html')

@app.route('/uploaded', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      f = request.files['file']
      path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename)
      model= ResNet50(weights='imagenet')
      img = image.load_img(path, target_size=(224,224))
      x = image.img_to_array(img)
      x = np.expand_dims(x, axis=0)
      x = preprocess_input(x)
      preds = model.predict(x)
      preds_decoded = decode_predictions(preds, top=3)[0] 
      print(decode_predictions(preds, top=3)[0])
      f.save(path)
      return render_template('uploaded.html', title='Success', predictions=preds_decoded, user_image=f.filename)


@app.route('/index')
def index():
    return render_template('index.html', title='Home')

@app.route('/map')
def map():
    return render_template('map.html', title='Map')


@app.route('/map/refresh', methods=['POST'])
def map_refresh():
    points = [(random.uniform(48.8434100, 48.8634100),
               random.uniform(2.3388000, 2.3588000))
              for _ in range(random.randint(2, 9))]
    return jsonify({'points': points})


@app.route('/contact')
def contact():
    return render_template('contact.html', title='Contact')

代码中的所有内容似乎都还不错。我已经检查了几次。

在Windows机器上的localhost上运行它时会很好。

我得到404 HTTP状态代码(找不到页面)。 e 404 POST

我尝试过:在 http://localhost:5000/ http://localhost:5500/

我还清理了浏览器...

1 个答案:

答案 0 :(得分:1)

更改 @ app.route('/ uploaded',方法= ['GET','POST']) 至 @ app.route('/ upload.php',方法= ['GET','POST'])

或者,更改您的前端表单以发布到上载。