mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 04:25:42 +08:00
text: improve DL-based samples
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <opencv2/text.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/dnn.hpp>
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
@@ -27,22 +28,27 @@ bool fileExists (const std::string& filename)
|
||||
return f.good();
|
||||
}
|
||||
|
||||
void textbox_draw(Mat src, std::vector<Rect>& groups, std::vector<float>& probs, float thres)
|
||||
void textbox_draw(Mat src, std::vector<Rect>& groups, std::vector<float>& probs, std::vector<int>& indexes)
|
||||
{
|
||||
for (size_t i = 0; i < groups.size(); i++)
|
||||
for (size_t i = 0; i < indexes.size(); i++)
|
||||
{
|
||||
if(probs[i] > thres)
|
||||
if (src.type() == CV_8UC3)
|
||||
{
|
||||
if (src.type() == CV_8UC3)
|
||||
{
|
||||
rectangle(src, groups[i], Scalar( 0, 255, 255 ), 2, LINE_AA);
|
||||
String label = format("%.2f", probs[i]);
|
||||
std::cout << "text box: " << groups[i] << " confidence: " << probs[i] << "\n";
|
||||
putText(src, label, groups.at(i).tl(), FONT_HERSHEY_PLAIN, 1, Scalar( 0,0,255 ), 1, LINE_AA);
|
||||
}
|
||||
else
|
||||
rectangle(src, groups[i], Scalar( 255 ), 3, 8 );
|
||||
Rect currrentBox = groups[indexes[i]];
|
||||
rectangle(src, currrentBox, Scalar( 0, 255, 255 ), 2, LINE_AA);
|
||||
String label = format("%.2f", probs[indexes[i]]);
|
||||
std::cout << "text box: " << currrentBox << " confidence: " << probs[indexes[i]] << "\n";
|
||||
|
||||
int baseLine = 0;
|
||||
Size labelSize = getTextSize(label, FONT_HERSHEY_PLAIN, 1, 1, &baseLine);
|
||||
int yLeftBottom = std::max(currrentBox.y, labelSize.height);
|
||||
rectangle(src, Point(currrentBox.x, yLeftBottom - labelSize.height),
|
||||
Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), Scalar( 255, 255, 255 ), FILLED);
|
||||
|
||||
putText(src, label, Point(currrentBox.x, yLeftBottom), FONT_HERSHEY_PLAIN, 1, Scalar( 0,0,0 ), 1, LINE_AA);
|
||||
}
|
||||
else
|
||||
rectangle(src, groups[i], Scalar( 255 ), 3, 8 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +68,7 @@ int main(int argc, const char * argv[])
|
||||
|
||||
if (!fileExists(modelArch) || !fileExists(moddelWeights))
|
||||
{
|
||||
std::cout<<getHelpStr(argv[0]);
|
||||
std::cout << getHelpStr(argv[0]);
|
||||
std::cout << "Model files not found in the current directory. Aborting!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
@@ -71,13 +77,16 @@ int main(int argc, const char * argv[])
|
||||
|
||||
std::cout << "Starting Text Box Demo" << std::endl;
|
||||
Ptr<text::TextDetectorCNN> textSpotter =
|
||||
text::TextDetectorCNN::create(modelArch, moddelWeights, false);
|
||||
text::TextDetectorCNN::create(modelArch, moddelWeights);
|
||||
|
||||
std::vector<Rect> bbox;
|
||||
std::vector<float> outProbabillities;
|
||||
textSpotter->detect(image, bbox, outProbabillities);
|
||||
|
||||
textbox_draw(image, bbox, outProbabillities, 0.5f);
|
||||
std::vector<int> indexes;
|
||||
cv::dnn::NMSBoxes(bbox, outProbabillities, 0.3f, 0.4f, indexes);
|
||||
|
||||
textbox_draw(image, bbox, outProbabillities, indexes);
|
||||
|
||||
imshow("TextBox Demo",image);
|
||||
std::cout << "Done!" << std::endl << std::endl;
|
||||
|
Reference in New Issue
Block a user