_T

_T

  _T("")是一个宏,定义于tchar.h下。 [1]   #define __T(x) L ## x  #define _T(x) __T(x)  他的作用是让你的程序支持Unicode编码  因为Windows使用两种 字符集ANSI和UNICODE,  前者就是通常使用的单字节方式,  但这种方式处理象中文这样的双字节字符不方便,  容易出现半个汉字的情况。  而后者是双字节方式,方便处理双字节字符。  Windows NT的所有与字符有关的函数都提供两种方式的版本,而Windows 9x只支持ANSI方式。  如果你编译一个程序为ANSI方式,  _T实际不起任何作用。  而如果编译一个程序为UNICODE方式,则编译器会把"Hello"字符串以UNICODE方式保存。_T和_L的区别在于,_L不管你是以什么方式编译,一律以UNICODE方式保存。  LPSTR:32bit指针指向一个字符串,每个字符占1字节  LPCSTR:32-bit指针指向一个常字符串,每个字符占1字节  LPCTSTR:32-bit指针指向一个常字符串,每字符可能占1字节或2字节,取决于Unicode是否定义  LPTSTR:32-bit指针每字符可能占1字节或2字节,取决于Unicode是否定义  L是表示字符串资源为Unicode的。  比如   wchar_t  Str[] = L"Hello World!";  这个就是双子节存储字符了。  _T是一个适配的宏~  当  #ifdef _UNICODE的时候  _T就是L  没有#ifdef _UNICODE的时候  _T就是ANSI的。  比如  LPTSTR lpStr = new TCHAR[32];  TCHAR* szBuf = _T("Hello");  以上两句使得无论是在UNICODE编译条件下都是正确编译的。  而且MS推荐你使用相匹配的字符串函数。  比如处理LPTSTR或者LPCTSTR 的时候,不要用strlen ,而是要用_tcslen  否则在UNICODE的编译条件下,strlen不能处理 wchar_t*的字符串。  T是非常有意思的一个符号(TCHAR、LPCTSTR、LPTSTR、_T()、_TEXT()...),它表示使用一种中间类型,既不明确表示使用 MBCS,也不明确表示使用 UNICODE。那到底使用哪种字符集?编译的时候才决定

以下是NCNN头文件:c_api.h。/* Copyright 2020 Tencent * SPDX-License-Identifier: BSD-3-Clause */ #ifndef NCNN_C_API_H #define NCNN_C_API_H #include "platform.h" #if NCNN_C_API #include <stddef.h> #ifdef __cplusplus extern "C" { #endif NCNN_EXPORT const char* ncnn_version(void); /* allocator api */ typedef struct __ncnn_allocator_t* ncnn_allocator_t; struct NCNN_EXPORT __ncnn_allocator_t { void* pthis; void* (*fast_malloc)(ncnn_allocator_t allocator, size_t size); void (*fast_free)(ncnn_allocator_t allocator, void* ptr); }; NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_pool_allocator(void); NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator(void); NCNN_EXPORT void ncnn_allocator_destroy(ncnn_allocator_t allocator); /* option api */ typedef struct __ncnn_option_t* ncnn_option_t; NCNN_EXPORT ncnn_option_t ncnn_option_create(void); NCNN_EXPORT void ncnn_option_destroy(ncnn_option_t opt); NCNN_EXPORT int ncnn_option_get_num_threads(const ncnn_option_t opt); NCNN_EXPORT void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads); NCNN_EXPORT int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt); NCNN_EXPORT void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator); NCNN_EXPORT void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator); NCNN_EXPORT void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator); NCNN_EXPORT int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt); NCNN_EXPORT void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute); /* mat api */ typedef struct __ncnn_mat_t* ncnn_mat_t; NCNN_EXPORT ncnn_mat_t ncnn_mat_create(void); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d_elem(int w, int h, int d, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d_elem(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); NCNN_EXPORT void ncnn_mat_destroy(ncnn_mat_t mat); NCNN_EXPORT void ncnn_mat_fill_float(ncnn_mat_t mat, float v); NCNN_EXPORT ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator); NCNN_EXPORT int ncnn_mat_get_dims(const ncnn_mat_t mat); NCNN_EXPORT int ncnn_mat_get_w(const ncnn_mat_t mat); NCNN_EXPORT int ncnn_mat_get_h(const ncnn_mat_t mat); NCNN_EXPORT int ncnn_mat_get_d(const ncnn_mat_t mat); NCNN_EXPORT int ncnn_mat_get_c(const ncnn_mat_t mat); NCNN_EXPORT size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat); NCNN_EXPORT int ncnn_mat_get_elempack(const ncnn_mat_t mat); NCNN_EXPORT size_t ncnn_mat_get_cstep(const ncnn_mat_t mat); NCNN_EXPORT void* ncnn_mat_get_data(const ncnn_mat_t mat); NCNN_EXPORT void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c); #if NCNN_PIXEL /* mat pixel api */ #define NCNN_MAT_PIXEL_RGB 1 #define NCNN_MAT_PIXEL_BGR 2 #define NCNN_MAT_PIXEL_GRAY 3 #define NCNN_MAT_PIXEL_RGBA 4 #define NCNN_MAT_PIXEL_BGRA 5 #define NCNN_MAT_PIXEL_X2Y(X, Y) (X | (Y << 16)) NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, ncnn_allocator_t allocator); NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, ncnn_allocator_t allocator); NCNN_EXPORT void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride); NCNN_EXPORT void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride); #endif /* NCNN_PIXEL */ NCNN_EXPORT void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals); NCNN_EXPORT void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt); NCNN_EXPORT void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt); /* blob api */ typedef struct __ncnn_blob_t* ncnn_blob_t; #if NCNN_STRING NCNN_EXPORT const char* ncnn_blob_get_name(const ncnn_blob_t blob); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_blob_get_producer(const ncnn_blob_t blob); NCNN_EXPORT int ncnn_blob_get_consumer(const ncnn_blob_t blob); NCNN_EXPORT void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c); /* paramdict api */ typedef struct __ncnn_paramdict_t* ncnn_paramdict_t; NCNN_EXPORT ncnn_paramdict_t ncnn_paramdict_create(void); NCNN_EXPORT void ncnn_paramdict_destroy(ncnn_paramdict_t pd); NCNN_EXPORT int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id); NCNN_EXPORT int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def); NCNN_EXPORT float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def); NCNN_EXPORT ncnn_mat_t ncnn_paramdict_get_array(const ncnn_paramdict_t pd, int id, const ncnn_mat_t def); NCNN_EXPORT void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i); NCNN_EXPORT void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f); NCNN_EXPORT void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t v); /* datareader api */ typedef struct __ncnn_datareader_t* ncnn_datareader_t; struct NCNN_EXPORT __ncnn_datareader_t { void* pthis; #if NCNN_STRING int (*scan)(ncnn_datareader_t dr, const char* format, void* p); #endif /* NCNN_STRING */ size_t (*read)(ncnn_datareader_t dr, void* buf, size_t size); }; NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create(void); #if NCNN_STDIO NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp); #endif /* NCNN_STDIO */ NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem); NCNN_EXPORT void ncnn_datareader_destroy(ncnn_datareader_t dr); /* modelbin api */ typedef struct __ncnn_modelbin_t* ncnn_modelbin_t; struct NCNN_EXPORT __ncnn_modelbin_t { void* pthis; ncnn_mat_t (*load_1d)(const ncnn_modelbin_t mb, int w, int type); ncnn_mat_t (*load_2d)(const ncnn_modelbin_t mb, int w, int h, int type); ncnn_mat_t (*load_3d)(const ncnn_modelbin_t mb, int w, int h, int c, int type); }; NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr); NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n); NCNN_EXPORT void ncnn_modelbin_destroy(ncnn_modelbin_t mb); /* layer api */ typedef struct __ncnn_layer_t* ncnn_layer_t; struct NCNN_EXPORT __ncnn_layer_t { void* pthis; int (*load_param)(ncnn_layer_t layer, const ncnn_paramdict_t pd); int (*load_model)(ncnn_layer_t layer, const ncnn_modelbin_t mb); int (*create_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt); int (*destroy_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt); int (*forward_1)(const ncnn_layer_t layer, const ncnn_mat_t bottom_blob, ncnn_mat_t* top_blob, const ncnn_option_t opt); int (*forward_n)(const ncnn_layer_t layer, const ncnn_mat_t* bottom_blobs, int n, ncnn_mat_t* top_blobs, int n2, const ncnn_option_t opt); int (*forward_inplace_1)(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt); int (*forward_inplace_n)(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt); }; NCNN_EXPORT ncnn_layer_t ncnn_layer_create(void); NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex); #if NCNN_STRING NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_type(const char* type); NCNN_EXPORT int ncnn_layer_type_to_index(const char* type); #endif /* NCNN_STRING */ NCNN_EXPORT void ncnn_layer_destroy(ncnn_layer_t layer); #if NCNN_STRING NCNN_EXPORT const char* ncnn_layer_get_name(const ncnn_layer_t layer); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_layer_get_typeindex(const ncnn_layer_t layer); #if NCNN_STRING NCNN_EXPORT const char* ncnn_layer_get_type(const ncnn_layer_t layer); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_support_inplace(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_support_packing(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer); NCNN_EXPORT void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable); NCNN_EXPORT void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable); NCNN_EXPORT void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable); NCNN_EXPORT void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable); NCNN_EXPORT void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable); NCNN_EXPORT void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable); NCNN_EXPORT int ncnn_layer_get_bottom_count(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i); NCNN_EXPORT int ncnn_layer_get_top_count(const ncnn_layer_t layer); NCNN_EXPORT int ncnn_layer_get_top(const ncnn_layer_t layer, int i); NCNN_EXPORT void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c); NCNN_EXPORT void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c); /* layer factory function */ typedef ncnn_layer_t (*ncnn_layer_creator_t)(void* userdata); typedef void (*ncnn_layer_destroyer_t)(ncnn_layer_t layer, void* userdata); typedef struct __ncnn_net_custom_layer_factory_t* ncnn_net_custom_layer_factory_t; struct __ncnn_net_custom_layer_factory_t { ncnn_layer_creator_t creator; ncnn_layer_destroyer_t destroyer; void* userdata; ncnn_net_custom_layer_factory_t next; }; /* net api */ typedef struct __ncnn_net_t* ncnn_net_t; struct __ncnn_net_t { void* pthis; ncnn_net_custom_layer_factory_t custom_layer_factory; }; NCNN_EXPORT ncnn_net_t ncnn_net_create(void); NCNN_EXPORT void ncnn_net_destroy(ncnn_net_t net); NCNN_EXPORT ncnn_option_t ncnn_net_get_option(ncnn_net_t net); NCNN_EXPORT void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt); #if NCNN_VULKAN NCNN_EXPORT void ncnn_net_set_vulkan_device(ncnn_net_t net, int device_index); #endif #if NCNN_STRING NCNN_EXPORT void ncnn_net_register_custom_layer_by_type(ncnn_net_t net, const char* type, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata); #endif /* NCNN_STRING */ NCNN_EXPORT void ncnn_net_register_custom_layer_by_typeindex(ncnn_net_t net, int typeindex, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata); #if NCNN_STDIO #if NCNN_STRING NCNN_EXPORT int ncnn_net_load_param(ncnn_net_t net, const char* path); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_net_load_param_bin(ncnn_net_t net, const char* path); NCNN_EXPORT int ncnn_net_load_model(ncnn_net_t net, const char* path); #endif /* NCNN_STDIO */ #if NCNN_STDIO #if NCNN_STRING NCNN_EXPORT int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem); #endif /* NCNN_STRING */ #endif /* NCNN_STDIO */ NCNN_EXPORT int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem); NCNN_EXPORT int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem); #if NCNN_STRING NCNN_EXPORT int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr); NCNN_EXPORT int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr); NCNN_EXPORT void ncnn_net_clear(ncnn_net_t net); NCNN_EXPORT int ncnn_net_get_input_count(const ncnn_net_t net); NCNN_EXPORT int ncnn_net_get_output_count(const ncnn_net_t net); #if NCNN_STRING NCNN_EXPORT const char* ncnn_net_get_input_name(const ncnn_net_t net, int i); NCNN_EXPORT const char* ncnn_net_get_output_name(const ncnn_net_t net, int i); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_net_get_input_index(const ncnn_net_t net, int i); NCNN_EXPORT int ncnn_net_get_output_index(const ncnn_net_t net, int i); /* extractor api */ typedef struct __ncnn_extractor_t* ncnn_extractor_t; NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); NCNN_EXPORT void ncnn_extractor_destroy(ncnn_extractor_t ex); NCNN_EXPORT void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt); #if NCNN_STRING NCNN_EXPORT int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat); NCNN_EXPORT int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat); #endif /* NCNN_STRING */ NCNN_EXPORT int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat); NCNN_EXPORT int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat); /* mat process api */ #define NCNN_BORDER_CONSTANT 0 #define NCNN_BORDER_REPLICATE 1 #define NCNN_BORDER_REFLECT 2 #define NCNN_BORDER_TRANSPARENT -233 NCNN_EXPORT void ncnn_copy_make_border(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int type, float v, const ncnn_option_t opt); NCNN_EXPORT void ncnn_copy_make_border_3d(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int front, int behind, int type, float v, const ncnn_option_t opt); NCNN_EXPORT void ncnn_copy_cut_border(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, const ncnn_option_t opt); NCNN_EXPORT void ncnn_copy_cut_border_3d(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int front, int behind, const ncnn_option_t opt); #if NCNN_PIXEL_DRAWING /* mat pixel drawing api*/ NCNN_EXPORT void ncnn_draw_rectangle_c1(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_rectangle_c2(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_rectangle_c3(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_rectangle_c4(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_text_c1(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color); NCNN_EXPORT void ncnn_draw_text_c2(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color); NCNN_EXPORT void ncnn_draw_text_c3(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color); NCNN_EXPORT void ncnn_draw_text_c4(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color); NCNN_EXPORT void ncnn_draw_circle_c1(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_circle_c2(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_circle_c3(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_circle_c4(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_line_c1(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_line_c2(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_line_c3(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness); NCNN_EXPORT void ncnn_draw_line_c4(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness); #endif /* NCNN_PIXEL_DRAWING */ #ifdef __cplusplus } /* extern "C" */ #endif #endif /* NCNN_C_API */ #endif /* NCNN_C_API_H */
12-17
<think>根据提供的头文件,ncnn的C API中创建extractor的函数是`ncnn_extractor_create`,而不是`ncnn_net_create_extractor`。因此,我们需要修改之前代码中关于extractor创建的部分。 在头文件中,extractor的创建函数声明如下: ```c NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); ``` 因此,在Lua中,我们应该使用`ncnn_extractor_create`来创建extractor。 修改点: 1. 在FFI的cdef中,确保有`ncnn_extractor_create`的声明。 2. 在封装中,将创建extractor的函数改为`ncnn_extractor_create`。 另外,头文件还提供了`ncnn_extractor_destroy`用于销毁extractor。 我们还需要注意,在头文件中,extractor的输入和提取函数有按名称和按索引两种版本。我们通常使用按名称的版本(如果模型中有输入和输出的名称)。在之前的代码中,我们使用了按名称的版本,所以需要确保在cdef中声明了: ```c #if NCNN_STRING NCNN_EXPORT int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat); NCNN_EXPORT int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat); #endif /* NCNN_STRING */ ``` 由于我们使用了名称(如"data"和"output"),所以需要确保在编译ncnn时启用了字符串支持(即定义了`NCNN_STRING`)。 现在,修改Lua代码: 在FFI的cdef部分,我们已经有了`ncnn_extractor_create`的声明(根据头文件)。但是,在之前的代码中,我们错误地使用了`ncnn_net_create_extractor`,现在改为`ncnn_extractor_create`。 修改封装部分: ```lua ncnn_wrapper.Net.create_extractor = function(net) local ex = ncnn.ncnn_extractor_create(net) if ex == ffi.NULL or ex == nil then error("创建提取器失败") end return ffi.gc(ex, ncnn.ncnn_extractor_destroy) end ``` 同时,在FFI cdef中,我们需要确保有`ncnn_extractor_create`和`ncnn_extractor_destroy`的声明。根据头文件,我们在cdef中已经包含: ```lua ffi.cdef[[ ... /* extractor api */ typedef struct __ncnn_extractor_t* ncnn_extractor_t; NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); NCNN_EXPORT void ncnn_extractor_destroy(ncnn_extractor_t ex); ... ]] ``` 因此,我们只需要修改封装函数。 另外,在之前的代码中,我们尝试了多个函数名(包括`ncnn_net_create_extractor`和`ncnn_create_extractor`),现在只需要使用`ncnn_extractor_create`。 修改后的完整代码片段如下: ```lua local ffi = require("ffi") -- 1. 增强的ncnn库加载函数 local function load_ncnn() local ncnn_paths = { "/data/local/tmp/libs/libncnn.so", "/data/local/tmp/libncnn.so", "/system/lib/libncnn.so", "/vendor/lib/libncnn.so", "./libncnn.so" } for _, path in ipairs(ncnn_paths) do print("尝试加载: " .. path) local ok, lib = pcall(ffi.load, path, true) -- 设置全局加载 if ok and lib ~= nil then -- 验证关键函数是否存在 local has_create_extractor = pcall(function() ffi.cast("void* (*)(void*)", lib.ncnn_extractor_create) end) if has_create_extractor then print("成功加载ncnn库:", path) return lib else print("库缺少关键函数: " .. path) end else print("加载失败:", path, lib) end end error("无法加载有效的ncnn库") end -- 2. 使用兼容的FFI接口定义(根据头文件) ffi.cdef[[ /* 基础类型 */ typedef void* ncnn_mat_t; typedef void* ncnn_net_t; typedef void* ncnn_extractor_t; typedef void* ncnn_allocator_t; /* 矩阵操作 */ ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator); ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator); ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator); void ncnn_mat_destroy(ncnn_mat_t mat); int ncnn_mat_get_dims(const ncnn_mat_t mat); int ncnn_mat_get_w(const ncnn_mat_t mat); int ncnn_mat_get_h(const ncnn_mat_t mat); int ncnn_mat_get_c(const ncnn_mat_t mat); size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat); void* ncnn_mat_get_data(const ncnn_mat_t mat); /* 网络操作 */ ncnn_net_t ncnn_net_create(); void ncnn_net_destroy(ncnn_net_t net); int ncnn_net_load_param(ncnn_net_t net, const char* param_path); int ncnn_net_load_model(ncnn_net_t net, const char* model_path); /* 提取器操作 */ ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); void ncnn_extractor_destroy(ncnn_extractor_t ex); int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat); int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat); /* 内存分配器接口 */ ncnn_allocator_t ncnn_allocator_create_pool_allocator(); void ncnn_allocator_destroy(ncnn_allocator_t allocator); ]] -- 3. 加载ncnn库 local ncnn = load_ncnn() -- 4. 增强的ncnn封装 local ncnn_wrapper = { Allocator = { create_pool = function() local alloc = ncnn.ncnn_allocator_create_pool_allocator() if alloc == ffi.NULL or alloc == nil then error("创建内存池分配器失败") end return ffi.gc(alloc, ncnn.ncnn_allocator_destroy) end }, Net = { create = function(allocator) local net = ncnn.ncnn_net_create() if net == ffi.NULL or net == nil then error("创建网络失败") end return ffi.gc(net, ncnn.ncnn_net_destroy) end, load_param = function(net, path) local result = ncnn.ncnn_net_load_param(net, path) if result ~= 0 then error(string.format("加载参数文件失败: %s (错误码: %d)", path, result)) end return true end, load_model = function(net, path) local result = ncnn.ncnn_net_load_model(net, path) if result ~= 0 then error(string.format("加载模型文件失败: %s (错误码: %d)", path, result)) end return true end, create_extractor = function(net) local ex = ncnn.ncnn_extractor_create(net) if ex == ffi.NULL or ex == nil then error("创建提取器失败") end return ffi.gc(ex, ncnn.ncnn_extractor_destroy) end }, Mat = { create = function(w, h, c, allocator) allocator = allocator or ffi.NULL local mat if c then mat = ncnn.ncnn_mat_create_3d(w, h, c, allocator) elseif h then mat = ncnn.ncnn_mat_create_2d(w, h, allocator) else mat = ncnn.ncnn_mat_create_1d(w, allocator) end if mat == ffi.NULL or mat == nil then error("创建矩阵失败") end return ffi.gc(mat, ncnn.ncnn_mat_destroy) end, data = function(mat) local ptr = ncnn.ncnn_mat_get_data(mat) if ptr == ffi.NULL then error("获取矩阵数据失败") end return ffi.cast("float*", ptr) end, dims = function(mat) return ncnn.ncnn_mat_get_dims(mat) end, w = function(mat) return ncnn.ncnn_mat_get_w(mat) end, h = function(mat) return ncnn.ncnn_mat_get_h(mat) end, c = function(mat) return ncnn.ncnn_mat_get_c(mat) end, elemsize = function(mat) return ncnn.ncnn_mat_get_elemsize(mat) end }, Extractor = { input = function(ex, name, mat) local result = ncnn.ncnn_extractor_input(ex, name, mat) if result ~= 0 then error(string.format("设置输入失败: %s (错误码: %d)", name, result)) end return true end, extract = function(ex, name) local mat_ptr = ffi.new("ncnn_mat_t[1]") local result = ncnn.ncnn_extractor_extract(ex, name, mat_ptr) if result == 0 then return ffi.gc(mat_ptr[0], ncnn.ncnn_mat_destroy) else error(string.format("提取输出失败: %s (错误码: %d)", name, result)) end end } } -- 5. 修改树木检测器中的reload_model函数,使用新的extractor创建方式 function TreeDetector:reload_model() -- ... 其他代码同上 ... -- 测试创建提取器(提前发现问题) local ok, test_ex = pcall(ncnn_wrapper.Net.create_extractor, self.net) if not ok then print("创建提取器测试失败:", test_ex) self.net = nil return false end -- 成功创建后立即销毁测试提取器 ffi.gc(test_ex, nil) ncnn.ncnn_extractor_destroy(test_ex) print("提取器创建测试成功") return true end -- 其余代码保持不变... ``` 注意:在`ncnn_wrapper.Net.create_extractor`中,我们直接使用`ncnn.ncnn_extractor_create`来创建extractor,并且用`ncnn.ncnn_extractor_destroy`来销毁。 这样修改后,应该可以解决"undefined symbol: ncnn_net_create_extractor"的错误,因为现在使用的是正确的函数名`ncnn_extractor_create`。 如果问题仍然存在,请检查加载的ncnn库是否包含`ncnn_extractor_create`符号,可以通过`nm`命令检查: ```bash nm -D libncnn.so | grep ncnn_extractor_create ``` 最后,运行修改后的代码,应该能够成功创建extractor。 §§相关问题§§: 1. 如何解决LuaJIT FFI调用C函数时出现的"undefined symbol"错误? 2. 如何为Android设备编译支持C API的ncnn库? 3. 在移动设备上使用FFI有哪些常见的内存管理陷阱? 4. 如何实现LuaJIT FFI的跨版本兼容性? </think> # 解决 NCNN C API 中的提取器创建问题 根据您提供的 `c_api.h` 头文件,您遇到的 `undefined symbol: ncnn_net_create_extractor` 错误是因为函数名称不匹配。在 NCNN 的 C API 中,创建提取器的正确函数是 `ncnn_extractor_create`,而不是 `ncnn_net_create_extractor`。 以下是修复后的完整代码: ```lua local ffi = require("ffi") -- 1. 增强的ncnn库加载函数 local function load_ncnn() local ncnn_paths = { "/data/local/tmp/libs/libncnn.so", "/data/local/tmp/libncnn.so", "/system/lib/libncnn.so", "/vendor/lib/libncnn.so", "./libncnn.so" } for _, path in ipairs(ncnn_paths) do print("尝试加载: " .. path) local ok, lib = pcall(ffi.load, path, true) -- 设置全局加载 if ok and lib ~= nil then -- 验证关键函数是否存在 local has_create_extractor = pcall(function() ffi.cast("void* (*)(void*)", lib.ncnn_extractor_create) end) if has_create_extractor then print("成功加载ncnn库:", path) return lib else print("库缺少关键函数: " .. path) end else print("加载失败:", path, lib) end end error("无法加载有效的ncnn库") end -- 2. 使用兼容的FFI接口定义(根据头文件) ffi.cdef[[ /* 基础类型 */ typedef void* ncnn_mat_t; typedef void* ncnn_net_t; typedef void* ncnn_extractor_t; typedef void* ncnn_allocator_t; /* 矩阵操作 */ ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator); ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator); ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator); void ncnn_mat_destroy(ncnn_mat_t mat); int ncnn_mat_get_dims(const ncnn_mat_t mat); int ncnn_mat_get_w(const ncnn_mat_t mat); int ncnn_mat_get_h(const ncnn_mat_t mat); int ncnn_mat_get_c(const ncnn_mat_t mat); size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat); void* ncnn_mat_get_data(const ncnn_mat_t mat); /* 网络操作 */ ncnn_net_t ncnn_net_create(void); void ncnn_net_destroy(ncnn_net_t net); int ncnn_net_load_param(ncnn_net_t net, const char* param_path); int ncnn_net_load_model(ncnn_net_t net, const char* model_path); /* 提取器操作 - 使用头文件中的正确函数名 */ ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); void ncnn_extractor_destroy(ncnn_extractor_t ex); int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat); int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat); /* 内存分配器接口 */ ncnn_allocator_t ncnn_allocator_create_pool_allocator(void); void ncnn_allocator_destroy(ncnn_allocator_t allocator); ]] -- 3. 加载ncnn库 local ncnn = load_ncnn() -- 4. 增强的ncnn封装(使用正确的函数名) local ncnn_wrapper = { Allocator = { create_pool = function() local alloc = ncnn.ncnn_allocator_create_pool_allocator() if alloc == ffi.NULL or alloc == nil then error("创建内存池分配器失败") end return ffi.gc(alloc, ncnn.ncnn_allocator_destroy) end }, Net = { create = function(allocator) local net = ncnn.ncnn_net_create() if net == ffi.NULL or net == nil then error("创建网络失败") end return ffi.gc(net, ncnn.ncnn_net_destroy) end, load_param = function(net, path) local result = ncnn.ncnn_net_load_param(net, path) if result ~= 0 then error(string.format("加载参数文件失败: %s (错误码: %d)", path, result)) end return true end, load_model = function(net, path) local result = ncnn.ncnn_net_load_model(net, path) if result ~= 0 then error(string.format("加载模型文件失败: %s (错误码: %d)", path, result)) end return true end, -- 使用正确的提取器创建函数 create_extractor = function(net) local ex = ncnn.ncnn_extractor_create(net) if ex == ffi.NULL or ex == nil then error("创建提取器失败") end return ffi.gc(ex, ncnn.ncnn_extractor_destroy) end }, Mat = { create = function(w, h, c, allocator) allocator = allocator or ffi.NULL local mat if c then mat = ncnn.ncnn_mat_create_3d(w, h, c, allocator) elseif h then mat = ncnn.ncnn_mat_create_2d(w, h, allocator) else mat = ncnn.ncnn_mat_create_1d(w, allocator) end if mat == ffi.NULL or mat == nil then error("创建矩阵失败") end return ffi.gc(mat, ncnn.ncnn_mat_destroy) end, data = function(mat) local ptr = ncnn.ncnn_mat_get_data(mat) if ptr == ffi.NULL then error("获取矩阵数据失败") end return ffi.cast("float*", ptr) end, dims = function(mat) return ncnn.ncnn_mat_get_dims(mat) end, w = function(mat) return ncnn.ncnn_mat_get_w(mat) end, h = function(mat) return ncnn.ncnn_mat_get_h(mat) end, c = function(mat) return ncnn.ncnn_mat_get_c(mat) end, elemsize = function(mat) return ncnn.ncnn_mat_get_elemsize(mat) end }, Extractor = { input = function(ex, name, mat) local result = ncnn.ncnn_extractor_input(ex, name, mat) if result ~= 0 then error(string.format("设置输入失败: %s (错误码: %d)", name, result)) end return true end, extract = function(ex, name) local mat_ptr = ffi.new("ncnn_mat_t[1]") local result = ncnn.ncnn_extractor_extract(ex, name, mat_ptr) if result == 0 then return ffi.gc(mat_ptr[0], ncnn.ncnn_mat_destroy) else error(string.format("提取输出失败: %s (错误码: %d)", name, result)) end end } } -- 5. 修改树木检测器中的reload_model函数 function TreeDetector:reload_model() -- 清理现有资源 if self.net then ffi.gc(self.net, nil) -- 移除GC钩子 ncnn.ncnn_net_destroy(self.net) self.net = nil end -- 创建新网络 self.net = ncnn_wrapper.Net.create() -- 加载模型参数 local param_path = self.model_dir .. "/yolov8n.param" local model_path = self.model_dir .. "/yolov8n.bin" local status, err = pcall(function() ncnn_wrapper.Net.load_param(self.net, param_path) ncnn_wrapper.Net.load_model(self.net, model_path) end) if not status then print("模型加载失败:", err) self.net = nil return false end print("模型加载成功") -- 测试创建提取器(提前发现问题) local ok, test_ex = pcall(ncnn_wrapper.Net.create_extractor, self.net) if not ok then print("创建提取器测试失败:", test_ex) self.net = nil return false end -- 成功创建后立即销毁测试提取器 ffi.gc(test_ex, nil) ncnn.ncnn_extractor_destroy(test_ex) print("提取器创建测试成功") return true end -- 6. 截图函数(示例实现) function get_screenshot() -- 在实际应用中,这里应替换为真实的截图实现 -- 模拟返回宽高为640x480的RGB图像 local width, height = 640, 480 local size = width * height * 3 local data = ffi.new("uint8_t[?]", size) -- 生成模拟图像数据(随机像素) math.randomseed(os.time()) for i = 0, size - 1 do data[i] = math.random(0, 255) end return true, data, width, height end -- 7. 健壮的主循环实现 local function main() print("启动树木检测程序...") local detector = TreeDetector.new() local consecutive_failures = 0 local max_failures = 5 while true do -- 获取截图 local success, img_data, width, height = get_screenshot() if not success or not img_data then print("截图失败:", img_data or "未知错误") consecutive_failures = consecutive_failures + 1 else -- 执行检测 local ok, trees = pcall(detector.detect_trees, detector, img_data, width, height) if not ok then print("检测失败:", trees) consecutive_failures = consecutive_failures + 1 else consecutive_failures = 0 print("检测到树木数量:", #trees) -- 处理检测结果... -- 例如:绘制边界框、记录日志等 end end -- 处理连续失败 if consecutive_failures >= max_failures then print("连续失败超过阈值,尝试重载模型...") if detector:reload_model() then consecutive_failures = 0 print("模型重载成功") else print("模型重载失败,重启检测器...") detector = TreeDetector.new() consecutive_failures = 0 end end -- 添加延迟避免过度占用CPU os.execute("sleep 0.5") end end -- 8. 带崩溃恢复和日志记录的入口点 local function protected_main() while true do local status, err = xpcall(main, debug.traceback) if not status then print("程序崩溃:", err) print("5秒后尝试恢复...") os.execute("sleep 5") else break -- 正常退出 end end end -- 启动程序 protected_main() ``` ## 关键修复点 ### 1. 函数名修正 根据头文件中的定义: ```c NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); ``` 我已将所有 `ncnn_net_create_extractor` 的引用替换为 `ncnn_extractor_create`。 ### 2. 函数签名更新 修正了所有相关函数的签名定义,使其与头文件匹配: ```lua ffi.cdef[[ ... /* 提取器操作 - 使用头文件中的正确函数名 */ ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); void ncnn_extractor_destroy(ncnn_extractor_t ex); ... ]] ``` ### 3. 额外的错误检查 在关键操作中添加了额外的空指针检查: ```lua create_extractor = function(net) local ex = ncnn.ncnn_extractor_create(net) if ex == ffi.NULL or ex == nil then error("创建提取器失败") end return ffi.gc(ex, ncnn.ncnn_extractor_destroy) end ``` ### 4. 预测试机制 在模型加载后立即测试提取器创建: ```lua -- 测试创建提取器(提前发现问题) local ok, test_ex = pcall(ncnn_wrapper.Net.create_extractor, self.net) if not ok then print("创建提取器测试失败:", test_ex) self.net = nil return false end -- 成功创建后立即销毁测试提取器 ffi.gc(test_ex, nil) ncnn.ncnn_extractor_destroy(test_ex) ``` ### 5. 更健壮的内存管理 确保所有创建的对象都被正确销毁: ```lua function TreeDetector:reload_model() -- 清理现有资源 if self.net then ffi.gc(self.net, nil) -- 移除GC钩子 ncnn.ncnn_net_destroy(self.net) self.net = nil end ... end ``` ## 验证步骤 1. **检查符号存在性**: ```bash adb shell "nm -D /data/local/tmp/libs/libncnn.so | grep ncnn_extractor_create" ``` 应该输出类似: ``` 0000000000123456 T ncnn_extractor_create ``` 2. **运行最小测试**: ```lua local ffi = require("ffi") local ncnn = ffi.load("/data/local/tmp/libs/libncnn.so", true) ffi.cdef[[ typedef void* ncnn_net_t; typedef void* ncnn_extractor_t; ncnn_net_t ncnn_net_create(); ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); ]] local net = ncnn.ncnn_net_create() if net ~= nil then local ex = ncnn.ncnn_extractor_create(net) if ex ~= nil then print("成功创建提取器!") else print("创建提取器失败") end else print("创建网络失败") end ``` 3. **完整测试**: 运行修复后的 TreeDetection.lua 脚本,应该不再出现 "undefined symbol" 错误。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值