快速识别你家的猫猫狗狗,教你用ModelBox开发AI萌宠应用

modelbox,ai · 浏览次数 : 0

小编点评

# 使用 ModelBox 开发动物目标检测 AI 应用 本教程将引导你使用华为云 ModelBox 开发一个动物目标检测的 AI 应用。我们将逐步介绍如何准备环境、创建工程、开发推理功能单元、修改流程图和准备动物图片。 ## 准备环境 首先,确保你的计算机上已经安装了 ModelBox 端云协同 AI 开发套件。你可以参考 [ModelBox 官方文档](https://www.huawei.com/en-gb/consumer/cn/product/modelbox) 进行下载和安装。 ## 创建工程 在 ModelBox sdk 目录下,使用 create.bat 脚本创建 yolov7_pet 工程: ```shell D:\modelbox-win10-x64-1.5.3> .\create.bat -t server -n yolov7_pet (tensorflow) ``` 设置相关环境变量,如: ```shell D:\modelbox-win10-x64-1.5.3> ``` ## 应用开发 ### 创建推理功能单元 使用 create.bat 脚本创建 yolov7_infer 推理功能单元: ```shell D:\modelbox-win10-x64-1.5.3> .\create.bat -t infer -n yolov7_infer -p yolov7_pet (tensorflow) ``` ### 修改模型配置文件 在 yolov7_pet 工程的 graph 目录下,找到 yolov7_pet.toml 文件并打开。更新配置参数如图像大小、推理引擎类型等。 ### 修改后处理功能单元 使用 create.bat 脚本创建 yolov7_post 后处理功能单元: ```shell D:\modelbox-win10-x64-1.5.3> .\create.bat -t python -n yolov7_post -p yolov7_pet (tensorflow) ``` 在 yolov7_postFlowUnit 类中,修改 postprocess 函数以满足您的需求。 ### 修改流程图 在 yolov7_pet 工程的 graph 目录下的 yolov7_pet.toml 文件中,更新 flow 图和描述。 ### 准备动物图片和测试脚本 准备好动物图片文件,并编写一个用于测试的测试脚本,如下所示: ```python # 导入库 import cv2 import json import base64 # 初始化 http 请求参数 http_config = HttpConfig("ip", "port", "url", img_base64_str) # 读取图片并转换为 base64 编码 img_data, img_base64_str = read_image("test.jpg") ``` ## 运行应用 在 yolov7_pet 工程目录下执行以下命令运行应用: ```shell .\bin\main.bat (tensorflow) ``` ## 测试 使用你编写的测试脚本发起 HTTP 请求进行测试。 通过以上步骤,你已经成功使用华为云 ModelBox 开发了一个动物目标检测的 AI 应用。

正文

本文分享自华为云社区《ModelBox-AI应用开发:动物目标检测【玩转华为云】》,作者:阳光大猫。

一、准备环境

ModelBox端云协同AI开发套件(Windows)环境准备视频教程

二、应用开发

1. 创建工程

ModelBox sdk目录下使用create.bat创建yolov7_pet工程

(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t server -n yolov7_pet 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\ 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=

(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t server -n yolov7_pet
sdk version is modelbox-win10-x64-1.5.3
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet/graph\modelbox.conf to Unix format...
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet/graph\yolov7_pet.toml to Unix format...
dos2unix: converting file D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet/bin\mock_task.toml to Unix format...
success: create yolov7_pet in D:\modelbox-win10-x64-1.5.3\workspace

create.bat工具的参数中,-t表示所创建实例的类型,包括serverModelBox工程)、python(Python功能单元)、c++(C++功能单元)、infer(推理功能单元)等;-n表示所创建实例的名称,开发者自行命名。

2. 创建推理功能单元

ModelBox sdk目录下使用create.bat创建yolov7_infer推理功能单元

(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t infer -n yolov7_infer -p yolov7_pet  

(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\ 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH= 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME= 

(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t infer -n yolov7_infer -p yolov7_pet    
sdk version is modelbox-win10-x64-1.5.3
success: create infer yolov7_infer in D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet/model/yolov7_infer

create.bat工具使用时,-t infer 即表示创建的是推理功能单元;-n xxx_infer 表示创建的功能单元名称为xxx_infer-p yolov7_infer 表示所创建的功能单元属于yolov7_infer应用。

a. 下载转换好的模型

运行此Notebook下载转换好的ONNX格式模型

屏幕截图 2024-06-10 062945.png

b. 修改模型配置文件

模型和配置文件保持在同级目录下

# Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.

[base]
name = "yolov7_infer"
device = "cpu"
version = "1.0.0"
description = "your description"
entry = "./best.onnx"  # model file path, use relative path
type = "inference" 
virtual_type = "onnx" # inference engine type: win10 now only support onnx
group_type = "Inference"  # flowunit group attribution, do not change

# Input ports description
[input]
[input.input1]  # input port number, Format is input.input[N]
name = "Input"  # input port name
type = "float"  # input port data type ,e.g. float or uint8
device = "cpu"  # input buffer type: cpu, win10 now copy input from cpu

# Output ports description
[output]
[output.output1] # output port number, Format is output.output[N]
name = "Output"  # output port name
type = "float"   # output port data type ,e.g. float or uint8

3. 创建后处理功能单元

ModelBox sdk目录下使用create.bat创建yolov7_post后处理功能单元

(tensorflow) PS D:\modelbox-win10-x64-1.5.3> .\create.bat -t python -n yolov7_post -p yolov7_pet  

(tensorflow) D:\modelbox-win10-x64-1.5.3>set BASE_PATH=D:\modelbox-win10-x64-1.5.3\ 

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PATH=D:\modelbox-win10-x64-1.5.3\\python-embed;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONPATH=

(tensorflow) D:\modelbox-win10-x64-1.5.3>set PYTHONHOME=

(tensorflow) D:\modelbox-win10-x64-1.5.3>python.exe -u D:\modelbox-win10-x64-1.5.3\\create.py -t python -n yolov7_post -p yolov7_pet
sdk version is modelbox-win10-x64-1.5.3
success: create python yolov7_post in D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet/etc/flowunit/yolov7_post

a. 修改配置文件

# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.

# Basic config
[base]
name = "yolov7_post" # The FlowUnit name
device = "cpu" # The flowunit runs on cpu
version = "1.0.0" # The version of the flowunit
type = "python" # Fixed value, do not change
description = "description" # The description of the flowunit
entry = "yolov7_post@yolov7_postFlowUnit" # Python flowunit entry function
group_type = "Generic"  # flowunit group attribution, change as Input/Output/Image/Generic ...

# Flowunit Type
stream = false # Whether the flowunit is a stream flowunit
condition = false # Whether the flowunit is a condition flowunit
collapse = false # Whether the flowunit is a collapse flowunit
collapse_all = false # Whether the flowunit will collapse all the data
expand = false #  Whether the flowunit is a expand flowunit

# The default Flowunit config
[config]
net_h = 640
net_w = 640
num_classes = 2
conf_threshold = 0.5
iou_threshold = 0.45

# Input ports description
[input]
[input.input1] # Input port number, the format is input.input[N]
name = "in_feat" # Input port name
type = "float" # Input port type

# Output ports description
[output]
[output.output1] # Output port number, the format is output.output[N]
name = "out_data" # Output port name
type = "string" # Output port type

b. 修改逻辑代码

# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import _flowunit as modelbox
import numpy as np
import json
import cv2

class yolov7_postFlowUnit(modelbox.FlowUnit):
    # Derived from modelbox.FlowUnit
    def __init__(self):
        super().__init__()

    # Open the flowunit to obtain configuration information
    def open(self, config):
        # 获取功能单元的配置参数
        self.params = {}
        self.params['net_h'] = config.get_int('net_h')
        self.params['net_w'] = config.get_int('net_w')
        self.params['num_classes'] = config.get_int('num_classes')
        self.params['conf_thre'] = config.get_float('conf_threshold')
        self.params['nms_thre'] = config.get_float('iou_threshold')
        self.num_classes = config.get_int('num_classes')

        return modelbox.Status.StatusCode.STATUS_SUCCESS

    # Process the data
    def process(self, data_context):
        # 从DataContext中获取输入输出BufferList对象
        in_feat = data_context.input("in_feat")
        out_data = data_context.output("out_data")

        # yolov7_post process code.
        # 循环处理每一个输入Buffer数据
        for buffer_feat in in_feat:
            # 将输入Buffer转换为numpy对象
            feat_data = np.array(buffer_feat.as_object(), copy=False)
            feat_data = feat_data.reshape((-1, self.num_classes + 5))

            # 业务处理:解码yolov7模型的输出数据,得到检测框,转化为json数据
            bboxes = self.postprocess(feat_data, self.params)
            result = {"det_result": str(bboxes)}
            print(result)

            # 将业务处理返回的结果数据转换为Buffer
            result_str = json.dumps(result)
            out_buffer = modelbox.Buffer(self.get_bind_device(), result_str)

            # 将输出Buffer放入输出BufferList中
            out_data.push_back(out_buffer)

        return modelbox.Status.StatusCode.STATUS_SUCCESS
    
    # model post-processing function
    def postprocess(self, feat_data, params):
        """postprocess for yolo7 model"""
        boxes = []
        class_ids = []
        confidences = []
        for detection in feat_data:
            scores = detection[5:]
            class_id = np.argmax(scores)
            if params['num_classes'] == 1:
                confidence = detection[4]
            else:
                confidence = detection[4] * scores[class_id] 
            if confidence > params['conf_thre'] and detection[4] > params['conf_thre']:
                center_x = detection[0] / params['net_w']
                center_y = detection[1] / params['net_h']
                width = detection[2] / params['net_w']
                height = detection[3] / params['net_h']

                left = center_x - width / 2
                top = center_y - height / 2

                class_ids.append(class_id)
                confidences.append(confidence)
                boxes.append([left, top, width, height])

        # use nms algorithm in opencv
        box_idx = cv2.dnn.NMSBoxes(
            boxes, confidences, params['conf_thre'], params['nms_thre'])

        detections = []
        for i in box_idx:
            boxes[i][0] = max(0.0, boxes[i][0])  # [0, 1]
            boxes[i][1] = max(0.0, boxes[i][1])  # [0, 1]
            boxes[i][2] = min(1.0, boxes[i][0] + boxes[i][2])  # [0, 1]
            boxes[i][3] = min(1.0, boxes[i][1] + boxes[i][3])  # [0, 1]
            dets = np.concatenate(
                [boxes[i], np.array([confidences[i]]), np.array([class_ids[i]])], 0).tolist()
            detections.append(dets)

        return detections

    def close(self):
        # Close the flowunit
        return modelbox.Status()

    def data_pre(self, data_context):
        # Before streaming data starts
        return modelbox.Status()

    def data_post(self, data_context):
        # After streaming data ends
        return modelbox.Status()

    def data_group_pre(self, data_context):
        # Before all streaming data starts
        return modelbox.Status()

    def data_group_post(self, data_context):
        # After all streaming data ends
        return modelbox.Status()

4. 修改流程图

yolov7_pet工程graph目录下存放流程图,默认的流程图yolov7_pet.toml与工程同名,其内容为(以Windows版ModelBox为例):

# Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.

[driver]
dir = ["${HILENS_APP_ROOT}/etc/flowunit",
"${HILENS_APP_ROOT}/etc/flowunit/cpp",
"${HILENS_APP_ROOT}/model",
"${HILENS_MB_SDK_PATH}/flowunit"]
skip-default = true
[profile]
profile=false
trace=false
dir="${HILENS_DATA_DIR}/mb_profile"
[graph]
format = "graphviz"
graphconf = """digraph yolov7_pet {
    node [shape=Mrecord]
    queue_size = 4
    batch_size = 1
    input1[type=input,flowunit=input,device=cpu,deviceid=0]

    httpserver_sync_receive[type=flowunit, flowunit=httpserver_sync_receive_v2, device=cpu, deviceid=0, time_out_ms=5000, endpoint="http://0.0.0.0:8083/v1/yolov7_pet", max_requests=100]
    image_decoder[type=flowunit, flowunit=image_decoder, device=cpu, key="image_base64", queue_size=4]
    image_resize[type=flowunit, flowunit=resize, device=cpu, deviceid=0, image_width=640, image_height=640]
    image_transpose[type=flowunit, flowunit=packed_planar_transpose, device=cpu, deviceid=0]
    normalize[type=flowunit flowunit=normalize device=cpu deviceid=0 standard_deviation_inverse="0.0039215686,0.0039215686,0.0039215686"]
    yolov7_infer[type=flowunit, flowunit=yolov7_infer, device=cpu, deviceid=0, batch_size = 1]
    yolov7_post[type=flowunit, flowunit=yolov7_post, device=cpu, deviceid=0]
    httpserver_sync_reply[type=flowunit, flowunit=httpserver_sync_reply_v2, device=cpu, deviceid=0]
    
    input1:input -> httpserver_sync_receive:in_url
    httpserver_sync_receive:out_request_info -> image_decoder:in_encoded_image
    image_decoder:out_image -> image_resize:in_image
    image_resize:out_image -> image_transpose:in_image
    image_transpose:out_image -> normalize:in_data
    normalize:out_data -> yolov7_infer:Input
    yolov7_infer:Output -> yolov7_post:in_feat
    yolov7_post:out_data -> httpserver_sync_reply:in_reply_info
}"""
[flow]
desc = "yolov7_pet run in modelbox-win10-x64"

5. 准备动物图片和测试脚本

a. 动物图片

yolov7_pet工程data目录下存放动物图片文件夹test_imgs

Abyssinian_1.jpg

saint_bernard_143.jpg

b. 测试脚本

yolov7_pet工程data目录下存放测试脚本test_http.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.

import os
import cv2
import json
import base64
import http.client
class HttpConfig:
    '''http调用的参数配置'''
    def __init__(self, host_ip, port, url, img_base64_str):
        self.hostIP = host_ip
        self.Port = port

        self.httpMethod = "POST"
        self.requstURL = url
        self.headerdata = {
            "Content-Type": "application/json"
        }
        self.test_data = {
            "image_base64": img_base64_str
        }
        self.body = json.dumps(self.test_data)
def read_image(img_path):
    '''读取图片数据并转为base64编码的字符串'''
    img_data = cv2.imread(img_path)
    img_str = cv2.imencode('.jpg', img_data)[1].tostring()
    img_bin = base64.b64encode(img_str)
    img_base64_str = str(img_bin, encoding='utf8')
    return img_data, img_base64_str
def decode_car_bboxes(bbox_str, input_shape):
    try:
        labels = [0, 1]  # cat, dog
        bboxes = json.loads(json.loads(bbox_str)['det_result'])
        bboxes = list(filter(lambda x: int(x[5]) in labels, bboxes))
    except Exception as ex:
        print(str(ex))
        return []
    else:
        for bbox in bboxes:
            bbox[0] = int(bbox[0] * input_shape[1])
            bbox[1] = int(bbox[1] * input_shape[0])
            bbox[2] = int(bbox[2] * input_shape[1])
            bbox[3] = int(bbox[3] * input_shape[0])
        return bboxes
def draw_bboxes(img_data, bboxes):
    '''画框'''
    for bbox in bboxes:
        x1, y1, x2, y2, score, label = bbox
        color = (0, 0, 255)
        names = ['cat', 'dog']  
        score = '%.2f' % score
        label = '%s:%s' % (names[int(label)], score)
        cv2.rectangle(img_data, (x1, y1), (x2, y2), color, 2)
        cv2.putText(img_data, label, (x1, y1 - 10), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (0, 255, 0), thickness=1)
    return img_data
def test_image(img_path, ip, port, url):
    '''单张图片测试'''
    img_data, img_base64_str = read_image(img_path)
    http_config = HttpConfig(ip, port, url, img_base64_str)

    conn = http.client.HTTPConnection(host=http_config.hostIP, port=http_config.Port)
    conn.request(method=http_config.httpMethod, url=http_config.requstURL,
                body=http_config.body, headers=http_config.headerdata)

    response = conn.getresponse().read().decode()
    print('response: ', response)

    bboxes = decode_car_bboxes(response, img_data.shape)
    imt_out = draw_bboxes(img_data, bboxes)
    cv2.imwrite('./result-' + os.path.basename(img_path), imt_out)
if __name__ == "__main__":
    port = 8083
    ip = "127.0.0.1"
    url = "/v1/yolov7_pet"
    img_path = "./test.jpg"
    img_folder = './test_imgs'
    file_list = os.listdir(img_folder)
    for img_file in file_list:
        print("\n================ {} ================".format(img_file))
        img_path = os.path.join(img_folder, img_file)
        test_image(img_path, ip, port, url)

三、运行应用

yolov7_pet工程目录下执行.\bin\main.bat运行应用:

(tensorflow) PS D:\modelbox-win10-x64-1.5.3> cd D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet
(tensorflow) PS D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet> .\bin\main.bat

(tensorflow) D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet>set PATH=D:/modelbox-win10-x64-1.5.3/workspace/yolov7_pet/bin/../../../python-embed;D:/modelbox-win10-x64-1.5.3/workspace/yolov7_pet/bin/../../../modelbox-win10-x64/bin;D:/modelbox-win10-x64-1.5.3/workspace/yolov7_pet/bin/../dependence/lib;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3\envs\tensorflow;C:\Users\yanso\miniconda3\envs\tensorflow\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\usr\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Library\bin;C:\Users\yanso\miniconda3\envs\tensorflow\Scripts;C:\Users\yanso\miniconda3\envs\tensorflow\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\yanso\miniconda3\envs\tensorflow\lib\site-packages\pywin32_system32;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Library\mingw-w64\bin;C:\Users\yanso\miniconda3\Library\usr\bin;C:\Users\yanso\miniconda3\Library\bin;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\bin;C:\Users\yanso\miniconda3\condabin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin;C:\Windows\System32\HWAudioDriverLibs;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\yanso\miniconda3;C:\Users\yanso\miniconda3\Scripts;C:\Users\yanso\miniconda3\Library\bin;.;C:\Program Files\Git LFS;C:\Users\yanso\AppData\Local\Microsoft\WindowsApps;.;C:\Users\yanso\AppData\Local\Programs\Microsoft VS Code\bin 

(tensorflow) D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet>modelbox.exe -c D:/modelbox-win10-x64-1.5.3/workspace/yolov7_pet/bin/../graph/modelbox.conf 
[2024-06-10 06:42:50,922][ WARN][    iva_config.cc:143 ] update vas url failed. Fault, no vas projectid or iva endpoint
open log file D:/modelbox-win10-x64-1.5.3/workspace/yolov7_pet/bin/../hilens_data_dir/log/modelbox.log failed, No error
input dims is:1,3,640,640,
output dims is:1,25200,7,

HTTP服务启动后可以在另一个终端进行请求测试,进入yolov7_pet工程目录data文件夹中使用test_http.py脚本发起HTTP请求进行测试:

(tensorflow) PS D:\modelbox-win10-x64-1.5.3> cd D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet\data                                                        
(tensorflow) PS D:\modelbox-win10-x64-1.5.3\workspace\yolov7_pet\data> python .\test_http.py                                                                 

================ Abyssinian_1.jpg ================
.\test_http.py:33: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
  img_str = cv2.imencode('.jpg', img_data)[1].tostring()
response:  {"det_result": "[[0.554308044910431, 0.1864600658416748, 0.7089953303337098, 0.3776256084442139, 0.82369065284729, 0.0]]"}

================ saint_bernard_143.jpg ================
response:  {"det_result": "[[0.46182055473327643, 0.30239262580871584, 0.8193012714385988, 0.4969032764434815, 0.7603430151939392, 1.0]]"}

屏幕截图 2024-06-10 064427.png

四、小结

本章我们介绍了如何使用ModelBox开发一个动物目标检测的AI应用,我们只需要准备模型文件以及简单的配置即可创建一个HTTP服务。同时我们可以了解到图片标注、数据处理和模型训练方法,以及对应的推理应用逻辑。

 

点击关注,第一时间了解华为云新鲜技术~

 

与快速识别你家的猫猫狗狗,教你用ModelBox开发AI萌宠应用相似的内容:

快速识别你家的猫猫狗狗,教你用ModelBox开发AI萌宠应用

本文介绍了如何使用ModelBox开发一个动物目标检测的AI应用,从而掌握图片标注、数据处理和模型训练方法,以及对应的推理应用逻辑。

带你认识数仓的增量备份核心设计

摘要:增量备份是重要的常规备份策略,正确快速识别增量变化文件的相关信息对增量备份至关重要。 本文分享自华为云社区《其疾如风,GaussDB(DWS)增量备份核心设计》,作者: 我的橘子呢 。 1、认识增量备份 GaussDB(DWS)数仓的备份恢复工具Roach支持集群级增量备份。全量备份会将源数据

云小课|云小课带你玩转可视化分析ELB日志

阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击此处。 云日志服务支持可视化查看日志,当ELB日志接入云日志服务后,可通过SQL语句查询分析日志,将日志结果保存为多种图表,并将图表

Redis命令监控与简单分析

Redis命令监控与简单分析 前言 为了能够快速识别分析redis的命令 自己在环境上面进行了一些简单的跟踪以及脚本 这里不全是进行metrics, 细致到具体的命令分析 脚本部分-1 mkdir -p /redismonitor/ cd /redismonitor/ find . -mtime +

未来5年,只有这种产品团队才能开启上帝视角【玩转IPD】

一家企业如何在波涛汹涌的市场浪潮中站稳脚跟?一个团队如何快速识别风险发现机遇,成为行业的标杆?市场瞬息万变,如何准确地响应市场动向,紧跟用户需求?这些问题,已成为企业发展乃至生存的重要保障。尽管市场和用户对于不同的企业的要求不尽相同。但对于产品、技术为导向的企业来说,自主研发能力的强弱直接反映了企业

[转帖]USE方法

https://www.jianshu.com/p/2d05e94e15b7 USE方法全称"Utilization Saturation and Errors Method",主要用于分析系统性能问题,可以指导用户快速识别资源瓶颈以及错误的方法。正如USE方法的名字所表示的含义,USE方法主要关注

浅谈常态化压测 | 京东物流技术团队

随着业务的不断增长,支撑业务系统的压力也逐渐增加,会面临如系统越来越厚重、逻辑越来复杂、迭代节奏越来越快等繁杂的情况。我们当前并没有做到在每次变化时快速识别出性能风险,检测产品或系统的稳定性、可靠性,而且我们还在不断的投入人力成本在压测这件事情上也是不合理的,所以我们要将性能验证融入到我们日常的工作中,把压测做到常态化,做成平常的一件事。

4种API性能恶化根因分析

摘要:服务发生性能恶化时,需要投入大量人力分析性能异常根因,分析成本高,耗时长。我们提出了一种先在异常调用链内部分析候选根因,再在全局拓扑环境下对候选根因进行汇聚的二级分析方法,克服了调用链之间异常相互影响导致根因难以确定的问题,快速识别和定位恶化接口的根因。 本文分享自华为云社区《【AIOps专题

潦草手写体也能轻松识别,快速提取文字不用愁

基于文本识别(OCR)技术的成熟与应用,日常生活中的大部分“印刷体识别”需求都能被满足,替代了人工信息录入与检测等操作,大大降低输入成本。 而对于复杂的手写体识别需求,业界识别质量却参差不齐。大部分手写体存在字迹潦草,排版不固定,背景复杂,且不同的字体风格各异等问题,给手写体识别带来极大的挑战,不过

【OpenVINO™】使用OpenVINO™ C# API 部署 YOLO-World实现实时开放词汇对象检测

YOLO-World是一个融合了实时目标检测与增强现实(AR)技术的创新平台,旨在将现实世界与数字世界无缝对接。该平台以YOLO(You Only Look Once)算法为核心,实现了对视频中物体的快速准确识别,并通过AR技术将虚拟元素与真实场景相结合,为用户带来沉浸式的交互体验。在本文中,我们将...