PaddleOCR 是一个基于 PaddlePaddle 的开源 OCR(光学字符识别)工具
pip install paddlepaddle
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_train.tar
tar -xf ch_ppocr_mobile_v2.0_det_train.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_train.tar
tar -xf ch_ppocr_mobile_v2.0_rec_train.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_train.tar
tar -xf ch_ppocr_mobile_v2.0_cls_train.tar
#include<iostream>
#include <opencv2/opencv.hpp>
#include "PaddleOCR/ocr.h"
int main() {
// 初始化 OCR 引擎
std::string det_model_dir = "ch_ppocr_mobile_v2.0_det_train";
std::string rec_model_dir = "ch_ppocr_mobile_v2.0_rec_train";
std::string cls_model_dir = "ch_ppocr_mobile_v2.0_cls_train";
bool use_gpu = false;
int gpu_id = 0;
int gpu_mem = 4000;
int cpu_threads = 10;
bool enable_mkldnn = true;
bool use_tensorrt = false;
bool use_fp16 = false;
paddleocr::OCR ocr(det_model_dir, rec_model_dir, cls_model_dir, use_gpu, gpu_id, gpu_mem, cpu_threads, enable_mkldnn, use_tensorrt, use_fp16);
// 读取图像
cv::Mat img = cv::imread("test.jpg");
// 执行 OCR
std::vector<paddleocr::OCRResult> results = ocr.Run(img);
// 输出结果
for (const auto& result : results) {
std::cout << "Text: "<< result.text<< std::endl;
std::cout << "Score: "<< result.score<< std::endl;
std::cout << "Rect: ("<< result.rect[0] << ", "<< result.rect[1] << ", "<< result.rect[2] << ", "<< result.rect[3] << ")"<< std::endl;
}
return 0;
}
注意:这个示例需要 OpenCV 和 PaddleOCR 的头文件。请确保已经正确配置了这些依赖项。