Skip to content

Commit 25ab11a

Browse files
authored
Merge pull request #623 from jw098/read-number
read_number_waterfill: add another parameter line_index for logging purposes, when multithreaded
2 parents 1153f36 + d74cd30 commit 25ab11a

File tree

6 files changed

+73
-52
lines changed

6 files changed

+73
-52
lines changed

SerialPrograms/Source/CommonTools/OCR/OCR_NumberReader.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,24 @@ int read_number(Logger& logger, const ImageViewRGB32& image, Language language){
102102
int read_number_waterfill(
103103
Logger& logger, const ImageViewRGB32& image,
104104
uint32_t rgb32_min, uint32_t rgb32_max,
105-
bool text_inside_range
105+
bool text_inside_range,
106+
int8_t line_index
106107
){
107108
std::string ocr_text = read_number_waterfill_no_normalization(logger, image, rgb32_min, rgb32_max, text_inside_range, UINT32_MAX, false);
108109

109110
std::string normalized = run_number_normalization(ocr_text);
110111

112+
std::string line_index_str = "";
113+
if (line_index != -1){
114+
line_index_str = "Line " + std::to_string(line_index) + ": ";
115+
}
111116
if (normalized.empty()){
112-
logger.log("OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> Unable to read.", COLOR_RED);
117+
logger.log(line_index_str + "OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> Unable to read.", COLOR_RED);
113118
return -1;
114119
}
115120

116121
int number = std::atoi(normalized.c_str());
117-
logger.log("OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> " + std::to_string(number));
122+
logger.log(line_index_str + "OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> " + std::to_string(number));
118123

119124
return number;
120125
}
@@ -192,9 +197,13 @@ int read_number_waterfill_multifilter(
192197
std::vector<std::pair<uint32_t, uint32_t>> filters,
193198
uint32_t width_max,
194199
bool text_inside_range,
195-
bool prioritize_numeric_only_results
200+
bool prioritize_numeric_only_results,
201+
int8_t line_index
196202
){
197-
203+
std::string line_index_str = "";
204+
if (line_index != -1){
205+
line_index_str = "Line " + std::to_string(line_index) + ": ";
206+
}
198207

199208
std::map<int, uint8_t> candidates;
200209
for (std::pair<uint32_t, uint32_t> filter : filters){
@@ -210,7 +219,7 @@ int read_number_waterfill_multifilter(
210219
}
211220

212221
int candidate = std::atoi(normalized.c_str());
213-
logger.log("OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> " + std::to_string(candidate));
222+
logger.log(line_index_str + "OCR Text: \"" + ocr_text + "\" -> \"" + normalized + "\" -> " + std::to_string(candidate));
214223

215224
if (prioritize_numeric_only_results && is_digits(ocr_text)){
216225
candidates[candidate] += 2;
@@ -220,19 +229,19 @@ int read_number_waterfill_multifilter(
220229
}
221230

222231
if (candidates.empty()){
223-
logger.log("No valid OCR candidates. Unable to read number.", COLOR_ORANGE);
232+
logger.log(line_index_str + "No valid OCR candidates. Unable to read number.", COLOR_ORANGE);
224233
return -1;
225234
}
226235

227236
std::pair<int, uint8_t> best;
228237
for (const auto& item : candidates){
229-
logger.log("Candidate " + std::to_string(item.first) + ": " + std::to_string(item.second) + " votes");
238+
logger.log(line_index_str + "Candidate " + std::to_string(item.first) + ": " + std::to_string(item.second) + " votes");
230239
if (item.second > best.second){
231240
best = item;
232241
}
233242
}
234243

235-
logger.log("Best candidate: --------------------------> " + std::to_string(best.first));
244+
logger.log(line_index_str + "Best candidate: --------------------------> " + std::to_string(best.first));
236245
return best.first;
237246
}
238247

SerialPrograms/Source/CommonTools/OCR/OCR_NumberReader.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ int read_number(Logger& logger, const ImageViewRGB32& image, Language language =
2727
// This version attempts to improve reliability by first isolating each number
2828
// via waterfill. Then it OCRs each number by itself and recombines them at the
2929
// end. This requires specifying the color range for the text.
30+
//
31+
// line_index: specifies the current number's row. for logging purposes, when multithreaded.
3032
int read_number_waterfill(
3133
Logger& logger, const ImageViewRGB32& image,
3234
uint32_t rgb32_min, uint32_t rgb32_max,
33-
bool text_inside_range = true
35+
bool text_inside_range = true,
36+
int8_t line_index = -1
3437
);
3538

3639
// run OCR on each individual character in the string of numbers.
@@ -53,12 +56,15 @@ int read_number_waterfill(
5356
// prioritize_numeric_only_results:
5457
// - if true: if OCR reads only numeric characters, the candidate gets 2 votes. If OCR reads non-numeric characters, the candidate gets only 1 vote.
5558
// - if false: all reads only get 1 vote
59+
//
60+
// line_index: specifies the current number's row. for logging purposes, when multithreaded.
5661
int read_number_waterfill_multifilter(
5762
Logger& logger, const ImageViewRGB32& image,
5863
std::vector<std::pair<uint32_t, uint32_t>> filters,
5964
uint32_t width_max,
6065
bool text_inside_range = true,
61-
bool prioritize_numeric_only_results = true
66+
bool prioritize_numeric_only_results = true,
67+
int8_t line_index = -1
6268
);
6369

6470

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ void ItemPrinterMaterialDetector::make_overlays(VideoOverlaySet& items) const{
8585

8686
int16_t ItemPrinterMaterialDetector::read_number(
8787
Logger& logger, AsyncDispatcher& dispatcher,
88-
const ImageViewRGB32& screen, const ImageFloatBox& box
88+
const ImageViewRGB32& screen, const ImageFloatBox& box,
89+
int8_t row_index
8990
) const{
9091

9192
ImageViewRGB32 cropped = extract_box_reference(screen, box);
@@ -98,39 +99,41 @@ int16_t ItemPrinterMaterialDetector::read_number(
9899
bool is_dark_text_light_background = image_stats(filtered).average.sum() > 400;
99100
// std::cout << "Average sum of filtered: "<< std::to_string(image_stats(filtered).average.sum()) << std::endl;
100101

101-
int16_t number;
102-
if (is_dark_text_light_background){
103-
const std::vector<std::pair<uint32_t, uint32_t>> filters = {
104-
// {0xff000000, 0xffb0b0b0},
105-
{0xff000000, 0xffa0a0a0},
106-
{0xff000000, 0xff959595},
107-
{0xff000000, 0xff909090},
108-
{0xff000000, 0xff858585},
109-
{0xff000000, 0xff808080},
110-
// {0xff000000, 0xff707070},
111-
// {0xff000000, 0xff606060},
112-
// {0xff000000, 0xff505050},
113-
// {0xff000000, 0xff404040},
114-
// {0xff000000, 0xff303030},
115-
// {0xff000000, 0xff202020},
116-
// {0xff000000, 0xff101010},
117-
};
118-
number = (int16_t)OCR::read_number_waterfill_multifilter(logger, cropped, filters, 24);
119-
}else{
120-
const std::vector<std::pair<uint32_t, uint32_t>> filters = {
121-
{0xff808080, 0xffffffff},
122-
{0xff858585, 0xffffffff},
123-
{0xff909090, 0xffffffff},
124-
{0xff959595, 0xffffffff},
125-
{0xffa0a0a0, 0xffffffff},
126-
// {0xffb0b0b0, 0xffffffff},
127-
// {0xffc0c0c0, 0xffffffff},
128-
// {0xffd0d0d0, 0xffffffff},
129-
// {0xffe0e0e0, 0xffffffff},
130-
// {0xfff0f0f0, 0xffffffff},
131-
};
132-
number = (int16_t)OCR::read_number_waterfill_multifilter(logger, cropped, filters, 24);
133-
}
102+
const std::vector<std::pair<uint32_t, uint32_t>> filters =
103+
[&](){
104+
if (is_dark_text_light_background){
105+
return std::vector<std::pair<uint32_t, uint32_t>>{
106+
// {0xff000000, 0xffb0b0b0},
107+
{0xff000000, 0xffa0a0a0},
108+
{0xff000000, 0xff959595},
109+
{0xff000000, 0xff909090},
110+
{0xff000000, 0xff858585},
111+
{0xff000000, 0xff808080},
112+
// {0xff000000, 0xff707070},
113+
// {0xff000000, 0xff606060},
114+
// {0xff000000, 0xff505050},
115+
// {0xff000000, 0xff404040},
116+
// {0xff000000, 0xff303030},
117+
// {0xff000000, 0xff202020},
118+
// {0xff000000, 0xff101010},
119+
};
120+
}else{
121+
return std::vector<std::pair<uint32_t, uint32_t>>{
122+
{0xff808080, 0xffffffff},
123+
{0xff858585, 0xffffffff},
124+
{0xff909090, 0xffffffff},
125+
{0xff959595, 0xffffffff},
126+
{0xffa0a0a0, 0xffffffff},
127+
// {0xffb0b0b0, 0xffffffff},
128+
// {0xffc0c0c0, 0xffffffff},
129+
// {0xffd0d0d0, 0xffffffff},
130+
// {0xffe0e0e0, 0xffffffff},
131+
// {0xfff0f0f0, 0xffffffff},
132+
};
133+
}
134+
}();
135+
136+
int16_t number = (int16_t)OCR::read_number_waterfill_multifilter(logger, cropped, filters, 24, true, true, row_index);
134137

135138
if (number < 1 || number > 999){
136139
number = -1;
@@ -225,7 +228,7 @@ std::vector<int8_t> ItemPrinterMaterialDetector::find_material_value_row_index(
225228
std::vector<std::unique_ptr<AsyncTask>> tasks(total_rows);
226229
for (int8_t row_index = 0; row_index < total_rows; row_index++){
227230
tasks[row_index] = dispatcher.dispatch([&, row_index]{
228-
int16_t value = read_number(stream.logger(), dispatcher, snapshot, m_box_mat_value[row_index]);
231+
int16_t value = read_number(stream.logger(), dispatcher, snapshot, m_box_mat_value[row_index], row_index);
229232
if (value == material_value){
230233
WriteSpinLock lg(lock);
231234
row_indexes.push_back(row_index);
@@ -246,7 +249,7 @@ int16_t ItemPrinterMaterialDetector::detect_material_quantity(
246249
) const{
247250
context.wait_for_all_requests();
248251
VideoSnapshot snapshot = stream.video().snapshot();
249-
int16_t value = read_number(stream.logger(), dispatcher, snapshot, m_box_mat_quantity[row_index]);
252+
int16_t value = read_number(stream.logger(), dispatcher, snapshot, m_box_mat_quantity[row_index], row_index);
250253
return value;
251254
}
252255

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterMaterialDetector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class ItemPrinterMaterialDetector{
7676

7777
int16_t read_number(
7878
Logger& logger, AsyncDispatcher& dispatcher,
79-
const ImageViewRGB32& screen, const ImageFloatBox& box
79+
const ImageViewRGB32& screen, const ImageFloatBox& box,
80+
int8_t row_index
8081
) const;
8182

8283

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterPrizeReader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ std::array<int16_t, 10> ItemPrinterPrizeReader::read_quantity(
164164
// filtered.save("DebugDumps/test"+ std::to_string(i) +".png");
165165

166166
tasks[i] = dispatcher.dispatch([&, i]{
167-
results[i] = read_number(logger, screen, boxes[i]);
167+
results[i] = read_number(logger, screen, boxes[i], (int8_t)i);
168168
});
169169
}
170170

@@ -179,11 +179,12 @@ std::array<int16_t, 10> ItemPrinterPrizeReader::read_quantity(
179179
int16_t ItemPrinterPrizeReader::read_number(
180180
Logger& logger,
181181
const ImageViewRGB32& screen,
182-
const ImageFloatBox& box
182+
const ImageFloatBox& box,
183+
int8_t line_index
183184
) const{
184185

185186
ImageViewRGB32 cropped = extract_box_reference(screen, box);
186-
int16_t number = (int16_t)OCR::read_number_waterfill(logger, cropped, 0xff808000, 0xffffffff);
187+
int16_t number = (int16_t)OCR::read_number_waterfill(logger, cropped, 0xff808000, 0xffffffff, true, line_index);
187188

188189
if (number < 1 || number > 40){
189190
number = 1; // default to 1 if we can't read the prize quantity

SerialPrograms/Source/PokemonSV/Inference/ItemPrinter/PokemonSV_ItemPrinterPrizeReader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class ItemPrinterPrizeReader{
4343
int16_t read_number(
4444
Logger& logger,
4545
const ImageViewRGB32& screen,
46-
const ImageFloatBox& box
46+
const ImageFloatBox& box,
47+
int8_t line_index = -1
4748
) const;
4849

4950
double average_sum_filtered(const ImageViewRGB32& screen, const ImageFloatBox& box) const;

0 commit comments

Comments
 (0)