【后台开发--Service】flask上传图片及视频文件接口开发

 

 

1、Flask接收文件-图片、视频

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、Base64 & File 上传接口开发

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")

  data = base64.b64encode(open(path, 'rb').read()).decode('ascii')
  return jsonify(result=1, data=data, filename='result.png')

 

4、客户端--base64上传

import json
import base64
import requests
 
data = {}
with open('111.png', mode='rb') as file:
    img = file.read()
data['image_str'] = base64.encodebytes(img).decode("utf-8")
 
print(json.dumps(data))
 
url = "http://127.0.0.1:5000"
result = requests.request("POST", url, json=data)
print(result)

 

5、客户端文件上传

    import requests

fl = open(f, 'rb') files = {'file': ('pic.jpg', fl, 'image/jpeg')} r = requests.post(url, files=files, data=data)

 

 

参考链接: https://blog.csdn.net/Xin_101/article/details/87100973

参考链接:base64 上传 https://blog.csdn.net/MELENY/article/details/102764821

posted @ 2021-05-21 10:06  戒骄戒躁-沉淀积蓄  阅读(1114)  评论(0编辑  收藏  举报