From 40db962641ded7f125a0baecddf193968cf6656c Mon Sep 17 00:00:00 2001 From: sghoshcvc Date: Thu, 22 Jun 2017 19:19:43 +0200 Subject: [PATCH] Add sample script --- modules/text/samples/textbox_demo.cpp | 146 ++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 modules/text/samples/textbox_demo.cpp diff --git a/modules/text/samples/textbox_demo.cpp b/modules/text/samples/textbox_demo.cpp new file mode 100644 index 000000000..a41558935 --- /dev/null +++ b/modules/text/samples/textbox_demo.cpp @@ -0,0 +1,146 @@ +/* + * dictnet_demo.cpp + * + * Demonstrates simple use of the holistic word classifier in C++ + * + * Created on: June 26, 2016 + * Author: Anguelos Nicolaou + */ + +#include "opencv2/text.hpp" +#include "opencv2/highgui.hpp" +#include "opencv2/imgproc.hpp" + +#include +#include +#include +#include +#include + +inline std::string getHelpStr(std::string progFname){ + std::stringstream out; + out << " Demo of text detection CNN for text detection." << std::endl; + out << " Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015"< " << std::endl; + out << " Caffe Model files (textbox.caffemodel, textbox_deploy.prototxt)"< &groups,std::vector &probs,std::vector wordList,float thres=0.6) +{ + for (int i=0;i<(int)groups.size(); i++) + { + if(probs[i]>thres) + { + if (src.type() == CV_8UC3) + { + cv::rectangle(src,groups.at(i).tl(),groups.at(i).br(),cv::Scalar( 0, 255, 255 ), 3, 8 ); + cv::putText(src, wordList[i],groups.at(i).tl() , cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar( 0,0,255 )); + } + else + rectangle(src,groups.at(i).tl(),groups.at(i).br(),cv::Scalar( 255 ), 3, 8 ); + } + } +} + + +int main(int argc, const char * argv[]){ + if(!cv::text::cnn_config::caffe_backend::getCaffeAvailable()){ + std::cout<<"The text module was compiled without Caffe which is the only available DeepCNN backend.\nAborting!\n"; + exit(1); + } + //set to true if you have a GPU with more than 3GB + cv::text::cnn_config::caffe_backend::setCaffeGpuMode(false); + + if (argc < 3){ + std::cout< textSpotter=cv::text::textDetector::create( + "textbox_deploy.prototxt","textbox.caffemodel"); + + //cv::Ptr wordSpotter= + // cv::text::textDetector::create(cnn); + std::cout<<"Created Text Spotter with text Boxes"; + + std::vector bbox; + std::vector outProbabillities; + textSpotter->textDetectInImage(image,bbox,outProbabillities); + // textbox_draw(image, bbox,outProbabillities); + float thres =0.6; + std::vector imageList; + for(int imageIdx=0;imageIdx<(int)bbox.size();imageIdx++){ + if(outProbabillities[imageIdx]>thres){ + imageList.push_back(image(bbox.at(imageIdx))); + } + + } + // call dict net here for all detected parts + cv::Ptr cnn=cv::text::DeepCNN::createDictNet( + "dictnet_vgg_deploy.prototxt","dictnet_vgg.caffemodel"); + + cv::Ptr wordSpotter= + cv::text::OCRHolisticWordRecognizer::create(cnn,"dictnet_vgg_labels.txt"); + + std::vector wordList; + std::vector wordProbabillities; + wordSpotter->recogniseImageBatch(imageList,wordList,wordProbabillities); + // write the output in file + std::ofstream out; + out.open(argv[1]); + + + for (int i=0;i<(int)wordList.size(); i++) + { + cv::Point tl_ = bbox.at(i).tl(); + cv::Point br_ = bbox.at(i).br(); + + out<