Skip to content

Commit ebfbfe9

Browse files
committed
init commit for ocr behold
1 parent 7f527ef commit ebfbfe9

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

src/beholdhelper.cpp

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <vector>
88

99
#include <opencv2/opencv.hpp>
10+
#include <tesseract/baseapi.h>
1011

1112
#include "beholdhelper.h"
1213
#include "networkhelper.h"
@@ -120,6 +121,22 @@ class Searcher
120121
return true;
121122
}
122123

124+
void setHeightMap(nlohmann::json map) {
125+
_heightMap = map;
126+
}
127+
128+
std::vector<nlohmann::json> lookupHeight(const char *name) {
129+
std::string comp = std::string(name);
130+
std::vector<nlohmann::json> results;
131+
for (auto &element : _heightMap) {
132+
std::string name = element["short_name"].get<std::string>();
133+
if (strcmp(name.c_str(), comp.c_str()) == 0) {
134+
results.push_back(element);
135+
}
136+
}
137+
return results;
138+
}
139+
123140
MatchResult Match(cv::Mat image)
124141
{
125142
cv::Mat features = _descriptor.Describe(image);
@@ -134,7 +151,7 @@ class Searcher
134151

135152
// group by image index
136153
std::map<int, int> occurences;
137-
for (const auto &match : matches) {
154+
for (const auto &match : matches) {
138155
//if (_symbols[match.imgIdx] != "behold_title") {
139156
occurences[match.imgIdx]++;
140157
//}
@@ -150,6 +167,7 @@ class Searcher
150167
private:
151168
Descriptor _descriptor;
152169
cv::Ptr<cv::DescriptorMatcher> _matcher;
170+
nlohmann::json _heightMap;
153171

154172
// Order here must match order of training in matcher (as it deals in indices
155173
// only)
@@ -230,6 +248,8 @@ class BeholdHelper : public IBeholdHelper
230248
private:
231249
int CountFullStars(cv::Mat refMat, cv::Mat tplMat, double threshold = 0.8) noexcept;
232250

251+
std::shared_ptr<tesseract::TessBaseAPI> _tesseract;
252+
233253
Trainer _trainer;
234254
Searcher _searcher;
235255
NetworkHelper _networkHelper;
@@ -281,6 +301,17 @@ bool BeholdHelper::ReInitialize(bool forceReTraining, const std::string &jsonpat
281301
_beholdTitle = cv::imread(fs::path(_dataPath + "behold_title.png").make_preferred().string());
282302

283303
_searcher.Clear();
304+
_tesseract = std::make_shared<tesseract::TessBaseAPI>();
305+
306+
if (_tesseract->Init(fs::path(_dataPath + "tessdata").make_preferred().string().c_str(), "Eurostile")) {
307+
// "Could not initialize tesseract"
308+
return false;
309+
}
310+
311+
//_tesseract->DefaultPageSegMode = PageSegMode.SingleWord;
312+
313+
// _tesseract->SetVariable("tessedit_char_whitelist", "0123456789");
314+
// _tesseract->SetVariable("classify_bln_numeric_mode", "1");
284315

285316
if (!_trainer.TrainInternal(_beholdTitle, "behold_title", forceReTraining))
286317
return false;
@@ -289,6 +320,8 @@ bool BeholdHelper::ReInitialize(bool forceReTraining, const std::string &jsonpat
289320
_searcher.Add(tr, "behold_title");
290321

291322
std::ifstream assetStream(fs::path(jsonpath + "crew.json").make_preferred().string());
323+
std::ifstream heightStream(fs::path(_dataPath + "height_info.json").make_preferred().string());
324+
292325
nlohmann::json j;
293326
assetStream >> j;
294327

@@ -306,6 +339,10 @@ bool BeholdHelper::ReInitialize(bool forceReTraining, const std::string &jsonpat
306339
}
307340
}
308341

342+
nlohmann::json jh;
343+
heightStream >> jh;
344+
_searcher.setHeightMap(jh);
345+
309346
std::ifstream assetStream2(fs::path(jsonpath + "ship_schematics.json").make_preferred().string());
310347
nlohmann::json j2;
311348
assetStream2 >> j2;
@@ -432,6 +469,51 @@ SearchResults BeholdHelper::AnalyzeBehold(cv::Mat query, size_t fileSize)
432469
// cv::imwrite("name1.png", name1);
433470

434471
// TODO: OCR
472+
_tesseract->SetImage((uchar *)name1.data, name1.size().width, name1.size().height, name1.channels(),
473+
(int)name1.step1());
474+
_tesseract->SetSourceResolution(70);
475+
_tesseract->Recognize(0);
476+
const char *outName1 = _tesseract->GetUTF8Text();
477+
478+
_tesseract->SetImage((uchar *)name2.data, name2.size().width, name2.size().height, name2.channels(),
479+
(int)name2.step1());
480+
_tesseract->SetSourceResolution(70);
481+
_tesseract->Recognize(0);
482+
const char *outName2 = _tesseract->GetUTF8Text();
483+
484+
_tesseract->SetImage((uchar *)name3.data, name3.size().width, name3.size().height, name3.channels(),
485+
(int)name3.step1());
486+
_tesseract->SetSourceResolution(70);
487+
_tesseract->Recognize(0);
488+
const char *outName3 = _tesseract->GetUTF8Text();
489+
490+
try {
491+
auto nameInfo1 = _searcher.lookupHeight(outName1);
492+
auto nameInfo2 = _searcher.lookupHeight(outName2);
493+
auto nameInfo3 = _searcher.lookupHeight(outName3);
494+
std::cout << "OCR Results:";
495+
for (auto &elem : nameInfo1) {
496+
std::cout << elem["name"];
497+
if (elem["low"] == true) {
498+
std::cout << "Is Short";
499+
}
500+
}
501+
for (auto &elem : nameInfo2) {
502+
std::cout << elem["name"];
503+
if (elem["low"] == true) {
504+
std::cout << "Is Short";
505+
}
506+
}
507+
for (auto &elem : nameInfo3) {
508+
std::cout << elem["name"];
509+
if (elem["low"] == true) {
510+
std::cout << "Is Short";
511+
}
512+
}
513+
}
514+
catch(std::exception e) {
515+
std::cout << "OCR Failed";
516+
}
435517
}
436518

437519
return results;

0 commit comments

Comments
 (0)