Skip to content

Commit

Permalink
configure ocr errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarenko committed May 16, 2023
1 parent bab7a40 commit 4aac275
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions settings.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[config]
# available: chi_sim, chi_tra, eng, kor, jpn, spa, fra, rus, tha, vie, deu, ind, por
language=eng
# how many wrong letters is allowed for Paimon's name. lesser value == more strict text recognition; default = 1 letter
ocr_max_errors=1

[genshin_window_name]
genshin_chi_sim=原神
Expand Down
7 changes: 4 additions & 3 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

std::map<std::string, std::string> ParseConfig(const std::string &fileName) {
std::map<std::string, std::string> configMap = {
{"language", "eng"},
{"genshin_eng", "Genshin Impact"},
{"paimon_eng", "Paimon"}
{"language", "eng"},
{"genshin_eng", "Genshin Impact"},
{"paimon_eng", "Paimon"},
{"ocr_max_errors", "1"}
};
std::ifstream file(fileName);
if (file.is_open()) {
Expand Down
5 changes: 4 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
typedef struct GenshinWindowInfo {
std::wstring windowName = L"Genshin Impact";
std::wstring windowClass = L"UnityWndClass";
int maxOcrErrors = 1;
HWND hwnd = nullptr;
int width = 0;
int height = 0;
Expand Down Expand Up @@ -144,7 +145,8 @@ bool IsPaimonSpeaking(const std::string &paimonName) {
(int) (OUT_DIALOGUE_NAME_POS.val[3] * frame.rows) - (int) (OUT_DIALOGUE_NAME_POS.val[1] * frame.rows));
std::string defaultDialogue = GetTextFromImageByRect(frame, cropDefault);
std::string outDialogue = GetTextFromImageByRect(frame, cropOut);
return IsStringsSimilar(defaultDialogue, paimonName) || IsStringsSimilar(outDialogue, paimonName);
return IsStringsSimilar(defaultDialogue, paimonName, gwi.maxOcrErrors) ||
IsStringsSimilar(outDialogue, paimonName, gwi.maxOcrErrors);
}


Expand Down Expand Up @@ -223,6 +225,7 @@ int PaimonShutUp() {
if (InitTesseract(nullptr, configMap["language"].c_str()))
return 1;

gwi.maxOcrErrors = std::stoi(configMap["ocr_max_errors"]);
std::string paimonName = configMap["paimon_" + configMap["language"]];
std::cout << "Waiting for GenshinImpact.exe process." << std::endl;
bool paimonWasHere = false;
Expand Down
8 changes: 5 additions & 3 deletions source/tesseract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ unsigned int LevenshteinDistance(const std::string &s1, const std::string &s2) {
}


bool IsStringsSimilar(std::string s1, std::string s2, double overlap) {
bool IsStringsSimilar(std::string s1, std::string s2, int maxDifference) {
if (s1.length() == 0) {
return false;
}
std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
std::transform(s2.begin(), s2.end(), s2.begin(), ::toupper);
double nonSimilarity = s1.length() > 0 ? (double) LevenshteinDistance(s1, s2) / s1.length() : 1;
return nonSimilarity <= overlap;
return LevenshteinDistance(s1, s2) <= maxDifference;
}


Expand Down
2 changes: 1 addition & 1 deletion source/tesseract.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ std::string GetTextFromImage(const cv::Mat &image);

unsigned int LevenshteinDistance(const std::string &s1, const std::string &s2);

bool IsStringsSimilar(std::string s1, std::string s2, double overlap = 0.4);
bool IsStringsSimilar(std::string s1, std::string s2, int maxDifference);

void DownloadTessdataFileIfNecessary(const std::string &language);

Expand Down

0 comments on commit 4aac275

Please sign in to comment.