Files
FastDeploy/examples/vision/ocr/PP-OCRv3/c/README_CN.md
chenjian c8bcada1a2 [Doc] Add docs for ppocr ppseg examples (#1429)
* add docs for examples

* add english doc

* fix

* fix docs
2023-02-28 20:13:01 +08:00

7.7 KiB
Raw Blame History

English | 简体中文

PPOCRv3 C部署示例

本目录下提供infer.c来调用C API快速完成PPOCRv3模型在CPU/GPU上部署的示例。

在部署前,需确认以下两个步骤

以Linux上推理为例在本目录执行如下命令即可完成编译测试支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>=1.0.4)

mkdir build
cd build
# 下载FastDeploy预编译库用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
make -j


# 下载模型,图片和字典文件
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
tar -xvf ch_PP-OCRv3_det_infer.tar

wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar

wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
tar -xvf ch_PP-OCRv3_rec_infer.tar

wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg

wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_keys_v1.txt

# CPU推理
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 0
# GPU推理
./infer_demo ./ch_PP-OCRv3_det_infer ./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer ./ppocr_keys_v1.txt ./12.jpg 1

以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考:

如果用户使用华为昇腾NPU部署, 请参考以下方式在部署前初始化部署环境:

运行完成可视化结果如下图所示

PPOCRv3 C API接口

配置

FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper()

创建一个RuntimeOption的配置对象并且返回操作它的指针。

返回

  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
void FD_C_RuntimeOptionWrapperUseCpu(
     FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper)

开启CPU推理

参数

  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
void FD_C_RuntimeOptionWrapperUseGpu(
    FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
    int gpu_id)

开启GPU推理

参数

  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针
  • gpu_id(int): 显卡号

模型

FD_C_DBDetectorWrapper* FD_C_CreateDBDetectorWrapper(
    const char* model_file, const char* params_file,
    FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
    const FD_C_ModelFormat model_format
)

创建一个DBDetector的模型并且返回操作它的指针。

参数

  • model_file(const char*): 模型文件路径
  • params_file(const char*): 参数文件路径
  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针表示后端推理配置
  • model_format(FD_C_ModelFormat): 模型格式

返回

  • fd_c_dbdetector_wrapper(FD_C_DBDetectorWrapper*): 指向DBDetector模型对象的指针
FD_C_ClassifierWrapper* FD_C_CreateClassifierWrapper(
    const char* model_file, const char* params_file,
    FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
    const FD_C_ModelFormat model_format
)

创建一个Classifier的模型并且返回操作它的指针。

参数

  • model_file(const char*): 模型文件路径
  • params_file(const char*): 参数文件路径
  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针表示后端推理配置
  • model_format(FD_C_ModelFormat): 模型格式

返回

  • fd_c_classifier_wrapper(FD_C_ClassifierWrapper*): 指向Classifier模型对象的指针
FD_C_RecognizerWrapper* FD_C_CreateRecognizerWrapper(
    const char* model_file, const char* params_file, const char* label_path,
    FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
    const FD_C_ModelFormat model_format
)

创建一个Recognizer的模型并且返回操作它的指针。

参数

  • model_file(const char*): 模型文件路径
  • params_file(const char*): 参数文件路径
  • label_path(const char*): 标签文件路径
  • fd_c_runtime_option_wrapper(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针表示后端推理配置
  • model_format(FD_C_ModelFormat): 模型格式

返回

  • fd_c_recognizer_wrapper(FD_C_RecognizerWrapper*): 指向Recognizer模型对象的指针
FD_C_PPOCRv3Wrapper* FD_C_CreatePPOCRv3Wrapper(
    FD_C_DBDetectorWrapper* det_model,
    FD_C_ClassifierWrapper* cls_model,
    FD_C_RecognizerWrapper* rec_model
)

创建一个PPOCRv3的模型并且返回操作它的指针。

参数

  • det_model(FD_C_DBDetectorWrapper*): DBDetector模型
  • cls_model(FD_C_ClassifierWrapper*): Classifier模型
  • rec_model(FD_C_RecognizerWrapper*): Recognizer模型

返回

  • fd_c_ppocrv3_wrapper(FD_C_PPOCRv3Wrapper*): 指向PPOCRv3模型对象的指针

读写图像

FD_C_Mat FD_C_Imread(const char* imgpath)

读取一个图像并且返回cv::Mat的指针。

参数

  • imgpath(const char*): 图像文件路径

返回

  • imgmat(FD_C_Mat): 指向图像数据cv::Mat的指针。
FD_C_Bool FD_C_Imwrite(const char* savepath,  FD_C_Mat img);

将图像写入文件中。

参数

  • savepath(const char*): 保存图像的路径
  • img(FD_C_Mat): 指向图像数据的指针

返回

  • result(FD_C_Bool): 表示操作是否成功

Predict函数

FD_C_Bool FD_C_PPOCRv3WrapperPredict(
    FD_C_PPOCRv3Wrapper* fd_c_ppocrv3_wrapper,
    FD_C_Mat img,
    FD_C_OCRResult* result)

模型预测接口,输入图像直接并生成结果。

参数

  • fd_c_ppocrv3_wrapper(FD_C_PPOCRv3Wrapper*): 指向PPOCRv3模型的指针
  • imgFD_C_Mat: 输入图像的指针指向cv::Mat对象可以调用FD_C_Imread读取图像获取
  • result(FD_C_OCRResult*): OCR预测结果,包括由检测模型输出的检测框位置,分类模型输出的方向分类,以及识别模型输出的识别结果, OCRResult说明参考视觉模型预测结果

Predict结果

FD_C_Mat FD_C_VisOcr(FD_C_Mat im, FD_C_OCRResult* ocr_result)

对结果进行可视化,返回可视化的图像。

参数

  • im(FD_C_Mat): 指向输入图像的指针
  • ocr_result(FD_C_OCRResult*): 指向 FD_C_OCRResult结构的指针

返回

  • vis_im(FD_C_Mat): 指向可视化图像的指针

其它文档