mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 16:48:03 +08:00
[Android] Support PP-OCRv2 & PP-OCRv3 in Android (#445)
* [Android] Add Android build docs and demo (#26) * [Backend] Add override flag to lite backend * [Docs] Add Android C++ SDK build docs * [Doc] fix android_build_docs typos * Update CMakeLists.txt * Update android.md * [Doc] Add PicoDet Android demo docs * [Doc] Update PicoDet Andorid demo docs * [Doc] Update PaddleClasModel Android demo docs * [Doc] Update fastdeploy android jni docs * [Doc] Update fastdeploy android jni usage docs * [Android] init fastdeploy android jar package * [Backend] support int8 option for lite backend * [Model] add Backend::Lite to paddle model * [Backend] use CopyFromCpu for lite backend. * [Android] package jni srcs and java api into aar * Update infer.cc * Update infer.cc * [Android] Update package build.gradle * [Android] Update android app examples * [Android] update android detection app * [Android] Support PP-OCRv2 & PP-OCRv3 in Android * [Android] bind ORCResult with JNI * [Android] move static class jni method to instance method * [Android] bind VisOcr and VisClassification via JNI * [Android] Add PP-OCRv2 & v3 android demo
This commit is contained in:
@@ -28,6 +28,7 @@ add_library(
|
||||
SHARED
|
||||
utils_jni.cc
|
||||
bitmap_jni.cc
|
||||
pipeline/ppocr_jni.cc
|
||||
vision/results_jni.cc
|
||||
vision/visualize_jni.cc
|
||||
vision/detection/picodet_jni.cc
|
||||
|
418
java/android/fastdeploy/src/main/cpp/pipeline/ppocr_jni.cc
Normal file
418
java/android/fastdeploy/src/main/cpp/pipeline/ppocr_jni.cc
Normal file
@@ -0,0 +1,418 @@
|
||||
// Copyright (c) 2022 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 <jni.h> // NOLINT
|
||||
|
||||
#include "fastdeploy_jni.h" // NOLINT
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace jni {
|
||||
namespace pipeline {
|
||||
|
||||
enum PPOCRVersion {
|
||||
OCR_V1 = 0,
|
||||
OCR_V2 = 1,
|
||||
OCR_V3 = 2
|
||||
};
|
||||
|
||||
/// Handle the native PP-OCR pipeline resources.
|
||||
class PPOCRHandler {
|
||||
public:
|
||||
PPOCRHandler() = default;
|
||||
|
||||
PPOCRHandler(fastdeploy::vision::ocr::DBDetector *det_model,
|
||||
fastdeploy::vision::ocr::Classifier *cls_model,
|
||||
fastdeploy::vision::ocr::Recognizer *rec_model,
|
||||
fastdeploy::pipeline::PPOCRv2 *ppocr_v2) :
|
||||
detector_(det_model), classifier_(cls_model),
|
||||
recognizer_(rec_model), ppocr_v2_(ppocr_v2) {
|
||||
if (detector_ != nullptr && classifier_ != nullptr
|
||||
&& recognizer_ != nullptr && ppocr_v2_ != nullptr) {
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
PPOCRHandler(fastdeploy::vision::ocr::DBDetector *det_model,
|
||||
fastdeploy::vision::ocr::Recognizer *rec_model,
|
||||
fastdeploy::pipeline::PPOCRv2 *ppocr_v2) :
|
||||
detector_(det_model), recognizer_(rec_model),
|
||||
ppocr_v2_(ppocr_v2) {
|
||||
if (detector_ != nullptr && recognizer_ != nullptr
|
||||
&& ppocr_v2_ != nullptr) {
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
PPOCRHandler(fastdeploy::vision::ocr::DBDetector *det_model,
|
||||
fastdeploy::vision::ocr::Classifier *cls_model,
|
||||
fastdeploy::vision::ocr::Recognizer *rec_model,
|
||||
fastdeploy::pipeline::PPOCRv3 *ppocr_v3) :
|
||||
detector_(det_model), classifier_(cls_model),
|
||||
recognizer_(rec_model), ppocr_v3_(ppocr_v3) {
|
||||
if (detector_ != nullptr && classifier_ != nullptr
|
||||
&& recognizer_ != nullptr && ppocr_v3_ != nullptr) {
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
PPOCRHandler(fastdeploy::vision::ocr::DBDetector *det_model,
|
||||
fastdeploy::vision::ocr::Recognizer *rec_model,
|
||||
fastdeploy::pipeline::PPOCRv3 *ppocr_v3) :
|
||||
detector_(det_model), recognizer_(rec_model),
|
||||
ppocr_v3_(ppocr_v3) {
|
||||
if (detector_ != nullptr && recognizer_ != nullptr
|
||||
&& ppocr_v3_ != nullptr) {
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SetPPOCRVersion(PPOCRVersion version_tag) {
|
||||
ppocr_version_tag_ = version_tag;
|
||||
}
|
||||
|
||||
bool Predict(cv::Mat* img, fastdeploy::vision::OCRResult* result) {
|
||||
if (ppocr_version_tag_ == PPOCRVersion::OCR_V2) {
|
||||
if (ppocr_v2_ != nullptr) {
|
||||
return ppocr_v2_->Predict(img, result);
|
||||
}
|
||||
return false;
|
||||
} else if (ppocr_version_tag_ == PPOCRVersion::OCR_V3) {
|
||||
if (ppocr_v3_ != nullptr) {
|
||||
return ppocr_v3_->Predict(img, result);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Initialized() {
|
||||
if (!initialized_) {
|
||||
return false;
|
||||
}
|
||||
if (ppocr_version_tag_ == PPOCRVersion::OCR_V2) {
|
||||
if (ppocr_v2_ != nullptr) {
|
||||
return ppocr_v2_->Initialized();
|
||||
}
|
||||
return false;
|
||||
} else if (ppocr_version_tag_ == PPOCRVersion::OCR_V3) {
|
||||
if (ppocr_v3_ != nullptr) {
|
||||
return ppocr_v3_->Initialized();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Call init manually if you want to release the allocated
|
||||
// PP-OCRv2/v3's memory by 'new' operator via 'delete'.
|
||||
bool ReleaseAllocatedOCRMemories() {
|
||||
if (!Initialized()) {
|
||||
return false;
|
||||
}
|
||||
if (detector_ != nullptr) {
|
||||
delete detector_;
|
||||
detector_ = nullptr;
|
||||
LOGD("[End] Release DBDetector in native !");
|
||||
}
|
||||
if (classifier_ != nullptr) {
|
||||
delete classifier_;
|
||||
classifier_ = nullptr;
|
||||
LOGD("[End] Release Classifier in native !");
|
||||
}
|
||||
if (recognizer_ != nullptr) {
|
||||
delete recognizer_;
|
||||
recognizer_ = nullptr;
|
||||
LOGD("[End] Release Recognizer in native !");
|
||||
}
|
||||
if (ppocr_v2_ != nullptr) {
|
||||
delete ppocr_v2_;
|
||||
ppocr_v2_ = nullptr;
|
||||
LOGD("[End] Release PP-OCRv2 in native !");
|
||||
}
|
||||
if (ppocr_v3_ != nullptr) {
|
||||
delete ppocr_v3_;
|
||||
ppocr_v3_ = nullptr;
|
||||
LOGD("[End] Release PP-OCRv3 in native !");
|
||||
}
|
||||
initialized_ = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PrintPPOCRHandlerTimeOfRuntime() const {
|
||||
if ((detector_ != nullptr) && (detector_->EnabledRecordTimeOfRuntime())) {
|
||||
auto det_info_of_runtime = detector_->PrintStatisInfoOfRuntime();
|
||||
LOGD("[Det] Avg runtime costs %f ms", det_info_of_runtime["avg_time"] * 1000.0f);
|
||||
}
|
||||
if ((classifier_ != nullptr) && (classifier_->EnabledRecordTimeOfRuntime())) {
|
||||
auto cls_info_of_runtime = classifier_->PrintStatisInfoOfRuntime();
|
||||
LOGD("[Cls] Avg runtime costs %f ms", cls_info_of_runtime["avg_time"] * 1000.0f);
|
||||
}
|
||||
if ((recognizer_ != nullptr) && (recognizer_->EnabledRecordTimeOfRuntime())) {
|
||||
auto rec_info_of_runtime = recognizer_->PrintStatisInfoOfRuntime();
|
||||
LOGD("[Rec] Avg runtime costs %f ms", rec_info_of_runtime["avg_time"] * 1000.0f);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
fastdeploy::vision::ocr::DBDetector *detector_ = nullptr;
|
||||
fastdeploy::vision::ocr::Classifier *classifier_ = nullptr;
|
||||
fastdeploy::vision::ocr::Recognizer *recognizer_ = nullptr;
|
||||
fastdeploy::pipeline::PPOCRv2 *ppocr_v2_ = nullptr;
|
||||
fastdeploy::pipeline::PPOCRv3 *ppocr_v3_ = nullptr;
|
||||
|
||||
private:
|
||||
bool initialized_ = false;
|
||||
PPOCRVersion ppocr_version_tag_ = PPOCRVersion::OCR_V2;
|
||||
};
|
||||
|
||||
} // namespace pipeline
|
||||
} // namespace jni
|
||||
} // namespace fastdeploy
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_pipeline_PPOCRBase_bindNative(
|
||||
JNIEnv *env,
|
||||
jobject thiz,
|
||||
jint ocr_version_tag,
|
||||
jstring det_model_file,
|
||||
jstring det_params_file,
|
||||
jstring cls_model_file,
|
||||
jstring cls_params_file,
|
||||
jstring rec_model_file,
|
||||
jstring rec_params_file,
|
||||
jstring rec_label_path,
|
||||
jint det_cpu_num_thread,
|
||||
jint cls_cpu_num_thread,
|
||||
jint rec_cpu_num_thread,
|
||||
jboolean det_enable_lite_fp16,
|
||||
jboolean cls_enable_lite_fp16,
|
||||
jboolean rec_enable_lite_fp16,
|
||||
jint det_lite_power_mode,
|
||||
jint cls_lite_power_mode,
|
||||
jint rec_lite_power_mode,
|
||||
jstring det_lite_optimized_model_dir,
|
||||
jstring cls_lite_optimized_model_dir,
|
||||
jstring rec_lite_optimized_model_dir,
|
||||
jboolean det_enable_record_time_of_runtime,
|
||||
jboolean cls_enable_record_time_of_runtime,
|
||||
jboolean rec_enable_record_time_of_runtime,
|
||||
jboolean have_cls_model) {
|
||||
auto c_ocr_version_tag = static_cast<
|
||||
fastdeploy::jni::pipeline::PPOCRVersion>(ocr_version_tag);
|
||||
if (c_ocr_version_tag == fastdeploy::jni::pipeline::PPOCRVersion::OCR_V1) {
|
||||
LOGE("Not support for PPOCRVersion::OCR_V1 now!");
|
||||
return 0;
|
||||
}
|
||||
// TODO(qiuyanjun): Allows users to set model parameters, such as det_db_box_thresh,
|
||||
// det_db_thresh, use_dilation, etc. These parameters should be passed in via JNI.
|
||||
std::string c_det_model_file = fastdeploy::jni::ConvertTo<std::string>(env, det_model_file);
|
||||
std::string c_det_params_file = fastdeploy::jni::ConvertTo<std::string>(env, det_params_file);
|
||||
std::string c_cls_model_file = fastdeploy::jni::ConvertTo<std::string>(env, cls_model_file);
|
||||
std::string c_cls_params_file = fastdeploy::jni::ConvertTo<std::string>(env, cls_params_file);
|
||||
std::string c_rec_model_file = fastdeploy::jni::ConvertTo<std::string>(env, rec_model_file);
|
||||
std::string c_rec_params_file = fastdeploy::jni::ConvertTo<std::string>(env, rec_params_file);
|
||||
std::string c_rec_label_path = fastdeploy::jni::ConvertTo<std::string>(env, rec_label_path);
|
||||
auto c_det_cpu_num_thread = static_cast<int>(det_cpu_num_thread);
|
||||
auto c_cls_cpu_num_thread = static_cast<int>(cls_cpu_num_thread);
|
||||
auto c_rec_cpu_num_thread = static_cast<int>(rec_cpu_num_thread);
|
||||
auto c_det_enable_lite_fp16 = static_cast<bool>(det_enable_lite_fp16);
|
||||
auto c_cls_enable_lite_fp16 = static_cast<bool>(cls_enable_lite_fp16);
|
||||
auto c_rec_enable_lite_fp16 = static_cast<bool>(rec_enable_lite_fp16);
|
||||
auto c_det_lite_power_mode = static_cast<fastdeploy::LitePowerMode>(det_lite_power_mode);
|
||||
auto c_cls_lite_power_mode = static_cast<fastdeploy::LitePowerMode>(cls_lite_power_mode);
|
||||
auto c_rec_lite_power_mode = static_cast<fastdeploy::LitePowerMode>(rec_lite_power_mode);
|
||||
std::string c_det_lite_optimized_model_dir = fastdeploy::jni::ConvertTo<std::string>(
|
||||
env, det_lite_optimized_model_dir);
|
||||
std::string c_cls_lite_optimized_model_dir = fastdeploy::jni::ConvertTo<std::string>(
|
||||
env, cls_lite_optimized_model_dir);
|
||||
std::string c_rec_lite_optimized_model_dir = fastdeploy::jni::ConvertTo<std::string>(
|
||||
env, rec_lite_optimized_model_dir);
|
||||
auto c_det_enable_record_time_of_runtime = static_cast<bool>(det_enable_record_time_of_runtime);
|
||||
auto c_cls_enable_record_time_of_runtime = static_cast<bool>(cls_enable_record_time_of_runtime);
|
||||
auto c_rec_enable_record_time_of_runtime = static_cast<bool>(rec_enable_record_time_of_runtime);
|
||||
auto c_have_cls_model = static_cast<bool>(have_cls_model);
|
||||
|
||||
// RuntimeOptions in native
|
||||
fastdeploy::RuntimeOption c_det_option;
|
||||
c_det_option.UseCpu();
|
||||
c_det_option.UseLiteBackend();
|
||||
c_det_option.SetCpuThreadNum(c_det_cpu_num_thread);
|
||||
c_det_option.SetLitePowerMode(c_det_lite_power_mode);
|
||||
c_det_option.SetLiteOptimizedModelDir(c_det_lite_optimized_model_dir);
|
||||
if (c_det_enable_lite_fp16) {
|
||||
c_det_option.EnableLiteFP16();
|
||||
}
|
||||
fastdeploy::RuntimeOption c_cls_option;
|
||||
c_cls_option.UseCpu();
|
||||
c_cls_option.UseLiteBackend();
|
||||
c_cls_option.SetCpuThreadNum(c_cls_cpu_num_thread);
|
||||
c_cls_option.SetLitePowerMode(c_cls_lite_power_mode);
|
||||
c_cls_option.SetLiteOptimizedModelDir(c_cls_lite_optimized_model_dir);
|
||||
if (c_cls_enable_lite_fp16) {
|
||||
c_cls_option.EnableLiteFP16();
|
||||
}
|
||||
fastdeploy::RuntimeOption c_rec_option;
|
||||
c_rec_option.UseCpu();
|
||||
c_rec_option.UseLiteBackend();
|
||||
c_rec_option.SetCpuThreadNum(c_rec_cpu_num_thread);
|
||||
c_rec_option.SetLitePowerMode(c_rec_lite_power_mode);
|
||||
c_rec_option.SetLiteOptimizedModelDir(c_rec_lite_optimized_model_dir);
|
||||
if (c_rec_enable_lite_fp16) {
|
||||
c_rec_option.EnableLiteFP16();
|
||||
}
|
||||
|
||||
// Init PP-OCR pipeline
|
||||
auto c_det_model_ptr = new fastdeploy::vision::ocr::DBDetector(
|
||||
c_det_model_file, c_det_params_file, c_det_option);
|
||||
auto c_rec_model_ptr = new fastdeploy::vision::ocr::Recognizer(
|
||||
c_rec_model_file, c_rec_params_file, c_rec_label_path, c_rec_option);
|
||||
// Enable record Runtime time costs.
|
||||
if (c_det_enable_record_time_of_runtime) {
|
||||
c_det_model_ptr->EnableRecordTimeOfRuntime();
|
||||
}
|
||||
if (c_rec_enable_record_time_of_runtime) {
|
||||
c_rec_model_ptr->EnableRecordTimeOfRuntime();
|
||||
}
|
||||
|
||||
// PP-OCRv2
|
||||
if (c_ocr_version_tag == fastdeploy::jni::pipeline::PPOCRVersion::OCR_V2) {
|
||||
if (c_have_cls_model) {
|
||||
auto c_cls_model_ptr = new fastdeploy::vision::ocr::Classifier(
|
||||
c_cls_model_file, c_cls_params_file, c_cls_option);
|
||||
if (c_cls_enable_record_time_of_runtime) {
|
||||
c_cls_model_ptr->EnableRecordTimeOfRuntime();
|
||||
}
|
||||
auto c_ppocr_pipeline_ptr = new fastdeploy::pipeline::PPOCRv2(
|
||||
c_det_model_ptr, c_cls_model_ptr, c_rec_model_ptr);
|
||||
// PP-OCRv2 handler with cls model
|
||||
auto c_ppocr_handler_ptr = new fastdeploy::jni::pipeline::PPOCRHandler(
|
||||
c_det_model_ptr, c_cls_model_ptr, c_rec_model_ptr, c_ppocr_pipeline_ptr);
|
||||
c_ppocr_handler_ptr->SetPPOCRVersion(c_ocr_version_tag);
|
||||
// WARN: need to release manually in Java !
|
||||
return reinterpret_cast<jlong>(c_ppocr_handler_ptr); // native handler context
|
||||
} else {
|
||||
auto c_ppocr_pipeline_ptr = new fastdeploy::pipeline::PPOCRv2(
|
||||
c_det_model_ptr, c_rec_model_ptr);
|
||||
// PP-OCRv2 handler without cls model
|
||||
auto c_ppocr_handler_ptr = new fastdeploy::jni::pipeline::PPOCRHandler(
|
||||
c_det_model_ptr, c_rec_model_ptr, c_ppocr_pipeline_ptr);
|
||||
c_ppocr_handler_ptr->SetPPOCRVersion(c_ocr_version_tag);
|
||||
// WARN: need to release manually in Java !
|
||||
return reinterpret_cast<jlong>(c_ppocr_handler_ptr); // native handler context
|
||||
}
|
||||
} // PP-OCRv3
|
||||
else if (c_ocr_version_tag == fastdeploy::jni::pipeline::PPOCRVersion::OCR_V3) {
|
||||
if (c_have_cls_model) {
|
||||
auto c_cls_model_ptr = new fastdeploy::vision::ocr::Classifier(
|
||||
c_cls_model_file, c_cls_params_file, c_cls_option);
|
||||
if (c_cls_enable_record_time_of_runtime) {
|
||||
c_cls_model_ptr->EnableRecordTimeOfRuntime();
|
||||
}
|
||||
auto c_ppocr_pipeline_ptr = new fastdeploy::pipeline::PPOCRv3(
|
||||
c_det_model_ptr, c_cls_model_ptr, c_rec_model_ptr);
|
||||
// PP-OCRv3 handler with cls model
|
||||
auto c_ppocr_handler_ptr = new fastdeploy::jni::pipeline::PPOCRHandler(
|
||||
c_det_model_ptr, c_cls_model_ptr, c_rec_model_ptr, c_ppocr_pipeline_ptr);
|
||||
c_ppocr_handler_ptr->SetPPOCRVersion(c_ocr_version_tag);
|
||||
// WARN: need to release manually in Java !
|
||||
return reinterpret_cast<jlong>(c_ppocr_handler_ptr); // native handler context
|
||||
} else {
|
||||
auto c_ppocr_pipeline_ptr = new fastdeploy::pipeline::PPOCRv3(
|
||||
c_det_model_ptr, c_rec_model_ptr);
|
||||
// PP-OCRv3 handler without cls model
|
||||
auto c_ppocr_handler_ptr = new fastdeploy::jni::pipeline::PPOCRHandler(
|
||||
c_det_model_ptr, c_rec_model_ptr, c_ppocr_pipeline_ptr);
|
||||
c_ppocr_handler_ptr->SetPPOCRVersion(c_ocr_version_tag);
|
||||
// WARN: need to release manually in Java !
|
||||
return reinterpret_cast<jlong>(c_ppocr_handler_ptr); // native handler context
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_pipeline_PPOCRBase_predictNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_handler_context,
|
||||
jobject argb8888_bitmap, jboolean saved, jstring saved_image_path,
|
||||
jboolean rendering) {
|
||||
if (native_handler_context == 0) {
|
||||
return 0;
|
||||
}
|
||||
cv::Mat c_bgr;
|
||||
auto t = fastdeploy::jni::GetCurrentTime();
|
||||
if (!fastdeploy::jni::ARGB888Bitmap2BGR(env, argb8888_bitmap, &c_bgr)) {
|
||||
return 0;
|
||||
}
|
||||
LOGD("Read from bitmap costs %f ms", fastdeploy::jni::GetElapsedTime(t));
|
||||
auto c_ppocr_handler_ptr = reinterpret_cast<fastdeploy::jni::pipeline::PPOCRHandler*>(
|
||||
native_handler_context);
|
||||
auto c_result_ptr = new fastdeploy::vision::OCRResult();
|
||||
t = fastdeploy::jni::GetCurrentTime();
|
||||
if (!c_ppocr_handler_ptr->Predict(&c_bgr, c_result_ptr)) {
|
||||
delete c_result_ptr;
|
||||
return 0;
|
||||
}
|
||||
LOGD("Predict from native costs %f ms", fastdeploy::jni::GetElapsedTime(t));
|
||||
// DEBUG: show result
|
||||
LOGD("Result: %s", c_result_ptr->Str().c_str());
|
||||
c_ppocr_handler_ptr->PrintPPOCRHandlerTimeOfRuntime();
|
||||
if (!c_result_ptr->boxes.empty() && rendering) {
|
||||
t = fastdeploy::jni::GetCurrentTime();
|
||||
auto c_vis_im = fastdeploy::vision::VisOcr(c_bgr, *(c_result_ptr));
|
||||
LOGD("Visualize from native costs %f ms", fastdeploy::jni::GetElapsedTime(t));
|
||||
// Rendering to bitmap
|
||||
t = fastdeploy::jni::GetCurrentTime();
|
||||
if (!fastdeploy::jni::BGR2ARGB888Bitmap(env, argb8888_bitmap, c_vis_im)) {
|
||||
delete c_result_ptr;
|
||||
return 0;
|
||||
}
|
||||
LOGD("Write to bitmap from native costs %f ms",
|
||||
fastdeploy::jni::GetElapsedTime(t));
|
||||
std::string c_saved_image_path =
|
||||
fastdeploy::jni::ConvertTo<std::string>(env, saved_image_path);
|
||||
if (!c_saved_image_path.empty() && saved) {
|
||||
t = fastdeploy::jni::GetCurrentTime();
|
||||
cv::imwrite(c_saved_image_path, c_vis_im);
|
||||
LOGD("Save image from native costs %f ms, path: %s",
|
||||
fastdeploy::jni::GetElapsedTime(t), c_saved_image_path.c_str());
|
||||
}
|
||||
}
|
||||
// WARN: need to release it manually in Java !
|
||||
return reinterpret_cast<jlong>(c_result_ptr); // native result context
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_pipeline_PPOCRBase_releaseNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_handler_context) {
|
||||
if (native_handler_context == 0) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
auto c_ppocr_handler_ptr = reinterpret_cast<fastdeploy::jni::pipeline::PPOCRHandler*>(
|
||||
native_handler_context);
|
||||
if (!c_ppocr_handler_ptr->ReleaseAllocatedOCRMemories()) {
|
||||
delete c_ppocr_handler_ptr;
|
||||
return JNI_FALSE;
|
||||
}
|
||||
delete c_ppocr_handler_ptr;
|
||||
LOGD("[End] Release PPOCRHandler in native !");
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -22,7 +22,7 @@ extern "C" {
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_classification_PaddleClasModel_bindNative(
|
||||
JNIEnv *env, jclass clazz, jstring model_file, jstring params_file,
|
||||
JNIEnv *env, jobject thiz, jstring model_file, jstring params_file,
|
||||
jstring config_file, jint cpu_num_thread, jboolean enable_lite_fp16,
|
||||
jint lite_power_mode, jstring lite_optimized_model_dir,
|
||||
jboolean enable_record_time_of_runtime, jstring label_file) {
|
||||
@@ -66,7 +66,7 @@ Java_com_baidu_paddle_fastdeploy_vision_classification_PaddleClasModel_bindNativ
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_classification_PaddleClasModel_predictNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_model_context,
|
||||
JNIEnv *env, jobject thiz, jlong native_model_context,
|
||||
jobject argb8888_bitmap, jboolean saved, jstring saved_image_path,
|
||||
jfloat score_threshold, jboolean rendering) {
|
||||
if (native_model_context == 0) {
|
||||
@@ -129,7 +129,7 @@ Java_com_baidu_paddle_fastdeploy_vision_classification_PaddleClasModel_predictNa
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_classification_PaddleClasModel_releaseNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_model_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_model_context) {
|
||||
auto c_model_ptr =
|
||||
reinterpret_cast<fastdeploy::vision::classification::PaddleClasModel *>(
|
||||
native_model_context);
|
||||
|
@@ -22,7 +22,7 @@ extern "C" {
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_detection_PicoDet_bindNative(
|
||||
JNIEnv *env, jclass clazz, jstring model_file, jstring params_file,
|
||||
JNIEnv *env, jobject thiz, jstring model_file, jstring params_file,
|
||||
jstring config_file, jint cpu_num_thread, jboolean enable_lite_fp16,
|
||||
jint lite_power_mode, jstring lite_optimized_model_dir,
|
||||
jboolean enable_record_time_of_runtime, jstring label_file) {
|
||||
@@ -66,7 +66,7 @@ Java_com_baidu_paddle_fastdeploy_vision_detection_PicoDet_bindNative(
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_detection_PicoDet_predictNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_model_context,
|
||||
JNIEnv *env, jobject thiz, jlong native_model_context,
|
||||
jobject argb8888_bitmap, jboolean saved, jstring saved_image_path,
|
||||
jfloat score_threshold, jboolean rendering) {
|
||||
if (native_model_context == 0) {
|
||||
@@ -128,7 +128,7 @@ Java_com_baidu_paddle_fastdeploy_vision_detection_PicoDet_predictNative(
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_detection_PicoDet_releaseNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_model_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_model_context) {
|
||||
if (native_model_context == 0) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
@@ -25,7 +25,10 @@ extern "C" {
|
||||
/// Native DetectionResult for vision::DetectionResult.
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyBoxesNumFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return 0;
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::DetectionResult *>(
|
||||
native_result_context);
|
||||
return static_cast<jint>(c_result_ptr->boxes.size());
|
||||
@@ -33,7 +36,10 @@ Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyBoxesNumFromNative(
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyBoxesFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::DetectionResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->boxes.empty()) {
|
||||
@@ -50,7 +56,10 @@ Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyBoxesFromNative(
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyScoresFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::DetectionResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->scores.empty()) {
|
||||
@@ -63,7 +72,10 @@ Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyScoresFromNative(
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyLabelIdsFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::DetectionResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->label_ids.empty()) {
|
||||
@@ -76,7 +88,7 @@ Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_copyLabelIdsFromNative(
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_releaseNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
@@ -90,7 +102,10 @@ Java_com_baidu_paddle_fastdeploy_vision_DetectionResult_releaseNative(
|
||||
/// Native ClassifyResult for vision::ClassifyResult.
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_copyScoresFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::ClassifyResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->scores.empty()) {
|
||||
@@ -103,7 +118,10 @@ Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_copyScoresFromNative(
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_copyLabelIdsFromNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::ClassifyResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->label_ids.empty()) {
|
||||
@@ -116,7 +134,7 @@ Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_copyLabelIdsFromNative(
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_releaseNative(
|
||||
JNIEnv *env, jclass clazz, jlong native_result_context) {
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
@@ -127,6 +145,123 @@ Java_com_baidu_paddle_fastdeploy_vision_ClassifyResult_releaseNative(
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
/// Native OCRResult for vision::OCRResult.
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyBoxesNumFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return 0;
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
return static_cast<jint>(c_result_ptr->boxes.size());
|
||||
}
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyBoxesFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->boxes.empty()) {
|
||||
return {};
|
||||
}
|
||||
const auto len = static_cast<int64_t>(c_result_ptr->boxes.size());
|
||||
int buffer[len * 8];
|
||||
const auto &boxes = c_result_ptr->boxes;
|
||||
for (int64_t i = 0; i < len; ++i) {
|
||||
std::memcpy((buffer + i * 8), (boxes.at(i).data()), 8 * sizeof(int));
|
||||
}
|
||||
return fastdeploy::jni::ConvertTo<jintArray>(env, buffer, len * 4);
|
||||
}
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyTextFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->text.empty()) {
|
||||
return {};
|
||||
}
|
||||
const auto len = static_cast<int64_t>(c_result_ptr->text.size());
|
||||
jclass jstr_clazz = env->FindClass("java/lang/String");
|
||||
jobjectArray jstr_array = env->NewObjectArray(
|
||||
static_cast<jsize>(len), jstr_clazz,env->NewStringUTF(""));
|
||||
for (int64_t i = 0; i < len; ++i) {
|
||||
env->SetObjectArrayElement(jstr_array, static_cast<jsize>(i),
|
||||
fastdeploy::jni::ConvertTo<jstring>(
|
||||
env, c_result_ptr->text.at(i)));
|
||||
}
|
||||
return jstr_array;
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyRecScoresFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->rec_scores.empty()) {
|
||||
return {};
|
||||
}
|
||||
const auto len = static_cast<int64_t>(c_result_ptr->rec_scores.size());
|
||||
const float *buffer = static_cast<float *>(c_result_ptr->rec_scores.data());
|
||||
return fastdeploy::jni::ConvertTo<jfloatArray>(env, buffer, len);
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyClsScoresFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->cls_scores.empty()) {
|
||||
return {};
|
||||
}
|
||||
const auto len = static_cast<int64_t>(c_result_ptr->cls_scores.size());
|
||||
const float *buffer = static_cast<float *>(c_result_ptr->cls_scores.data());
|
||||
return fastdeploy::jni::ConvertTo<jfloatArray>(env, buffer, len);
|
||||
}
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_copyClsLabelsFromNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return {};
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
if (c_result_ptr->cls_labels.empty()) {
|
||||
return {};
|
||||
}
|
||||
const auto len = static_cast<int64_t>(c_result_ptr->cls_labels.size());
|
||||
const int *buffer = static_cast<int *>(c_result_ptr->cls_labels.data());
|
||||
return fastdeploy::jni::ConvertTo<jintArray>(env, buffer, len);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_OCRResult_releaseNative(
|
||||
JNIEnv *env, jobject thiz, jlong native_result_context) {
|
||||
if (native_result_context == 0) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
auto c_result_ptr = reinterpret_cast<fastdeploy::vision::OCRResult *>(
|
||||
native_result_context);
|
||||
delete c_result_ptr;
|
||||
LOGD("Release OCRResult in native !");
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@@ -19,6 +19,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/// VisDetection
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_Visualize_visDetectionNative(
|
||||
JNIEnv *env, jclass clazz, jobject argb8888_bitmap, jobjectArray boxes,
|
||||
@@ -32,6 +33,8 @@ Java_com_baidu_paddle_fastdeploy_vision_Visualize_visDetectionNative(
|
||||
}
|
||||
fastdeploy::vision::DetectionResult c_result;
|
||||
c_result.Resize(len);
|
||||
|
||||
// boxes [n,4]
|
||||
bool check_validation = true;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
auto j_box =
|
||||
@@ -48,18 +51,19 @@ Java_com_baidu_paddle_fastdeploy_vision_Visualize_visDetectionNative(
|
||||
if (!check_validation) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
// scores [n]
|
||||
jfloat *j_scores_ptr = env->GetFloatArrayElements(scores, nullptr);
|
||||
std::memcpy(c_result.scores.data(), j_scores_ptr, len * sizeof(float));
|
||||
env->ReleaseFloatArrayElements(scores, j_scores_ptr, 0);
|
||||
// label_ids [n]
|
||||
jint *j_label_ids_ptr = env->GetIntArrayElements(label_ids, nullptr);
|
||||
std::memcpy(c_result.label_ids.data(), j_label_ids_ptr, len * sizeof(int));
|
||||
env->ReleaseIntArrayElements(label_ids, j_label_ids_ptr, 0);
|
||||
|
||||
// Get labels from Java
|
||||
// Get labels from Java [n]
|
||||
std::vector<std::string> c_labels;
|
||||
int label_len = env->GetArrayLength(labels);
|
||||
if (label_len > 0) {
|
||||
c_labels.reserve(label_len);
|
||||
for (int i = 0; i < label_len; ++i) {
|
||||
auto j_str =
|
||||
reinterpret_cast<jstring>(env->GetObjectArrayElement(labels, i));
|
||||
@@ -87,6 +91,158 @@ Java_com_baidu_paddle_fastdeploy_vision_Visualize_visDetectionNative(
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
/// VisClassification
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_Visualize_visClassificationNative(
|
||||
JNIEnv *env, jclass clazz, jobject argb8888_bitmap, jfloatArray scores,
|
||||
jintArray label_ids, jfloat score_threshold, jfloat font_size,
|
||||
jobjectArray labels) {
|
||||
// Draw ClassifyResult to ARGB8888 Bitmap
|
||||
int len = env->GetArrayLength(scores);
|
||||
if ((len == 0) || (len != env->GetArrayLength(label_ids))) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
fastdeploy::vision::ClassifyResult c_result;
|
||||
c_result.scores.resize(len);
|
||||
c_result.label_ids.resize(len);
|
||||
// scores [n]
|
||||
jfloat *j_scores_ptr = env->GetFloatArrayElements(scores, nullptr);
|
||||
std::memcpy(c_result.scores.data(), j_scores_ptr, len * sizeof(float));
|
||||
env->ReleaseFloatArrayElements(scores, j_scores_ptr, 0);
|
||||
// label_ids [n]
|
||||
jint *j_label_ids_ptr = env->GetIntArrayElements(label_ids, nullptr);
|
||||
std::memcpy(c_result.label_ids.data(), j_label_ids_ptr, len * sizeof(int));
|
||||
env->ReleaseIntArrayElements(label_ids, j_label_ids_ptr, 0);
|
||||
|
||||
// Get labels from Java [n]
|
||||
std::vector<std::string> c_labels;
|
||||
int label_len = env->GetArrayLength(labels);
|
||||
if (label_len > 0) {
|
||||
for (int i = 0; i < label_len; ++i) {
|
||||
auto j_str =
|
||||
reinterpret_cast<jstring>(env->GetObjectArrayElement(labels, i));
|
||||
c_labels.push_back(fastdeploy::jni::ConvertTo<std::string>(env, j_str));
|
||||
}
|
||||
}
|
||||
cv::Mat c_bgr;
|
||||
// From ARGB Bitmap to BGR
|
||||
if (!fastdeploy::jni::ARGB888Bitmap2BGR(env, argb8888_bitmap, &c_bgr)) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
cv::Mat c_vis_im;
|
||||
if (!c_labels.empty()) {
|
||||
c_vis_im = fastdeploy::vision::VisClassification(
|
||||
c_bgr, c_result, c_labels, 5, score_threshold, font_size);
|
||||
} else {
|
||||
c_vis_im = fastdeploy::vision::VisClassification(
|
||||
c_bgr, c_result, 5, score_threshold, font_size);
|
||||
}
|
||||
// Rendering to bitmap
|
||||
if (!fastdeploy::jni::BGR2ARGB888Bitmap(env, argb8888_bitmap, c_vis_im)) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
/// VisOcr
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_baidu_paddle_fastdeploy_vision_Visualize_visOcrNative(
|
||||
JNIEnv *env, jclass clazz, jobject argb8888_bitmap,
|
||||
jobjectArray boxes, jobjectArray text, jfloatArray rec_scores,
|
||||
jfloatArray cls_scores, jintArray cls_labels) {
|
||||
// Draw OCRResult to ARGB8888 Bitmap
|
||||
int len = env->GetArrayLength(boxes);
|
||||
if ((len == 0) || (len != env->GetArrayLength(text)) ||
|
||||
(len != env->GetArrayLength(rec_scores)) ||
|
||||
(len != env->GetArrayLength(cls_scores)) ||
|
||||
(len != env->GetArrayLength(cls_labels))) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
fastdeploy::vision::OCRResult c_result;
|
||||
c_result.boxes.resize(len);
|
||||
c_result.rec_scores.resize(len);
|
||||
c_result.cls_scores.resize(len);
|
||||
c_result.cls_labels.resize(len);
|
||||
|
||||
// boxes [n,8]
|
||||
bool check_validation = true;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
auto j_box =
|
||||
reinterpret_cast<jintArray>(env->GetObjectArrayElement(boxes, i));
|
||||
if (env->GetArrayLength(j_box) == 8) {
|
||||
jint *j_box_ptr = env->GetIntArrayElements(j_box, nullptr);
|
||||
std::memcpy(c_result.boxes[i].data(), j_box_ptr, 8 * sizeof(int));
|
||||
env->ReleaseIntArrayElements(j_box, j_box_ptr, 0);
|
||||
} else {
|
||||
check_validation = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!check_validation) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
// text [n]
|
||||
int text_len = env->GetArrayLength(text);
|
||||
if (text_len > 0) {
|
||||
for (int i = 0; i < text_len; ++i) {
|
||||
auto j_str =
|
||||
reinterpret_cast<jstring>(env->GetObjectArrayElement(text, i));
|
||||
c_result.text.push_back(fastdeploy::jni::ConvertTo<std::string>(env, j_str));
|
||||
}
|
||||
}
|
||||
|
||||
// rec_scores [n]
|
||||
jfloat *j_rec_scores_ptr = env->GetFloatArrayElements(rec_scores, nullptr);
|
||||
std::memcpy(c_result.rec_scores.data(), j_rec_scores_ptr, len * sizeof(float));
|
||||
env->ReleaseFloatArrayElements(rec_scores, j_rec_scores_ptr, 0);
|
||||
// cls_scores [n]
|
||||
jfloat *j_cls_scores_ptr = env->GetFloatArrayElements(cls_scores, nullptr);
|
||||
std::memcpy(c_result.cls_scores.data(), j_cls_scores_ptr, len * sizeof(float));
|
||||
env->ReleaseFloatArrayElements(cls_scores, j_cls_scores_ptr, 0);
|
||||
// cls_labels [n]
|
||||
jint *j_cls_label_ptr = env->GetIntArrayElements(cls_labels, nullptr);
|
||||
std::memcpy(c_result.cls_labels.data(), j_cls_label_ptr, len * sizeof(int));
|
||||
env->ReleaseIntArrayElements(cls_labels, j_cls_label_ptr, 0);
|
||||
|
||||
cv::Mat c_bgr;
|
||||
// From ARGB Bitmap to BGR
|
||||
if (!fastdeploy::jni::ARGB888Bitmap2BGR(env, argb8888_bitmap, &c_bgr)) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
auto c_vis_im = fastdeploy::vision::VisOcr(c_bgr, c_result);
|
||||
// Rendering to bitmap
|
||||
if (!fastdeploy::jni::BGR2ARGB888Bitmap(env, argb8888_bitmap, c_vis_im)) {
|
||||
return JNI_FALSE;
|
||||
}
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -0,0 +1,195 @@
|
||||
package com.baidu.paddle.fastdeploy.pipeline;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.FastDeployInitializer;
|
||||
import com.baidu.paddle.fastdeploy.vision.OCRResult;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Classifier;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Recognizer;
|
||||
|
||||
public class PPOCRBase {
|
||||
protected long mNativeHandlerContext = 0; // Context from native.
|
||||
protected boolean mInitialized = false;
|
||||
|
||||
public PPOCRBase() {
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
// Constructor w/o classifier
|
||||
public PPOCRBase(DBDetector detModel,
|
||||
Recognizer recModel,
|
||||
PPOCRVersion OCRVersionTag) {
|
||||
init_(detModel, new Classifier(), recModel, OCRVersionTag);
|
||||
}
|
||||
|
||||
public PPOCRBase(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel,
|
||||
PPOCRVersion OCRVersionTag) {
|
||||
init_(detModel, clsModel, recModel, OCRVersionTag);
|
||||
}
|
||||
|
||||
// Call init manually w/o classifier
|
||||
public boolean init(DBDetector detModel,
|
||||
Recognizer recModel,
|
||||
PPOCRVersion OCRVersionTag) {
|
||||
return init_(detModel, new Classifier(), recModel, OCRVersionTag);
|
||||
}
|
||||
|
||||
public boolean init(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel,
|
||||
PPOCRVersion OCRVersionTag) {
|
||||
return init_(detModel, clsModel, recModel, OCRVersionTag);
|
||||
}
|
||||
|
||||
public boolean release() {
|
||||
mInitialized = false;
|
||||
if (mNativeHandlerContext == 0) {
|
||||
return false;
|
||||
}
|
||||
return releaseNative(mNativeHandlerContext);
|
||||
}
|
||||
|
||||
public boolean initialized() {
|
||||
return mInitialized;
|
||||
}
|
||||
|
||||
// Predict without image saving and bitmap rendering.
|
||||
public OCRResult predict(Bitmap ARGB8888Bitmap) {
|
||||
if (mNativeHandlerContext == 0) {
|
||||
return new OCRResult();
|
||||
}
|
||||
// Only support ARGB8888 bitmap in native now.
|
||||
return new OCRResult(predictNative(
|
||||
mNativeHandlerContext, ARGB8888Bitmap, false,
|
||||
"", false));
|
||||
}
|
||||
|
||||
// Predict with image saving and bitmap rendering (will cost more times)
|
||||
public OCRResult predict(Bitmap ARGB8888Bitmap,
|
||||
String savedImagePath) {
|
||||
// scoreThreshold is for visualizing only.
|
||||
if (mNativeHandlerContext == 0) {
|
||||
return new OCRResult();
|
||||
}
|
||||
// Only support ARGB8888 bitmap in native now.
|
||||
return new OCRResult(predictNative(
|
||||
mNativeHandlerContext, ARGB8888Bitmap, true,
|
||||
savedImagePath, true));
|
||||
}
|
||||
|
||||
public boolean init_(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel,
|
||||
PPOCRVersion OCRVersionTag) {
|
||||
if (!mInitialized) {
|
||||
mNativeHandlerContext = bindNative(
|
||||
OCRVersionTag.ordinal(),
|
||||
detModel.mModelFile,
|
||||
detModel.mParamsFile,
|
||||
clsModel.mModelFile,
|
||||
clsModel.mParamsFile,
|
||||
recModel.mModelFile,
|
||||
recModel.mParamsFile,
|
||||
recModel.mLabelPath,
|
||||
detModel.mRuntimeOption.mCpuThreadNum,
|
||||
clsModel.mRuntimeOption.mCpuThreadNum,
|
||||
recModel.mRuntimeOption.mCpuThreadNum,
|
||||
detModel.mRuntimeOption.mEnableLiteFp16,
|
||||
clsModel.mRuntimeOption.mEnableLiteFp16,
|
||||
recModel.mRuntimeOption.mEnableLiteFp16,
|
||||
detModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
clsModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
recModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
detModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
clsModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
recModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
detModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
clsModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
recModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
clsModel.initialized());
|
||||
if (mNativeHandlerContext != 0) {
|
||||
mInitialized = true;
|
||||
}
|
||||
return mInitialized;
|
||||
} else {
|
||||
// release current native context and bind a new one.
|
||||
if (release()) {
|
||||
mNativeHandlerContext = bindNative(
|
||||
OCRVersionTag.ordinal(),
|
||||
detModel.mModelFile,
|
||||
detModel.mParamsFile,
|
||||
clsModel.mModelFile,
|
||||
clsModel.mParamsFile,
|
||||
recModel.mModelFile,
|
||||
recModel.mParamsFile,
|
||||
recModel.mLabelPath,
|
||||
detModel.mRuntimeOption.mCpuThreadNum,
|
||||
clsModel.mRuntimeOption.mCpuThreadNum,
|
||||
recModel.mRuntimeOption.mCpuThreadNum,
|
||||
detModel.mRuntimeOption.mEnableLiteFp16,
|
||||
clsModel.mRuntimeOption.mEnableLiteFp16,
|
||||
recModel.mRuntimeOption.mEnableLiteFp16,
|
||||
detModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
clsModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
recModel.mRuntimeOption.mLitePowerMode.ordinal(),
|
||||
detModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
clsModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
recModel.mRuntimeOption.mLiteOptimizedModelDir,
|
||||
detModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
clsModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
recModel.mRuntimeOption.mEnableRecordTimeOfRuntime,
|
||||
clsModel.initialized());
|
||||
if (mNativeHandlerContext != 0) {
|
||||
mInitialized = true;
|
||||
}
|
||||
return mInitialized;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Bind predictor from native context.
|
||||
private native long bindNative(int PPOCRVersionTag,
|
||||
String detModelFile,
|
||||
String detParamsFile,
|
||||
String clsModelFile,
|
||||
String clsParamsFile,
|
||||
String recModelFile,
|
||||
String recParamsFile,
|
||||
String recLabelPath,
|
||||
int detCpuNumThread,
|
||||
int clsCpuNumThread,
|
||||
int recCpuNumThread,
|
||||
boolean detEnableLiteFp16,
|
||||
boolean clsEnableLiteFp16,
|
||||
boolean recEnableLiteFp16,
|
||||
int detLitePowerMode,
|
||||
int clsLitePowerMode,
|
||||
int recLitePowerMode,
|
||||
String detLiteOptimizedModelDir,
|
||||
String clsLiteOptimizedModelDir,
|
||||
String recLiteOptimizedModelDir,
|
||||
boolean detEnableRecordTimeOfRuntime,
|
||||
boolean clsEnableRecordTimeOfRuntime,
|
||||
boolean recEnableRecordTimeOfRuntime,
|
||||
boolean haveClsModel);
|
||||
|
||||
// Call prediction from native context.
|
||||
private native long predictNative(long nativeHandlerContext,
|
||||
Bitmap ARGB8888Bitmap,
|
||||
boolean saved,
|
||||
String savedImagePath,
|
||||
boolean rendering);
|
||||
|
||||
// Release buffers allocated in native context.
|
||||
private native boolean releaseNative(long nativeHandlerContext);
|
||||
|
||||
// Initializes at the beginning.
|
||||
static {
|
||||
FastDeployInitializer.init();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package com.baidu.paddle.fastdeploy.pipeline;
|
||||
|
||||
public enum PPOCRVersion {
|
||||
OCR_V1,
|
||||
OCR_V2,
|
||||
OCR_V3
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package com.baidu.paddle.fastdeploy.pipeline;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Classifier;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Recognizer;
|
||||
|
||||
public class PPOCRv2 extends PPOCRBase {
|
||||
public PPOCRv2() {
|
||||
super();
|
||||
}
|
||||
|
||||
// Constructor w/o classifier
|
||||
public PPOCRv2(DBDetector detModel,
|
||||
Recognizer recModel) {
|
||||
super(detModel, recModel, PPOCRVersion.OCR_V2);
|
||||
}
|
||||
|
||||
public PPOCRv2(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel) {
|
||||
super(detModel, clsModel, recModel, PPOCRVersion.OCR_V2);
|
||||
}
|
||||
|
||||
// Call init manually w/o classifier
|
||||
public boolean init(DBDetector detModel,
|
||||
Recognizer recModel) {
|
||||
return init(detModel, recModel, PPOCRVersion.OCR_V2);
|
||||
}
|
||||
|
||||
public boolean init(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel) {
|
||||
return init(detModel, clsModel, recModel, PPOCRVersion.OCR_V2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -0,0 +1,36 @@
|
||||
package com.baidu.paddle.fastdeploy.pipeline;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Classifier;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector;
|
||||
import com.baidu.paddle.fastdeploy.vision.ocr.Recognizer;
|
||||
|
||||
public class PPOCRv3 extends PPOCRBase {
|
||||
public PPOCRv3() {
|
||||
super();
|
||||
}
|
||||
|
||||
// Constructor w/o classifier
|
||||
public PPOCRv3(DBDetector detModel,
|
||||
Recognizer recModel) {
|
||||
super(detModel, recModel, PPOCRVersion.OCR_V3);
|
||||
}
|
||||
|
||||
public PPOCRv3(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel) {
|
||||
super(detModel, clsModel, recModel, PPOCRVersion.OCR_V3);
|
||||
}
|
||||
|
||||
// Call init manually w/o classifier
|
||||
public boolean init(DBDetector detModel,
|
||||
Recognizer recModel) {
|
||||
return init(detModel, recModel, PPOCRVersion.OCR_V3);
|
||||
}
|
||||
|
||||
public boolean init(DBDetector detModel,
|
||||
Classifier clsModel,
|
||||
Recognizer recModel) {
|
||||
return init(detModel, clsModel, recModel, PPOCRVersion.OCR_V3);
|
||||
}
|
||||
}
|
||||
|
@@ -42,10 +42,10 @@ public class ClassifyResult {
|
||||
}
|
||||
|
||||
// Fetch native buffers from native context.
|
||||
private static native float[] copyScoresFromNative(long nativeResultContext);
|
||||
private native float[] copyScoresFromNative(long nativeResultContext);
|
||||
|
||||
private static native int[] copyLabelIdsFromNative(long nativeResultContext);
|
||||
private native int[] copyLabelIdsFromNative(long nativeResultContext);
|
||||
|
||||
private static native boolean releaseNative(long nativeResultContext);
|
||||
private native boolean releaseNative(long nativeResultContext);
|
||||
|
||||
}
|
||||
|
@@ -63,15 +63,15 @@ public class DetectionResult {
|
||||
}
|
||||
|
||||
// Fetch native buffers from native context.
|
||||
private static native int copyBoxesNumFromNative(long nativeResultContext);
|
||||
private native int copyBoxesNumFromNative(long nativeResultContext);
|
||||
|
||||
private static native float[] copyBoxesFromNative(long nativeResultContext);
|
||||
private native float[] copyBoxesFromNative(long nativeResultContext);
|
||||
|
||||
private static native float[] copyScoresFromNative(long nativeResultContext);
|
||||
private native float[] copyScoresFromNative(long nativeResultContext);
|
||||
|
||||
private static native int[] copyLabelIdsFromNative(long nativeResultContext);
|
||||
private native int[] copyLabelIdsFromNative(long nativeResultContext);
|
||||
|
||||
private static native boolean releaseNative(long nativeResultContext);
|
||||
private native boolean releaseNative(long nativeResultContext);
|
||||
|
||||
// Initializes at the beginning.
|
||||
static {
|
||||
|
@@ -0,0 +1,99 @@
|
||||
package com.baidu.paddle.fastdeploy.vision;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.FastDeployInitializer;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class OCRResult {
|
||||
public int[][] mBoxes; // [n,8]
|
||||
public String[] mText; // [n]
|
||||
public float[] mRecScores; // [n]
|
||||
public float[] mClsScores; // [n]
|
||||
public int[] mClsLabels; // [n]
|
||||
public boolean mInitialized = false;
|
||||
|
||||
public OCRResult() {
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
public OCRResult(long nativeResultContext) {
|
||||
mInitialized = copyAllFromNativeContext(nativeResultContext);
|
||||
}
|
||||
|
||||
public boolean initialized() {
|
||||
return mInitialized;
|
||||
}
|
||||
|
||||
// Setup results from native buffers.
|
||||
private boolean copyAllFromNativeContext(long nativeResultContext) {
|
||||
if (nativeResultContext == 0) {
|
||||
return false;
|
||||
}
|
||||
if (copyBoxesNumFromNative(nativeResultContext) > 0) {
|
||||
setBoxes(copyBoxesFromNative(nativeResultContext));
|
||||
setText(copyTextFromNative(nativeResultContext));
|
||||
setRecScores(copyRecScoresFromNative(nativeResultContext));
|
||||
setClsScores(copyClsScoresFromNative(nativeResultContext));
|
||||
setClsLabels(copyClsLabelsFromNative(nativeResultContext));
|
||||
}
|
||||
// WARN: must release ctx.
|
||||
return releaseNative(nativeResultContext);
|
||||
}
|
||||
|
||||
private void setBoxes(@NonNull int[] boxesBuffer) {
|
||||
int boxesNum = boxesBuffer.length / 8;
|
||||
if (boxesNum > 0) {
|
||||
mBoxes = new int[boxesNum][8];
|
||||
for (int i = 0; i < boxesNum; ++i) {
|
||||
mBoxes[i] = Arrays.copyOfRange(
|
||||
boxesBuffer, i * 8, (i + 1) * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setText(@NonNull String[] textBuffer) {
|
||||
if (textBuffer.length > 0) {
|
||||
mText = textBuffer.clone();
|
||||
}
|
||||
}
|
||||
|
||||
private void setRecScores(@NonNull float[] recScoresBuffer) {
|
||||
if (recScoresBuffer.length > 0) {
|
||||
mRecScores = recScoresBuffer.clone();
|
||||
}
|
||||
}
|
||||
|
||||
private void setClsScores(@NonNull float[] clsScoresBuffer) {
|
||||
if (clsScoresBuffer.length > 0) {
|
||||
mClsScores = clsScoresBuffer.clone();
|
||||
}
|
||||
}
|
||||
|
||||
private void setClsLabels(@NonNull int[] clsLabelBuffer) {
|
||||
if (clsLabelBuffer.length > 0) {
|
||||
mClsLabels = clsLabelBuffer.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch native buffers from native context.
|
||||
private native int copyBoxesNumFromNative(long nativeResultContext);
|
||||
|
||||
private native int[] copyBoxesFromNative(long nativeResultContext);
|
||||
|
||||
private native String[] copyTextFromNative(long nativeResultContext);
|
||||
|
||||
private native float[] copyRecScoresFromNative(long nativeResultContext);
|
||||
|
||||
private native float[] copyClsScoresFromNative(long nativeResultContext);
|
||||
|
||||
private native int[] copyClsLabelsFromNative(long nativeResultContext);
|
||||
|
||||
private native boolean releaseNative(long nativeResultContext);
|
||||
|
||||
// Initializes at the beginning.
|
||||
static {
|
||||
FastDeployInitializer.init();
|
||||
}
|
||||
}
|
@@ -52,10 +52,10 @@ public class Visualize {
|
||||
|
||||
public static boolean visDetection(Bitmap ARGB8888Bitmap,
|
||||
DetectionResult result,
|
||||
String[] labels,
|
||||
float score_threshold,
|
||||
int line_size,
|
||||
float font_size,
|
||||
String[] labels) {
|
||||
float font_size) {
|
||||
return visDetectionNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mBoxes,
|
||||
@@ -67,15 +67,97 @@ public class Visualize {
|
||||
labels);
|
||||
}
|
||||
|
||||
// Visualize ClassifyResult without labels
|
||||
public static boolean visClassification(Bitmap ARGB8888Bitmap,
|
||||
ClassifyResult result) {
|
||||
return visClassificationNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mScores,
|
||||
result.mLabelIds,
|
||||
0.f, 1,
|
||||
new String[]{});
|
||||
|
||||
}
|
||||
|
||||
public static boolean visClassification(Bitmap ARGB8888Bitmap,
|
||||
ClassifyResult result,
|
||||
float score_threshold,
|
||||
float font_size) {
|
||||
return visClassificationNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mScores,
|
||||
result.mLabelIds,
|
||||
score_threshold,
|
||||
font_size,
|
||||
new String[]{});
|
||||
|
||||
}
|
||||
|
||||
// Visualize ClassifyResult with labels
|
||||
public static boolean visClassification(Bitmap ARGB8888Bitmap,
|
||||
ClassifyResult result,
|
||||
String[] labels) {
|
||||
return visClassificationNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mScores,
|
||||
result.mLabelIds,
|
||||
0.f, 1,
|
||||
labels);
|
||||
|
||||
}
|
||||
|
||||
public static boolean visClassification(Bitmap ARGB8888Bitmap,
|
||||
ClassifyResult result,
|
||||
String[] labels,
|
||||
float score_threshold,
|
||||
float font_size) {
|
||||
return visClassificationNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mScores,
|
||||
result.mLabelIds,
|
||||
score_threshold,
|
||||
font_size,
|
||||
labels);
|
||||
|
||||
}
|
||||
|
||||
// Visualize OCRResult
|
||||
public static boolean visOcr(Bitmap ARGB8888Bitmap,
|
||||
OCRResult result) {
|
||||
return visOcrNative(
|
||||
ARGB8888Bitmap,
|
||||
result.mBoxes,
|
||||
result.mText,
|
||||
result.mRecScores,
|
||||
result.mClsScores,
|
||||
result.mClsLabels);
|
||||
}
|
||||
|
||||
// VisDetection in native
|
||||
public static native boolean visDetectionNative(Bitmap ARGB8888Bitmap,
|
||||
float[][] boxes,
|
||||
float[] scores,
|
||||
int[] labelIds,
|
||||
float score_threshold,
|
||||
int line_size,
|
||||
float font_size,
|
||||
String[] labels);
|
||||
private static native boolean visDetectionNative(Bitmap ARGB8888Bitmap,
|
||||
float[][] boxes,
|
||||
float[] scores,
|
||||
int[] labelIds,
|
||||
float score_threshold,
|
||||
int line_size,
|
||||
float font_size,
|
||||
String[] labels);
|
||||
|
||||
// VisClassification in native
|
||||
private static native boolean visClassificationNative(Bitmap ARGB8888Bitmap,
|
||||
float[] scores,
|
||||
int[] labelIds,
|
||||
float score_threshold,
|
||||
float font_size,
|
||||
String[] labels);
|
||||
|
||||
// VisOCRResult in native
|
||||
private static native boolean visOcrNative(Bitmap ARGB8888Bitmap,
|
||||
int[][] boxes,
|
||||
String[] text,
|
||||
float[] recScores,
|
||||
float[] clsScores,
|
||||
int[] clsLabels);
|
||||
|
||||
|
||||
/* Initializes at the beginning */
|
||||
|
@@ -143,26 +143,26 @@ public class PaddleClasModel {
|
||||
|
||||
|
||||
// Bind predictor from native context.
|
||||
private static native long bindNative(String modelFile,
|
||||
String paramsFile,
|
||||
String configFile,
|
||||
int cpuNumThread,
|
||||
boolean enableLiteFp16,
|
||||
int litePowerMode,
|
||||
String liteOptimizedModelDir,
|
||||
boolean enableRecordTimeOfRuntime,
|
||||
String labelFile);
|
||||
private native long bindNative(String modelFile,
|
||||
String paramsFile,
|
||||
String configFile,
|
||||
int cpuNumThread,
|
||||
boolean enableLiteFp16,
|
||||
int litePowerMode,
|
||||
String liteOptimizedModelDir,
|
||||
boolean enableRecordTimeOfRuntime,
|
||||
String labelFile);
|
||||
|
||||
// Call prediction from native context.
|
||||
private static native long predictNative(long nativeModelContext,
|
||||
Bitmap ARGB8888Bitmap,
|
||||
boolean saved,
|
||||
String savedImagePath,
|
||||
float scoreThreshold,
|
||||
boolean rendering);
|
||||
private native long predictNative(long nativeModelContext,
|
||||
Bitmap ARGB8888Bitmap,
|
||||
boolean saved,
|
||||
String savedImagePath,
|
||||
float scoreThreshold,
|
||||
boolean rendering);
|
||||
|
||||
// Release buffers allocated in native context.
|
||||
private static native boolean releaseNative(long nativeModelContext);
|
||||
private native boolean releaseNative(long nativeModelContext);
|
||||
|
||||
// Initializes at the beginning.
|
||||
static {
|
||||
|
@@ -141,26 +141,26 @@ public class PicoDet {
|
||||
}
|
||||
|
||||
// Bind predictor from native context.
|
||||
private static native long bindNative(String modelFile,
|
||||
String paramsFile,
|
||||
String configFile,
|
||||
int cpuNumThread,
|
||||
boolean enableLiteFp16,
|
||||
int litePowerMode,
|
||||
String liteOptimizedModelDir,
|
||||
boolean enableRecordTimeOfRuntime,
|
||||
String labelFile);
|
||||
private native long bindNative(String modelFile,
|
||||
String paramsFile,
|
||||
String configFile,
|
||||
int cpuNumThread,
|
||||
boolean enableLiteFp16,
|
||||
int litePowerMode,
|
||||
String liteOptimizedModelDir,
|
||||
boolean enableRecordTimeOfRuntime,
|
||||
String labelFile);
|
||||
|
||||
// Call prediction from native context.
|
||||
private static native long predictNative(long nativeModelContext,
|
||||
Bitmap ARGB8888Bitmap,
|
||||
boolean saved,
|
||||
String savedImagePath,
|
||||
float scoreThreshold,
|
||||
boolean rendering);
|
||||
private native long predictNative(long nativeModelContext,
|
||||
Bitmap ARGB8888Bitmap,
|
||||
boolean saved,
|
||||
String savedImagePath,
|
||||
float scoreThreshold,
|
||||
boolean rendering);
|
||||
|
||||
// Release buffers allocated in native context.
|
||||
private static native boolean releaseNative(long nativeModelContext);
|
||||
private native boolean releaseNative(long nativeModelContext);
|
||||
|
||||
// Initializes at the beginning.
|
||||
static {
|
||||
|
@@ -0,0 +1,41 @@
|
||||
package com.baidu.paddle.fastdeploy.vision.ocr;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.RuntimeOption;
|
||||
|
||||
public class Classifier {
|
||||
// TODO(qiuyanjun): Allows users to set model parameters,
|
||||
// such as cls_thresh, cls_image_shape, is_scale, etc.
|
||||
// These parameters should be passed in via JNI.
|
||||
public String mModelFile;
|
||||
public String mParamsFile;
|
||||
public RuntimeOption mRuntimeOption;
|
||||
public boolean mInitialized = false;
|
||||
|
||||
public Classifier() {
|
||||
mModelFile = "";
|
||||
mParamsFile = "";
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
public Classifier(String modelFile,
|
||||
String paramsFile) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public Classifier(String modelFile,
|
||||
String paramsFile,
|
||||
RuntimeOption option) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mRuntimeOption = option;
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public boolean initialized() {
|
||||
return mInitialized;
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.baidu.paddle.fastdeploy.vision.ocr;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.RuntimeOption;
|
||||
|
||||
public class DBDetector {
|
||||
// TODO(qiuyanjun): Allows users to set model parameters,
|
||||
// such as det_db_box_thresh, det_db_thresh, use_dilation, etc.
|
||||
// These parameters should be passed in via JNI.
|
||||
public String mModelFile;
|
||||
public String mParamsFile;
|
||||
public RuntimeOption mRuntimeOption;
|
||||
public boolean mInitialized = false;
|
||||
|
||||
public DBDetector() {
|
||||
mModelFile = "";
|
||||
mParamsFile = "";
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
public DBDetector(String modelFile,
|
||||
String paramsFile) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public DBDetector(String modelFile,
|
||||
String paramsFile,
|
||||
RuntimeOption option) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mRuntimeOption = option;
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public boolean initialized() {
|
||||
return mInitialized;
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package com.baidu.paddle.fastdeploy.vision.ocr;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.RuntimeOption;
|
||||
|
||||
public class Recognizer {
|
||||
// TODO(qiuyanjun): Allows users to set model parameters,
|
||||
// such as rec_img_h, rec_img_w, rec_image_shape, etc.
|
||||
// These parameters should be passed in via JNI.
|
||||
public String mModelFile;
|
||||
public String mParamsFile;
|
||||
public String mLabelPath;
|
||||
public RuntimeOption mRuntimeOption;
|
||||
public boolean mInitialized = false;
|
||||
|
||||
public Recognizer() {
|
||||
mModelFile = "";
|
||||
mParamsFile = "";
|
||||
mLabelPath = "";
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
public Recognizer(String modelFile,
|
||||
String paramsFile,
|
||||
String labelPath) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mLabelPath = labelPath;
|
||||
mRuntimeOption = new RuntimeOption();
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public Recognizer(String modelFile,
|
||||
String paramsFile,
|
||||
String labelPath,
|
||||
RuntimeOption option) {
|
||||
mModelFile = modelFile;
|
||||
mParamsFile = paramsFile;
|
||||
mLabelPath = labelPath;
|
||||
mRuntimeOption = option;
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
public boolean initialized() {
|
||||
return mInitialized;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user