diff --git a/tutorials/ImageDetectionSample/python/README.md b/tutorials/ImageDetectionSample/python/README.md index 4dddd4a0b881562fef94bdcef435135110c8276a..6601c5028827e4788928ea2641af55460ae1230e 100644 --- a/tutorials/ImageDetectionSample/python/README.md +++ b/tutorials/ImageDetectionSample/python/README.md @@ -4,8 +4,7 @@ 提供的demo,实现图像检测样例运行并且输出检测结果写入图片result.jpg ### 准备工作 -样例获取链接(https://gitee.com/zhangdwe/mindxsdk-referenceapps/tree/master/tutorials/ImageDetectionSample/python) -将样例目录python 从mxsdkreferenceapps/tutorials/ImageDetectionSample文件夹下 移动到${SDK安装路径}/mxVision/samples/mxVision/python/路径下 +将样例目录python 从mxsdkreferenceapps/utorials/ImageDetectionSample文件夹下 移动到${SDK安装路径}/mxVision/samples/mxVision/python/路径下 可以使用mv 命令 进入到移动后的工程路径下 @@ -18,7 +17,7 @@ bash model_conversion.sh ``` ### pipeline 准备 -将main.py 文件中 mxpi_modelinfer0插件中的labelPath路径中的${SDK安装路径} 替换为自己的SDK安装路径 +将main.py 文件中 mxpi_objectpostprocessor0插件中的postProcessLibPath路径中的${SDK安装路径} 替换为自己的SDK安装路径 ### 配置环境变量 将${SDK安装路径}替换为自己的SDK安装路径; 将${MX_SDK_HOME}替换成对应路径 diff --git a/tutorials/ImageDetectionSample/python/main.py b/tutorials/ImageDetectionSample/python/main.py index 5989c075551a78ecfad00e1647dae8185449e9ab..40c4f3aeb9031a3c5982a6c3cf1a654a44a765d8 100644 --- a/tutorials/ImageDetectionSample/python/main.py +++ b/tutorials/ImageDetectionSample/python/main.py @@ -20,7 +20,9 @@ limitations under the License. import json import os import cv2 -from StreamManagerApi import StreamManagerApi, MxDataInput + +import MxpiDataType_pb2 as MxpiDataType +from StreamManagerApi import StreamManagerApi, MxDataInput, StringVector if __name__ == '__main__': streamManagerApi = StreamManagerApi() @@ -36,41 +38,46 @@ if __name__ == '__main__': "stream_config": { "deviceId": "0" }, + "appsrc0": { + "props": { + "blocksize": "409600" + }, + "factory": "appsrc", + "next": "mxpi_imagedecoder0" + }, "mxpi_imagedecoder0": { + "props": { + "deviceId": "0" + }, "factory": "mxpi_imagedecoder", "next": "mxpi_imageresize0" }, "mxpi_imageresize0": { - "factory": "mxpi_imageresize", - "next": "mxpi_modelinfer0" - }, - "mxpi_modelinfer0": { "props": { - "modelPath": "models/yolov3_tf_bs1_fp16.om", - "postProcessConfigPath": "models/yolov3_tf_bs1_fp16.cfg", - "labelPath": "models/coco.names", - "postProcessLibPath": "libMpYOLOv3PostProcessor.so" + "dataSource": "mxpi_imagedecoder0", + "resizeHeight": "416", + "resizeWidth": "416" }, - "factory": "mxpi_modelinfer", - "next": "mxpi_imagecrop0" - }, - "mxpi_imagecrop0": { - "factory": "mxpi_imagecrop", - "next": "mxpi_dataserialize0" + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" }, - "mxpi_dataserialize0": { + "mxpi_tensorinfer0": { "props": { - "outputDataKeys": "mxpi_modelinfer0" + "dataSource": "mxpi_imageresize0", + "modelPath": "models/yolov3_tf_bs1_fp16.om" }, - "factory": "mxpi_dataserialize", - "next": "appsink0" + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" }, - "appsrc0": { + "mxpi_objectpostprocessor0": { "props": { - "blocksize": "409600" + "dataSource": "mxpi_tensorinfer0", + "postProcessConfigPath": "models/yolov3_tf_bs1_fp16.cfg", + "labelPath": "models/coco.names", + "postProcessLibPath": "${SDK安装路径}/lib/modelpostprocessors/libyolov3postprocess.so" }, - "factory": "appsrc", - "next": "mxpi_imagedecoder0" + "factory": "mxpi_objectpostprocessor", + "next": "appsink0" }, "appsink0": { "props": { @@ -98,31 +105,43 @@ if __name__ == '__main__': # Inputs data to a specified stream based on streamName. streamName = b'detection' inPluginId = 0 - uniqueId = streamManagerApi.SendDataWithUniqueId(streamName, inPluginId, dataInput) + + uniqueId = streamManagerApi.SendData(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())) + key = b"mxpi_objectpostprocessor0" + keyVec = StringVector() + keyVec.push_back(key) + + infer_result = streamManagerApi.GetProtobuf(streamName, 0, keyVec) + + if infer_result.size() == 0: + print("infer_result is null") exit() + if infer_result[0].errorCode != 0: + print("GetProtobuf error. errorCode=%d, errorMsg=%s" % ( + infer_result[0].errorCode, infer_result[0].data.decode())) + exit() + + # mxpi_objectpostprocessor0模型后处理插件输出信息 + objectList = MxpiDataType.MxpiObjectList() + objectList.ParseFromString(infer_result[0].messageBuf) + # print the infer result - print(infer_result.data.decode()) + print(objectList) + results = objectList.objectVec[0] - results = json.loads(infer_result.data.decode()) bboxes = [] - for bbox in results['MxpiObject']: - bboxes = {'x0': int(bbox['x0']), - 'x1': int(bbox['x1']), - 'y0': int(bbox['y0']), - 'y1': int(bbox['y1']), - 'confidence': round(bbox['classVec'][0]['confidence'], 4), - 'text': bbox['classVec'][0]['className']} + bboxes = {'x0': int(results.x0), + 'x1': int(results.x1), + 'y0': int(results.y0), + 'y1': int(results.y1), + 'confidence': round(results.classVec[0].confidence, 4), + 'text': results.classVec[0].className} img_path = "test.jpg" img = cv2.imread(img_path) text = "{}{}".format(str(bboxes['confidence']), " ")