diff --git a/tutorials/OsdSample/C++/main.cpp b/tutorials/OsdSample/C++/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0c487c68ac5d3873f55b9882cfc2f69c496b964 --- /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 0000000000000000000000000000000000000000..eebe9bc79f4a35fdd9eee02e3f539e672c2f1ed1 --- /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/main.py b/tutorials/OsdSample/python/main.py new file mode 100644 index 0000000000000000000000000000000000000000..e5476c83a507a9e6c5151917a6f250a35d3c8d05 --- /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()