diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-28 14:38:55 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-28 14:48:23 +0530 |
| commit | 397c0a613e66487fead7afb4b89b783d887389f3 (patch) | |
| tree | e0c24e6de5964a845f3704916ef64f8869fd81ac | |
| parent | 9a5453aa190834b01e78cd971c445f1f0e34ee41 (diff) | |
| download | chelleport-397c0a613e66487fead7afb4b89b783d887389f3.tar.gz chelleport-397c0a613e66487fead7afb4b89b783d887389f3.zip | |
Create a sorted list of ocr matches by position on screen
Diffstat (limited to '')
| -rw-r--r-- | cpp/libchelleport.cpp | 17 | ||||
| -rw-r--r-- | include/libchelleport.h | 14 |
2 files changed, 20 insertions, 11 deletions
diff --git a/cpp/libchelleport.cpp b/cpp/libchelleport.cpp index 20c2d4f..5845bc4 100644 --- a/cpp/libchelleport.cpp +++ b/cpp/libchelleport.cpp @@ -10,7 +10,7 @@ #include "../include/recognizer.h" extern "C" OCRMatch *findWordCoordinates(const char *image_path, int *size) { - std::vector<OCRMatch> matches; + OCRMatchSet matches; MEASURE("OCR", { matches = extractTextMatches(image_path); }); std::cout << "Match count: " << matches.size() << std::endl; @@ -22,12 +22,11 @@ extern "C" OCRMatch *findWordCoordinates(const char *image_path, int *size) { return ptr; } -std::vector<OCRMatch> extractTextMatches(const char *imagePath) { - std::vector<OCRMatch> results; - +OCRMatchSet extractTextMatches(const char *imagePath) { Pix *image = image::loadImage(imagePath); - if (image == nullptr) - return results; + if (image == nullptr) { + return OCRMatchSet(); + } // printf("imagePath: %s\n", imagePath); // pixWrite(imagePath, image, IFF_JFIF_JPEG); @@ -51,10 +50,10 @@ std::vector<OCRMatch> extractTextMatches(const char *imagePath) { return runRecognizers(recognizers, image); } -std::vector<OCRMatch> +OCRMatchSet runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers, Pix *image) { - std::vector<OCRMatch> results; + OCRMatchSet results; std::shared_ptr<Pix> sharedImage(image, [](Pix *p) { pixDestroy(&p); }); std::vector<std::thread> workers; @@ -72,7 +71,7 @@ runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers, for (auto &ext : recognizers) { for (auto &match : ext->getResults()) - results.push_back(match); + results.insert(match); } return results; diff --git a/include/libchelleport.h b/include/libchelleport.h index c022975..d4e202f 100644 --- a/include/libchelleport.h +++ b/include/libchelleport.h @@ -19,8 +19,18 @@ extern "C" OCRMatch *findWordCoordinates(const char *image_path, /* returns */ int *size); -std::vector<OCRMatch> extractTextMatches(const char *imagePath); +struct ScreenPositionComparator { + bool operator()(const OCRMatch &a, const OCRMatch &b) const { + if (abs(a.startY - b.startY) < 5) + return a.startX < b.startX; + return a.startY < b.startY; + } +}; -std::vector<OCRMatch> +typedef std::set<OCRMatch, ScreenPositionComparator> OCRMatchSet; + +OCRMatchSet extractTextMatches(const char *imagePath); + +OCRMatchSet runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers, Pix *image); |
