1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您需要使用多個(gè) ajax 調(diào)用來(lái)實(shí)現(xiàn)這一點(diǎn)。類似于以下內(nèi)容:
首先制定處理圖像上傳的途徑
@app.route('/uploaded', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
filename = secure_filename(file.filename)
f = request.files['file']
f.save(secure_filename(f.filename))
image = cv2.imread(filename)
im = Image.fromarray(image)
# make some adjustments to the image (not shown here) and then save it...
im.save(os.path.join(app.config['UPLOAD_FOLDER'], 'your_file.jpg'))
# Here you need to convert your image to a base64 string and return that string to your ajax call
# do some more analysis, then finally:
return 'YOUR BASE64 ENCODED IMAGE STRING'
這條路線將處理您的分析。您的第二個(gè)嵌套 ajax 調(diào)用將與此路由從flask import Response 進(jìn)行通信
@app.route('/analyze', methods=['GET', 'POST'])
def analyze():
# run some analysis
return Response(status=200, response="Anlysis Done")
這就是您的 javascript 代碼的樣子。您可以將其放置在模板中的腳本標(biāo)記中。如果你把它放在一個(gè)單獨(dú)的 JS 文件中,一定要看看我在第二個(gè) ajax 調(diào)用中對(duì) url_for 的評(píng)論
$('form').submit(function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $(this).serialize()
}).done(function(res){
// Here you write code that will display the returned base64 image
// from the upload_file route Just update your img tag src to the
// base64 string you received as the response
// SECOND AJAX CALL TO RUN THE ANALYTICS
$.ajax({
url: "{{url_for('analysis')}}", // if this JS code in a separate JS file you will have to declare a URL variable in a script tag in your template as {{url_for}} is only accessible from your template
type: 'POST',
data: 'any data you want to send can be in JSON format'
}).done(function(res){
// analysis is done
})
})
})
添加回答
舉報(bào)