Flask创建REST API调用以进行简单的图像上传/下载

时间:2019-11-25 06:02:37

标签: python-3.x rest flask flask-restful

我正在尝试使用Flask建立一个简单的图像上传/下载系统。

在上传的图像上,我们进行了一些图像处理(例如

def imgRecog(x): 
    ...
    return recog_image

我当前拥有的代码如下

import os
from flask import Flask, request, render_template, url_for, redirect
import frlib
from imgProc import doImgProc
import cv2

db_dir = os.path.join(os.getcwd(), "imagesDb")
recog_dir = os.path.join(os.getcwd(), "temp")
print(db_dir)

app = Flask(__name__)

@app.route("/")
def fileFrontPage():
    return render_template('index.html')

@app.route("/handleUpload", methods=['POST'])
def handleFileUpload():
    if 'photo' in request.files:
        photo = request.files['photo']
        if photo.filename != '':            
            photo.save(os.path.join(db_dir, photo.filename))
    return redirect(url_for('fileFrontPage'))

@app.route("/doImgProcx", methods=['POST'])
def doImgProcx():
    if 'photo' in request.files:
        photo = request.files['photo']
        if photo.filename != '':            
            photo.save(os.path.join(recog_dir, 'img.jpeg'))
            img_path = os.path.join(recog_dir, 'img.jpeg')
            img = cv2.imread(img_path)
            ns, ds = doImgProc(img)
            if ns!= None:
                print(ns)
            if ds != None:
                print(ds)
    return redirect(url_for('fileFrontPage'))

if __name__ == '__main__':
    app.run()     

Q1。如何(a)首先创建(b)然后测试其余API是否成功运行?

Q2。如何使用图像作为参数创建jsonable API?

Q3。如何将文件保存到Google驱动器或保管箱而不是使用本地路径(适用于localhost)?

谢谢 钱德拉

0 个答案:

没有答案