OpenCV 文字檢測與識別模塊
該模塊在擴展模塊中,需自行下載
下載地址:https://github.com/opencv/opencv_contrib/tree/4.0.0
說明文檔:
文字檢測 https://docs.opencv.org/4.0.0/da/d56/group__text__detect.html
文字識別 https://docs.opencv.org/4.0.0/d8/df2/group__text__recognize.html
參考文章:https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/text_recognition_cnn.cpp
https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp
文字檢測
1.textDetectorCNN
OpenCV的文字檢測模塊textDetectorCNN中使用了TextBoxes:具有單個深度神經網絡的快速文本檢測器 鏈接地址爲: https://github.com/MhLiao/TextBoxes
其中已經訓練過的文件:
函數名 | 內容 | 地址 |
---|---|---|
modelWeightsFilename | 描述分類器體系結構的prototxt文件的相對或絕對路徑。 | textbox.prototxt 在下載的擴展模塊源碼中opencv_contrib/modules/text/samples/textbox.prototxt |
modelWeightsFilename | 包含caffe-binary形式的模型的預訓練權重的文件的相對或絕對路徑。 | TextBoxes_icdar13.caffemodel http://pan.baidu.com/s/1qY73XHq |
cv::Mat temp;
src.convertTo(temp, CV_8UC3, 1);//src 輸入圖像
cv::imshow("src", temp);
dst1 = temp.clone();
cv::Ptr<cv::text::TextDetectorCNN> detector= cv::text::TextDetectorCNN::create("textbox.prototxt", "TextBoxes_icdar13.caffemodel");
std::vector < cv::Rect > boxes;//識別區域
std::vector < float > sources;//評估分數
detector->detect(temp, boxes, sources);
float threshold = 0.5;
for (int i = 0; i < boxes.size(); i++)
{
if (sources[i] > threshold)
{
cv::Rect rect = boxes[i];
cv::rectangle(dst1, rect, cv::Scalar(255, 0, 0), 2);
}
}
cv::imshow("Text detection result", dst1);
原圖
結果
2.erGrouping
文字識別
1.OCRHolisticWordRecognizer
OCRHolisticWordRecognizer類提供了分段詞語的功能。給定預定義的詞彙表,使用DictNet來選擇給定輸入圖像的最可能的詞。
DictNet詳細描述於:Max Jaderberg等:使用卷積神經網絡閱讀野外文本,IJCV 2015 http://arxiv.org/abs/1412.1842
模型文件下載地址: http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg.caffemodel
http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_deploy.prototxt
http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_labels.txt
wordSpotter = (cv::text::OCRHolisticWordRecognizer::create("dictnet_vgg_deploy.prototxt", "dictnet_vgg.caffemodel", "dictnet_vgg_labels.txt"));
dst1 = src.clone();
for (size_t i = 0; i < textBoxes.size(); i++)
{
cv::Mat wordImg;
cv::cvtColor(src(textBoxes[i]), wordImg, cv::COLOR_BGR2GRAY);
std::string word;
std::vector<float> confs;
wordSpotter->run(wordImg, word, NULL, NULL, &confs);//檢測
cv::Rect currrentBox = textBoxes[i];
cv::rectangle(dst1, currrentBox, cv::Scalar(0, 255, 255), 2, cv::LINE_AA);
int baseLine = 0;
cv::Size labelSize = cv::getTextSize(word, cv::FONT_HERSHEY_PLAIN, 1, 1, &baseLine);
int yLeftBottom = currrentBox.y>labelSize.height? currrentBox.y: labelSize.height;
cv::rectangle(dst1, cv::Point(currrentBox.x, yLeftBottom - labelSize.height),
cv::Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), cv::Scalar(255, 255, 255), cv::FILLED);
cv::putText(dst1, word, cv::Point(currrentBox.x, yLeftBottom), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, cv::LINE_AA);
}
cv::imshow("Text recognition", dst1);
結果