Download east_text_detection/demo.cpp from opencv/opencv_contribution: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/opencv/opencv_contribution/resolve/refs%2Fpr%2F13/east_text_detection/demo.cpp
- Command line
-
hf download hf://opencv/opencv_contribution@refs/pr/13/east_text_detection/demo.cpp
-
curl -L -o demo.cpp https://huggingface.co/opencv/opencv_contribution/resolve/refs%2Fpr%2F13/east_text_detection/demo.cpp
1.65 kB
| 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; | |
| } | |