- Ubuntu18.04
- Flask
- Tensorflow1.13.0
1 图像及视频文件
flask上传文件有专用获取文件的方法:request.files,该方法可接受图像和视频及其他格式文件,下面以图像文件为例进行说明。
from flask import Flask, jsonify, request
import json
import requests
import os
import base64
import tensorflow as tf
basedir = os.path.abspath(os.path.dirname(__name__))
app = Flask(__name__)
@app.route('/imageprocess', methods=['GET', 'POST'])
def image_preprocess():
# get upload image and save
image = request.files['image']
path = basedir + "/source_images/"
file_path = path + image.filename
image.save(file_path)
# tensorflow process image and save
with tf.Session() as sess:
image_path = "./source_images/" + image.filename
image_raw_data = tf.gfile.FastGFile(image_path, 'rb').read()
image_decode = tf.image.decode_png(image_raw_data)
height, width, _ = sess.run(image_decode).shape
image_type = tf.image.convert_image_dtype(image_decode, dtype=tf.float32)
# keep all image info, just change image size
image_resized = tf.image.resize_images(image_type, [300, 300], method=0)
# for save, transfor image to uint8 type
image_data = tf.image.convert_image_dtype(image_resized, dtype=tf.uint8)
encode_image = tf.image.encode_png(image_data)
# plt.savefig("./processed_images/resized.png")
if os.path.exists("processed_images") is False:
os.mkdir("processed_images")
with tf.gfile.GFile("./processed_images/resized.png", 'wb') as f:
f.write(sess.run(encode_image))
return jsonify({"width":width, "height":height})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
2 base64文件
from flask import Flask, jsonify, request
import json
import requests
import os
import base64
import tensorflow as tf
@app.route("/base64image", methods=['GET', 'POST'])
def base64image():
base64_image = request.json['image']
with open("./source_images/b64test.png", 'wb') as fdecode:
decode_base64 = base64.b64decode(base64_image)
fdecode.write(decode_base64)
return str(decode_base64)
with tf.Session() as sess:
image_path = "./source_images/b64test.png"
image_raw_data = tf.gfile.FastGFile(image_path, 'rb').read()
image_decode = tf.image.decode_png(image_raw_data)
height, width, _ = sess.run(image_decode).shape
image_type = tf.image.convert_image_dtype(image_decode, dtype=tf.float32)
# keep all image info, just change image size
image_resized = tf.image.resize_images(image_type, [300, 300], method=0)
# for save, transfor image to uint8 type
image_data = tf.image.convert_image_dtype(image_resized, dtype=tf.uint8)
encode_image = tf.image.encode_png(image_data)
if os.path.exists("processed_images") is False:
os.mkdir("processed_images")
with tf.gfile.GFile("./processed_images/resized.png", 'wb') as f:
f.write(sess.run(encode_image))
return jsonify({"width":width, "height":height})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
3 多格式文件上传
添加数据和数据类型判断即可。
from flask import Flask, request
import base64
app = Flask(__name__)
@app.route("/upload_data", methods=["POST"])
def upload_data():
if request.json and "input" in request.json:
# 传输base64的json文件
data = request.json["input"]
with open("./path/image.jpg", "wb") as fdecode:
decode_image = base64.b64decode(data)
fdecode.write(decode_image)
elif request.files and "input" in request.files:
# 传输图片文件
data = request.files["input"]
data.save("./path/image.jpg")
4 总结
(1) flask后台获取图片有两种方式:直接上传图片,上传base64格式数据;
(2) 上传图片,进一步处理需要先保存图片,然后获取文件路径,进行下一步处理;上传base64先将文件解码,然后保存;
(3) 支持多种文件格式上传,对数据格式进行判断即可;
(4) request.files方法用于传不同格式的文件。
[参考文献]
[1]https://blog.csdn.net/Xin_101/article/details/87096120