1. OCR文字識別簡介
文字識別也是圖像領(lǐng)域一個常見問題。然而,對于自然場景圖像,首先要定位圖像中的文字位置,然后才能進(jìn)行文字的識別。所以一般包含兩個步驟:
文字檢測:解決的問題是哪里有文字,文字的范圍有多少。
文字識別:對定位好的文字區(qū)域進(jìn)行識別,主要解決的問題是每個文字是什么,將圖像中的文字區(qū)域進(jìn)轉(zhuǎn)化為字符信息。
我們的OCR算法是基于CTPN+CRNN設(shè)計的。CTPN是一種文字檢測算法,能有效的檢測出復(fù)雜場景的橫向分布的文字,是目前比較好的文字檢測算法。CRNN算法主要用于端到端地對不定長的文本序列進(jìn)行識別,不用先對單個文字進(jìn)行切割,而是將文本識別轉(zhuǎn)化為時序依賴的序列學(xué)習(xí)問題,就是基于圖像的序列識別。
基于EASY-EAI-Nano硬件主板的運行效率:
算法種類 | 模型大小 | 運行效率 |
文字檢測算法 | 2.79MB | 227ms |
文字識別算法 | 4.56MB | 89ms |
2. 快速上手
2.1 開發(fā)環(huán)境準(zhǔn)備
如果您初次閱讀此文檔,請閱讀《入門指南/開發(fā)環(huán)境準(zhǔn)備/Easy-Eai編譯環(huán)境準(zhǔn)備與更新》,并按照其相關(guān)的操作,進(jìn)行編譯環(huán)境的部署。
在PC端Ubuntu系統(tǒng)中執(zhí)行run腳本,進(jìn)入EASY-EAI編譯環(huán)境,具體如下所示。
cd ~/develop_environment ./run.sh
2.2 源碼下載以及例程編譯
在EASY-EAI編譯環(huán)境下創(chuàng)建存放源碼倉庫的管理目錄:
cd /opt mkdir EASY-EAI-Toolkit cd EASY-EAI-Toolkit
通過git工具,在管理目錄內(nèi)克隆遠(yuǎn)程倉庫
git clone https://github.com/EASY-EAI/EASY-EAI-Toolkit-C-Demo.git
注:
* 此處可能會因網(wǎng)絡(luò)原因造成卡頓,請耐心等待。
* 如果實在要在gitHub網(wǎng)頁上下載,也要把整個倉庫下載下來,不能單獨下載本實例對應(yīng)的目錄。
進(jìn)入到對應(yīng)的例程目錄執(zhí)行編譯操作,具體命令如下所示:
cd EASY-EAI-Toolkit-C-Demo/algorithm-ocr/ ./build.sh
注:
* 若build.sh腳本帶有cpres參數(shù),則會把Release/目錄下的所有資源都拷貝到開發(fā)板上。
* 若build.sh腳本不帶任何參數(shù),則僅會拷貝demo編譯出來的可執(zhí)行文件。
* 由于依賴庫部署在板卡上,因此交叉編譯過程中必須保持adb連接。
2.3 模型部署
要完成算法Demo的執(zhí)行,需要先下載算法模型。
百度網(wǎng)盤鏈接為:https://pan.baidu.com/s/1w4tf1YolUCpcAFtyP3aJZQ (提取碼:6666 )。
同時需要把下載的OCR識別算法模型復(fù)制粘貼到Release/目錄:
再通過下方命令將模型署到板卡中,如下所示。
cp Release/ /mnt/userdata/ -rf
2.4 例程運行
通過按鍵Ctrl+Shift+T創(chuàng)建一個新窗口,執(zhí)行adb shell命令,進(jìn)入板卡運行環(huán)境。
adb shell
進(jìn)入板卡后,定位到例程上傳的位置,如下所示:
cd /userdata/Release/
運行例程命令如下所示:
./test-ocr test.jpg
2.5 運行效果
test-ocr的Demo執(zhí)行效果如下所示:
再開一個窗口,在PC端Ubuntu環(huán)境通過以下命令可以把圖片拉回來:
adb pull /userdata/Demo/result.jpg .
結(jié)果圖片如下所示:
API的詳細(xì)說明,以及API的調(diào)用(本例程源碼),詳細(xì)信息見下方說明。
3. OCR文字識別API說明
3.1 引用方式
為方便客戶在本地工程中直接調(diào)用我們的EASY EAI api庫,此處列出工程中需要鏈接的庫以及頭文件等,方便用戶直接添加。
選項 | 描述 |
頭文件目錄 | easyeai-api/algorithm_api/ocr |
庫文件目錄 | easyeai-api/algorithm_api/ocr |
庫鏈接參數(shù) | -lpthread -locr -lrknn_api |
3.2 OCR檢測初始化函數(shù)
設(shè)置OCR檢測初始化函數(shù)原型如下所示。
int ocr_det_init(const char* model_path, rknn_app_context_t* app_ctx);
具體介紹如下所示。
函數(shù)名: ocr_det_init | |
頭文件 | ocr.h |
輸入?yún)?shù) |
model_path:算法模型名字/路徑 |
app_ctx:算法模型句柄 | |
返回值 | 成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
3.3 OCR檢測運行函數(shù)
設(shè)置OCR檢測運行原型如下所示。
int ocr_det_run(rknn_app_context_t* app_ctx, cv::Mat input_image, ocr_det_postprocess_params* params, ocr_det_result* out_result);
具體介紹如下所示。
函數(shù)名: ocr_det_run | |
頭文件 | ocr.h |
輸入?yún)?shù) |
app_ctx:算法模型句柄 |
input_image:Cv::Mat輸入圖像 | |
Params:ocr檢測算法參數(shù) | |
out_result:返回結(jié)果 | |
返回值 |
成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
3.4 OCR檢測釋放函數(shù)
設(shè)置OCR檢測釋放原型如下所示。
int ocr_det_release(rknn_app_context_t* app_ctx);
具體介紹如下所示。
函數(shù)名: ocr_det_release | |
頭文件 | ocr.h |
輸入?yún)?shù) | app_ctx:算法模型句柄 |
返回值 | 成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
3.5 OCR識別初始化函數(shù)
OCR識別初始化函數(shù)原型如下所示。
int ocr_rec_init(const char* model_path, rknn_app_context_t* app_ctx);
具體介紹如下所示。
函數(shù)名: ocr_rec_init | |
頭文件 | ocr.h |
輸入?yún)?shù) |
model_path:算法模型名字/路徑 |
app_ctx:算法模型句柄 | |
返回值 |
成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
3.6 OCR識別運行函數(shù)
OCR識別運行函數(shù)原型如下所示。
int ocr_rec_run(rknn_app_context_t* app_ctx, cv::Mat input_image, ocr_rec_result* out_result);
具體介紹如下所示。
函數(shù)名: ocr_rec_run | |
頭文件 | ocr.h |
輸入?yún)?shù) |
app_ctx:算法模型句柄 |
input_image:輸入圖像 | |
out_result:返回結(jié)果 | |
返回值 |
成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
3.7 OCR識別釋放函數(shù)
OCR識別釋放函數(shù)原型如下所示。
int ocr_rec_release(rknn_app_context_t* app_ctx);
具體介紹如下所示。
函數(shù)名: ocr_rec_release | |
頭文件 | ocr.h |
輸入?yún)?shù) | app_ctx:算法模型句柄 |
返回值 | 成功返回:0 |
失敗返回:-1 | |
注意事項 | 無 |
4. OCR識別算法例程
例程目錄為Toolkit-C-Demo/ocr/test-ocr.cpp,操作流程如下。
參考例程如下所示。
#include #include #include #include"ocr.h" using namespace cv; using namespace std; #define INDENT " " #define THRESHOLD 0.3 // pixel score threshold #define BOX_THRESHOLD 0.9 // box score threshold #define USE_DILATION false // whether to do dilation, true or false #define DB_UNCLIP_RATIO 1.5 // unclip ratio for poly type int main(int argc, char **argv) { if (argc != 2) { printf("%s n", argv[0]); return -1; } /* 參數(shù)初始化 */ const char *img_path = argv[1]; Mat input_image, rgb_img; input_image = imread(img_path); if (input_image.empty()) { cout < "Error: Could not load image" < endl; return -1; } cv::cvtColor(input_image, rgb_img, COLOR_BGR2RGB); rknn_app_context_t ocr_det_ctx, ocr_rec_ctx; memset(&ocr_det_ctx, 0, sizeof(rknn_app_context_t)); memset(&ocr_rec_ctx, 0, sizeof(rknn_app_context_t)); /* OCR算法檢測模型&識別模型初始化 */ ocr_det_init("ocr_det.model", &ocr_det_ctx); ocr_rec_init("ocr_ret.model", &ocr_rec_ctx); struct timeval start; struct timeval end; float time_use=0; /* OCR算法檢測模型運行 */ ocr_det_result results; ocr_det_postprocess_params params; params.threshold = THRESHOLD; params.box_threshold = BOX_THRESHOLD; params.use_dilate = USE_DILATION; params.db_score_mode = (char*)"slow"; params.db_box_type = (char*)"poly"; params.db_unclip_ratio = DB_UNCLIP_RATIO; gettimeofday(&start,NULL); int ret; ret = ocr_det_run(&ocr_det_ctx, rgb_img, ?ms, &results); if (ret != 0) { printf("inference_ppocr_rec_model fail! ret=%dn", ret); } gettimeofday(&end,NULL); time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒 printf("time_use is %fn",time_use/1000); /* 截取文字信息和畫框 */ printf("DRAWING OBJECTn"); for (int i = 0; i < results.count; i++) { printf("[%d]: [(%d, %d), (%d, %d), (%d, %d), (%d, %d)] %fn", i, results.box[i].left_top.x, results.box[i].left_top.y, results.box[i].right_top.x, results.box[i].right_top.y, results.box[i].right_bottom.x, results.box[i].right_bottom.y, results.box[i].left_bottom.x, results.box[i].left_bottom.y, results.box[i].score); line(input_image, Point(results.box[i].left_top.x, results.box[i].left_top.y), Point(results.box[i].right_top.x, results.box[i].right_top.y), Scalar(0, 255, 0), 1, LINE_AA); line(input_image, Point(results.box[i].right_top.x, results.box[i].right_top.y), Point(results.box[i].right_bottom.x, results.box[i].right_bottom.y), Scalar(0, 255, 0), 1, LINE_AA); line(input_image, Point(results.box[i].right_bottom.x, results.box[i].right_bottom.y), Point(results.box[i].left_bottom.x, results.box[i].left_bottom.y), Scalar(0, 255, 0), 1, LINE_AA); line(input_image, Point(results.box[i].left_bottom.x, results.box[i].left_bottom.y), Point(results.box[i].left_top.x, results.box[i].left_top.y), Scalar(0, 255, 0), 1, LINE_AA); cv::Mat rgb_crop_image = GetRotateCropImage(rgb_img, results.box[i]); /* OCR算法識別模型運行 */ ocr_rec_result rec_results; ocr_rec_run(&ocr_rec_ctx, rgb_crop_image, &rec_results); // print text result printf("regconize result: %s, score=%fn", rec_results.str, rec_results.score); } cv::imwrite("result.jpg", input_image); return 0; } 審核編輯 黃宇
-
算法
+關(guān)注
關(guān)注
23文章
4687瀏覽量
94445 -
文字識別
+關(guān)注
關(guān)注
0文章
18瀏覽量
8752 -
OCR
+關(guān)注
關(guān)注
0文章
156瀏覽量
16665 -
rv1126
+關(guān)注
關(guān)注
0文章
106瀏覽量
3288
發(fā)布評論請先 登錄
基于RV1126開發(fā)板實現(xiàn)人臉檢測方案

基于RV1126開發(fā)板實現(xiàn)二維碼識別方案

基于RV1126開發(fā)板實現(xiàn)人臉識別方案

基于RV1126開發(fā)板實現(xiàn)人臉識別方案

基于RV1126開發(fā)板實現(xiàn)人臉識別方案

評論