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 的頭文件。請確保已經正確配置了這些依賴項。