Customizing Plugin Metadata

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.

Creating Custom Plugin Metadata

Figure 1 Creation process

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. Custom a data list. The following is an example (myDataList is used as an example):
    1
    std::shared_ptr<MxTools::MxpiCustomDataList> myDataList = std::make_shared<MxTools::MxpiCustomDataList>();
    
  2. Add custom data to the data list. The following is an example: (The add_datavec function is generated by compiling the protocol buffer.)
    1
    MxTools::MxpiCustomData* arrayData = myDataList->add_datavec();
    
  3. Use mutable_map to add a map key-value pair. The following is an example: (The mutable_map function is generated by compiling the protobuf buffer.)
    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.

Reading Custom Plugin Metadata

Figure 2 Reading process

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. Obtain the metadata from the upstream plugin and convert the metadata into the MxpiCustomDataList type. The following is an example:
    1
    2
    auto metadata = mxpiMetadataManager.GetMetadataWithType("mxpi_CustomPlugin0", "MxpiCustomDataList");
    auto tmpDataList = std::static_pointer_cast<MxpiCustomDataList>(metadata);
    
  2. Read elements in the list and obtain the value based on the corresponding key. The following is an example of obtaining the value of the first element:
    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.