flask上传图片及视频文件接口开发

  • 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


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值