利用flask搭建深度学习api,并且自行测试,使用json方式传送

flask服务器端

引入文件:

#coding=utf-8
from flask import Flask,request
from util import image_to_base64,base64_to_image
from flask_cors import *
import json
project_root_path = os.path.join(os.path.dirname(__file__), '../')
app = Flask(__name__)
CORS(app)

路由函数:

@app.route('/cv/v1/face_detect',methods=['GET','POST'])
def face_detect():
    img_base64 = request.json['img_base64']
    img_PIL = base64_to_image(img_base64)
    # 此处为逻辑处理过程
    
    #返回内容,返回json
    jsonresult = {}
    jsonresult['base64'] = image_to_base64(img_PIL)
    dataPModel = {"msg": "face detect success", "code": 200, "result": jsonresult}
    return json.dumps(dataPModel)

main函数起服务:

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8502,use_reloader=False)

base64与PIL转换函数

def image_to_base64(img):
    output_buffer = BytesIO()
    img.save(output_buffer, format='JPEG')
    byte_data = output_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    return base64_str.decode()

def base64_to_image(base64_str):
    base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
    byte_data = base64.b64decode(base64_data)
    image_data = BytesIO(byte_data)
    img = Image.open(image_data)
    return img

测试程序

当服务启动完成,需要测试api是否调试正确,测试程序如下:

from io import BytesIO
from PIL import Image
import base64
import json
import re
import requests
from time import time

#发送代码
def json_send(dataPModel,url):
    headers = {"Content-type": "application/json", "Accept": "text/plain", "charset": "UTF-8"}
    response = requests.post(url=url, headers=headers, data=json.dumps(dataPModel))
    return json.loads(response.text)

if __name__ == "__main__":
    url = 'http://192.168.102.198:8502/cv/v1/face_detect'
    img = Image.open('3.jpg')
    # print(img.size)
    img_base64 = image_to_base64(img)
    dataPModel = {"img_base64": img_base64}
    a = time()
    for i in range(100):
        result = json_send(dataPModel,url)
        base64_to_image(result['result']['base64'])

    print(time()-a)
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值