4.5 图形运算加速模块

BMCV提供了一套基于算丰AI芯片优化的机器视觉库,目前可以完成色彩空间转换、尺度变换、仿射变换、投射变换、线性变换、画框、JPEG编码、BASE64编码、NMS、排序、特征匹配等操作。关于BMCV模块详细内容请阅读《BMCV用户开发手册》

​Python接口的实现请参考《SAIL用户开发手册》

​BMCV API均是围绕bm_image来进行的。一个bm_image结构对应于一张图片。

4.5.1 C语言编程接口

bm_image结构体

struct bm_image {
    int width;
    int height;
    bm_image_format_ext image_format;
    bm_data_format_ext data_type;
    bm_image_private* image_private;
};

bm_image 结构成员包括图片的宽高,图片格式,图片数据格式,以及该结构的私有数据。

关于bm_image初始化,我们不建议用户直接填充bm_image结构使用,而是通过以下API来创建/销毁一个bm_image结构

图片格式 image_format枚举类型

typedef enum bm_image_format_ext_{
    FORMAT_YUV420P,
    FORMAT_NV12,
    FORMAT_NV21,
    FORMAT_NV16,
    FORMAT_NV61,
    FORMAT_RGB_PLANAR,
    FORMAT_BGR_PLANAR,
    FORMAT_RGB_PACKED,
    FORMAT_BGR_PACKED,
    FORMAT_GRAY,
    FORMAT_COMPRESSED
}bm_image_format_ext;

数据存储格式枚举

typedef enum bm_image_data_format_ext_{
    DATA_TYPE_EXT_FLOAT32,
    DATA_TYPE_EXT_1N_BYTE,
    DATA_TYPE_EXT_4N_BYTE,
    DATA_TYPE_EXT_1N_BYTE_SIGNED,
    DATA_TYPE_EXT_4N_BYTE_SIGNED,
}bm_image_data_format_ext;

​ 关于bm_image初始化,我们不建议用户直接填充bm_image结构使用,而是通过以下API来创建/销毁一个bm_image结构

  • bm_image_create_batch

    创建物理内存连续的多个bm image。

 /*
 * @param [in]     handle       handle of low level device             
 * @param [in]     img_h        image height                 
 * @param [in]     img_w        image width
 * @param [in]     img_format   format of image: BGR or YUV
 * @param [in]     data_type    data type of image: INT8 or FP32 
 * @param [out]    image        pointer of bm image object
 * @param [in]     batch_num    batch size
 */
 static inline bool bm_image_create_batch (bm_handle_t              handle,  
                                          int                      img_h,  
                                        int                      img_w,    
                                          bm_image_format_ext      img_format,
                                        bm_image_data_format_ext data_type,  
                                          bm_image                 *image, 
                                          int                      batch_num)
  • bm_image_destroy_batch

    释放物理内存连续的多个bm image。要和bm_image_create_batch接口成对使用。

/*
* @param [in]     image        pointer of bm image object
* @param [in]     batch_num    batch size
*/
static inline bool bm_image_destroy_batch (bm_image *image, int batch_num)
  • bm_image_alloc_contiguous_mem

    为多个 image 分配连续的内存

bm_status_t bm_image_alloc_contiguous_mem(
                int image_num,
                bm_image *images,
                int bmcv_image_usage
);
  • bm_image_free_contiguous_mem

    释放通过bm_image_alloc_contiguous_mem申请的内存

bm_status_t bm_image_free_contiguous_mem(
                int image_num,
                bm_image *images
        );
  • bmcv_image_vpp_convert

    bm1684上有专门的视频后处理硬件,满足一定条件下可以一次实现csc + crop + resize功能,速度比TPU更快。

bmcv_image_vpp_convert(
bm_handle_t           handle,
      int                   output_num,
      bm_image              input,
      bm_image *            output,
      bmcv_rect_t *         crop_rect,
      bmcv_resize_algorithm algorithm = BMCV_INTER_LINEAR);

该API将输入图像格式转化为输出图像格式,并支持crop + resize功能, 支持从1张输入中crop多张输出并resize到输出图片大小。

  • bmcv_convert_to

    实现图像像素线性变化,具体数据关系可用公式表示

bm_status_t bmcv_convert_to (bm_handle_t handle,int input_num,
                        bmcv_convert_to_attr convert_to_attr, 
                        bm_image* input, bm_image* output)

结构体bmcv_convert_to_attr_s

typedef struct bmcv_convert_to_attr_s {
    float alpha_0;                 
    float beta_0;  
    float alpha_1;
    float beta_1;
    float alpha_2;
    float beta_2;
} bmcv_convert_to_attr;

结构体描述了三通道中的alpha和beta。实际要根据推理的输入数据是几通道来进行参数配置。

4.5.2 Python语言编程接口

本章节只介绍了用例py_ffmpeg_bmcv_sail中用的的接口函数

更多接口定义请查阅《SAIL用户开发手册》

  • init

def __init__(handle): 
""" Constructor.
Parameters 
---------
handle : sail.Handle Handle instance 
"""
  • tensor_to_bm_image

def tensor_to_bm_image(tensor): 
""" Convert tensor to image.
Parameters 
---------
tensor : sail.Tensor Tensor instance
Returns 
------
image : sail.BMImage BMImage instance 
"""
  • convert_to

def convert_to(input, alpha_beta): 
""" Applies a linear transformation to an image.
Parameters
---------
input : sail.BMImage Input image 
alpha_beta: tuple (a0, b0), (a1, b1), (a2, b2) factors
Returns 
---------
output : sail.BMImage Output image 
"""
  • vpp_resize

def vpp_resize(input, resize_w, resize_h): 
""" Resize an image with interpolation of INTER_NEAREST using vpp.
Parameters 
---------
input : sail.BMImage Input image 
resize_w : int Target width 
resize_h : int Target height
Returns 
---------
output : sail.BMImage Output image 
"""

最后更新于