File size: 1,653 Bytes
6aa92fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <string>
#include <vector>

static std::string argVal(int argc, char** argv, const std::string& key, const std::string& def)
{
    for (int i = 1; i + 1 < argc; ++i)
        if (key == argv[i]) return argv[i + 1];
    return def;
}

int main(int argc, char** argv)
{
    std::string model  = argVal(argc, argv, "--model",  "east_text_detection_2026jul.onnx");
    std::string image  = argVal(argc, argv, "--image",  "example_outputs/input_image.png");
    std::string output = argVal(argc, argv, "--output", "example_outputs/output_image.png");

    cv::Mat img = cv::imread(image);
    if (img.empty())
    {
        std::cerr << "could not read image: " << image << std::endl;
        return 1;
    }

    cv::dnn::TextDetectionModel_EAST east(model);
    east.setConfidenceThreshold(0.5f).setNMSThreshold(0.4f);
    east.setInputParams(1.0, cv::Size(320, 320), cv::Scalar(123.68, 116.78, 103.94), true, false);

    std::vector<cv::RotatedRect> boxes;
    east.detectTextRectangles(img, boxes);
    std::cout << "detections " << boxes.size() << std::endl;

    cv::Mat out = img.clone();
    for (const cv::RotatedRect& box : boxes)
    {
        cv::Mat pts;
        cv::boxPoints(box, pts);
        std::vector<cv::Point> poly(4);
        for (int i = 0; i < 4; ++i)
            poly[i] = cv::Point(cvRound(pts.at<float>(i, 0)), cvRound(pts.at<float>(i, 1)));
        cv::polylines(out, poly, true, cv::Scalar(0, 255, 0), 2);
    }
    cv::imwrite(output, out);
    std::cout << "wrote " << output << std::endl;
    return 0;
}