When developing plugins, the existing metadata structure may not align with the actual service needs. In such cases, you can use MxpiCustomDataList and MxpiCustomData to define new data structures. MxpiCustomData contains the map data type that allows you to add a custom field by specifying a string key-value pair.
As shown in Figure 1, you can perform the following steps to create and add custom plugin metadata. A list of created custom data will be output through plugins.
1
|
std::shared_ptr<MxTools::MxpiCustomDataList> myDataList = std::make_shared<MxTools::MxpiCustomDataList>(); |
1
|
MxTools::MxpiCustomData* arrayData = myDataList->add_datavec(); |
1
|
(*arrayData->mutable_map())["myKey"] = "myValue" ; |
The key and value of a map must be strings. If the data is not strings, serialize the data to strings and then add the data.
As shown in Figure 2, the downstream plugins can access the custom plugin metadata that is transferred by the upstream plugin by following these steps. You can then process the obtained data.
1 2 |
auto metadata = mxpiMetadataManager.GetMetadataWithType("mxpi_CustomPlugin0", "MxpiCustomDataList"); auto tmpDataList = std::static_pointer_cast<MxpiCustomDataList>(metadata); |
1 2 |
auto messageData = tmpDataList->datavec(0); auto data = (*(messageData.mutable_map()))["myKey"]; // myValue |
The obtained value is a string. You need to deserialize the data and restore the data to the original data type for subsequent processing.