Flask App - 为什么我只得到一个占位符选择而不是上传的图像?
Flask App - why do I just get a placeholder pick instead of uploaded image?
我创建了一个 Flask 应用来上传图像以预测其标签。这是app.py相关部分的代码:
@app.route("/", methods=['GET', 'POST'])
def main():
return render_template("index.html")
@app.route("/submit", methods = ['GET', 'POST'])
def get_output():
if request.method == 'POST':
img = request.files['my_image']
licenseplate = "static/" + img.filename
img.save(licenseplate)
p = predictLicensePlate(licenseplate)
这是index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Number Plate Recognition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1 class="jumbotron bg-primary">Number Plate Recognition</h1>
<br><br>
<form class="form-horizontal" action="/submit" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Upload Your Image :</label>
<div class="col-sm-10">
<input type="file" class="form-control" placeholder="Hours Studied" name="my_image" id="pwd">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% if prediction %}
<img src="{{img_path}}" height="200px" width="300px">
<h2> Your Prediction : <i> {{prediction}} </i></h2>
{% endif %}
</div>
</body>
</html>
但是当我上传图片时,我只会在那里看到一个占位符图片,如下所示。你能找出我的代码有什么问题吗?我的图片格式是png,预测没问题。
您需要在模板中设置 img_path
变量:
<img src="{{img_path}}" height="200px" width="300px">
我创建了一个 Flask 应用来上传图像以预测其标签。这是app.py相关部分的代码:
@app.route("/", methods=['GET', 'POST'])
def main():
return render_template("index.html")
@app.route("/submit", methods = ['GET', 'POST'])
def get_output():
if request.method == 'POST':
img = request.files['my_image']
licenseplate = "static/" + img.filename
img.save(licenseplate)
p = predictLicensePlate(licenseplate)
这是index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Number Plate Recognition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1 class="jumbotron bg-primary">Number Plate Recognition</h1>
<br><br>
<form class="form-horizontal" action="/submit" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Upload Your Image :</label>
<div class="col-sm-10">
<input type="file" class="form-control" placeholder="Hours Studied" name="my_image" id="pwd">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% if prediction %}
<img src="{{img_path}}" height="200px" width="300px">
<h2> Your Prediction : <i> {{prediction}} </i></h2>
{% endif %}
</div>
</body>
</html>
但是当我上传图片时,我只会在那里看到一个占位符图片,如下所示。你能找出我的代码有什么问题吗?我的图片格式是png,预测没问题。
您需要在模板中设置 img_path
变量:
<img src="{{img_path}}" height="200px" width="300px">