From 893b9b46efe4babc2bff0fbeff938f3440fd529d Mon Sep 17 00:00:00 2001 From: kongchibin Date: Mon, 5 Jul 2021 11:29:43 +0800 Subject: [PATCH 1/2] new file: OsdSample/C++/ExternalOsdInstances.txt new file: OsdSample/C++/main.cpp new file: OsdSample/pipeline/SampleOsd.pipeline new file: OsdSample/python/ExternalOsdInstances.txt new file: OsdSample/python/main.py --- .../OsdSample/C++/ExternalOsdInstances.txt | 78 ++++++++ tutorials/OsdSample/C++/main.cpp | 168 ++++++++++++++++++ .../OsdSample/pipeline/SampleOsd.pipeline | 56 ++++++ .../OsdSample/python/ExternalOsdInstances.txt | 78 ++++++++ tutorials/OsdSample/python/main.py | 78 ++++++++ 5 files changed, 458 insertions(+) create mode 100644 tutorials/OsdSample/C++/ExternalOsdInstances.txt create mode 100644 tutorials/OsdSample/C++/main.cpp create mode 100644 tutorials/OsdSample/pipeline/SampleOsd.pipeline create mode 100644 tutorials/OsdSample/python/ExternalOsdInstances.txt create mode 100644 tutorials/OsdSample/python/main.py diff --git a/tutorials/OsdSample/C++/ExternalOsdInstances.txt b/tutorials/OsdSample/C++/ExternalOsdInstances.txt new file mode 100644 index 000000000..3ce60ac44 --- /dev/null +++ b/tutorials/OsdSample/C++/ExternalOsdInstances.txt @@ -0,0 +1,78 @@ +{ +  "osdInstancesVec": [ +    { +      "osdTextVec": [ +        { +          "text": "Hello, world!", +          "x0": 150, +          "y0": 400, +          "fontFace": 1, +          "fontScale": 2.0, +          "osdParams": { +            "scalorB": 60, +            "scalorG": 20, +            "scalorR": 220, +            "thickness": 2, +            "lineType": 8 +          } +        } +      ], +      "osdRectVec": [ +        { +          "x0": 0, +          "y0": 0, +          "x1": 512, +          "y1": 512, +          "osdParams": { +            "scalorB": 42, +            "scalorG": 42, +            "scalorR": 165, +            "thickness": 5, +            "lineType": 8 +          } +        } +      ], +      "osdCircleVec": [ +        { +          "x0": 0, +          "y0": 320, +          "radius": 10, +          "osdParams": { +            "scalorB": 0, +            "scalorG": 128, +            "scalorR": 255, +            "thickness": -1, +            "lineType": 8 +          } +        }, +        { +          "x0": 512, +          "y0": 320, +          "radius": 10, +          "osdParams": { +            "scalorB": 0, +            "scalorG": 128, +            "scalorR": 255, +            "thickness": -1, +            "lineType": 8 +          } +        } +      ], +      "osdLineVec": [ +        { +          "x0": 0, +          "y0": 320, +          "x1": 512, +          "y1": 320, +          "osdParams": { +            "scalorB": 255, +            "scalorG": 128, +            "scalorR": 0, +            "thickness": 5, +            "lineType": 8 +          } +        } +      ] +    } +  ] +} \ No newline at end of file diff --git a/tutorials/OsdSample/C++/main.cpp b/tutorials/OsdSample/C++/main.cpp new file mode 100644 index 000000000..f0c487c68 --- /dev/null +++ b/tutorials/OsdSample/C++/main.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2020.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. + */ + +#include +#include +#include "MxBase/Log/Log.h" +#include "MxStream/StreamManager/MxStreamManager.h" +#include "MxTools/Proto/MxpiOSDType.pb.h" + +namespace { +APP_ERROR ReadFile(const std::string& filePath, MxStream::MxstDataInput& dataBuffer) +{ + char c[PATH_MAX + 1] = { 0x00 }; + size_t count = filePath.copy(c, PATH_MAX + 1); + if (count != filePath.length()) { + LogError << "Failed to copy file path(" << c << ")."; + return APP_ERR_COMM_FAILURE; + } + // Get the absolute path of input file + char path[PATH_MAX + 1] = { 0x00 }; + if ((strlen(c) > PATH_MAX) || (realpath(c, path) == nullptr)) { + LogError << "Failed to get image, the image path is (" << filePath << ")."; + return APP_ERR_COMM_NO_EXIST; + } + // Open file with reading mode + FILE *fp = fopen(path, "rb"); + if (fp == nullptr) { + LogError << "Failed to open file (" << path << ")."; + return APP_ERR_COMM_OPEN_FAIL; + } + // Get the length of input file + fseek(fp, 0, SEEK_END); + long fileSize = ftell(fp); + fseek(fp, 0, SEEK_SET); + // If file not empty, read it into FileInfo and return it + if (fileSize > 0) { + dataBuffer.dataSize = fileSize; + dataBuffer.dataPtr = new (std::nothrow) uint32_t[fileSize]; + if (dataBuffer.dataPtr == nullptr) { + LogError << "allocate memory with \"new uint32_t\" failed."; + return APP_ERR_COMM_FAILURE; + } + + uint32_t readRet = fread(dataBuffer.dataPtr, 1, fileSize, fp); + if (readRet <= 0) { + fclose(fp); + return APP_ERR_COMM_READ_FAIL; + } + fclose(fp); + return APP_ERR_OK; + } + fclose(fp); + return APP_ERR_COMM_FAILURE; +} + +std::string ReadFileContent(const std::string& filePath) +{ + std::ifstream file(filePath, std::ios::binary); + if (!file) { + LogError << "Invalid file. filePath(" << filePath << ")"; + return ""; + } + + file.seekg(0, std::ifstream::end); + uint32_t fileSize = file.tellg(); + file.seekg(0); + std::vector buffer; + buffer.resize(fileSize); + file.read(buffer.data(), fileSize); + file.close(); + + return std::string(buffer.data(), fileSize); +} + +std::string ReadPipelineConfig(const std::string& pipelineConfigPath) +{ + std::ifstream file(pipelineConfigPath.c_str(), std::ifstream::binary); + if (!file) { + LogError << pipelineConfigPath <<" file dose not exist."; + return ""; + } + file.seekg(0, std::ifstream::end); + uint32_t fileSize = file.tellg(); + file.seekg(0); + std::unique_ptr data(new char[fileSize]); + file.read(data.get(), fileSize); + file.close(); + std::string pipelineConfig(data.get(), fileSize); + return pipelineConfig; +} +} + +int main(int argc, char* argv[]) +{ + // read image file and build stream input + MxStream::MxstDataInput dataBuffer; + APP_ERROR ret = ReadFile("./test.jpg", dataBuffer); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Failed to read image file."; + return ret; + } + // read pipeline config file + std::string pipelineConfigPath = "../pipeline/SampleOsd.pipeline"; + std::string pipelineConfig = ReadPipelineConfig(pipelineConfigPath); + if (pipelineConfig == "") { + LogError << "Read pipeline failed."; + return APP_ERR_COMM_INIT_FAIL; + } + // init stream manager + MxStream::MxStreamManager mxStreamManager; + ret = mxStreamManager.InitManager(); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Failed to init Stream manager."; + return ret; + } + // create stream by pipeline config file + ret = mxStreamManager.CreateMultipleStreams(pipelineConfig); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Failed to create Stream."; + return ret; + } + std::string streamName = "encoder"; + + // send image into stream + ret = mxStreamManager.SendData(streamName, 0, dataBuffer); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Failed to send data to stream."; + return ret; + } + + // send osd instances protobuf. + std::string result = ReadFileContent("./ExternalOsdInstances.json"); + auto osdInstancesList = std::make_shared(); + google::protobuf::util::JsonStringToMessage(result, osdInstancesList.get()); + MxStream::MxstProtobufIn protobuf; + protobuf.key = "mxpi_parallel2serial0"; + protobuf.messagePtr = std::static_pointer_cast(osdInstancesList); + std::vector dataBufferVec; + dataBufferVec.push_back(protobuf); + // send data into stream + ret = mxStreamManager.SendProtobuf(streamName, 1, dataBufferVec); + if (ret != APP_ERR_OK) { + LogError << GetError(ret) << "Failed to send data to stream."; + return ret; + } + + sleep(2); + + // destroy streams + mxStreamManager.DestroyAllStreams(); + delete dataBuffer.dataPtr; + dataBuffer.dataPtr = nullptr; + + return 0; +} \ No newline at end of file diff --git a/tutorials/OsdSample/pipeline/SampleOsd.pipeline b/tutorials/OsdSample/pipeline/SampleOsd.pipeline new file mode 100644 index 000000000..eebe9bc79 --- /dev/null +++ b/tutorials/OsdSample/pipeline/SampleOsd.pipeline @@ -0,0 +1,56 @@ +{ +    "encoder":{ +        "stream_config":{ +            "deviceId":"0" +        }, +        "appsrc0": { +            "props": { +                "blocksize": "40960000" +            }, +            "factory": "appsrc", +            "next": "mxpi_imagedecoder0" +        }, +        "mxpi_imagedecoder0":{ +            "factory":"mxpi_imagedecoder", +            "next":"mxpi_imageresize0" +        }, +        "mxpi_imageresize0":{ +            "props":{ +                "resizeHeight":"512", +                "resizeWidth":"512" +            }, +            "factory":"mxpi_imageresize", +            "next":"mxpi_aicpuosd0:0" +        }, +        "appsrc1": { +            "props": { +                "blocksize": "40960000" +            }, +            "factory": "appsrc", +            "next":"mxpi_parallel2serial0" +        }, +        "mxpi_parallel2serial0":{ +            "factory":"mxpi_parallel2serial", +            "next":"mxpi_aicpuosd0:1" +        }, +        "mxpi_aicpuosd0":{ +            "props": { +    "dataSourceOsd":"mxpi_parallel2serial0", +    "dataSourceImage":"mxpi_imagedecoder0" +            }, +            "factory":"mxpi_opencvosd", +            "next":"mxpi_imageencoder0" +        }, +        "mxpi_imageencoder0":{ +            "factory":"mxpi_imageencoder", +            "next":"appsink0" +        }, +        "appsink0":{ +            "props": { +    "blocksize":"40960000", +                "location": "./testout.jpg" +            }, +            "factory":"filesink" +        } +    } +} \ No newline at end of file diff --git a/tutorials/OsdSample/python/ExternalOsdInstances.txt b/tutorials/OsdSample/python/ExternalOsdInstances.txt new file mode 100644 index 000000000..3ce60ac44 --- /dev/null +++ b/tutorials/OsdSample/python/ExternalOsdInstances.txt @@ -0,0 +1,78 @@ +{ +  "osdInstancesVec": [ +    { +      "osdTextVec": [ +        { +          "text": "Hello, world!", +          "x0": 150, +          "y0": 400, +          "fontFace": 1, +          "fontScale": 2.0, +          "osdParams": { +            "scalorB": 60, +            "scalorG": 20, +            "scalorR": 220, +            "thickness": 2, +            "lineType": 8 +          } +        } +      ], +      "osdRectVec": [ +        { +          "x0": 0, +          "y0": 0, +          "x1": 512, +          "y1": 512, +          "osdParams": { +            "scalorB": 42, +            "scalorG": 42, +            "scalorR": 165, +            "thickness": 5, +            "lineType": 8 +          } +        } +      ], +      "osdCircleVec": [ +        { +          "x0": 0, +          "y0": 320, +          "radius": 10, +          "osdParams": { +            "scalorB": 0, +            "scalorG": 128, +            "scalorR": 255, +            "thickness": -1, +            "lineType": 8 +          } +        }, +        { +          "x0": 512, +          "y0": 320, +          "radius": 10, +          "osdParams": { +            "scalorB": 0, +            "scalorG": 128, +            "scalorR": 255, +            "thickness": -1, +            "lineType": 8 +          } +        } +      ], +      "osdLineVec": [ +        { +          "x0": 0, +          "y0": 320, +          "x1": 512, +          "y1": 320, +          "osdParams": { +            "scalorB": 255, +            "scalorG": 128, +            "scalorR": 0, +            "thickness": 5, +            "lineType": 8 +          } +        } +      ] +    } +  ] +} \ No newline at end of file diff --git a/tutorials/OsdSample/python/main.py b/tutorials/OsdSample/python/main.py new file mode 100644 index 000000000..e5476c83a --- /dev/null +++ b/tutorials/OsdSample/python/main.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# coding=utf-8 + +""" +Copyright 2020 Huawei Technologies Co., Ltd + +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 StreamManagerApi.py +import json +import time +from StreamManagerApi import * +import MxpiOSDType_pb2 as MxpiOSDType +from google.protobuf.json_format import * + +if __name__ == '__main__': + # init stream manager + streamManagerApi = StreamManagerApi() + ret = streamManagerApi.InitManager() + if ret != 0: + print("Failed to init Stream manager, ret=%s" % str(ret)) + exit() + + # create streams by pipeline config file + with open("../pipeline/SampleOsd.pipeline", 'rb') as f: + pipelineStr = f.read() + ret = streamManagerApi.CreateMultipleStreams(pipelineStr) + if ret != 0: + print("Failed to create Stream, ret=%s" % str(ret)) + exit() + + # Construct the input of the stream + dataInput = MxDataInput() + with open("test.jpg", 'rb') as f: + dataInput.data = f.read() + + # Send image. + streamName = b'encoder' + inPluginId = 0 + ret = streamManagerApi.SendData(streamName, inPluginId, dataInput) + if ret < 0: + print("Failed to send data to stream.") + exit() + + # Send osd instances protobuf. + with open("ExternalOsdInstances.json", "r") as f: + messageJson = json.load(f) + print(messageJson) + inPluginId = 1 + osdInstancesList = MxpiOSDType.MxpiOsdInstancesList() + osdInstancesList = ParseDict(messageJson, osdInstancesList) + + protobufVec = InProtobufVector() + protobuf = MxProtobufIn() + protobuf.key = b'mxpi_parallel2serial0' + protobuf.type = b'MxTools.MxpiOsdInstancesList' + protobuf.protobuf = osdInstancesList.SerializeToString() + protobufVec.push_back(protobuf) + ret = streamManagerApi.SendProtobuf(streamName, inPluginId, protobufVec) + if ret < 0: + print("Failed to send protobuf to stream.") + exit() + + time.sleep(2) + + # destroy streams + streamManagerApi.DestroyAllStreams() -- Gitee From 1a576b29f16732333b0bc637445e09dbf53abfa3 Mon Sep 17 00:00:00 2001 From: kongchibin Date: Mon, 5 Jul 2021 14:23:36 +0800 Subject: [PATCH 2/2] deleted: OsdSample/C++/ExternalOsdInstances.txt deleted: OsdSample/python/ExternalOsdInstances.txt OsdSample/C++/ExternalOsdInstances.json OsdSample/README.md OsdSample/python/ExternalOsdInstances.json --- .../OsdSample/C++/ExternalOsdInstances.txt | 78 ------------------- .../OsdSample/python/ExternalOsdInstances.txt | 78 ------------------- 2 files changed, 156 deletions(-) delete mode 100644 tutorials/OsdSample/C++/ExternalOsdInstances.txt delete mode 100644 tutorials/OsdSample/python/ExternalOsdInstances.txt diff --git a/tutorials/OsdSample/C++/ExternalOsdInstances.txt b/tutorials/OsdSample/C++/ExternalOsdInstances.txt deleted file mode 100644 index 3ce60ac44..000000000 --- a/tutorials/OsdSample/C++/ExternalOsdInstances.txt +++ /dev/null @@ -1,78 +0,0 @@ -{ -  "osdInstancesVec": [ -    { -      "osdTextVec": [ -        { -          "text": "Hello, world!", -          "x0": 150, -          "y0": 400, -          "fontFace": 1, -          "fontScale": 2.0, -          "osdParams": { -            "scalorB": 60, -            "scalorG": 20, -            "scalorR": 220, -            "thickness": 2, -            "lineType": 8 -          } -        } -      ], -      "osdRectVec": [ -        { -          "x0": 0, -          "y0": 0, -          "x1": 512, -          "y1": 512, -          "osdParams": { -            "scalorB": 42, -            "scalorG": 42, -            "scalorR": 165, -            "thickness": 5, -            "lineType": 8 -          } -        } -      ], -      "osdCircleVec": [ -        { -          "x0": 0, -          "y0": 320, -          "radius": 10, -          "osdParams": { -            "scalorB": 0, -            "scalorG": 128, -            "scalorR": 255, -            "thickness": -1, -            "lineType": 8 -          } -        }, -        { -          "x0": 512, -          "y0": 320, -          "radius": 10, -          "osdParams": { -            "scalorB": 0, -            "scalorG": 128, -            "scalorR": 255, -            "thickness": -1, -            "lineType": 8 -          } -        } -      ], -      "osdLineVec": [ -        { -          "x0": 0, -          "y0": 320, -          "x1": 512, -          "y1": 320, -          "osdParams": { -            "scalorB": 255, -            "scalorG": 128, -            "scalorR": 0, -            "thickness": 5, -            "lineType": 8 -          } -        } -      ] -    } -  ] -} \ No newline at end of file diff --git a/tutorials/OsdSample/python/ExternalOsdInstances.txt b/tutorials/OsdSample/python/ExternalOsdInstances.txt deleted file mode 100644 index 3ce60ac44..000000000 --- a/tutorials/OsdSample/python/ExternalOsdInstances.txt +++ /dev/null @@ -1,78 +0,0 @@ -{ -  "osdInstancesVec": [ -    { -      "osdTextVec": [ -        { -          "text": "Hello, world!", -          "x0": 150, -          "y0": 400, -          "fontFace": 1, -          "fontScale": 2.0, -          "osdParams": { -            "scalorB": 60, -            "scalorG": 20, -            "scalorR": 220, -            "thickness": 2, -            "lineType": 8 -          } -        } -      ], -      "osdRectVec": [ -        { -          "x0": 0, -          "y0": 0, -          "x1": 512, -          "y1": 512, -          "osdParams": { -            "scalorB": 42, -            "scalorG": 42, -            "scalorR": 165, -            "thickness": 5, -            "lineType": 8 -          } -        } -      ], -      "osdCircleVec": [ -        { -          "x0": 0, -          "y0": 320, -          "radius": 10, -          "osdParams": { -            "scalorB": 0, -            "scalorG": 128, -            "scalorR": 255, -            "thickness": -1, -            "lineType": 8 -          } -        }, -        { -          "x0": 512, -          "y0": 320, -          "radius": 10, -          "osdParams": { -            "scalorB": 0, -            "scalorG": 128, -            "scalorR": 255, -            "thickness": -1, -            "lineType": 8 -          } -        } -      ], -      "osdLineVec": [ -        { -          "x0": 0, -          "y0": 320, -          "x1": 512, -          "y1": 320, -          "osdParams": { -            "scalorB": 255, -            "scalorG": 128, -            "scalorR": 0, -            "thickness": 5, -            "lineType": 8 -          } -        } -      ] -    } -  ] -} \ No newline at end of file -- Gitee