1. OCR文字識別簡介
文字識別也是圖像領域一個常見問題。然而,對于自然場景圖像,首先要定位圖像中的文字位置,然后才能進行文字的識別。所以一般包含兩個步驟:
文字檢測:解決的問題是哪里有文字,文字的范圍有多少。
文字識別:對定位好的文字區域進行識別,主要解決的問題是每個文字是什么,將圖像中的文字區域進轉化為字符信息。
我們的OCR算法是基于CTPN+CRNN設計的。CTPN是一種文字檢測算法,能有效的檢測出復雜場景的橫向分布的文字,是目前比較好的文字檢測算法。CRNN算法主要用于端到端地對不定長的文本序列進行識別,不用先對單個文字進行切割,而是將文本識別轉化為時序依賴的序列學習問題,就是基于圖像的序列識別。
基于EASY-EAI-Nano-TB硬件主板的運行效率:

2. 快速上手
2.1 開發環境準備
如果您初次閱讀此文檔,請閱讀《入門指南/開發環境準備/Easy-Eai編譯環境準備與更新》,并按照其相關的操作,進行編譯環境的部署。
在PC端Ubuntu系統中執行run腳本,進入EASY-EAI編譯環境,具體如下所示。
cd ~/develop_environment
./run.sh
2.2 源碼下載
在EASY-EAI編譯環境下創建存放源碼倉庫的管理目錄:
cd /opt
mkdir EASY-EAI-Toolkit
cd EASY-EAI-Toolkit通過git工具,在管理目錄內克隆遠程倉庫
git clone https://github.com/EASY-EAI/EASY-EAI-Toolkit-1126B.git
注:
* 此處可能會因網絡原因造成卡頓,請耐心等待。
* 如果實在要在gitHub網頁上下載,也要把整個倉庫下載下來,不能單獨下載本實例對應的目錄。
2.3 模型部署
要完成算法Demo的執行,需要先下載OCR算法模型。
百度網盤鏈接為:https://pan.baidu.com/s/1imI86u1O9xH6T6V_0k2jxw?pwd=1234 (提取碼:1234 )。

同時需要把下載的OCR算法模型復制粘貼到Release/目錄:

2.4 例程編譯
進入到對應的例程目錄執行編譯操作,具體命令如下所示:
cd EASY-EAI-Toolkit-1126B/Demos/algorithm-ocr/
./build.sh cpres注:
* 由于依賴庫部署在板卡上,因此交叉編譯過程中必須保持/mnt掛載。
* 若build.sh腳本帶有cpres參數,則會把Release/目錄下的所有資源都拷貝到開發板上。

2.5 例程運行及效果
通過串口調試或ssh調試,進入板卡后臺,定位到例程部署的位置,如下所示:
cd /userdata/Demo/algorithm-ocr/
運行例程命令如下所示:
sudo ./test-ocr test.jpg
在EASY-EAI編譯環境可以取回測試圖片:
cp /mnt/userdata/Demo/algorithm-ocr/result.jpg .
結果圖片如下所示:

API的詳細說明,以及API的調用(本例程源碼),詳細信息見下方說明。
3. OCR文字識別API說明
3.1 引用方式
為方便客戶在本地工程中直接調用我們的EASY EAI api庫,此處列出工程中需要鏈接的庫以及頭文件等,方便用戶直接添加。

3.2 OCR檢測初始化函數
設置OCR檢測初始化函數原型如下所示。
int ocr_det_init(const char* model_path, rknn_app_context_t* app_ctx);具體介紹如下所示。

3.3 OCR檢測運行函數
設置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);具體介紹如下所示。

3.4 OCR檢測釋放函數
設置OCR檢測釋放原型如下所示。
int ocr_det_release(rknn_app_context_t* app_ctx);具體介紹如下所示。

3.5 OCR識別初始化函數
OCR識別初始化函數原型如下所示。
int ocr_rec_init(const char* model_path, rknn_app_context_t* app_ctx);具體介紹如下所示。

3.6 OCR識別運行函數
OCR識別運行函數原型如下所示。
int ocr_rec_run(rknn_app_context_t* app_ctx, cv::Mat input_image, ocr_rec_result* out_result);具體介紹如下所示。

3.7 OCR識別釋放函數
OCR識別釋放函數原型如下所示。
int ocr_rec_release(rknn_app_context_t* app_ctx);具體介紹如下所示。

4. OCR檢測算法例程
例程目錄為Demos/algorithm-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;
}
/* 參數初始化 */
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-rec.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=%d\n", ret);
}
gettimeofday(&end,NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %f\n",time_use/1000);
/* 截取文字信息和畫框 */
printf("DRAWING OBJECT\n");
for (int i = 0; i < results.count; i++)
{
printf("[%d]: [(%d, %d), (%d, %d), (%d, %d), (%d, %d)] %f\n", 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=%f\n", rec_results.str, rec_results.score);
}
cv::imwrite("result.jpg", input_image);
return 0;
} -
Linux
+關注
關注
88文章
11758瀏覽量
219005 -
瑞芯微
+關注
關注
27文章
792瀏覽量
54278 -
EASY-EAI靈眸科技
+關注
關注
4文章
67瀏覽量
3611 -
RV1126B
+關注
關注
0文章
54瀏覽量
178
發布評論請先 登錄
瑞芯微(EASY EAI)RV1126B 音頻輸入
瑞芯微(EASY EAI)RV1126B PWM使用
【EASY EAI Nano-TB(RV1126B)開發板試用】+初識篇
【EASY EAI Nano-TB(RV1126B)開發板試用】命令行功能測試-shell腳本進行IO控制-燈閃
【EASY EAI Nano-TB(RV1126B)開發板試用】命令行功能測試-shell腳本進行IO控制-紅綠燈項目
【EASY EAI Nano-TB(RV1126B)開發板試用】命令行功能測試-shell腳本進行IO控制-紅綠燈按鈕項目
請問各位大佬,如何解決,瑞芯微 RV1126B 使用 mpp 自帶工具 調試時,內核直接報錯崩潰!
【EASY EAI Nano-TB(RV1126B)開發板試用】+1、開箱上電
【EASY EAI Nano-TB(RV1126B)開發板試用】介紹、系統安裝
RV1126系列選型指南:從RV1126到RV1126B,一文看懂升級差異
【免費試用】EASY EAI Nano-TB(RV1126B)開發套件評測
替代升級實錘!實測RV1126B,CPU性能吊打RV1126
瑞芯微(EASY EAI)RV1126B 人體關鍵點識別
瑞芯微(EASY EAI)RV1126B OCR文字識別
評論