diff --git a/libsvm/BUILD.gn b/libsvm/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b4614bb0dd33c91e1938560080ea11c4e965463e --- /dev/null +++ b/libsvm/BUILD.gn @@ -0,0 +1,82 @@ +import("//build/ohos.gni") + +declare_args() { + enable_svm_test = false +} + +config("config") { + cflags = [ + "-O3", + "-Wall", + "-Wno-extra-tokens", + "-Wno-incompatible-pointer-types", + "-Wno-parentheses", + "-Wno-unused-variable", + "-Wno-absolute-value", + "-Wno-unused-function", + "-Wno-unused-const-variable", + "-Wno-unused-label", + "-Wno-shift-negative-value", + "-Wno-tautological-pointer-compare", + "-Wno-duplicate-decl-specifier", + ] +} + +ohos_source_set("libsvm_source") { + sources = [ "//third_party/libsvm/svm.cpp" ] + include_dirs = [ "//third_party/libsvm/" ] + configs = [ ":config" ] +} + +ohos_executable("svm-scale") { + sources = [ "//third_party/libsvm/svm-scale.c" ] + include_dirs = [ "//third_party/libsvm/" ] + + configs = [ ":config" ] + + deps = [ "//third_party/libsvm:libsvm" ] + subsystem_name = "thirdparty" + part_name = "libsvm" + output_name = "svm-scale" +} + +ohos_executable("svm-train") { + sources = [ "//third_party/libsvm/svm-train.c" ] + include_dirs = [ "//third_party/libsvm/" ] + + configs = [ ":config" ] + + deps = [ "//third_party/libsvm:libsvm" ] + subsystem_name = "thirdparty" + part_name = "libsvm" + output_name = "svm-train" +} + +ohos_executable("svm-predict") { + sources = [ "//third_party/libsvm/svm-predict.c" ] + include_dirs = [ "//third_party/libsvm/" ] + + configs = [ ":config" ] + + deps = [ "//third_party/libsvm:libsvm" ] + subsystem_name = "thirdparty" + part_name = "libsvm" + output_name = "svm-predict" +} + +ohos_shared_library("libsvm") { + deps = [ ":libsvm_source" ] + part_name = "libsvm" + output_name = "svm" +} + +group("svm_targets") { + deps = [ ":libsvm" ] + if (enable_svm_test) { + deps += [ + ":svm-predict", + ":svm-scale", + ":svm-train", + ] + } +} diff --git a/libsvm/CHANGELOG.md b/libsvm/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..4963dc7546e7cc84baf8e836c544ddf43f554025 --- /dev/null +++ b/libsvm/CHANGELOG.md @@ -0,0 +1,3 @@ +# 1.0.0 + +添加BUILD.gn和cmake,适配在ohos上的编译 diff --git a/libsvm/CMakeLists.txt b/libsvm/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..17d8e42f8f8d084fbc700e8f546e000449aac913 --- /dev/null +++ b/libsvm/CMakeLists.txt @@ -0,0 +1,21 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.4.1) +project(libsvm) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/include) + +add_library(svm SHARED svm.cpp) + +file( + GLOB_RECURSE mains RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + "${CMAKE_CURRENT_SOURCE_DIR}/svm-scale.c" + "${CMAKE_CURRENT_SOURCE_DIR}/svm-train.c" + "${CMAKE_CURRENT_SOURCE_DIR}/svm-predict.c" +) + +foreach(mainfile IN LISTS mains) + get_filename_component(mainname ${mainfile} NAME_WE) + add_executable(${mainname} ${mainfile}) + target_link_libraries(${mainname} svm) +endforeach() diff --git a/libsvm/README.OpenSource b/libsvm/README.OpenSource new file mode 100644 index 0000000000000000000000000000000000000000..690b43464cf4f4d5302a39078850e2b67f21a033 --- /dev/null +++ b/libsvm/README.OpenSource @@ -0,0 +1,11 @@ +[ + { + "Name": "libsvm", + "License": "BSD", + "License File": "COPYRIGHT", + "Version Number": "v331", + "Owner" : "wupingyuan@huawei.com", + "Upstream URL": "https://github.com/cjlin1/libsvm", + "Description": "Libsvm is a simple, easy-to-use, and efficient software for SVM classification and regression." + } +] diff --git a/libsvm/README_zh.md b/libsvm/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..73a7c62cb1c0cca987e6442db52a24828588a459 --- /dev/null +++ b/libsvm/README_zh.md @@ -0,0 +1,19 @@ +# libsvm 三方库说明 + +## 功能简介 + +libsvm是一个支持向量机的库。 + +## 使用约束 + +- Rom版本:OpenHarmony3.2 beta2 +- IDE版本:DevEco Studio 3.1 Beta1 +- API版本:9 +- 三方库版本:v331 +- 当前适配的功能:向量机 +- License:[BSD](https://github.com/cjlin1/libsvm/blob/master/COPYRIGHT) + +## 集成方式 + +- [系统Rom包集成](./docs/rom_integrate.md) ++ [应用hap包集成](docs/hap_integrate.md) diff --git a/libsvm/adapted/config.h b/libsvm/adapted/config.h new file mode 100644 index 0000000000000000000000000000000000000000..e62bd003e7b3238914fab30b57ecebd9e546c587 --- /dev/null +++ b/libsvm/adapted/config.h @@ -0,0 +1,227 @@ +/* include/config.h.in. Generated from configure.ac by autoheader. */ + +/* whether to build support for Code 128 symbology */ +#define ENABLE_CODE128 1 + +/* whether to build support for Code 39 symbology */ +#define ENABLE_CODE39 1 + +/* whether to build support for EAN symbologies */ +#define ENABLE_EAN 1 + +/* whether to build support for Interleaved 2 of 5 symbology */ +#define ENABLE_I25 1 + +/* whether to build support for PDF417 symbology */ +#define ENABLE_PDF417 1 + +/* whether to build support for QR Code */ +#define ENABLE_QRCODE 1 + +/* Define to 1 if you have the `atexit' function. */ +#undef HAVE_ATEXIT + +/* Define to 1 if you have the header file. */ +#undef HAVE_DLFCN_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_FCNTL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_FEATURES_H + +/* Define to 1 if you have the `getpagesize' function. */ +#undef HAVE_GETPAGESIZE + +/* Define if you have the iconv() function and it works. */ +#undef HAVE_ICONV + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#undef HAVE_JPEGLIB_H + +/* Define to 1 if you have the `jpeg' library (-ljpeg). */ +//#undef HAVE_LIBJPEG +#define HAVE_LIBJPEG 1 + + +/* Define to 1 if you have the `pthread' library (-lpthread). */ +#undef HAVE_LIBPTHREAD + +/* Define to 1 if you have the header file. */ +#undef HAVE_LINUX_VIDEODEV2_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_LINUX_VIDEODEV_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `memset' function. */ +#define HAVE_MEMSET 1 + +/* Define to 1 if you have a working `mmap' system call. */ +#undef HAVE_MMAP + +/* Define to 1 if you have the header file. */ +#undef HAVE_POLL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_PTHREAD_H + +/* Define to 1 if you have the `setenv' function. */ +#undef HAVE_SETENV + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_IOCTL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_IPC_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_MMAN_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_SHM_H + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIMES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if the system has the type `uintptr_t'. */ +#define HAVE_UINTPTR_T 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if you have the header file. */ +#undef HAVE_VFW_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_X11_EXTENSIONS_XSHM_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_X11_EXTENSIONS_XVLIB_H + +/* Define as const if the declaration of iconv() needs const. */ +#undef ICONV_CONST + +/* Library major version */ +#define LIB_VERSION_MAJOR 0 + +/* Library minor version */ +#define LIB_VERSION_MINOR 2 + +/* Library revision */ +#define LIB_VERSION_REVISION 0 + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#undef LT_OBJDIR + +/* Define to 1 if assertions should be disabled. */ +//#undef NDEBUG + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +#undef NO_MINUS_C_MINUS_O + +/* Name of package */ +#undef PACKAGE "zbar" + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT "spadix@users.sourceforge.net" + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME "zbar" + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING "zbar 0.10" + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME "zbar" + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION "0.10" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#undef VERSION "0.10" + +/* Define to 1 if the X Window System is missing or not being used. */ +#define X_DISPLAY_MISSING 1 + +/* Program major version (before the '.') as a number */ +#define ZBAR_VERSION_MAJOR 0 + +/* Program minor version (after '.') as a number */ +#define ZBAR_VERSION_MINOR 10 + +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +#undef _UINT32_T + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +#undef _UINT8_T + +/* Minimum Windows API version */ +#undef _WIN32_WINNT + +/* used only for pthread debug attributes */ +#undef __USE_UNIX98 + +/* Define to empty if `const' does not conform to ANSI C. */ +#undef const + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +#undef inline +#endif + +/* Define to the type of a signed integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +#undef int32_t + +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +#undef uint32_t + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +#undef uint8_t + +/* Define to the type of an unsigned integer type wide enough to hold a + pointer, if such a type exists, and if the system does not define it. */ +#undef uintptr_t + +#ifndef X_DISPLAY_MISSING +# define HAVE_X +#endif + diff --git a/libsvm/bundle.json b/libsvm/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..e76aeea5d9a4bbe6f728a63011103d766fb621af --- /dev/null +++ b/libsvm/bundle.json @@ -0,0 +1,35 @@ +{ + "name": "@ohos/libsvm", + "description": "Libsvm is a simple, easy-to-use, and efficient software for SVM classification and regression.", + "version": "v331", + "license": "BSD 3-Clause", + "publishAs": "", + "segment": { + "destPath": "third_party/libsvm" + }, + "dirs": {}, + "scripts": {}, + "readmePath": { + "en": "README" + }, + "component": { + "name": "libsvm", + "subsystem": "thirdparty", + "syscap": [], + "features": [], + "adapted_system_type": ["standard"], + "rom": "", + "ram": "", + "deps": { + "components": [], + "third_party": [] + }, + "build": { + "sub_component": [ + "//third_party/libsvm:svm_targets" + ], + "inner_kits": [], + "test": [] + } + } +} \ No newline at end of file diff --git a/libsvm/docs/hap_integrate.md b/libsvm/docs/hap_integrate.md new file mode 100644 index 0000000000000000000000000000000000000000..39406ace713416846422a1ebe46df93392044465 --- /dev/null +++ b/libsvm/docs/hap_integrate.md @@ -0,0 +1,48 @@ +# libsvm如何集成到应用hap +## 准备应用工程 +本库是基于DevEco Studio 3.1 Beta1版本,在RK3568开发板上验证的,如果是从未使用过RK3568,可以先查看[润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 +### 准备应用开发环境 +开发环境的准备参考:[开发环境准备](https://gitee.com/openharmony-sig/knowledge_demo_temp/blob/master/docs/napi_study/docs/hello_napi.md#%E5%B7%A5%E7%A8%8B%E5%87%86%E5%A4%87) +### 增加构建脚本及配置文件 +- 下载本仓库,并解压 +- 三方库目录结构 + ``` + tpc_c_cplusplus/libsvm #三方库libsvm的目录结构如下 + ├── adapted #存放三方库适配需要的代码文件 + ├── docs #存放三方库相关文档的文件夹 + ├── CmakeLists.txt #构建脚本,支持hap包集成 + ├── bundle.json #三方库组件定义文件 + ├── README.OpenSource #说明三方库源码的下载地址,版本,license等信息 + ├── README_zh.md + ``` +- 将libsvm拷贝至工程xxxx/entry/src/main/cpp/thirdparty目录下 +### 准备三方库源码 +- 三方库下载地址:[libsvm](https://github.com/cjlin1/libsvm), 版本:master + 解压后修改库文件名为libsvm,拷贝至工程xxxx/entry/src/main/cpp/thirdparty/libsvm目录下 +## 应用中使用三方库 +- 将三方库加入工程中,目录结构如下 + ``` + demo/entry/src/main/cpp + ├── thirdparty #三方库存放目录 + │ ├── libsvm #三方库libsvm + ├── CMakeLists.txt #工程目录的构建脚本 + ├── ..... #工程目录的其他文件 + ``` +- 在工程顶级CMakeLists.txt中引入三方库,增加如下代码 + ``` + add_subdirectory(thirdparty/libsvm) #引入子目录下的CMakeLists.txt + target_link_libraries(工程库名 PUBLIC libsvm) #工程依赖三方库libsvm + target_include_directories(工程库名 PRIVATE thirdparty/libsvm) + ``` +## 编译工程 +编译工程,安装应用可以参考 [应用的安装和运行](https://gitee.com/openharmony-sig/knowledge_demo_temp/blob/master/docs/napi_study/docs/hello_napi.md#%E5%AE%89%E8%A3%85%E8%B0%83%E8%AF%95) + +## 运行效果 +- 在 [libsvm](https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/libsvm)中,运行效果如下图 +  ![演示](pic/hap.jpg) +## 参考资料 +- [润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld) +- [OpenHarmony三方库地址](https://gitee.com/openharmony-tpc) +- [OpenHarmony知识体系](https://gitee.com/openharmony-sig/knowledge) +- [通过DevEco Studio开发一个NAPI工程](https://gitee.com/openharmony-sig/knowledge_demo_temp/blob/master/docs/napi_study/docs/hello_napi.md) + diff --git a/libsvm/docs/pic/hap.jpg b/libsvm/docs/pic/hap.jpg new file mode 100644 index 0000000000000000000000000000000000000000..fccc49b3609c33c91bbc33374a575383121882c5 Binary files /dev/null and b/libsvm/docs/pic/hap.jpg differ diff --git a/libsvm/docs/pic/result.png b/libsvm/docs/pic/result.png new file mode 100644 index 0000000000000000000000000000000000000000..fef588e54c97af9cc4bbfcf9a9577f61716d73f1 Binary files /dev/null and b/libsvm/docs/pic/result.png differ diff --git a/libsvm/docs/rom_integrate.md b/libsvm/docs/rom_integrate.md new file mode 100644 index 0000000000000000000000000000000000000000..94f2d935cd44601b80c1ff1646d1db1d3e2fd3f1 --- /dev/null +++ b/libsvm/docs/rom_integrate.md @@ -0,0 +1,149 @@ +# libsvm如何集成到系统Rom + +## 准备源码工程 + +本库是基于OpenHarmony-v3.2-Beta2版本,在RK3568开发板上验证的,如果是从未使用过RK3568,可以先查看[润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 + +### 准备系统Rom源码 + +系统源码获取请参考:[OpenHarmony源码下载](https://gitee.com/openharmony/docs/blob/OpenHarmony-v3.2-Beta3/zh-cn/release-notes/OpenHarmony-v3.2-beta2.md) + +### 增加构建脚本及配置文件 + +- 下载本仓库代码 + + ``` + cd ~ + git clone git@gitee.com:openharmony-sig/tpc_c_cplusplus.git --depth=1 + ``` + +- 三方库目录结构 + + ``` + tpc_c_cplusplus/libsvm #三方库libsvm的目录结构如下 + ├── adapted #存放三方库适配需要的代码文件 + ├── docs #存放三方库相关文档的文件夹 + ├── BUILD.gn #构建脚本,支持rom包集成 + ├── bundle.json #三方库组件定义文件 + ├── README.OpenSource #说明三方库源码的下载地址,版本,license等信息 + ├── README_zh.md + ``` + +- 将三方库拷贝到OpenHarmony源码的third_party目录下 + + ``` + cp ~/tpc_c_cplusplus/libsvm ~/openharmony/third_party -rf + ``` + +### 准备三方库源码 + +``` +cd ~/openharmony/third_party/libsvm #进入三方库目录 +git clone https://github.com/cjlin1/libsvm.git #下载三方库源码 +``` + +## 系统Rom中引入三方库 + +准备完三方库代码后,我们需要将三方库加入到编译构建体系中。标准系统编译构建可以参考文档[标准系统编译构建指导](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Beta1/zh-cn/device-dev/subsystems/subsys-build-standard-large.md) 我们默认三方库是属于OpenHarmony的thirdparty子系统,如果需要自己定义子系统参考文档[如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md) 新增需要编译的组件,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件中,新增需要编译的组件,如下代码段所示,在thirdparty子系统下面新增libsvm组件 + +``` + { + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "libsvm", + "features": [] + } + ] + } +``` + +## 系统Rom中引入三方库测试程序 + +libsvm原生库提供了测试用例,如需要引入测试程序,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件,对应组件的features中打开编译选项: + +``` +{ + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "libsvm", + "features": [ "enable_svm_test=true" ] + } + ] +} +``` + +## 编译工程 + +在OpenHarmony源码根目录下 + +``` +cd ~/openharmony +``` + +- 执行编译 + + ``` + ./build.sh --product-name rk3568 --ccache + ``` + +- 生成文件的路径 + + 可执行文件和库生成在out/rk3568/thirdparty/libsvm + +## 运行效果 + +将编译生成的库和测试文件放到板子上运行,为避免每次将文件推入设备都烧录整个镜像,我们使用hdc_std工具将文件推到开发板上 + +- 首先将hdc_std工具编译出来 工具编译出来所在路径out/sdk/ohos-sdk/windows/toolchains/hdc_std.exe + + ``` + hb set #源码根目录下使用hb set 选择产品ohos-sdk + hb build #然后编译 + ``` + +- 将工具拷贝到Windows,可以为工具目录配置环境变量,也可以在工具所在目录打开windows命令行 + +- 将编译生成的svm-scale、svm-train、svm-predict可执行文件和训练数据[test.data](https://www.csie.ntu.edu.tw/~cjlin/papers/guide/data/)准备好 + +- 将准备好的文件推送到开发板,在windows命令行进行如下操作 + + ``` + hdc_std shell mount -o remount,rw / #修改系统权限为可读写 + hdc_std file send svm-scale /data #将可执行文件推入开发板data目录 + hdc_std file send svm-train /data #将可执行文件推入开发板data目录 + hdc_std file send svm-predict /data #将可执行文件推入开发板data目录 + + hdc_std file send libsvm.z.so /system/lib64 + hdc_std file send libsvm.z.so /system/lib + hdc_std file send test.data /data #将测试文件推入开发板data目录 + hdc_std shell #进入开发板 + chmod 777 svm-scale #添加权限 + chmod 777 svm-train #添加权限 + chmod 777 svm-predict #添加权限 + + ./svm-scale /data/test.data > /data/test.scale #scale + ./svm-train /data/test.scale /data/test.model #train + ./svm-predict /data/test.scale /data/test.model /data/test.predict #predict + ``` + +- 运行测试程序 效果如下图 + +- ![result](pic/result.png) + +## 参考资料 + +- [润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld) +- [OpenHarmony三方库地址](https://gitee.com/openharmony-tpc) +- [OpenHarmony知识体系](https://gitee.com/openharmony-sig/knowledge) +- [如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md) +- [标准系统编译构建指导](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Beta1/zh-cn/device-dev/subsystems/subsys-build-standard-large.md) \ No newline at end of file