gpt4 book ai didi

python - 使用 Flask 返回多个图像文件

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:54 26 4
gpt4 key购买 nike

首先,我进行了很多搜索,但还没有找到我的问题的直接答案。

我正在编写一个新程序,它接受一个图像,然后找到与之相似的图像,然后从数据库中提取图像名称。我用 python3 中的 Flask 创建了一个简单的响应,将这些图像文件的名称作为 JSON 格式。我需要将这些图像及其名称显示为网页,并在 Flask 中进行响应。主要问题是图像的数量不固定,可能会有所不同,有时变成 10 个,有时变成 0 个。实际上,我不熟悉 Flask 或 HTML,但我需要这样做。这是我的示例代码,它将图像名称响应为一个数组:

from flask import Flask, jsonify, request, redirect, send_file
import numpy
import os
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)


def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)

file = request.files['file']

if file.filename == '':
return redirect(request.url)

if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return detect_faces_in_image(file)

# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Face System</title>
<h1>Upload a picture and see if it's a picture of database!</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''


def detect_faces_in_image(file_stream):
... #some coding to create the founded_file_names
out=founded_file_names #array of desired file names
return jsonify(out)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001, debug=True)

谢谢。

最佳答案

首先,不要将 HTML 硬编码到您的 python 文件中。做一个templates/目录,并将所有这些 html 放入一个文件中,比方说 homepage.html .然后您可以使用 return render_template('homepage.html')呈现 html。


现在,对于主要问题。

The major problem is the number of images are not fixed and may vary, sometimes it became as 10 and sometimes as 0.

如果你使用 Flask 中包含的 Jinja,那将不是问题。如果您不熟悉它,请查看 docs .

查看您的代码,最好的方法如下:用户提交图片后,您可以显示另一个页面,比方说 results.html (你应该把它放在 templates/ 中),然后在那里显示图片。

from flask import Flask, jsonify, request, redirect, send_file
import numpy
import os
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)


def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)

file = request.files['file']
if file.filename == '':
return redirect(request.url)

if file and allowed_file(file.filename):
# The image file seems valid!
# Get the filenames and pass the on to the results.html page
return render_template(
'results.html', results=detect_faces_in_image(file)
)

# If no valid image file was uploaded, show the file upload form:
return render_template('homepage.html')


def detect_faces_in_image(file_stream):
# .. do something here ...
return file_names_found # list of the filenames, no need for JSON

if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001, debug=True)

现在,前端。这是results.html的粗略草稿可能看起来像:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Results</title>
</head>
<body>
{% if results %}
<table>
<tbody>
{% for img_name in results %}
<tr>
<td>{{ img_name }}</td>
<td><img src="/pathtoyourimg" alt="{{ img_name }}"></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
No results :(
{% endif %}
</body>
</html>

可以看到Jinja语法和python非常相似(if/else, for .. in ..)。要使用变量,必须用 {{ }} 将其括起来. HTML 文件的作用是:

  • 检查是否results if an empty list
    • 如果是,则显示“无结果:(”
    • 如果不是,则显示一个 html 表格。对于每个结果,我们制作一行(<tr>)并在左侧显示名称,在右侧显示图像

您应该将所有图像存储在名为 static/ 的文件夹中在 python 脚本旁边。如果你这样做,src="/pathtoyourimg"变成 src="static/{{ img_name }}"

关于python - 使用 Flask 返回多个图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44728047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com