Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wangsen
paddle_dbnet
Commits
f8889760
Unverified
Commit
f8889760
authored
May 11, 2021
by
MissPenguin
Committed by
GitHub
May 11, 2021
Browse files
Merge pull request #2426 from WenmuZhou/android_demo
add Android demo
parents
9c6ff0a7
a0c93dbd
Changes
73
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
5616 additions
and
1 deletion
+5616
-1
.gitignore
.gitignore
+5
-1
deploy/android_demo/.gitignore
deploy/android_demo/.gitignore
+9
-0
deploy/android_demo/README.md
deploy/android_demo/README.md
+19
-0
deploy/android_demo/app/.gitignore
deploy/android_demo/app/.gitignore
+1
-0
deploy/android_demo/app/build.gradle
deploy/android_demo/app/build.gradle
+98
-0
deploy/android_demo/app/proguard-rules.pro
deploy/android_demo/app/proguard-rules.pro
+21
-0
deploy/android_demo/app/src/androidTest/java/com/baidu/paddle/lite/demo/ocr/ExampleInstrumentedTest.java
...m/baidu/paddle/lite/demo/ocr/ExampleInstrumentedTest.java
+26
-0
deploy/android_demo/app/src/main/AndroidManifest.xml
deploy/android_demo/app/src/main/AndroidManifest.xml
+39
-0
deploy/android_demo/app/src/main/assets/images/0.jpg
deploy/android_demo/app/src/main/assets/images/0.jpg
+0
-0
deploy/android_demo/app/src/main/assets/images/180.jpg
deploy/android_demo/app/src/main/assets/images/180.jpg
+0
-0
deploy/android_demo/app/src/main/assets/images/270.jpg
deploy/android_demo/app/src/main/assets/images/270.jpg
+0
-0
deploy/android_demo/app/src/main/assets/images/90.jpg
deploy/android_demo/app/src/main/assets/images/90.jpg
+0
-0
deploy/android_demo/app/src/main/cpp/CMakeLists.txt
deploy/android_demo/app/src/main/cpp/CMakeLists.txt
+117
-0
deploy/android_demo/app/src/main/cpp/common.h
deploy/android_demo/app/src/main/cpp/common.h
+37
-0
deploy/android_demo/app/src/main/cpp/native.cpp
deploy/android_demo/app/src/main/cpp/native.cpp
+114
-0
deploy/android_demo/app/src/main/cpp/native.h
deploy/android_demo/app/src/main/cpp/native.h
+137
-0
deploy/android_demo/app/src/main/cpp/ocr_clipper.cpp
deploy/android_demo/app/src/main/cpp/ocr_clipper.cpp
+4380
-0
deploy/android_demo/app/src/main/cpp/ocr_clipper.hpp
deploy/android_demo/app/src/main/cpp/ocr_clipper.hpp
+544
-0
deploy/android_demo/app/src/main/cpp/ocr_cls_process.cpp
deploy/android_demo/app/src/main/cpp/ocr_cls_process.cpp
+46
-0
deploy/android_demo/app/src/main/cpp/ocr_cls_process.h
deploy/android_demo/app/src/main/cpp/ocr_cls_process.h
+23
-0
No files found.
.gitignore
View file @
f8889760
...
...
@@ -25,3 +25,7 @@ output/
build/
dist/
paddleocr.egg-info/
/deploy/android_demo/app/OpenCV/
/deploy/android_demo/app/PaddleLite/
/deploy/android_demo/app/.cxx/
/deploy/android_demo/app/cache/
deploy/android_demo/.gitignore
0 → 100644
View file @
f8889760
*.iml
.gradle
/local.properties
/.idea/*
.DS_Store
/build
/captures
.externalNativeBuild
deploy/android_demo/README.md
0 → 100644
View file @
f8889760
# 如何快速测试
### 1. 安装最新版本的Android Studio
可以从 https://developer.android.com/studio 下载。本Demo使用是4.0版本Android Studio编写。
### 2. 按照NDK 20 以上版本
Demo测试的时候使用的是NDK 20b版本,20版本以上均可以支持编译成功。
如果您是初学者,可以用以下方式安装和测试NDK编译环境。
点击 File -> New ->New Project, 新建 "Native C++" project
### 3. 导入项目
点击 File->New->Import Project..., 然后跟着Android Studio的引导导入
# 获得更多支持
前往
[
端计算模型生成平台EasyEdge
](
https://ai.baidu.com/easyedge/app/open_source_demo?referrerUrl=paddlelite
)
,获得更多开发支持:
-
Demo APP:可使用手机扫码安装,方便手机端快速体验文字识别
-
SDK:模型被封装为适配不同芯片硬件和操作系统SDK,包括完善的接口,方便进行二次开发
deploy/android_demo/app/.gitignore
0 → 100644
View file @
f8889760
/build
deploy/android_demo/app/build.gradle
0 → 100644
View file @
f8889760
import
java.security.MessageDigest
apply
plugin:
'com.android.application'
android
{
compileSdkVersion
29
defaultConfig
{
applicationId
"com.baidu.paddle.lite.demo.ocr"
minSdkVersion
23
targetSdkVersion
29
versionCode
1
versionName
"1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild
{
cmake
{
cppFlags
"-std=c++11 -frtti -fexceptions -Wno-format"
arguments
'-DANDROID_PLATFORM=android-23'
,
'-DANDROID_STL=c++_shared'
,
"-DANDROID_ARM_NEON=TRUE"
}
}
ndk
{
// abiFilters "arm64-v8a", "armeabi-v7a"
abiFilters
"arm64-v8a"
,
"armeabi-v7a"
ldLibs
"jnigraphics"
}
}
buildTypes
{
release
{
minifyEnabled
false
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
}
}
externalNativeBuild
{
cmake
{
path
"src/main/cpp/CMakeLists.txt"
version
"3.10.2"
}
}
}
dependencies
{
implementation
fileTree
(
include:
[
'*.jar'
],
dir:
'libs'
)
implementation
'androidx.appcompat:appcompat:1.1.0'
implementation
'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation
'junit:junit:4.12'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
}
def
archives
=
[
[
'src'
:
'https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/paddle_lite_libs_v2_9_0.tar.gz'
,
'dest'
:
'PaddleLite'
],
[
'src'
:
'https://paddlelite-demo.bj.bcebos.com/libs/android/opencv-4.2.0-android-sdk.tar.gz'
,
'dest'
:
'OpenCV'
],
[
'src'
:
'https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ocr_v2_for_cpu.tar.gz'
,
'dest'
:
'src/main/assets/models'
],
[
'src'
:
'https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ch_dict.tar.gz'
,
'dest'
:
'src/main/assets/labels'
]
]
task
downloadAndExtractArchives
(
type:
DefaultTask
)
{
doFirst
{
println
"Downloading and extracting archives including libs and models"
}
doLast
{
// Prepare cache folder for archives
String
cachePath
=
"cache"
if
(!
file
(
"${cachePath}"
).
exists
())
{
mkdir
"${cachePath}"
}
archives
.
eachWithIndex
{
archive
,
index
->
MessageDigest
messageDigest
=
MessageDigest
.
getInstance
(
'MD5'
)
messageDigest
.
update
(
archive
.
src
.
bytes
)
String
cacheName
=
new
BigInteger
(
1
,
messageDigest
.
digest
()).
toString
(
32
)
// Download the target archive if not exists
boolean
copyFiles
=
!
file
(
"${archive.dest}"
).
exists
()
if
(!
file
(
"${cachePath}/${cacheName}.tar.gz"
).
exists
())
{
ant
.
get
(
src:
archive
.
src
,
dest:
file
(
"${cachePath}/${cacheName}.tar.gz"
))
copyFiles
=
true
;
// force to copy files from the latest archive files
}
// Extract the target archive if its dest path does not exists
if
(
copyFiles
)
{
copy
{
from
tarTree
(
"${cachePath}/${cacheName}.tar.gz"
)
into
"${archive.dest}"
}
}
}
}
}
preBuild
.
dependsOn
downloadAndExtractArchives
\ No newline at end of file
deploy/android_demo/app/proguard-rules.pro
0 → 100644
View file @
f8889760
# Add project specific ProGuard rules here.
#
You
can
control
the
set
of
applied
configuration
files
using
the
#
proguardFiles
setting
in
build
.
gradle
.
#
#
For
more
details
,
see
#
http
://
developer
.
android
.
com
/
guide
/
developing
/
tools
/
proguard
.
html
#
If
your
project
uses
WebView
with
JS
,
uncomment
the
following
#
and
specify
the
fully
qualified
class
name
to
the
JavaScript
interface
#
class
:
#-
keepclassmembers
class
fqcn
.
of
.
javascript
.
interface
.
for
.
webview
{
#
public
*
;
#
}
#
Uncomment
this
to
preserve
the
line
number
information
for
#
debugging
stack
traces
.
#-
keepattributes
SourceFile
,
LineNumberTable
#
If
you
keep
the
line
number
information
,
uncomment
this
to
#
hide
the
original
source
file
name
.
#-
renamesourcefileattribute
SourceFile
deploy/android_demo/app/src/androidTest/java/com/baidu/paddle/lite/demo/ocr/ExampleInstrumentedTest.java
0 → 100644
View file @
f8889760
package
com.baidu.paddle.lite.demo.ocr
;
import
android.content.Context
;
import
android.support.test.InstrumentationRegistry
;
import
android.support.test.runner.AndroidJUnit4
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
static
org
.
junit
.
Assert
.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith
(
AndroidJUnit4
.
class
)
public
class
ExampleInstrumentedTest
{
@Test
public
void
useAppContext
()
{
// Context of the app under test.
Context
appContext
=
InstrumentationRegistry
.
getTargetContext
();
assertEquals
(
"com.baidu.paddle.lite.demo"
,
appContext
.
getPackageName
());
}
}
deploy/android_demo/app/src/main/AndroidManifest.xml
0 → 100644
View file @
f8889760
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.baidu.paddle.lite.demo.ocr"
>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.CAMERA"
/>
<application
android:allowBackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:label=
"@string/app_name"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:supportsRtl=
"true"
android:theme=
"@style/AppTheme"
>
<!-- to test MiniActivity, change this to com.baidu.paddle.lite.demo.ocr.MiniActivity -->
<activity
android:name=
"com.baidu.paddle.lite.demo.ocr.MainActivity"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
<category
android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity
android:name=
"com.baidu.paddle.lite.demo.ocr.SettingsActivity"
android:label=
"Settings"
>
</activity>
<provider
android:name=
"androidx.core.content.FileProvider"
android:authorities=
"com.baidu.paddle.lite.demo.ocr.fileprovider"
android:exported=
"false"
android:grantUriPermissions=
"true"
>
<meta-data
android:name=
"android.support.FILE_PROVIDER_PATHS"
android:resource=
"@xml/file_paths"
></meta-data>
</provider>
</application>
</manifest>
\ No newline at end of file
deploy/android_demo/app/src/main/assets/images/0.jpg
0 → 100644
View file @
f8889760
62.2 KB
deploy/android_demo/app/src/main/assets/images/180.jpg
0 → 100644
View file @
f8889760
63.1 KB
deploy/android_demo/app/src/main/assets/images/270.jpg
0 → 100644
View file @
f8889760
171 KB
deploy/android_demo/app/src/main/assets/images/90.jpg
0 → 100644
View file @
f8889760
61.1 KB
deploy/android_demo/app/src/main/cpp/CMakeLists.txt
0 → 100644
View file @
f8889760
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required
(
VERSION 3.4.1
)
# Creates and names a library, sets it as either STATIC or SHARED, and provides
# the relative paths to its source code. You can define multiple libraries, and
# CMake builds them for you. Gradle automatically packages shared libraries with
# your APK.
set
(
PaddleLite_DIR
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../../../PaddleLite"
)
include_directories
(
${
PaddleLite_DIR
}
/cxx/include
)
set
(
OpenCV_DIR
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../../../OpenCV/sdk/native/jni"
)
message
(
STATUS
"opencv dir:
${
OpenCV_DIR
}
"
)
find_package
(
OpenCV REQUIRED
)
message
(
STATUS
"OpenCV libraries:
${
OpenCV_LIBS
}
"
)
include_directories
(
${
OpenCV_INCLUDE_DIRS
}
)
aux_source_directory
(
. SOURCES
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-ffast-math -Ofast -Os"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections -ffunction-sections"
)
set
(
CMAKE_SHARED_LINKER_FLAGS
"
${
CMAKE_SHARED_LINKER_FLAGS
}
-Wl,--gc-sections -Wl,-z,nocopyreloc"
)
add_library
(
# Sets the name of the library.
Native
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${
SOURCES
}
)
find_library
(
# Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that you want CMake to locate.
log
)
add_library
(
# Sets the name of the library.
paddle_light_api_shared
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
IMPORTED
)
set_target_properties
(
# Specifies the target library.
paddle_light_api_shared
# Specifies the parameter you want to define.
PROPERTIES
IMPORTED_LOCATION
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libpaddle_light_api_shared.so
# Provides the path to the library you want to import.
)
# Specifies libraries CMake should link to your target library. You can link
# multiple libraries, such as libraries you define in this build script,
# prebuilt third-party libraries, or system libraries.
target_link_libraries
(
# Specifies the target library.
Native
paddle_light_api_shared
${
OpenCV_LIBS
}
GLESv2
EGL
jnigraphics
${
log-lib
}
)
add_custom_command
(
TARGET Native
POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E copy
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libc++_shared.so
${
CMAKE_LIBRARY_OUTPUT_DIRECTORY
}
/libc++_shared.so
)
add_custom_command
(
TARGET Native
POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E copy
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libpaddle_light_api_shared.so
${
CMAKE_LIBRARY_OUTPUT_DIRECTORY
}
/libpaddle_light_api_shared.so
)
add_custom_command
(
TARGET Native
POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E copy
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libhiai.so
${
CMAKE_LIBRARY_OUTPUT_DIRECTORY
}
/libhiai.so
)
add_custom_command
(
TARGET Native
POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E copy
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libhiai_ir.so
${
CMAKE_LIBRARY_OUTPUT_DIRECTORY
}
/libhiai_ir.so
)
add_custom_command
(
TARGET Native
POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E copy
${
PaddleLite_DIR
}
/cxx/libs/
${
ANDROID_ABI
}
/libhiai_ir_build.so
${
CMAKE_LIBRARY_OUTPUT_DIRECTORY
}
/libhiai_ir_build.so
)
\ No newline at end of file
deploy/android_demo/app/src/main/cpp/common.h
0 → 100644
View file @
f8889760
//
// Created by fu on 4/25/18.
//
#pragma once
#import <numeric>
#import <vector>
#ifdef __ANDROID__
#include <android/log.h>
#define LOG_TAG "OCR_NDK"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#else
#include <stdio.h>
#define LOGI(format, ...) \
fprintf(stdout, "[" LOG_TAG "]" format "\n", ##__VA_ARGS__)
#define LOGW(format, ...) \
fprintf(stdout, "[" LOG_TAG "]" format "\n", ##__VA_ARGS__)
#define LOGE(format, ...) \
fprintf(stderr, "[" LOG_TAG "]Error: " format "\n", ##__VA_ARGS__)
#endif
enum
RETURN_CODE
{
RETURN_OK
=
0
};
enum
NET_TYPE
{
NET_OCR
=
900100
,
NET_OCR_INTERNAL
=
991008
};
template
<
typename
T
>
inline
T
product
(
const
std
::
vector
<
T
>
&
vec
)
{
if
(
vec
.
empty
())
{
return
0
;
}
return
std
::
accumulate
(
vec
.
begin
(),
vec
.
end
(),
1
,
std
::
multiplies
<
T
>
());
}
deploy/android_demo/app/src/main/cpp/native.cpp
0 → 100644
View file @
f8889760
//
// Created by fujiayi on 2020/7/5.
//
#include "native.h"
#include "ocr_ppredictor.h"
#include <algorithm>
#include <paddle_api.h>
#include <string>
static
paddle
::
lite_api
::
PowerMode
str_to_cpu_mode
(
const
std
::
string
&
cpu_mode
);
extern
"C"
JNIEXPORT
jlong
JNICALL
Java_com_baidu_paddle_lite_demo_ocr_OCRPredictorNative_init
(
JNIEnv
*
env
,
jobject
thiz
,
jstring
j_det_model_path
,
jstring
j_rec_model_path
,
jstring
j_cls_model_path
,
jint
j_thread_num
,
jstring
j_cpu_mode
)
{
std
::
string
det_model_path
=
jstring_to_cpp_string
(
env
,
j_det_model_path
);
std
::
string
rec_model_path
=
jstring_to_cpp_string
(
env
,
j_rec_model_path
);
std
::
string
cls_model_path
=
jstring_to_cpp_string
(
env
,
j_cls_model_path
);
int
thread_num
=
j_thread_num
;
std
::
string
cpu_mode
=
jstring_to_cpp_string
(
env
,
j_cpu_mode
);
ppredictor
::
OCR_Config
conf
;
conf
.
thread_num
=
thread_num
;
conf
.
mode
=
str_to_cpu_mode
(
cpu_mode
);
ppredictor
::
OCR_PPredictor
*
orc_predictor
=
new
ppredictor
::
OCR_PPredictor
{
conf
};
orc_predictor
->
init_from_file
(
det_model_path
,
rec_model_path
,
cls_model_path
);
return
reinterpret_cast
<
jlong
>
(
orc_predictor
);
}
/**
* "LITE_POWER_HIGH" convert to paddle::lite_api::LITE_POWER_HIGH
* @param cpu_mode
* @return
*/
static
paddle
::
lite_api
::
PowerMode
str_to_cpu_mode
(
const
std
::
string
&
cpu_mode
)
{
static
std
::
map
<
std
::
string
,
paddle
::
lite_api
::
PowerMode
>
cpu_mode_map
{
{
"LITE_POWER_HIGH"
,
paddle
::
lite_api
::
LITE_POWER_HIGH
},
{
"LITE_POWER_LOW"
,
paddle
::
lite_api
::
LITE_POWER_HIGH
},
{
"LITE_POWER_FULL"
,
paddle
::
lite_api
::
LITE_POWER_FULL
},
{
"LITE_POWER_NO_BIND"
,
paddle
::
lite_api
::
LITE_POWER_NO_BIND
},
{
"LITE_POWER_RAND_HIGH"
,
paddle
::
lite_api
::
LITE_POWER_RAND_HIGH
},
{
"LITE_POWER_RAND_LOW"
,
paddle
::
lite_api
::
LITE_POWER_RAND_LOW
}};
std
::
string
upper_key
;
std
::
transform
(
cpu_mode
.
cbegin
(),
cpu_mode
.
cend
(),
upper_key
.
begin
(),
::
toupper
);
auto
index
=
cpu_mode_map
.
find
(
upper_key
);
if
(
index
==
cpu_mode_map
.
end
())
{
LOGE
(
"cpu_mode not found %s"
,
upper_key
.
c_str
());
return
paddle
::
lite_api
::
LITE_POWER_HIGH
;
}
else
{
return
index
->
second
;
}
}
extern
"C"
JNIEXPORT
jfloatArray
JNICALL
Java_com_baidu_paddle_lite_demo_ocr_OCRPredictorNative_forward
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
java_pointer
,
jfloatArray
buf
,
jfloatArray
ddims
,
jobject
original_image
)
{
LOGI
(
"begin to run native forward"
);
if
(
java_pointer
==
0
)
{
LOGE
(
"JAVA pointer is NULL"
);
return
cpp_array_to_jfloatarray
(
env
,
nullptr
,
0
);
}
cv
::
Mat
origin
=
bitmap_to_cv_mat
(
env
,
original_image
);
if
(
origin
.
size
==
0
)
{
LOGE
(
"origin bitmap cannot convert to CV Mat"
);
return
cpp_array_to_jfloatarray
(
env
,
nullptr
,
0
);
}
ppredictor
::
OCR_PPredictor
*
ppredictor
=
(
ppredictor
::
OCR_PPredictor
*
)
java_pointer
;
std
::
vector
<
float
>
dims_float_arr
=
jfloatarray_to_float_vector
(
env
,
ddims
);
std
::
vector
<
int64_t
>
dims_arr
;
dims_arr
.
resize
(
dims_float_arr
.
size
());
std
::
copy
(
dims_float_arr
.
cbegin
(),
dims_float_arr
.
cend
(),
dims_arr
.
begin
());
// 这里值有点大,就不调用jfloatarray_to_float_vector了
int64_t
buf_len
=
(
int64_t
)
env
->
GetArrayLength
(
buf
);
jfloat
*
buf_data
=
env
->
GetFloatArrayElements
(
buf
,
JNI_FALSE
);
float
*
data
=
(
jfloat
*
)
buf_data
;
std
::
vector
<
ppredictor
::
OCRPredictResult
>
results
=
ppredictor
->
infer_ocr
(
dims_arr
,
data
,
buf_len
,
NET_OCR
,
origin
);
LOGI
(
"infer_ocr finished with boxes %ld"
,
results
.
size
());
// 这里将std::vector<ppredictor::OCRPredictResult> 序列化成
// float数组,传输到java层再反序列化
std
::
vector
<
float
>
float_arr
;
for
(
const
ppredictor
::
OCRPredictResult
&
r
:
results
)
{
float_arr
.
push_back
(
r
.
points
.
size
());
float_arr
.
push_back
(
r
.
word_index
.
size
());
float_arr
.
push_back
(
r
.
score
);
for
(
const
std
::
vector
<
int
>
&
point
:
r
.
points
)
{
float_arr
.
push_back
(
point
.
at
(
0
));
float_arr
.
push_back
(
point
.
at
(
1
));
}
for
(
int
index
:
r
.
word_index
)
{
float_arr
.
push_back
(
index
);
}
}
return
cpp_array_to_jfloatarray
(
env
,
float_arr
.
data
(),
float_arr
.
size
());
}
extern
"C"
JNIEXPORT
void
JNICALL
Java_com_baidu_paddle_lite_demo_ocr_OCRPredictorNative_release
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
java_pointer
)
{
if
(
java_pointer
==
0
)
{
LOGE
(
"JAVA pointer is NULL"
);
return
;
}
ppredictor
::
OCR_PPredictor
*
ppredictor
=
(
ppredictor
::
OCR_PPredictor
*
)
java_pointer
;
delete
ppredictor
;
}
\ No newline at end of file
deploy/android_demo/app/src/main/cpp/native.h
0 → 100644
View file @
f8889760
//
// Created by fujiayi on 2020/7/5.
//
#pragma once
#include "common.h"
#include <android/bitmap.h>
#include <jni.h>
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
inline
std
::
string
jstring_to_cpp_string
(
JNIEnv
*
env
,
jstring
jstr
)
{
// In java, a unicode char will be encoded using 2 bytes (utf16).
// so jstring will contain characters utf16. std::string in c++ is
// essentially a string of bytes, not characters, so if we want to
// pass jstring from JNI to c++, we have convert utf16 to bytes.
if
(
!
jstr
)
{
return
""
;
}
const
jclass
stringClass
=
env
->
GetObjectClass
(
jstr
);
const
jmethodID
getBytes
=
env
->
GetMethodID
(
stringClass
,
"getBytes"
,
"(Ljava/lang/String;)[B"
);
const
jbyteArray
stringJbytes
=
(
jbyteArray
)
env
->
CallObjectMethod
(
jstr
,
getBytes
,
env
->
NewStringUTF
(
"UTF-8"
));
size_t
length
=
(
size_t
)
env
->
GetArrayLength
(
stringJbytes
);
jbyte
*
pBytes
=
env
->
GetByteArrayElements
(
stringJbytes
,
NULL
);
std
::
string
ret
=
std
::
string
(
reinterpret_cast
<
char
*>
(
pBytes
),
length
);
env
->
ReleaseByteArrayElements
(
stringJbytes
,
pBytes
,
JNI_ABORT
);
env
->
DeleteLocalRef
(
stringJbytes
);
env
->
DeleteLocalRef
(
stringClass
);
return
ret
;
}
inline
jstring
cpp_string_to_jstring
(
JNIEnv
*
env
,
std
::
string
str
)
{
auto
*
data
=
str
.
c_str
();
jclass
strClass
=
env
->
FindClass
(
"java/lang/String"
);
jmethodID
strClassInitMethodID
=
env
->
GetMethodID
(
strClass
,
"<init>"
,
"([BLjava/lang/String;)V"
);
jbyteArray
bytes
=
env
->
NewByteArray
(
strlen
(
data
));
env
->
SetByteArrayRegion
(
bytes
,
0
,
strlen
(
data
),
reinterpret_cast
<
const
jbyte
*>
(
data
));
jstring
encoding
=
env
->
NewStringUTF
(
"UTF-8"
);
jstring
res
=
(
jstring
)(
env
->
NewObject
(
strClass
,
strClassInitMethodID
,
bytes
,
encoding
));
env
->
DeleteLocalRef
(
strClass
);
env
->
DeleteLocalRef
(
encoding
);
env
->
DeleteLocalRef
(
bytes
);
return
res
;
}
inline
jfloatArray
cpp_array_to_jfloatarray
(
JNIEnv
*
env
,
const
float
*
buf
,
int64_t
len
)
{
if
(
len
==
0
)
{
return
env
->
NewFloatArray
(
0
);
}
jfloatArray
result
=
env
->
NewFloatArray
(
len
);
env
->
SetFloatArrayRegion
(
result
,
0
,
len
,
buf
);
return
result
;
}
inline
jintArray
cpp_array_to_jintarray
(
JNIEnv
*
env
,
const
int
*
buf
,
int64_t
len
)
{
jintArray
result
=
env
->
NewIntArray
(
len
);
env
->
SetIntArrayRegion
(
result
,
0
,
len
,
buf
);
return
result
;
}
inline
jbyteArray
cpp_array_to_jbytearray
(
JNIEnv
*
env
,
const
int8_t
*
buf
,
int64_t
len
)
{
jbyteArray
result
=
env
->
NewByteArray
(
len
);
env
->
SetByteArrayRegion
(
result
,
0
,
len
,
buf
);
return
result
;
}
inline
jlongArray
int64_vector_to_jlongarray
(
JNIEnv
*
env
,
const
std
::
vector
<
int64_t
>
&
vec
)
{
jlongArray
result
=
env
->
NewLongArray
(
vec
.
size
());
jlong
*
buf
=
new
jlong
[
vec
.
size
()];
for
(
size_t
i
=
0
;
i
<
vec
.
size
();
++
i
)
{
buf
[
i
]
=
(
jlong
)
vec
[
i
];
}
env
->
SetLongArrayRegion
(
result
,
0
,
vec
.
size
(),
buf
);
delete
[]
buf
;
return
result
;
}
inline
std
::
vector
<
int64_t
>
jlongarray_to_int64_vector
(
JNIEnv
*
env
,
jlongArray
data
)
{
int
data_size
=
env
->
GetArrayLength
(
data
);
jlong
*
data_ptr
=
env
->
GetLongArrayElements
(
data
,
nullptr
);
std
::
vector
<
int64_t
>
data_vec
(
data_ptr
,
data_ptr
+
data_size
);
env
->
ReleaseLongArrayElements
(
data
,
data_ptr
,
0
);
return
data_vec
;
}
inline
std
::
vector
<
float
>
jfloatarray_to_float_vector
(
JNIEnv
*
env
,
jfloatArray
data
)
{
int
data_size
=
env
->
GetArrayLength
(
data
);
jfloat
*
data_ptr
=
env
->
GetFloatArrayElements
(
data
,
nullptr
);
std
::
vector
<
float
>
data_vec
(
data_ptr
,
data_ptr
+
data_size
);
env
->
ReleaseFloatArrayElements
(
data
,
data_ptr
,
0
);
return
data_vec
;
}
inline
cv
::
Mat
bitmap_to_cv_mat
(
JNIEnv
*
env
,
jobject
bitmap
)
{
AndroidBitmapInfo
info
;
int
result
=
AndroidBitmap_getInfo
(
env
,
bitmap
,
&
info
);
if
(
result
!=
ANDROID_BITMAP_RESULT_SUCCESS
)
{
LOGE
(
"AndroidBitmap_getInfo failed, result: %d"
,
result
);
return
cv
::
Mat
{};
}
if
(
info
.
format
!=
ANDROID_BITMAP_FORMAT_RGBA_8888
)
{
LOGE
(
"Bitmap format is not RGBA_8888 !"
);
return
cv
::
Mat
{};
}
unsigned
char
*
srcData
=
NULL
;
AndroidBitmap_lockPixels
(
env
,
bitmap
,
(
void
**
)
&
srcData
);
cv
::
Mat
mat
=
cv
::
Mat
::
zeros
(
info
.
height
,
info
.
width
,
CV_8UC4
);
memcpy
(
mat
.
data
,
srcData
,
info
.
height
*
info
.
width
*
4
);
AndroidBitmap_unlockPixels
(
env
,
bitmap
);
cv
::
cvtColor
(
mat
,
mat
,
cv
::
COLOR_RGBA2BGR
);
/**
if (!cv::imwrite("/sdcard/1/copy.jpg", mat)){
LOGE("Write image failed " );
}
*/
return
mat
;
}
deploy/android_demo/app/src/main/cpp/ocr_clipper.cpp
0 → 100644
View file @
f8889760
This diff is collapsed.
Click to expand it.
deploy/android_demo/app/src/main/cpp/ocr_clipper.hpp
0 → 100644
View file @
f8889760
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 6.4.2 *
* Date : 27 February 2017 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2017 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1. *
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm: *
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
*******************************************************************************/
#ifndef clipper_hpp
#define clipper_hpp
#define CLIPPER_VERSION "6.4.2"
// use_int32: When enabled 32bit ints are used instead of 64bit ints. This
// improve performance but coordinate values are limited to the range +/- 46340
//#define use_int32
// use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance.
//#define use_xyz
// use_lines: Enables line clipping. Adds a very minor cost to performance.
#define use_lines
// use_deprecated: Enables temporary support for the obsolete functions
//#define use_deprecated
#include <cstdlib>
#include <cstring>
#include <functional>
#include <list>
#include <ostream>
#include <queue>
#include <set>
#include <stdexcept>
#include <vector>
namespace
ClipperLib
{
enum
ClipType
{
ctIntersection
,
ctUnion
,
ctDifference
,
ctXor
};
enum
PolyType
{
ptSubject
,
ptClip
};
// By far the most widely used winding rules for polygon filling are
// EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32)
// Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL)
// see http://glprogramming.com/red/chapter11.html
enum
PolyFillType
{
pftEvenOdd
,
pftNonZero
,
pftPositive
,
pftNegative
};
#ifdef use_int32
typedef
int
cInt
;
static
cInt
const
loRange
=
0x7FFF
;
static
cInt
const
hiRange
=
0x7FFF
;
#else
typedef
signed
long
long
cInt
;
static
cInt
const
loRange
=
0x3FFFFFFF
;
static
cInt
const
hiRange
=
0x3FFFFFFFFFFFFFFFLL
;
typedef
signed
long
long
long64
;
// used by Int128 class
typedef
unsigned
long
long
ulong64
;
#endif
struct
IntPoint
{
cInt
X
;
cInt
Y
;
#ifdef use_xyz
cInt
Z
;
IntPoint
(
cInt
x
=
0
,
cInt
y
=
0
,
cInt
z
=
0
)
:
X
(
x
),
Y
(
y
),
Z
(
z
){};
#else
IntPoint
(
cInt
x
=
0
,
cInt
y
=
0
)
:
X
(
x
),
Y
(
y
){};
#endif
friend
inline
bool
operator
==
(
const
IntPoint
&
a
,
const
IntPoint
&
b
)
{
return
a
.
X
==
b
.
X
&&
a
.
Y
==
b
.
Y
;
}
friend
inline
bool
operator
!=
(
const
IntPoint
&
a
,
const
IntPoint
&
b
)
{
return
a
.
X
!=
b
.
X
||
a
.
Y
!=
b
.
Y
;
}
};
//------------------------------------------------------------------------------
typedef
std
::
vector
<
IntPoint
>
Path
;
typedef
std
::
vector
<
Path
>
Paths
;
inline
Path
&
operator
<<
(
Path
&
poly
,
const
IntPoint
&
p
)
{
poly
.
push_back
(
p
);
return
poly
;
}
inline
Paths
&
operator
<<
(
Paths
&
polys
,
const
Path
&
p
)
{
polys
.
push_back
(
p
);
return
polys
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
IntPoint
&
p
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
Path
&
p
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
Paths
&
p
);
struct
DoublePoint
{
double
X
;
double
Y
;
DoublePoint
(
double
x
=
0
,
double
y
=
0
)
:
X
(
x
),
Y
(
y
)
{}
DoublePoint
(
IntPoint
ip
)
:
X
((
double
)
ip
.
X
),
Y
((
double
)
ip
.
Y
)
{}
};
//------------------------------------------------------------------------------
#ifdef use_xyz
typedef
void
(
*
ZFillCallback
)(
IntPoint
&
e1bot
,
IntPoint
&
e1top
,
IntPoint
&
e2bot
,
IntPoint
&
e2top
,
IntPoint
&
pt
);
#endif
enum
InitOptions
{
ioReverseSolution
=
1
,
ioStrictlySimple
=
2
,
ioPreserveCollinear
=
4
};
enum
JoinType
{
jtSquare
,
jtRound
,
jtMiter
};
enum
EndType
{
etClosedPolygon
,
etClosedLine
,
etOpenButt
,
etOpenSquare
,
etOpenRound
};
class
PolyNode
;
typedef
std
::
vector
<
PolyNode
*>
PolyNodes
;
class
PolyNode
{
public:
PolyNode
();
virtual
~
PolyNode
(){};
Path
Contour
;
PolyNodes
Childs
;
PolyNode
*
Parent
;
PolyNode
*
GetNext
()
const
;
bool
IsHole
()
const
;
bool
IsOpen
()
const
;
int
ChildCount
()
const
;
private:
// PolyNode& operator =(PolyNode& other);
unsigned
Index
;
// node index in Parent.Childs
bool
m_IsOpen
;
JoinType
m_jointype
;
EndType
m_endtype
;
PolyNode
*
GetNextSiblingUp
()
const
;
void
AddChild
(
PolyNode
&
child
);
friend
class
Clipper
;
// to access Index
friend
class
ClipperOffset
;
};
class
PolyTree
:
public
PolyNode
{
public:
~
PolyTree
()
{
Clear
();
};
PolyNode
*
GetFirst
()
const
;
void
Clear
();
int
Total
()
const
;
private:
// PolyTree& operator =(PolyTree& other);
PolyNodes
AllNodes
;
friend
class
Clipper
;
// to access AllNodes
};
bool
Orientation
(
const
Path
&
poly
);
double
Area
(
const
Path
&
poly
);
int
PointInPolygon
(
const
IntPoint
&
pt
,
const
Path
&
path
);
void
SimplifyPolygon
(
const
Path
&
in_poly
,
Paths
&
out_polys
,
PolyFillType
fillType
=
pftEvenOdd
);
void
SimplifyPolygons
(
const
Paths
&
in_polys
,
Paths
&
out_polys
,
PolyFillType
fillType
=
pftEvenOdd
);
void
SimplifyPolygons
(
Paths
&
polys
,
PolyFillType
fillType
=
pftEvenOdd
);
void
CleanPolygon
(
const
Path
&
in_poly
,
Path
&
out_poly
,
double
distance
=
1.415
);
void
CleanPolygon
(
Path
&
poly
,
double
distance
=
1.415
);
void
CleanPolygons
(
const
Paths
&
in_polys
,
Paths
&
out_polys
,
double
distance
=
1.415
);
void
CleanPolygons
(
Paths
&
polys
,
double
distance
=
1.415
);
void
MinkowskiSum
(
const
Path
&
pattern
,
const
Path
&
path
,
Paths
&
solution
,
bool
pathIsClosed
);
void
MinkowskiSum
(
const
Path
&
pattern
,
const
Paths
&
paths
,
Paths
&
solution
,
bool
pathIsClosed
);
void
MinkowskiDiff
(
const
Path
&
poly1
,
const
Path
&
poly2
,
Paths
&
solution
);
void
PolyTreeToPaths
(
const
PolyTree
&
polytree
,
Paths
&
paths
);
void
ClosedPathsFromPolyTree
(
const
PolyTree
&
polytree
,
Paths
&
paths
);
void
OpenPathsFromPolyTree
(
PolyTree
&
polytree
,
Paths
&
paths
);
void
ReversePath
(
Path
&
p
);
void
ReversePaths
(
Paths
&
p
);
struct
IntRect
{
cInt
left
;
cInt
top
;
cInt
right
;
cInt
bottom
;
};
// enums that are used internally ...
enum
EdgeSide
{
esLeft
=
1
,
esRight
=
2
};
// forward declarations (for stuff used internally) ...
struct
TEdge
;
struct
IntersectNode
;
struct
LocalMinimum
;
struct
OutPt
;
struct
OutRec
;
struct
Join
;
typedef
std
::
vector
<
OutRec
*>
PolyOutList
;
typedef
std
::
vector
<
TEdge
*>
EdgeList
;
typedef
std
::
vector
<
Join
*>
JoinList
;
typedef
std
::
vector
<
IntersectNode
*>
IntersectList
;
//------------------------------------------------------------------------------
// ClipperBase is the ancestor to the Clipper class. It should not be
// instantiated directly. This class simply abstracts the conversion of sets of
// polygon coordinates into edge objects that are stored in a LocalMinima list.
class
ClipperBase
{
public:
ClipperBase
();
virtual
~
ClipperBase
();
virtual
bool
AddPath
(
const
Path
&
pg
,
PolyType
PolyTyp
,
bool
Closed
);
bool
AddPaths
(
const
Paths
&
ppg
,
PolyType
PolyTyp
,
bool
Closed
);
virtual
void
Clear
();
IntRect
GetBounds
();
bool
PreserveCollinear
()
{
return
m_PreserveCollinear
;
};
void
PreserveCollinear
(
bool
value
)
{
m_PreserveCollinear
=
value
;
};
protected:
void
DisposeLocalMinimaList
();
TEdge
*
AddBoundsToLML
(
TEdge
*
e
,
bool
IsClosed
);
virtual
void
Reset
();
TEdge
*
ProcessBound
(
TEdge
*
E
,
bool
IsClockwise
);
void
InsertScanbeam
(
const
cInt
Y
);
bool
PopScanbeam
(
cInt
&
Y
);
bool
LocalMinimaPending
();
bool
PopLocalMinima
(
cInt
Y
,
const
LocalMinimum
*&
locMin
);
OutRec
*
CreateOutRec
();
void
DisposeAllOutRecs
();
void
DisposeOutRec
(
PolyOutList
::
size_type
index
);
void
SwapPositionsInAEL
(
TEdge
*
edge1
,
TEdge
*
edge2
);
void
DeleteFromAEL
(
TEdge
*
e
);
void
UpdateEdgeIntoAEL
(
TEdge
*&
e
);
typedef
std
::
vector
<
LocalMinimum
>
MinimaList
;
MinimaList
::
iterator
m_CurrentLM
;
MinimaList
m_MinimaList
;
bool
m_UseFullRange
;
EdgeList
m_edges
;
bool
m_PreserveCollinear
;
bool
m_HasOpenPaths
;
PolyOutList
m_PolyOuts
;
TEdge
*
m_ActiveEdges
;
typedef
std
::
priority_queue
<
cInt
>
ScanbeamList
;
ScanbeamList
m_Scanbeam
;
};
//------------------------------------------------------------------------------
class
Clipper
:
public
virtual
ClipperBase
{
public:
Clipper
(
int
initOptions
=
0
);
bool
Execute
(
ClipType
clipType
,
Paths
&
solution
,
PolyFillType
fillType
=
pftEvenOdd
);
bool
Execute
(
ClipType
clipType
,
Paths
&
solution
,
PolyFillType
subjFillType
,
PolyFillType
clipFillType
);
bool
Execute
(
ClipType
clipType
,
PolyTree
&
polytree
,
PolyFillType
fillType
=
pftEvenOdd
);
bool
Execute
(
ClipType
clipType
,
PolyTree
&
polytree
,
PolyFillType
subjFillType
,
PolyFillType
clipFillType
);
bool
ReverseSolution
()
{
return
m_ReverseOutput
;
};
void
ReverseSolution
(
bool
value
)
{
m_ReverseOutput
=
value
;
};
bool
StrictlySimple
()
{
return
m_StrictSimple
;
};
void
StrictlySimple
(
bool
value
)
{
m_StrictSimple
=
value
;
};
// set the callback function for z value filling on intersections (otherwise Z
// is 0)
#ifdef use_xyz
void
ZFillFunction
(
ZFillCallback
zFillFunc
);
#endif
protected:
virtual
bool
ExecuteInternal
();
private:
JoinList
m_Joins
;
JoinList
m_GhostJoins
;
IntersectList
m_IntersectList
;
ClipType
m_ClipType
;
typedef
std
::
list
<
cInt
>
MaximaList
;
MaximaList
m_Maxima
;
TEdge
*
m_SortedEdges
;
bool
m_ExecuteLocked
;
PolyFillType
m_ClipFillType
;
PolyFillType
m_SubjFillType
;
bool
m_ReverseOutput
;
bool
m_UsingPolyTree
;
bool
m_StrictSimple
;
#ifdef use_xyz
ZFillCallback
m_ZFill
;
// custom callback
#endif
void
SetWindingCount
(
TEdge
&
edge
);
bool
IsEvenOddFillType
(
const
TEdge
&
edge
)
const
;
bool
IsEvenOddAltFillType
(
const
TEdge
&
edge
)
const
;
void
InsertLocalMinimaIntoAEL
(
const
cInt
botY
);
void
InsertEdgeIntoAEL
(
TEdge
*
edge
,
TEdge
*
startEdge
);
void
AddEdgeToSEL
(
TEdge
*
edge
);
bool
PopEdgeFromSEL
(
TEdge
*&
edge
);
void
CopyAELToSEL
();
void
DeleteFromSEL
(
TEdge
*
e
);
void
SwapPositionsInSEL
(
TEdge
*
edge1
,
TEdge
*
edge2
);
bool
IsContributing
(
const
TEdge
&
edge
)
const
;
bool
IsTopHorz
(
const
cInt
XPos
);
void
DoMaxima
(
TEdge
*
e
);
void
ProcessHorizontals
();
void
ProcessHorizontal
(
TEdge
*
horzEdge
);
void
AddLocalMaxPoly
(
TEdge
*
e1
,
TEdge
*
e2
,
const
IntPoint
&
pt
);
OutPt
*
AddLocalMinPoly
(
TEdge
*
e1
,
TEdge
*
e2
,
const
IntPoint
&
pt
);
OutRec
*
GetOutRec
(
int
idx
);
void
AppendPolygon
(
TEdge
*
e1
,
TEdge
*
e2
);
void
IntersectEdges
(
TEdge
*
e1
,
TEdge
*
e2
,
IntPoint
&
pt
);
OutPt
*
AddOutPt
(
TEdge
*
e
,
const
IntPoint
&
pt
);
OutPt
*
GetLastOutPt
(
TEdge
*
e
);
bool
ProcessIntersections
(
const
cInt
topY
);
void
BuildIntersectList
(
const
cInt
topY
);
void
ProcessIntersectList
();
void
ProcessEdgesAtTopOfScanbeam
(
const
cInt
topY
);
void
BuildResult
(
Paths
&
polys
);
void
BuildResult2
(
PolyTree
&
polytree
);
void
SetHoleState
(
TEdge
*
e
,
OutRec
*
outrec
);
void
DisposeIntersectNodes
();
bool
FixupIntersectionOrder
();
void
FixupOutPolygon
(
OutRec
&
outrec
);
void
FixupOutPolyline
(
OutRec
&
outrec
);
bool
IsHole
(
TEdge
*
e
);
bool
FindOwnerFromSplitRecs
(
OutRec
&
outRec
,
OutRec
*&
currOrfl
);
void
FixHoleLinkage
(
OutRec
&
outrec
);
void
AddJoin
(
OutPt
*
op1
,
OutPt
*
op2
,
const
IntPoint
offPt
);
void
ClearJoins
();
void
ClearGhostJoins
();
void
AddGhostJoin
(
OutPt
*
op
,
const
IntPoint
offPt
);
bool
JoinPoints
(
Join
*
j
,
OutRec
*
outRec1
,
OutRec
*
outRec2
);
void
JoinCommonEdges
();
void
DoSimplePolygons
();
void
FixupFirstLefts1
(
OutRec
*
OldOutRec
,
OutRec
*
NewOutRec
);
void
FixupFirstLefts2
(
OutRec
*
InnerOutRec
,
OutRec
*
OuterOutRec
);
void
FixupFirstLefts3
(
OutRec
*
OldOutRec
,
OutRec
*
NewOutRec
);
#ifdef use_xyz
void
SetZ
(
IntPoint
&
pt
,
TEdge
&
e1
,
TEdge
&
e2
);
#endif
};
//------------------------------------------------------------------------------
class
ClipperOffset
{
public:
ClipperOffset
(
double
miterLimit
=
2.0
,
double
roundPrecision
=
0.25
);
~
ClipperOffset
();
void
AddPath
(
const
Path
&
path
,
JoinType
joinType
,
EndType
endType
);
void
AddPaths
(
const
Paths
&
paths
,
JoinType
joinType
,
EndType
endType
);
void
Execute
(
Paths
&
solution
,
double
delta
);
void
Execute
(
PolyTree
&
solution
,
double
delta
);
void
Clear
();
double
MiterLimit
;
double
ArcTolerance
;
private:
Paths
m_destPolys
;
Path
m_srcPoly
;
Path
m_destPoly
;
std
::
vector
<
DoublePoint
>
m_normals
;
double
m_delta
,
m_sinA
,
m_sin
,
m_cos
;
double
m_miterLim
,
m_StepsPerRad
;
IntPoint
m_lowest
;
PolyNode
m_polyNodes
;
void
FixOrientations
();
void
DoOffset
(
double
delta
);
void
OffsetPoint
(
int
j
,
int
&
k
,
JoinType
jointype
);
void
DoSquare
(
int
j
,
int
k
);
void
DoMiter
(
int
j
,
int
k
,
double
r
);
void
DoRound
(
int
j
,
int
k
);
};
//------------------------------------------------------------------------------
class
clipperException
:
public
std
::
exception
{
public:
clipperException
(
const
char
*
description
)
:
m_descr
(
description
)
{}
virtual
~
clipperException
()
throw
()
{}
virtual
const
char
*
what
()
const
throw
()
{
return
m_descr
.
c_str
();
}
private:
std
::
string
m_descr
;
};
//------------------------------------------------------------------------------
}
// ClipperLib namespace
#endif // clipper_hpp
deploy/android_demo/app/src/main/cpp/ocr_cls_process.cpp
0 → 100644
View file @
f8889760
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ocr_cls_process.h"
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iostream>
#include <vector>
const
std
::
vector
<
int
>
CLS_IMAGE_SHAPE
=
{
3
,
48
,
192
};
cv
::
Mat
cls_resize_img
(
const
cv
::
Mat
&
img
)
{
int
imgC
=
CLS_IMAGE_SHAPE
[
0
];
int
imgW
=
CLS_IMAGE_SHAPE
[
2
];
int
imgH
=
CLS_IMAGE_SHAPE
[
1
];
float
ratio
=
float
(
img
.
cols
)
/
float
(
img
.
rows
);
int
resize_w
=
0
;
if
(
ceilf
(
imgH
*
ratio
)
>
imgW
)
resize_w
=
imgW
;
else
resize_w
=
int
(
ceilf
(
imgH
*
ratio
));
cv
::
Mat
resize_img
;
cv
::
resize
(
img
,
resize_img
,
cv
::
Size
(
resize_w
,
imgH
),
0.
f
,
0.
f
,
cv
::
INTER_CUBIC
);
if
(
resize_w
<
imgW
)
{
cv
::
copyMakeBorder
(
resize_img
,
resize_img
,
0
,
0
,
0
,
int
(
imgW
-
resize_w
),
cv
::
BORDER_CONSTANT
,
{
0
,
0
,
0
});
}
return
resize_img
;
}
\ No newline at end of file
deploy/android_demo/app/src/main/cpp/ocr_cls_process.h
0 → 100644
View file @
f8889760
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "common.h"
#include <opencv2/opencv.hpp>
#include <vector>
extern
const
std
::
vector
<
int
>
CLS_IMAGE_SHAPE
;
cv
::
Mat
cls_resize_img
(
const
cv
::
Mat
&
img
);
\ No newline at end of file
Prev
1
2
3
4
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment