样例实现图像分类功能。具体流程为图像解码
->图像缩放
->模型推理
->模型后处理
。
由于上一课已经安装过python的opencv,所以这里不再做安装操作
- 安装python的opencv
pip3.7 install opencv-python -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
创建一个resnet50 文件夹。然后下载resnet50
模型
wget --no-check-certificate https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/model/Resnet50V1.5/zh/1.1/ATC_Resnet50_V1.5_from_Tensorflow_Ascend310.zip
解压ATC_Resnet50_V1.5_from_Tensorflow_Ascend310.zip 文件
unzip ATC_Resnet50_V1.5_from_Tensorflow_Ascend310.zip
aipp文件 转模型配置文件aipp_resnet50_224_224.aippconfig
aipp_op {
aipp_mode: static
input_format : YUV420SP_U8
csc_switch : true
rbuv_swap_switch : false
matrix_r0c0 : 256
matrix_r0c1 : 0
matrix_r0c2 : 359
matrix_r1c0 : 256
matrix_r1c1 : -88
matrix_r1c2 : -183
matrix_r2c0 : 256
matrix_r2c1 : 454
matrix_r2c2 : 0
input_bias_0 : 0
input_bias_1 : 128
input_bias_2 : 128
mean_chn_0 : 124
mean_chn_1 : 117
mean_chn_2 : 104
}
vi aipp_resnet50_224_224.aippconfig
然后在文件里写入上面信息
转换模型
atc --model=./resnet50_v1.5.pb --framework=3 --output=resnet50_aipp_tf --output_type=FP32 --soc_version=Ascend310 --input_shape=input_tensor:1,224,224,3 --insert_op_conf=./aipp_resnet50_224_224.aippconfig
模型转换指令,自行尝试转换模型,输出的om模型要求为resnet50_aipp_tf.om,并把转换好om模型拷贝到工程的model目录下,若转换有难度,请直接下载转好的模型。链接:https://pan.baidu.com/s/1rrMC8dYz9MxP9-whmlKJtw 提取码:g095
模型转换完成
新建作业文件夹,下载好的作业代码文件通过ftp传到对应文件夹
cd /home/HwHiAiUser/MindX
cp /home/HwHiAiUser/MindX/resnet50/resnet50_aipp_tf.om ./zuoye/model/
/home/HwHiAiUser/MindX/zuoye
- 配置环境变量
export MX_SDK_HOME=/home/HwHiAiUser/MindX/MindXSDK/mxVision
export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:/usr/local/Ascend/driver/lib64/
export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner
export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins
export PYTHONPATH=${MX_SDK_HOME}/python
- 补全main.py代码
#!/usr/bin/env python
# coding=utf-8
# Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import cv2
import json
import os
from StreamManagerApi import StreamManagerApi, MxDataInput
if __name__ == '__main__':
streamManagerApi = StreamManagerApi()
# init stream manager
ret = streamManagerApi.InitManager()
if ret != 0:
print("Failed to init Stream manager, ret=%s" % str(ret))
exit()
path=b"./test.pipeline"
ret = streamManagerApi.CreateMultipleStreamsFromFile(path)
if ret != 0:
print("Failed to create Stream, ret=%s" % str(ret))
exit()
# Construct the input of the stream
dataInput = MxDataInput()
if os.path.exists('test.jpg') != 1:
print("The test image does not exist.")
with open("test.jpg", 'rb') as f:
dataInput.data = f.read()
# Inputs data to a specified stream based on streamName.
streamName = b'classication'
inPluginId = 0
uniqueId = streamManagerApi.SendDataWithUniqueId(streamName, inPluginId, dataInput)
if uniqueId < 0:
print("Failed to send data to stream.")
exit()
# Obtain the inference result by specifying streamName and uniqueId.
infer_result = streamManagerApi.GetResultWithUniqueId(streamName, uniqueId, 3000)
if infer_result.errorCode != 0:
print("GetResultWithUniqueId error. errorCode=%d, errorMsg=%s" % (
infer_result.errorCode, infer_result.data.decode()))
exit()
results = json.loads(infer_result.data.decode())
print(results)
#可视化代码填写处
bboxes = []
#从results取数据,获取分类的className,并利用opencv把标签名标记到图片
img_path = "test.jpg"
img = cv2.imread(img_path)
bboxes = []
for bbox in results['MxpiClass']:
bboxes = { 'classId': int(bbox['classId']),
'className': bbox['className'],
'confidence': round(bbox['confidence'],9)}
img = cv2.imread(img_path)
#可视化代码填写处
#从results取数据,获取分类的className,并利用opencv把标签名标记到图片
#className = 'class:' +bboxes['className']
className = '[' +bboxes['className'] + ']'
print(className)
cv2.putText(img, className, (50,50),cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1)
# 图像,文字内容, 坐标 ,字体,大小,颜色,字体厚度
cv2.imwrite("./result.jpg", img)
# destroy streams
streamManagerApi.DestroyAllStreams()
- 运行
python3 main.py
完成
图片输出 感谢辉哥帮助
学习任重而道远 加油!