#include #include #include #include #include #include #include "../include/image.h" #include "../include/libchelleport.h" #include "../include/recognizer.h" extern "C" { OCRMatch *findWordCoordinates(const char *image_path, int *size) { std::vector matches; MEASURE("OCR", { matches = extractTextMatches(image_path); }); std::cout << "Match count: " << matches.size() << std::endl; static OCRMatch *ptr = new OCRMatch[matches.size()]; std::copy(matches.begin(), matches.end(), ptr); *size = matches.size(); return ptr; } } std::vector extractTextMatches(const char *imagePath) { std::vector results; Pix *image = image::loadImage(imagePath); if (image == nullptr) return results; // printf("imagePath: %s\n", imagePath); // pixWrite(imagePath, image, IFF_JFIF_JPEG); int width = pixGetWidth(image); int height = pixGetHeight(image); std::vector> recognizers; recognizers.push_back( std::make_unique(0, 0, width / 2, height / 2)); recognizers.push_back( std::make_unique(width / 2, 0, width / 2, height / 2)); recognizers.push_back( std::make_unique(0, height / 2, width / 2, height / 2)); recognizers.push_back(std::make_unique(width / 2, height / 2, width / 2, height / 2)); return runRecognizers(recognizers, image); } std::vector runRecognizers(std::vector> &recognizers, Pix *image) { std::vector results; std::shared_ptr sharedImage(image, [](Pix *p) { pixDestroy(&p); }); std::vector workers; workers.reserve(recognizers.size()); for (auto &ext : recognizers) { workers.push_back(std::thread( [&ext, &sharedImage]() { ext->recognize(sharedImage.get()); })); } for (std::thread &t : workers) { if (t.joinable()) t.join(); } for (auto &ext : recognizers) { for (auto &match : ext->getResults()) results.push_back(match); } return results; }