Python implements the interface, calls the deep model prediction, and returns the prediction result to the front end

1. Use Apifox software to create an interface

insert image description here
If you want to send an image to the backend, you can use the following method. If there are only parameters, you can write the parameters in Params. As shown in the
insert image description here
figure below, a created interface, after python is configured later, you can send the request:
insert image description here

2. python interface

from io import BytesIO
import numpy as np
from PIL import Image
from flask import Flask, request
from flask import Flask
import matplotlib.pyplot as plt
from flask import render_template
import json
import base64
import cv2
import os
app = Flask(__name__)
from PIL import Image
from deeplab import DeeplabV3


@app.route("/test", methods=['POST'])
def check():
    # 默认返回内容
    return_dict = {
    
    'code': '200', 'message': '处理成功', 'result': None, 'label': None, 'ratio': None}
    # 接收图像
    data_file = request.files.get('MyName')
    # 接收id
    id = request.form.get("Id")

    image = Image.open(data_file)

    deeplab = DeeplabV3()
    result, ratio = deeplab.detect_image(image)
    result_ratio = [ratio]

    # 返回图像
    buffer = BytesIO()
    result.save(buffer, "PNG")  # 将Image对象转为二进制存入buffer。因BytesIO()是在内存中操作,所以实际是存入内存
    buf_bytes = buffer.getvalue()  # 从内存中取出bytes类型的图片
    base64_data2 = 'data:image/png;base64,' + str(base64.b64encode(buf_bytes), 'utf-8')

    return_dict['label'] = base64_data2
    return_dict['ratio'] = result_ratio
    return json.dumps(return_dict, ensure_ascii=False)


if __name__ == "__main__":

    app.run(debug=True)

3. Test

After configuring, click Send to get the predicted result, which can be directly copied to the browser to view
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44669966/article/details/126429661