python - 如何获取图像文件名并将其发送到flask中的特征提取过程?
问题描述
我在 python 中有一个图像检索程序,我想使用烧瓶制作一个基于 Web 的程序。但是,我不明白如何从烧瓶中获取输入图像。所以我可以在我的图像检索程序中处理输入图像,然后在我的烧瓶页面中显示结果。
这是我的烧瓶代码:
import os
from flask import Flask, flash, request, redirect, url_for, send_from_directory, Request, render_template
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'res/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return render_template('home.html')
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
app.run(host='0.0.0.0', port= 81)
这是我的图像检索程序的一部分,我需要烧瓶中的输入图像来填充'inputImage.jpg'
我的queryPath
:
from PIL import Image
# Define path of testing data and indexing file
queryPath = root_path + 'inputImage.jpg'
index_file = root_path + 'mtcd.csv'
# define the bins for quantization
colorBins = 64
R_Bins = 4
G_Bins = 4
B_Bins = 4
edgeBins = 18
query = cv2.imread(queryPath)
# Color Quantization
colorQuant = combineColorQuantization(query, B_Bins, G_Bins, R_Bins)
# Edge Quantization
edgeQuant = edgeQuantization(query, edgeBins)
# Texton Search
features = textonSearch(colorQuant, colorBins, edgeQuant, edgeBins)
# GLCM
glcm, en = GLCM(query)
features.extend(glcm[0])
features.extend(glcm[1])
features.extend(glcm[2])
features.extend(en)
# perform the search
searcher = Searcher(index_file)
results = searcher.search(features)
# display the query
fig = figure(figsize=(5,5))
title('Query Image')
imshow(array(Image.open(queryPath)))
axis('off')
# loop over the results
fig = figure(num=None, figsize=(20,5))
title('Result Image')
result = []
i = 1
for (score, resultID) in results:
# load the result image and display it
a = fig.add_subplot(2, 6, i)
image = imread(root_path + 'batik/'+resultID)
i += 1
imshow(image)
axis('off')
print(result)
解决方案
您可以像这样将图像处理代码定义为单独的函数
def process_file(path)
# Define path of testing data and indexing file
queryPath = path
index_file = root_path + 'mtcd.csv'
# define the bins for quantization
colorBins = 64
R_Bins = 4
G_Bins = 4
B_Bins = 4
edgeBins = 18
query = cv2.imread(queryPath)
# Color Quantization
colorQuant = combineColorQuantization(query, B_Bins, G_Bins, R_Bins)
# Edge Quantization
edgeQuant = edgeQuantization(query, edgeBins)
# Texton Search
features = textonSearch(colorQuant, colorBins, edgeQuant, edgeBins)
# GLCM
glcm, en = GLCM(query)
features.extend(glcm[0])
features.extend(glcm[1])
features.extend(glcm[2])
features.extend(en)
# perform the search
searcher = Searcher(index_file)
results = searcher.search(features)
# display the query
fig = figure(figsize=(5,5))
title('Query Image')
imshow(array(Image.open(queryPath)))
axis('off')
# loop over the results
fig = figure(num=None, figsize=(20,5))
title('Result Image')
result = []
i = 1
for (score, resultID) in results:
# load the result image and display it
a = fig.add_subplot(2, 6, i)
image = imread(root_path + 'batik/'+resultID)
i += 1
imshow(image)
axis('off')
print(result)
从烧瓶路由代码块调用 process_file() 方法
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
process_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return render_template('home.html')
推荐阅读
- flutter - 警告:无法加载文件 C:\Users\UserName\.android\repositories.cfg。(没有工作这个命令“颤振医生--android-licenses”)
- reactjs - 应用程序通过按钮单击 [ReactJs](已关闭)不断获取 firebase 数据
- mysql - 如何在相关表中添加子行?(续集 N:M 关系)
- node.js - 如何使用nodeJS更新mongodb中的值?
- linux - 什么是在Linux上修补二进制文件的好算法
- java - 每个接口实现类的静态创建者方法
- python - 将 pandas DataFrame 添加到 SQL
- c - 具有上次 strcat 调用数据的 Strcat 结果
- python - 此 Python 脚本目前受输入城市数量的限制,我希望它接受任意数量的城市
- android - 如何获取视图的 x & Y 坐标