append Merge branch 'develop' into gbd_android

This commit is contained in:
DefTruth
2022-11-07 20:25:30 +08:00
committed by WinterGeng
46 changed files with 1388 additions and 298 deletions

View File

@@ -20,4 +20,8 @@ FastDeploy
matting.md matting.md
face_recognition.md face_recognition.md
face_detection.md face_detection.md
face_alignment.md
headpose.md
vision_results_en.md vision_results_en.md
runtime.md
runtime_option.md

View File

@@ -0,0 +1,9 @@
# Runtime API
## fastdeploy.Runtime
```{eval-rst}
.. autoclass:: fastdeploy.Runtime
:members:
:inherited-members:
```

View File

@@ -0,0 +1,9 @@
# Runtime Option API
## fastdeploy.RuntimeOption
```{eval-rst}
.. autoclass:: fastdeploy.RuntimeOption
:members:
:inherited-members:
```

View File

@@ -0,0 +1,14 @@
PROJECT(runtime_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.12)
# 指定下载解压后的fastdeploy库路径
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})
add_executable(runtime_demo ${PROJECT_SOURCE_DIR}/infer_onnx_openvino.cc)
# 添加FastDeploy库依赖
target_link_libraries(runtime_demo ${FASTDEPLOY_LIBS})

View File

@@ -0,0 +1,59 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2.onnx";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, "", fd::ModelFormat::ONNX);
runtime_option.UseOpenVINOBackend();
runtime_option.SetCpuThreadNum(12);
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -0,0 +1,60 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2.onnx";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, "", fd::ModelFormat::ONNX);
runtime_option.UseGpu(0);
runtime_option.UseTrtBackend();
runtime_option.SetTrtInputShape("inputs", {1, 3, 224, 224});
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -0,0 +1,60 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2/inference.pdmodel";
std::string params_file = "mobilenetv2/inference.pdiparams";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, params_file, fd::ModelFormat::PADDLE);
runtime_option.UseOrtBackend();
runtime_option.SetCpuThreadNum(12);
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -0,0 +1,60 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2/inference.pdmodel";
std::string params_file = "mobilenetv2/inference.pdiparams";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, params_file, fd::ModelFormat::PADDLE);
runtime_option.UseOpenVINOBackend();
runtime_option.SetCpuThreadNum(12);
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -0,0 +1,65 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2/inference.pdmodel";
std::string params_file = "mobilenetv2/inference.pdiparams";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, params_file, fd::ModelFormat::PADDLE);
// CPU
runtime_option.UsePaddleBackend();
runtime_option.SetCpuThreadNum(12);
// GPU
// runtime_option.UseGpu(0);
// IPU
// runtime_option.UseIpu();
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -0,0 +1,61 @@
// 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 "fastdeploy/runtime.h"
namespace fd = fastdeploy;
int main(int argc, char* argv[]) {
std::string model_file = "mobilenetv2/inference.pdmodel";
std::string params_file = "mobilenetv2/inference.pdiparams";
// setup option
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, params_file, fd::ModelFormat::PADDLE);
runtime_option.UseGpu(0);
runtime_option.UseTrtBackend();
runtime_option.EnablePaddleToTrt();
// init runtime
std::unique_ptr<fd::Runtime> runtime =
std::unique_ptr<fd::Runtime>(new fd::Runtime());
if (!runtime->Init(runtime_option)) {
std::cerr << "--- Init FastDeploy Runitme Failed! "
<< "\n--- Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "--- Init FastDeploy Runitme Done! "
<< "\n--- Model: " << model_file << std::endl;
}
// init input tensor shape
fd::TensorInfo info = runtime->GetInputInfo(0);
info.shape = {1, 3, 224, 224};
std::vector<fd::FDTensor> input_tensors(1);
std::vector<fd::FDTensor> output_tensors(1);
std::vector<float> inputs_data;
inputs_data.resize(1 * 3 * 224 * 224);
for (size_t i = 0; i < inputs_data.size(); ++i) {
inputs_data[i] = std::rand() % 1000 / 1000.0f;
}
input_tensors[0].SetExternalData({1, 3, 224, 224}, fd::FDDataType::FP32, inputs_data.data());
//get input name
input_tensors[0].name = info.name;
runtime->Infer(input_tensors, &output_tensors);
output_tensors[0].PrintInfo();
return 0;
}

View File

@@ -27,6 +27,8 @@ option.set_model_path("mobilenetv2/inference.pdmodel",
# **** GPU 配置 *** # **** GPU 配置 ***
option.use_gpu(0) option.use_gpu(0)
option.use_trt_backend() option.use_trt_backend()
# using TensorRT integrated in Paddle Inference
# option.enable_paddle_to_trt()
# 初始化构造runtime # 初始化构造runtime
runtime = fd.Runtime(option) runtime = fd.Runtime(option)

View File

@@ -121,4 +121,4 @@ void Concat(const std::vector<FDTensor>& x, FDTensor* out, int axis) {
*out = std::move(out_temp); *out = std::move(out_temp);
} }
} // namespace fastdeploy } // namespace fastdeploy

0
fastdeploy/pybind/runtime.cc Executable file → Normal file
View File

View File

@@ -14,9 +14,6 @@
#include "fastdeploy/vision/classification/ppcls/model.h" #include "fastdeploy/vision/classification/ppcls/model.h"
#include "fastdeploy/vision/utils/utils.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
namespace classification { namespace classification {
@@ -25,8 +22,7 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file,
const std::string& params_file, const std::string& params_file,
const std::string& config_file, const std::string& config_file,
const RuntimeOption& custom_option, const RuntimeOption& custom_option,
const ModelFormat& model_format) { const ModelFormat& model_format) : preprocessor_(config_file) {
config_file_ = config_file;
valid_cpu_backends = {Backend::ORT, Backend::OPENVINO, Backend::PDINFER, valid_cpu_backends = {Backend::ORT, Backend::OPENVINO, Backend::PDINFER,
Backend::LITE}; Backend::LITE};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
@@ -38,11 +34,6 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file,
} }
bool PaddleClasModel::Initialize() { bool PaddleClasModel::Initialize() {
if (!BuildPreprocessPipelineFromConfig()) {
FDERROR << "Failed to build preprocess pipeline from configuration file."
<< std::endl;
return false;
}
if (!InitRuntime()) { if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl; FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false; return false;
@@ -50,105 +41,41 @@ bool PaddleClasModel::Initialize() {
return true; return true;
} }
bool PaddleClasModel::BuildPreprocessPipelineFromConfig() {
processors_.clear();
YAML::Node cfg;
try {
cfg = YAML::LoadFile(config_file_);
} catch (YAML::BadFile& e) {
FDERROR << "Failed to load yaml file " << config_file_
<< ", maybe you should check this file." << std::endl;
return false;
}
auto preprocess_cfg = cfg["PreProcess"]["transform_ops"];
processors_.push_back(std::make_shared<BGR2RGB>());
for (const auto& op : preprocess_cfg) {
FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type.");
auto op_name = op.begin()->first.as<std::string>();
if (op_name == "ResizeImage") {
int target_size = op.begin()->second["resize_short"].as<int>();
bool use_scale = false;
int interp = 1;
processors_.push_back(
std::make_shared<ResizeByShort>(target_size, 1, use_scale));
} else if (op_name == "CropImage") {
int width = op.begin()->second["size"].as<int>();
int height = op.begin()->second["size"].as<int>();
processors_.push_back(std::make_shared<CenterCrop>(width, height));
} else if (op_name == "NormalizeImage") {
auto mean = op.begin()->second["mean"].as<std::vector<float>>();
auto std = op.begin()->second["std"].as<std::vector<float>>();
auto scale = op.begin()->second["scale"].as<float>();
FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06,
"Only support scale in Normalize be 0.00392157, means the pixel "
"is in range of [0, 255].");
processors_.push_back(std::make_shared<Normalize>(mean, std));
} else if (op_name == "ToCHWImage") {
processors_.push_back(std::make_shared<HWC2CHW>());
} else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl;
return false;
}
}
return true;
}
bool PaddleClasModel::Preprocess(Mat* mat, FDTensor* output) {
for (size_t i = 0; i < processors_.size(); ++i) {
if (!(*(processors_[i].get()))(mat)) {
FDERROR << "Failed to process image data in " << processors_[i]->Name()
<< "." << std::endl;
return false;
}
}
int channel = mat->Channels();
int width = mat->Width();
int height = mat->Height();
output->name = InputInfoOfRuntime(0).name;
output->SetExternalData({1, channel, height, width}, FDDataType::FP32,
mat->Data());
return true;
}
bool PaddleClasModel::Postprocess(const FDTensor& infer_result,
ClassifyResult* result, int topk) {
int num_classes = infer_result.shape[1];
const float* infer_result_buffer =
reinterpret_cast<const float*>(infer_result.Data());
topk = std::min(num_classes, topk);
result->label_ids =
utils::TopKIndices(infer_result_buffer, num_classes, topk);
result->scores.resize(topk);
for (int i = 0; i < topk; ++i) {
result->scores[i] = *(infer_result_buffer + result->label_ids[i]);
}
return true;
}
bool PaddleClasModel::Predict(cv::Mat* im, ClassifyResult* result, int topk) { bool PaddleClasModel::Predict(cv::Mat* im, ClassifyResult* result, int topk) {
Mat mat(*im); postprocessor_.SetTopk(topk);
std::vector<FDTensor> processed_data(1); if (!Predict(*im, result)) {
if (!Preprocess(&mat, &(processed_data[0]))) { return false;
FDERROR << "Failed to preprocess input data while using model:" }
<< ModelName() << "." << std::endl; return true;
}
bool PaddleClasModel::Predict(const cv::Mat& im, ClassifyResult* result) {
std::vector<ClassifyResult> results;
if (!BatchPredict({im}, &results)) {
return false;
}
*result = std::move(results[0]);
return true;
}
bool PaddleClasModel::BatchPredict(const std::vector<cv::Mat>& images, std::vector<ClassifyResult>* results) {
std::vector<FDMat> fd_images = WrapMat(images);
if (!preprocessor_.Run(&fd_images, &reused_input_tensors)) {
FDERROR << "Failed to preprocess the input image." << std::endl;
return false; return false;
} }
std::vector<FDTensor> infer_result(1); reused_input_tensors[0].name = InputInfoOfRuntime(0).name;
if (!Infer(processed_data, &infer_result)) { if (!Infer(reused_input_tensors, &reused_output_tensors)) {
FDERROR << "Failed to inference while using model:" << ModelName() << "." FDERROR << "Failed to inference by runtime." << std::endl;
<< std::endl;
return false; return false;
} }
if (!Postprocess(infer_result[0], result, topk)) { if (!postprocessor_.Run(reused_output_tensors, results)) {
FDERROR << "Failed to postprocess while using model:" << ModelName() << "." FDERROR << "Failed to postprocess the inference results by runtime." << std::endl;
<< std::endl;
return false; return false;
} }
return true; return true;
} }

View File

@@ -14,8 +14,8 @@
#pragma once #pragma once
#include "fastdeploy/fastdeploy_model.h" #include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h" #include "fastdeploy/vision/classification/ppcls/preprocessor.h"
#include "fastdeploy/vision/common/result.h" #include "fastdeploy/vision/classification/ppcls/postprocessor.h"
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
@@ -43,28 +43,46 @@ class FASTDEPLOY_DECL PaddleClasModel : public FastDeployModel {
/// Get model's name /// Get model's name
virtual std::string ModelName() const { return "PaddleClas/Model"; } virtual std::string ModelName() const { return "PaddleClas/Model"; }
/** \brief Predict the classification result for an input image /** \brief DEPRECATED Predict the classification result for an input image, remove at 1.0 version
* *
* \param[in] im The input image data, comes from cv::imread() * \param[in] im The input image data, comes from cv::imread()
* \param[in] result The output classification result will be writen to this structure * \param[in] result The output classification result will be writen to this structure
* \param[in] topk (int)The topk result by the classify confidence score, default 1
* \return true if the prediction successed, otherwise false * \return true if the prediction successed, otherwise false
*/ */
// TODO(jiangjiajun) Batch is on the way
virtual bool Predict(cv::Mat* im, ClassifyResult* result, int topk = 1); virtual bool Predict(cv::Mat* im, ClassifyResult* result, int topk = 1);
/** \brief Predict the classification result for an input image
*
* \param[in] img The input image data, comes from cv::imread()
* \param[in] result The output classification result
* \return true if the prediction successed, otherwise false
*/
virtual bool Predict(const cv::Mat& img, ClassifyResult* result);
/** \brief Predict the classification results for a batch of input images
*
* \param[in] imgs, The input image list, each element comes from cv::imread()
* \param[in] results The output classification result list
* \return true if the prediction successed, otherwise false
*/
virtual bool BatchPredict(const std::vector<cv::Mat>& imgs,
std::vector<ClassifyResult>* results);
/// Get preprocessor reference of PaddleClasModel
virtual PaddleClasPreprocessor& GetPreprocessor() {
return preprocessor_;
}
/// Get postprocessor reference of PaddleClasModel
virtual PaddleClasPostprocessor& GetPostprocessor() {
return postprocessor_;
}
protected: protected:
bool Initialize(); bool Initialize();
PaddleClasPreprocessor preprocessor_;
bool BuildPreprocessPipelineFromConfig(); PaddleClasPostprocessor postprocessor_;
bool Preprocess(Mat* mat, FDTensor* outputs);
bool Postprocess(const FDTensor& infer_result, ClassifyResult* result,
int topk = 1);
std::vector<std::shared_ptr<Processor>> processors_;
std::string config_file_;
}; };
typedef PaddleClasModel PPLCNet; typedef PaddleClasModel PPLCNet;

View File

@@ -0,0 +1,53 @@
// 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 "fastdeploy/vision/classification/ppcls/postprocessor.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace classification {
PaddleClasPostprocessor::PaddleClasPostprocessor(int topk) {
topk_ = topk;
initialized_ = true;
}
bool PaddleClasPostprocessor::Run(const std::vector<FDTensor>& infer_result, std::vector<ClassifyResult>* results) {
if (!initialized_) {
FDERROR << "Postprocessor is not initialized." << std::endl;
return false;
}
int batch = infer_result[0].shape[0];
int num_classes = infer_result[0].shape[1];
const float* infer_result_data = reinterpret_cast<const float*>(infer_result[0].Data());
results->resize(batch);
int topk = std::min(num_classes, topk_);
for (int i = 0; i < batch; ++i) {
(*results)[i].label_ids = utils::TopKIndices(infer_result_data + i * num_classes, num_classes, topk);
(*results)[i].scores.resize(topk);
for (int j = 0; j < topk; ++j) {
(*results)[i].scores[j] = infer_result_data[i * num_classes + (*results)[i].label_ids[j]];
}
}
return true;
}
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,55 @@
// 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.
#pragma once
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
namespace fastdeploy {
namespace vision {
namespace classification {
/*! @brief Postprocessor object for PaddleClas serials model.
*/
class FASTDEPLOY_DECL PaddleClasPostprocessor {
public:
/** \brief Create a postprocessor instance for PaddleClas serials model
*
* \param[in] topk The topk result filtered by the classify confidence score, default 1
*/
explicit PaddleClasPostprocessor(int topk = 1);
/** \brief Process the result of runtime and fill to ClassifyResult structure
*
* \param[in] tensors The inference result from runtime
* \param[in] result The output result of classification
* \return true if the postprocess successed, otherwise false
*/
bool Run(const std::vector<FDTensor>& tensors,
std::vector<ClassifyResult>* result);
/// Set topk value
void SetTopk(int topk) { topk_ = topk; }
/// Get topk value
int GetTopk() const { return topk_; }
private:
int topk_ = 1;
bool initialized_ = false;
};
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -15,16 +15,62 @@
namespace fastdeploy { namespace fastdeploy {
void BindPaddleClas(pybind11::module& m) { void BindPaddleClas(pybind11::module& m) {
pybind11::class_<vision::classification::PaddleClasPreprocessor>(
m, "PaddleClasPreprocessor")
.def(pybind11::init<std::string>())
.def("run", [](vision::classification::PaddleClasPreprocessor& self, std::vector<pybind11::array>& im_list) {
std::vector<vision::FDMat> images;
for (size_t i = 0; i < im_list.size(); ++i) {
images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
}
std::vector<FDTensor> outputs;
if (!self.Run(&images, &outputs)) {
pybind11::eval("raise Exception('Failed to preprocess the input data in PaddleClasPreprocessor.')");
}
return outputs;
});
pybind11::class_<vision::classification::PaddleClasPostprocessor>(
m, "PaddleClasPostprocessor")
.def(pybind11::init<int>())
.def("run", [](vision::classification::PaddleClasPostprocessor& self, std::vector<FDTensor>& inputs) {
std::vector<vision::ClassifyResult> results;
if (!self.Run(inputs, &results)) {
pybind11::eval("raise Exception('Failed to postprocess the runtime result in PaddleClasPostprocessor.')");
}
return results;
})
.def("run", [](vision::classification::PaddleClasPostprocessor& self, std::vector<pybind11::array>& input_array) {
std::vector<vision::ClassifyResult> results;
std::vector<FDTensor> inputs;
PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
if (!self.Run(inputs, &results)) {
pybind11::eval("raise Exception('Failed to postprocess the runtime result in PaddleClasPostprocessor.')");
}
return results;
})
.def_property("topk", &vision::classification::PaddleClasPostprocessor::GetTopk, &vision::classification::PaddleClasPostprocessor::SetTopk);
pybind11::class_<vision::classification::PaddleClasModel, FastDeployModel>( pybind11::class_<vision::classification::PaddleClasModel, FastDeployModel>(
m, "PaddleClasModel") m, "PaddleClasModel")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption, .def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
ModelFormat>()) ModelFormat>())
.def("predict", [](vision::classification::PaddleClasModel& self, .def("predict", [](vision::classification::PaddleClasModel& self, pybind11::array& data) {
pybind11::array& data, int topk = 1) { cv::Mat im = PyArrayToCvMat(data);
auto mat = PyArrayToCvMat(data); vision::ClassifyResult result;
vision::ClassifyResult res; self.Predict(im, &result);
self.Predict(&mat, &res, topk); return result;
return res; })
}); .def("batch_predict", [](vision::classification::PaddleClasModel& self, std::vector<pybind11::array>& data) {
std::vector<cv::Mat> images;
for (size_t i = 0; i < data.size(); ++i) {
images.push_back(PyArrayToCvMat(data[i]));
}
std::vector<vision::ClassifyResult> results;
self.BatchPredict(images, &results);
return results;
})
.def_property_readonly("preprocessor", &vision::classification::PaddleClasModel::GetPreprocessor)
.def_property_readonly("postprocessor", &vision::classification::PaddleClasModel::GetPostprocessor);
} }
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -0,0 +1,108 @@
// 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 "fastdeploy/vision/classification/ppcls/preprocessor.h"
#include "fastdeploy/function/concat.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy {
namespace vision {
namespace classification {
PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) {
FDASSERT(BuildPreprocessPipelineFromConfig(config_file), "Failed to create PaddleClasPreprocessor.");
initialized_ = true;
}
bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig(const std::string& config_file) {
processors_.clear();
YAML::Node cfg;
try {
cfg = YAML::LoadFile(config_file);
} catch (YAML::BadFile& e) {
FDERROR << "Failed to load yaml file " << config_file
<< ", maybe you should check this file." << std::endl;
return false;
}
auto preprocess_cfg = cfg["PreProcess"]["transform_ops"];
processors_.push_back(std::make_shared<BGR2RGB>());
for (const auto& op : preprocess_cfg) {
FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type.");
auto op_name = op.begin()->first.as<std::string>();
if (op_name == "ResizeImage") {
int target_size = op.begin()->second["resize_short"].as<int>();
bool use_scale = false;
int interp = 1;
processors_.push_back(
std::make_shared<ResizeByShort>(target_size, 1, use_scale));
} else if (op_name == "CropImage") {
int width = op.begin()->second["size"].as<int>();
int height = op.begin()->second["size"].as<int>();
processors_.push_back(std::make_shared<CenterCrop>(width, height));
} else if (op_name == "NormalizeImage") {
auto mean = op.begin()->second["mean"].as<std::vector<float>>();
auto std = op.begin()->second["std"].as<std::vector<float>>();
auto scale = op.begin()->second["scale"].as<float>();
FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06,
"Only support scale in Normalize be 0.00392157, means the pixel "
"is in range of [0, 255].");
processors_.push_back(std::make_shared<Normalize>(mean, std));
} else if (op_name == "ToCHWImage") {
processors_.push_back(std::make_shared<HWC2CHW>());
} else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl;
return false;
}
}
// Fusion will improve performance
FuseTransforms(&processors_);
return true;
}
bool PaddleClasPreprocessor::Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs) {
if (!initialized_) {
FDERROR << "The preprocessor is not initialized." << std::endl;
return false;
}
if (images->size() == 0) {
FDERROR << "The size of input images should be greater than 0." << std::endl;
return false;
}
for (size_t i = 0; i < images->size(); ++i) {
for (size_t j = 0; j < processors_.size(); ++j) {
if (!(*(processors_[j].get()))(&((*images)[i]))) {
FDERROR << "Failed to processs image:" << i << " in " << processors_[i]->Name() << "." << std::endl;
return false;
}
}
}
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images->size());
for (size_t i = 0; i < images->size(); ++i) {
(*images)[i].ShareWithTensor(&(tensors[i]));
tensors[i].ExpandDim(0);
}
Concat(tensors, &((*outputs)[0]), 0);
return true;
}
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,50 @@
// 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.
#pragma once
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
namespace fastdeploy {
namespace vision {
namespace classification {
/*! @brief Preprocessor object for PaddleClas serials model.
*/
class FASTDEPLOY_DECL PaddleClasPreprocessor {
public:
/** \brief Create a preprocessor instance for PaddleClas serials model
*
* \param[in] config_file Path of configuration file for deployment, e.g resnet/infer_cfg.yml
*/
explicit PaddleClasPreprocessor(const std::string& config_file);
/** \brief Process the input image and prepare input tensors for runtime
*
* \param[in] images The input image data list, all the elements are returned by cv::imread()
* \param[in] outputs The output tensors which will feed in runtime
* \return true if the preprocess successed, otherwise false
*/
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
private:
bool BuildPreprocessPipelineFromConfig(const std::string& config_file);
std::vector<std::shared_ptr<Processor>> processors_;
bool initialized_ = false;
};
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -54,5 +54,12 @@ void DisableFlyCV() {
<< DefaultProcLib::default_lib << std::endl; << DefaultProcLib::default_lib << std::endl;
} }
void SetProcLibCpuNumThreads(int threads) {
cv::setNumThreads(threads);
#ifdef ENABLE_FLYCV
fcv::set_thread_num(threads);
#endif
}
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -31,6 +31,11 @@ FASTDEPLOY_DECL void EnableFlyCV();
/// Disable using FlyCV to process image while deploy vision models. /// Disable using FlyCV to process image while deploy vision models.
FASTDEPLOY_DECL void DisableFlyCV(); FASTDEPLOY_DECL void DisableFlyCV();
/*! @brief Set the cpu num threads of ProcLib. The cpu num threads
* of FlyCV and OpenCV is 2 by default.
*/
FASTDEPLOY_DECL void SetProcLibCpuNumThreads(int threads);
class FASTDEPLOY_DECL Processor { class FASTDEPLOY_DECL Processor {
public: public:
// default_lib has the highest priority // default_lib has the highest priority

View File

@@ -51,7 +51,7 @@ bool ResizeByShort::ImplByFlyCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but " FDERROR << "LimitByShort: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;

View File

@@ -35,6 +35,14 @@ std::string ClassifyResult::Str() {
return out; return out;
} }
ClassifyResult& ClassifyResult::operator=(ClassifyResult&& other) {
if (&other != this) {
label_ids = std::move(other.label_ids);
scores = std::move(other.scores);
}
return *this;
}
void Mask::Reserve(int size) { data.reserve(size); } void Mask::Reserve(int size) { data.reserve(size); }
void Mask::Resize(int size) { data.resize(size); } void Mask::Resize(int size) { data.resize(size); }

View File

@@ -44,6 +44,7 @@ struct FASTDEPLOY_DECL BaseResult {
/*! @brief Classify result structure for all the image classify models /*! @brief Classify result structure for all the image classify models
*/ */
struct FASTDEPLOY_DECL ClassifyResult : public BaseResult { struct FASTDEPLOY_DECL ClassifyResult : public BaseResult {
ClassifyResult() = default;
/// Classify result for an image /// Classify result for an image
std::vector<int32_t> label_ids; std::vector<int32_t> label_ids;
/// The confidence for each classify result /// The confidence for each classify result
@@ -53,6 +54,11 @@ struct FASTDEPLOY_DECL ClassifyResult : public BaseResult {
/// Clear result /// Clear result
void Clear(); void Clear();
/// Copy constructor
ClassifyResult(const ClassifyResult& other) = default;
/// Move assignment
ClassifyResult& operator=(ClassifyResult&& other);
/// Debug function, convert the result to string to print /// Debug function, convert the result to string to print
std::string Str(); std::string Str();
}; };

View File

@@ -15,14 +15,14 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".detection.MainActivity"> <activity android:name=".ocr.OcrMainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name=".detection.SettingsActivity" android:name=".ocr.OcrSettingsActivity"
android:label="Settings"> android:label="Settings">
</activity> </activity>
</application> </application>

View File

@@ -44,8 +44,8 @@ import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener { public class DetectionMainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener {
private static final String TAG = MainActivity.class.getSimpleName(); private static final String TAG = DetectionMainActivity.class.getSimpleName();
CameraSurfaceView svPreview; CameraSurfaceView svPreview;
TextView tvStatus; TextView tvStatus;
@@ -90,7 +90,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.default_activity_main); setContentView(R.layout.detection_activity_main);
// Clear all setting items to avoid app crashing due to the incorrect settings // Clear all setting items to avoid app crashing due to the incorrect settings
initSettings(); initSettings();
@@ -121,7 +121,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
resultImage.setImageBitmap(shutterBitmap); resultImage.setImageBitmap(shutterBitmap);
break; break;
case R.id.btn_settings: case R.id.btn_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class)); startActivity(new Intent(DetectionMainActivity.this, DetectionSettingsActivity.class));
break; break;
case R.id.realtime_toggle_btn: case R.id.realtime_toggle_btn:
toggleRealtimeStyle(); toggleRealtimeStyle();
@@ -216,11 +216,11 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888,true); originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888,true);
boolean modified = false; boolean modified = false;
DetectionResult result = predictor.predict( DetectionResult result = predictor.predict(
ARGB8888ImageBitmap, savedImagePath, SettingsActivity.scoreThreshold); ARGB8888ImageBitmap, savedImagePath, DetectionSettingsActivity.scoreThreshold);
modified = result.initialized(); modified = result.initialized();
if (!savedImagePath.isEmpty()) { if (!savedImagePath.isEmpty()) {
synchronized (this) { synchronized (this) {
MainActivity.this.savedImagePath = "result.jpg"; DetectionMainActivity.this.savedImagePath = "result.jpg";
} }
} }
lastFrameIndex++; lastFrameIndex++;
@@ -325,7 +325,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
results.add(new BaseResultModel(1, "cup", 0.4f)); results.add(new BaseResultModel(1, "cup", 0.4f));
results.add(new BaseResultModel(2, "pen", 0.6f)); results.add(new BaseResultModel(2, "pen", 0.6f));
results.add(new BaseResultModel(3, "tang", 1.0f)); results.add(new BaseResultModel(3, "tang", 1.0f));
final DetectResultAdapter adapter = new DetectResultAdapter(this, R.layout.default_result_page_item, results); final DetectResultAdapter adapter = new DetectResultAdapter(this, R.layout.detection_result_page_item, results);
detectResultView.setAdapter(adapter); detectResultView.setAdapter(adapter);
detectResultView.invalidate(); detectResultView.invalidate();
@@ -375,25 +375,25 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); editor.clear();
editor.commit(); editor.commit();
SettingsActivity.resetSettings(); DetectionSettingsActivity.resetSettings();
} }
public void checkAndUpdateSettings() { public void checkAndUpdateSettings() {
if (SettingsActivity.checkAndUpdateSettings(this)) { if (DetectionSettingsActivity.checkAndUpdateSettings(this)) {
String realModelDir = getCacheDir() + "/" + SettingsActivity.modelDir; String realModelDir = getCacheDir() + "/" + DetectionSettingsActivity.modelDir;
Utils.copyDirectoryFromAssets(this, SettingsActivity.modelDir, realModelDir); Utils.copyDirectoryFromAssets(this, DetectionSettingsActivity.modelDir, realModelDir);
String realLabelPath = getCacheDir() + "/" + SettingsActivity.labelPath; String realLabelPath = getCacheDir() + "/" + DetectionSettingsActivity.labelPath;
Utils.copyFileFromAssets(this, SettingsActivity.labelPath, realLabelPath); Utils.copyFileFromAssets(this, DetectionSettingsActivity.labelPath, realLabelPath);
String modelFile = realModelDir + "/" + "model.pdmodel"; String modelFile = realModelDir + "/" + "model.pdmodel";
String paramsFile = realModelDir + "/" + "model.pdiparams"; String paramsFile = realModelDir + "/" + "model.pdiparams";
String configFile = realModelDir + "/" + "infer_cfg.yml"; String configFile = realModelDir + "/" + "infer_cfg.yml";
String labelFile = realLabelPath; String labelFile = realLabelPath;
RuntimeOption option = new RuntimeOption(); RuntimeOption option = new RuntimeOption();
option.setCpuThreadNum(SettingsActivity.cpuThreadNum); option.setCpuThreadNum(DetectionSettingsActivity.cpuThreadNum);
option.setLitePowerMode(SettingsActivity.cpuPowerMode); option.setLitePowerMode(DetectionSettingsActivity.cpuPowerMode);
option.enableRecordTimeOfRuntime(); option.enableRecordTimeOfRuntime();
if (Boolean.parseBoolean(SettingsActivity.enableLiteFp16)) { if (Boolean.parseBoolean(DetectionSettingsActivity.enableLiteFp16)) {
option.enableLiteFp16(); option.enableLiteFp16();
} }
predictor.init(modelFile, paramsFile, configFile, labelFile, option); predictor.init(modelFile, paramsFile, configFile, labelFile, option);
@@ -405,7 +405,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
@NonNull int[] grantResults) { @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(DetectionMainActivity.this)
.setTitle("Permission denied") .setTitle("Permission denied")
.setMessage("Click to force quit the app, then open Settings->Apps & notifications->Target " + .setMessage("Click to force quit the app, then open Settings->Apps & notifications->Target " +
"App->Permissions to grant all of the permissions.") "App->Permissions to grant all of the permissions.")
@@ -413,7 +413,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
.setPositiveButton("Exit", new DialogInterface.OnClickListener() { .setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish(); DetectionMainActivity.this.finish();
} }
}).show(); }).show();
} }

View File

@@ -16,9 +16,9 @@ import com.baidu.paddle.fastdeploy.app.ui.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SettingsActivity extends AppCompatPreferenceActivity implements public class DetectionSettingsActivity extends AppCompatPreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = SettingsActivity.class.getSimpleName(); private static final String TAG = DetectionSettingsActivity.class.getSimpleName();
static public int selectedModelIdx = -1; static public int selectedModelIdx = -1;
static public String modelDir = ""; static public String modelDir = "";

View File

@@ -25,8 +25,8 @@ import android.widget.Toast;
import com.baidu.paddle.fastdeploy.RuntimeOption; import com.baidu.paddle.fastdeploy.RuntimeOption;
import com.baidu.paddle.fastdeploy.app.examples.R; import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.CameraSurfaceView; import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils; import com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView;
import com.baidu.paddle.fastdeploy.vision.OCRResult; import com.baidu.paddle.fastdeploy.vision.OCRResult;
import com.baidu.paddle.fastdeploy.pipeline.PPOCRv2; import com.baidu.paddle.fastdeploy.pipeline.PPOCRv2;
import com.baidu.paddle.fastdeploy.vision.ocr.Classifier; import com.baidu.paddle.fastdeploy.vision.ocr.Classifier;
@@ -37,8 +37,8 @@ import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
public class MainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener { public class OcrMainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener {
private static final String TAG = MainActivity.class.getSimpleName(); private static final String TAG = OcrMainActivity.class.getSimpleName();
CameraSurfaceView svPreview; CameraSurfaceView svPreview;
TextView tvStatus; TextView tvStatus;
@@ -64,7 +64,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main); setContentView(R.layout.ocr_activity_main);
// Clear all setting items to avoid app crashing due to the incorrect settings // Clear all setting items to avoid app crashing due to the incorrect settings
initSettings(); initSettings();
@@ -91,10 +91,10 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
synchronized (this) { synchronized (this) {
savedImagePath = Utils.getDCIMDirectory() + File.separator + date.format(new Date()).toString() + ".png"; savedImagePath = Utils.getDCIMDirectory() + File.separator + date.format(new Date()).toString() + ".png";
} }
Toast.makeText(MainActivity.this, "Save snapshot to " + savedImagePath, Toast.LENGTH_SHORT).show(); Toast.makeText(OcrMainActivity.this, "Save snapshot to " + savedImagePath, Toast.LENGTH_SHORT).show();
break; break;
case R.id.btn_settings: case R.id.btn_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class)); startActivity(new Intent(OcrMainActivity.this, OcrSettingsActivity.class));
break; break;
case R.id.realtime_toggle_btn: case R.id.realtime_toggle_btn:
toggleRealtimeStyle(); toggleRealtimeStyle();
@@ -128,14 +128,14 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) {
String savedImagePath = ""; String savedImagePath = "";
synchronized (this) { synchronized (this) {
savedImagePath = MainActivity.this.savedImagePath; savedImagePath = OcrMainActivity.this.savedImagePath;
} }
boolean modified = false; boolean modified = false;
OCRResult result = predictor.predict(ARGB8888ImageBitmap, savedImagePath); OCRResult result = predictor.predict(ARGB8888ImageBitmap, savedImagePath);
modified = result.initialized(); modified = result.initialized();
if (!savedImagePath.isEmpty()) { if (!savedImagePath.isEmpty()) {
synchronized (this) { synchronized (this) {
MainActivity.this.savedImagePath = "result.jpg"; OcrMainActivity.this.savedImagePath = "result.jpg";
} }
} }
lastFrameIndex++; lastFrameIndex++;
@@ -201,12 +201,12 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); editor.clear();
editor.commit(); editor.commit();
SettingsActivity.resetSettings(); OcrSettingsActivity.resetSettings();
} }
public void checkAndUpdateSettings() { public void checkAndUpdateSettings() {
if (SettingsActivity.checkAndUpdateSettings(this)) { if (OcrSettingsActivity.checkAndUpdateSettings(this)) {
String realModelDir = getCacheDir() + "/" + SettingsActivity.modelDir; String realModelDir = getCacheDir() + "/" + OcrSettingsActivity.modelDir;
// String detModelName = "ch_PP-OCRv2_det_infer"; // String detModelName = "ch_PP-OCRv2_det_infer";
String detModelName = "ch_PP-OCRv3_det_infer"; String detModelName = "ch_PP-OCRv3_det_infer";
// String detModelName = "ch_ppocr_mobile_v2.0_det_infer"; // String detModelName = "ch_ppocr_mobile_v2.0_det_infer";
@@ -217,14 +217,14 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
String realDetModelDir = realModelDir + "/" + detModelName; String realDetModelDir = realModelDir + "/" + detModelName;
String realClsModelDir = realModelDir + "/" + clsModelName; String realClsModelDir = realModelDir + "/" + clsModelName;
String realRecModelDir = realModelDir + "/" + recModelName; String realRecModelDir = realModelDir + "/" + recModelName;
String srcDetModelDir = SettingsActivity.modelDir + "/" + detModelName; String srcDetModelDir = OcrSettingsActivity.modelDir + "/" + detModelName;
String srcClsModelDir = SettingsActivity.modelDir + "/" + clsModelName; String srcClsModelDir = OcrSettingsActivity.modelDir + "/" + clsModelName;
String srcRecModelDir = SettingsActivity.modelDir + "/" + recModelName; String srcRecModelDir = OcrSettingsActivity.modelDir + "/" + recModelName;
Utils.copyDirectoryFromAssets(this, srcDetModelDir, realDetModelDir); Utils.copyDirectoryFromAssets(this, srcDetModelDir, realDetModelDir);
Utils.copyDirectoryFromAssets(this, srcClsModelDir, realClsModelDir); Utils.copyDirectoryFromAssets(this, srcClsModelDir, realClsModelDir);
Utils.copyDirectoryFromAssets(this, srcRecModelDir, realRecModelDir); Utils.copyDirectoryFromAssets(this, srcRecModelDir, realRecModelDir);
String realLabelPath = getCacheDir() + "/" + SettingsActivity.labelPath; String realLabelPath = getCacheDir() + "/" + OcrSettingsActivity.labelPath;
Utils.copyFileFromAssets(this, SettingsActivity.labelPath, realLabelPath); Utils.copyFileFromAssets(this, OcrSettingsActivity.labelPath, realLabelPath);
String detModelFile = realDetModelDir + "/" + "inference.pdmodel"; String detModelFile = realDetModelDir + "/" + "inference.pdmodel";
String detParamsFile = realDetModelDir + "/" + "inference.pdiparams"; String detParamsFile = realDetModelDir + "/" + "inference.pdiparams";
@@ -236,16 +236,16 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
RuntimeOption detOption = new RuntimeOption(); RuntimeOption detOption = new RuntimeOption();
RuntimeOption clsOption = new RuntimeOption(); RuntimeOption clsOption = new RuntimeOption();
RuntimeOption recOption = new RuntimeOption(); RuntimeOption recOption = new RuntimeOption();
detOption.setCpuThreadNum(SettingsActivity.cpuThreadNum); detOption.setCpuThreadNum(OcrSettingsActivity.cpuThreadNum);
clsOption.setCpuThreadNum(SettingsActivity.cpuThreadNum); clsOption.setCpuThreadNum(OcrSettingsActivity.cpuThreadNum);
recOption.setCpuThreadNum(SettingsActivity.cpuThreadNum); recOption.setCpuThreadNum(OcrSettingsActivity.cpuThreadNum);
detOption.setLitePowerMode(SettingsActivity.cpuPowerMode); detOption.setLitePowerMode(OcrSettingsActivity.cpuPowerMode);
clsOption.setLitePowerMode(SettingsActivity.cpuPowerMode); clsOption.setLitePowerMode(OcrSettingsActivity.cpuPowerMode);
recOption.setLitePowerMode(SettingsActivity.cpuPowerMode); recOption.setLitePowerMode(OcrSettingsActivity.cpuPowerMode);
detOption.enableRecordTimeOfRuntime(); detOption.enableRecordTimeOfRuntime();
clsOption.enableRecordTimeOfRuntime(); clsOption.enableRecordTimeOfRuntime();
recOption.enableRecordTimeOfRuntime(); recOption.enableRecordTimeOfRuntime();
if (Boolean.parseBoolean(SettingsActivity.enableLiteFp16)) { if (Boolean.parseBoolean(OcrSettingsActivity.enableLiteFp16)) {
detOption.enableLiteFp16(); detOption.enableLiteFp16();
clsOption.enableLiteFp16(); clsOption.enableLiteFp16();
recOption.enableLiteFp16(); recOption.enableLiteFp16();
@@ -263,7 +263,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
@NonNull int[] grantResults) { @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(OcrMainActivity.this)
.setTitle("Permission denied") .setTitle("Permission denied")
.setMessage("Click to force quit the app, then open Settings->Apps & notifications->Target " + .setMessage("Click to force quit the app, then open Settings->Apps & notifications->Target " +
"App->Permissions to grant all of the permissions.") "App->Permissions to grant all of the permissions.")
@@ -271,7 +271,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
.setPositiveButton("Exit", new DialogInterface.OnClickListener() { .setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish(); OcrMainActivity.this.finish();
} }
}).show(); }).show();
} }

View File

@@ -10,15 +10,15 @@ import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import com.baidu.paddle.fastdeploy.app.examples.R; import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.AppCompatPreferenceActivity; import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils; import com.baidu.paddle.fastdeploy.app.ui.view.AppCompatPreferenceActivity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SettingsActivity extends AppCompatPreferenceActivity implements public class OcrSettingsActivity extends AppCompatPreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = SettingsActivity.class.getSimpleName(); private static final String TAG = OcrSettingsActivity.class.getSimpleName();
static public int selectedModelIdx = -1; static public int selectedModelIdx = -1;
static public String modelDir = ""; static public String modelDir = "";

View File

@@ -1,99 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context="com.baidu.paddle.fastdeploy.app.examples.ocr.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWindow">
<com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView
android:id="@+id/sv_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="@dimen/top_bar_height"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/colorTopBar">
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/top_bar_left_right_margin"
android:layout_marginBottom="@dimen/top_bar_left_right_margin"
android:textColor="@color/colorText"
android:gravity="center"
android:textSize="@dimen/small_font_size" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/colorBottomBar"
android:orientation="horizontal">
<LinearLayout
android:layout_width="@dimen/bottom_bar_top_margin"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
<RelativeLayout
android:layout_width="@dimen/large_button_height"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/btn_switch"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_height"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/bottom_bar_left_right_margin"
android:layout_marginBottom="@dimen/bottom_bar_left_right_margin"
android:background="#00000000"
android:scaleType="fitXY"
android:src="@drawable/btn_switch" />
<ImageButton
android:id="@+id/btn_shutter"
android:layout_width="@dimen/large_button_width"
android:layout_height="@dimen/large_button_height"
android:layout_centerInParent="true"
android:background="@null"
android:focusable="true"
android:focusableInTouchMode="true"
android:scaleType="fitXY"
android:src="@drawable/btn_shutter" />
<ImageButton
android:id="@+id/btn_settings"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/bottom_bar_left_right_margin"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/btn_settings" />
</RelativeLayout>
<LinearLayout
android:layout_width="@dimen/bottom_bar_bottom_margin"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/detection_camera_page"
android:id="@+id/camera_page"></include>
<include
layout="@layout/detection_result_page"
android:id="@+id/result_page"
android:visibility="gone"></include>
</FrameLayout>

View File

@@ -4,11 +4,11 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<include <include
layout="@layout/default_camera_page" layout="@layout/ocr_camera_page"
android:id="@+id/camera_page"></include> android:id="@+id/camera_page"></include>
<include <include
layout="@layout/default_result_page" layout="@layout/ocr_result_page"
android:id="@+id/result_page" android:id="@+id/result_page"
android:visibility="gone"></include> android:visibility="gone"></include>
</FrameLayout> </FrameLayout>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/detection_camera_page"
android:id="@+id/camera_page"></include>
<include
layout="@layout/detection_result_page"
android:id="@+id/result_page"
android:visibility="gone"></include>
</FrameLayout>

View File

@@ -0,0 +1,161 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".detection.DetectionMainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWindow">
<com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout
android:id="@+id/action_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/back_in_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:paddingLeft="40px"
android:paddingTop="60px"
android:paddingRight="60px"
android:paddingBottom="40px"
android:src="@drawable/back_btn" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:orientation="horizontal">
<TextView
android:id="@+id/action_takepicture_btn"
style="@style/action_btn_selected"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/action_bar_take_photo"
android:textAlignment="center"
android:visibility="gone"/>
<TextView
android:id="@+id/action_realtime_btn"
style="@style/action_btn"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/action_bar_realtime"
android:textAlignment="center" />
</LinearLayout>
</com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout>
<!-- 实时-->
<com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView
android:id="@+id/sv_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/contral"
android:layout_below="@+id/action_bar_main"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/albumSelect"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="145dp"
android:background="@drawable/album_btn"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="60dp"
android:layout_marginRight="30dp"
android:textColor="@color/colorText"
android:textSize="@dimen/small_font_size" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/top_bar_height"
android:layout_alignParentTop="true"
android:background="@color/colorTopBar">
<ImageButton
android:id="@+id/btn_settings"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/btn_settings" />
</RelativeLayout>
<LinearLayout
android:id="@+id/contral"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorBottomBar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_bar_top_margin"
android:orientation="vertical"></LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/large_button_height">
<ImageButton
android:id="@+id/btn_switch"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="#00000000"
android:scaleType="fitXY"
android:src="@drawable/switch_side_btn" />
<ImageButton
android:id="@+id/btn_shutter"
android:layout_width="@dimen/large_button_width"
android:layout_height="@dimen/large_button_height"
android:layout_centerInParent="true"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/take_picture_btn" />
<ImageView
android:id="@+id/realtime_toggle_btn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="60dp"
android:scaleType="fitXY"
android:src="@drawable/realtime_stop_btn" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_bar_bottom_margin"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/ocr_camera_page"
android:id="@+id/camera_page"></include>
<include
layout="@layout/ocr_result_page"
android:id="@+id/result_page"
android:visibility="gone"></include>
</FrameLayout>

View File

@@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:keepScreenOn="true" android:keepScreenOn="true"
tools:context=".MainActivity"> tools:context=".ocr.OcrMainActivity">
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@@ -0,0 +1,160 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout
android:id="@+id/action_bar_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/back_in_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:paddingLeft="40px"
android:paddingTop="60px"
android:paddingRight="60px"
android:paddingBottom="40px"
android:src="@drawable/back_btn" />
<TextView
android:id="@+id/model_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:textColor="@color/textColor"
android:textSize="@dimen/action_btn_text_size" />
</com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="700px">
<ImageView
android:id="@+id/result_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bk_result_image_padding" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40px"
android:layout_marginTop="26px"
android:layout_marginBottom="20px"
android:text="@string/result_label"
android:textColor="@color/bk_black"
android:textSize="56px"
android:visibility="visible" />
<LinearLayout
android:id="@+id/result_seekbar_section"
android:layout_width="match_parent"
android:layout_height="130px"
android:layout_marginLeft="@dimen/result_list_padding_lr"
android:layout_marginRight="@dimen/result_list_padding_lr"
android:layout_marginBottom="@dimen/result_list_gap_width"
android:background="@drawable/result_page_border_section_bk"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:paddingLeft="30px"
android:text="@string/result_table_header_confidence"
android:textColor="@color/table_result_tableheader_text_color"
android:textSize="@dimen/result_list_view_text_size" />
<SeekBar
android:id="@+id/confidence_seekbar"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="6"
android:focusable="false"
android:maxHeight="8px"
android:progressDrawable="@drawable/seekbar_progress_result"
android:splitTrack="false"
android:thumb="@drawable/seekbar_handle" />
<TextView
android:id="@+id/seekbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingRight="30px"
android:textSize="@dimen/result_list_view_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/result_list_padding_lr"
android:layout_marginRight="@dimen/result_list_padding_lr"
android:layout_marginBottom="@dimen/result_list_gap_width"
android:background="@drawable/result_page_border_section_bk"
android:visibility="visible">
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result_table_header_index"
android:textColor="@color/table_result_tableheader_text_color" />
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result_table_header_name"
android:textColor="@color/table_result_tableheader_text_color" />
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="right"
android:text="@string/result_table_header_confidence"
android:textColor="@color/table_result_tableheader_text_color" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15px"
android:paddingLeft="@dimen/result_list_padding_lr"
android:paddingRight="@dimen/result_list_padding_lr">
<com.baidu.paddle.fastdeploy.app.ui.view.ResultListView
android:id="@+id/result_list_view"
android:layout_width="match_parent"
android:layout_height="700px"
android:divider="#FFFFFF"
android:dividerHeight="@dimen/result_list_gap_width"></com.baidu.paddle.fastdeploy.app.ui.view.ResultListView>
</ScrollView>
</FrameLayout>
</LinearLayout>
</FrameLayout>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/result_page_border_section_bk">
<TextView
android:id="@+id/index"
style="@style/list_result_view_item_style"
android:layout_width="wrap_content"
android:layout_weight="0.2" />
<TextView
android:id="@+id/name"
style="@style/list_result_view_item_style"
android:layout_width="wrap_content"
android:layout_weight="0.6"
android:maxWidth="300px" />
<TextView
android:id="@+id/confidence"
style="@style/list_result_view_item_style"
android:layout_weight="0.2"
android:layout_width="wrap_content" />
</LinearLayout>

View File

@@ -46,7 +46,7 @@ dependencies {
def archives = [ def archives = [
[ [
'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-0.5.0-shared-dev.tgz', 'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-latest-shared-dev.tgz',
'dest': 'libs' 'dest': 'libs'
] ]
] ]

View File

@@ -12,7 +12,7 @@ project("fastdeploy_jni")
# You can define multiple libraries, and CMake builds them for you. # You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK. # Gradle automatically packages shared libraries with your APK.
set(FastDeploy_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/fastdeploy-android-0.5.0-shared-dev") set(FastDeploy_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/fastdeploy-android-latest-shared-dev")
find_package(FastDeploy REQUIRED) find_package(FastDeploy REQUIRED)

View File

@@ -14,8 +14,9 @@
from __future__ import absolute_import from __future__ import absolute_import
from .contrib.yolov5cls import YOLOv5Cls from .contrib.yolov5cls import YOLOv5Cls
from .ppcls import PaddleClasModel from .ppcls import *
from .contrib.resnet import ResNet from .contrib.resnet import ResNet
PPLCNet = PaddleClasModel PPLCNet = PaddleClasModel
PPLCNetv2 = PaddleClasModel PPLCNetv2 = PaddleClasModel
EfficientNet = PaddleClasModel EfficientNet = PaddleClasModel

View File

@@ -18,6 +18,42 @@ from .... import FastDeployModel, ModelFormat
from .... import c_lib_wrap as C from .... import c_lib_wrap as C
class PaddleClasPreprocessor:
def __init__(self, config_file):
"""Create a preprocessor for PaddleClasModel from configuration file
:param config_file: (str)Path of configuration file, e.g resnet50/inference_cls.yaml
"""
self._preprocessor = C.vision.classification.PaddleClasPreprocessor(
config_file)
def run(self, input_ims):
"""Preprocess input images for PaddleClasModel
:param: input_ims: (list of numpy.ndarray)The input image
:return: list of FDTensor
"""
return self._preprocessor.run(input_ims)
class PaddleClasPostprocessor:
def __init__(self, topk=1):
"""Create a postprocessor for PaddleClasModel
:param topk: (int)Filter the top k classify label
"""
self._postprocessor = C.vision.classification.PaddleClasPostprocessor(
topk)
def run(self, runtime_results):
"""Postprocess the runtime results for PaddleClasModel
:param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
:return: list of ClassifyResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
"""
return self._postprocessor.run(runtime_results)
class PaddleClasModel(FastDeployModel): class PaddleClasModel(FastDeployModel):
def __init__(self, def __init__(self,
model_file, model_file,
@@ -45,9 +81,35 @@ class PaddleClasModel(FastDeployModel):
def predict(self, im, topk=1): def predict(self, im, topk=1):
"""Classify an input image """Classify an input image
:param im: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format :param im: (numpy.ndarray) The input image data, a 3-D array with layout HWC, BGR format
:param topk: (int)The topk result by the classify confidence score, default 1 :param topk: (int) Filter the topk classify result, default 1
:return: ClassifyResult :return: ClassifyResult
""" """
return self._model.predict(im, topk) self.postprocessor.topk = topk
return self._model.predict(im)
def batch_predict(self, images):
"""Classify a batch of input image
:param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
:return list of ClassifyResult
"""
return self._model.batch_predict(images)
@property
def preprocessor(self):
"""Get PaddleClasPreprocessor object of the loaded model
:return PaddleClasPreprocessor
"""
return self._model.preprocessor
@property
def postprocessor(self):
"""Get PaddleClasPostprocessor object of the loaded model
:return PaddleClasPostprocessor
"""
return self._model.postprocessor

View File

@@ -22,9 +22,11 @@ import runtime_config as rc
def test_classification_mobilenetv2(): def test_classification_mobilenetv2():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/MobileNetV1_x0_25_infer.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/MobileNetV1_x0_25_infer.tgz"
input_url = "https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg" input_url1 = "https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg"
input_url2 = "https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00030010.jpeg"
fd.download_and_decompress(model_url, "resources") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, "resources") fd.download(input_url1, "resources")
fd.download(input_url2, "resources")
model_path = "resources/MobileNetV1_x0_25_infer" model_path = "resources/MobileNetV1_x0_25_infer"
model_file = "resources/MobileNetV1_x0_25_infer/inference.pdmodel" model_file = "resources/MobileNetV1_x0_25_infer/inference.pdmodel"
@@ -33,18 +35,67 @@ def test_classification_mobilenetv2():
model = fd.vision.classification.PaddleClasModel( model = fd.vision.classification.PaddleClasModel(
model_file, params_file, config_file, runtime_option=rc.test_option) model_file, params_file, config_file, runtime_option=rc.test_option)
expected_label_ids = [153, 333, 259, 338, 265, 154] expected_label_ids_1 = [153, 333, 259, 338, 265, 154]
expected_scores = [ expected_scores_1 = [
0.221088, 0.109457, 0.078668, 0.076814, 0.052401, 0.048206 0.221088, 0.109457, 0.078668, 0.076814, 0.052401, 0.048206
] ]
expected_label_ids_2 = [80, 23, 93, 99, 143, 7]
expected_scores_2 = [
0.975599, 0.014083, 0.003821, 0.001571, 0.001233, 0.000924
]
# compare diff # compare diff
im = cv2.imread("./resources/ILSVRC2012_val_00000010.jpeg") im1 = cv2.imread("./resources/ILSVRC2012_val_00000010.jpeg")
for i in range(2): im2 = cv2.imread("./resources/ILSVRC2012_val_00030010.jpeg")
result = model.predict(im, topk=6)
diff_label = np.fabs( # for i in range(3000000):
np.array(result.label_ids) - np.array(expected_label_ids)) while True:
diff_scores = np.fabs( # test single predict
np.array(result.scores) - np.array(expected_scores)) model.postprocessor.topk = 6
assert diff_label.max() < 1e-06, "There's difference in classify label." result1 = model.predict(im1)
assert diff_scores.max( result2 = model.predict(im2)
) < 1e-05, "There's difference in classify score."
diff_label_1 = np.fabs(
np.array(result1.label_ids) - np.array(expected_label_ids_1))
diff_label_2 = np.fabs(
np.array(result2.label_ids) - np.array(expected_label_ids_2))
diff_scores_1 = np.fabs(
np.array(result1.scores) - np.array(expected_scores_1))
diff_scores_2 = np.fabs(
np.array(result2.scores) - np.array(expected_scores_2))
assert diff_label_1.max(
) < 1e-06, "There's difference in classify label 1."
assert diff_scores_1.max(
) < 1e-05, "There's difference in classify score 1."
assert diff_label_2.max(
) < 1e-06, "There's difference in classify label 2."
assert diff_scores_2.max(
) < 1e-05, "There's difference in classify score 2."
# test batch predict
results = model.batch_predict([im1, im2])
result1 = results[0]
result2 = results[1]
diff_label_1 = np.fabs(
np.array(result1.label_ids) - np.array(expected_label_ids_1))
diff_label_2 = np.fabs(
np.array(result2.label_ids) - np.array(expected_label_ids_2))
diff_scores_1 = np.fabs(
np.array(result1.scores) - np.array(expected_scores_1))
diff_scores_2 = np.fabs(
np.array(result2.scores) - np.array(expected_scores_2))
assert diff_label_1.max(
) < 1e-06, "There's difference in classify label 1."
assert diff_scores_1.max(
) < 1e-05, "There's difference in classify score 1."
assert diff_label_2.max(
) < 1e-06, "There's difference in classify label 2."
assert diff_scores_2.max(
) < 1e-05, "There's difference in classify score 2."
if __name__ == "__main__":
test_classification_mobilenetv2()