42. How to convert NvDsInferTensorMeta output data to numpy ndarray
1.Get NvDsInferTensorMeta from user meta
tensor_meta = pyds.NvDsInferTensorMeta.cast(user_meta.user_meta_data)
# Boxes in the tensor meta should be in network resolution which is
# found in tensor_meta.network_info. Use this info to scale boxes to
# the input frame resolution.
layers_info = []
for i in range(tensor_meta.num_output_layers):
layer = pyds.get_nvds_LayerInfo(tensor_meta, i)
layers_info.append(layer)
2.Find the specified output layer
def layer_finder(output_layer_info, name):
""" Return the layer contained in output_layer_info which corresponds
to the given name.
"""
for layer in output_layer_info:
# dataType == 0 <=> dataType == FLOAT
if layer.dataType == 0 and layer.layerName == name:
return layer
return None
3.Convert the tensor buffer of the output layer to numpy.ndarray
def layer_tensor_to_ndarray(layer: pyds.NvDsInferLayerInfo) -> np.ndarray:
import ctypes
if layer.dataType == pyds.NvDsInferDataType.FLOAT:
# print(f"int_addr {type(pyds.get_ptr(layer.buffer))}") <class 'int'>
addr = pyds.get_ptr(layer.buffer)
if addr == 0:
print("Buffer address is 0")
return None
# print(f"addr {type(addr)}") <class 'ctypes.c_int'>
data_ptr = ctypes.cast(addr, ctypes.POINTER(ctypes.c_float))
num_dims = layer.inferDims.numDims
shape = []
for i in range(num_dims):
shape.append(layer.inferDims.d[i])
# print(f"{shape}")
layer_array = np.ctypeslib.as_array(data_ptr, shape=shape)
# print(f"layer_array {type(layer_array)}")
layer_ny = np.frombuffer(layer_array, dtype=np.float32)
# print(f"boxes {type(layer_ny)}")
return layer_ny
return None