0

我一直在学习关于图像分类的在线课程,但似乎无法让它发挥作用。课程链接:https : //academy.zenva.com/course/applied-deep-learning/ 使用 python 应用深度学习。当它应该重定向我并显示结果时,它会一直加载,直到我收到以下错误:

werkzeug.routing.BuildError: Could not build url for endpoint '_uploads.uploaded_file' with values ['filename', 'setname']. Did you mean 'upload' instead?

这是python脚本:

import uuid
import numpy as np
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

from flask import Flask, render_template, request, redirect, url_for  
from flask_uploads import UploadSet, configure_uploads, IMAGES

app = Flask(__name__)

model = ResNet50(weights='imagenet')
photos = UploadSet(name='photos', extensions=IMAGES)

app.config['UPLOADED_PHOTOS_DEST'] = 'static/img'
configure_uploads(app, upload_sets=photos)

@app.route('/', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        filename = photos.save(request.files['photo'], name=uuid.uuid4().hex[:8] + '.')
        return redirect(url_for('show', filename=filename))
    return render_template('upload.html')

@app.route('/photo/<filename>')
def show(filename):
    img_path = app.config['UPLOADED_PHOTOS_DEST'] + '/' + filename
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = x[np.newaxis, ...]
    x = preprocess_input(x)

    y_pred = model.predict(x)
    predictions = decode_predictions(y_pred, top=5)[0]
    url = photos.url(filename)
    return render_template('view_results.html', filename=filename, url=url, predictions=predictions)

这是第一个 HTML 文件:

<head>
    <title>Image Classifier Project</title>
</head>

<body>
    <h1>Upload an Image</h1>
    <form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}">
        <input type=file name=photo>
        <input type="submit">
    </form>
</body>

</html>

和“重定向页面”:

<html>
<head>
    <title>Classification Results Of The Image</title>
</head>

<body>
    <h1>Classification Results</h1>

    <img src="{{ url }}">
    <p>File name: {{ filename }} </p>
    <p></p>

   <table>
        <thead>
            <th>Class</th>
            <th>Probability</th>
        </thead>
    
        <tbody>
            {% for prediction in predictions %}
                <tr>
                    <td>{{ prediction[1] }}</td>
                    <td>{{ prediction[2] }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

</body>
</html>
4

0 回答 0