-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGridBarcodeScanner.cpp
More file actions
1174 lines (1041 loc) · 32.3 KB
/
Copy pathGridBarcodeScanner.cpp
File metadata and controls
1174 lines (1041 loc) · 32.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../../Include/DynamsoftCaptureVisionRouter.h"
#include "../../Include/DynamsoftUtility.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <chrono>
#include <mutex>
#include <unordered_map>
#include <filesystem>
#include <thread>
#include <set>
#include <map>
#include <fstream>
#include <atomic>
using namespace dynamsoft::cvr;
using namespace dynamsoft::license;
using namespace dynamsoft::basic_structures;
using namespace dynamsoft::dbr;
using namespace dynamsoft::utility;
namespace fs = std::filesystem;
// region: The following code only applies to Windows.
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCorex64.lib")
#else
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCorex86.lib")
#endif
#endif
// endregion
#define SAMPLE_IMAGE_PATH "../../Images/sample_grid.png"
#ifndef GRID_TEMPLATE_BASE_PATH
#if defined(_WIN32)
#define GRID_TEMPLATE_BASE_PATH "../../CustomTemplates/"
#else
#define GRID_TEMPLATE_BASE_PATH "./"
#endif
#endif
#define LIGHTWEIGHT_TEMPLATE_PATH GRID_TEMPLATE_BASE_PATH "GridFastScan.json"
#define LIGHTWEIGHT_TEMPLATE_NAME "GridFastScan"
#define DEEP_DECODE_TEMPLATE_PATH GRID_TEMPLATE_BASE_PATH "GridDeepDecode.json"
#define DEEP_DECODE_TEMPLATE_NAME "GridDeepDecode"
#define RESULT_DIR "./Result/"
#define SCALE_FACTOR 2.0f
CPoint computeCenter(const CQuadrilateral& quad)
{
CPoint center{ 0,0 };
for (const auto& p : quad.points) {
center[0] += p[0];
center[1] += p[1];
}
center[0] *= (1.0f / 4);
center[1] *= (1.0f / 4);
return center;
}
CQuadrilateral expandQuad(const CQuadrilateral& quad, float scale)
{
CPoint center = computeCenter(quad);
CQuadrilateral result;
for (int i = 0; i < 4; ++i)
{
result.points[i][0] = center[0] + (quad.points[i][0] - center[0]) * scale;
result.points[i][1] = center[1] + (quad.points[i][1] - center[1]) * scale;
}
return result;
}
enum class LayoutAnalysisAndDecodeTextResultType
{
LAADTRT_DECODE_FAILED,
LAADTRT_DECODED,
LAADTRT_INFERRED
};
struct InputParams
{
bool exit{ false };
std::string imagePath{ "" };
};
class CommonDecodeResult
{
public:
CommonDecodeResult(int err = 0) : error(err) {}
void prepareForLayoutAnalysis()
{
// set id to quad in order to find the corresponding text for layout analysis result.
for (int i = 0; i < locations.size(); ++i)
{
locations[i].id = i;
}
}
std::string getText(int id) const
{
if (id < 0 || id >= texts.size())
throw std::out_of_range("Invalid id.");
return texts[id];
}
public:
std::vector<CQuadrilateral> locations;
std::vector<std::string> texts;
int error{ 0 };
private:
static bool sameQuadrilateral(const CQuadrilateral& quad1, const CQuadrilateral& quad2)
{
for (int i = 0; i < 4; ++i)
{
if (quad1.points[i][0] != quad2.points[i][0] || quad1.points[i][1] != quad2.points[i][1])
return false;
}
return true;
}
};
class LayoutAnalysisAndDecodeTextResultItem
{
public:
LayoutAnalysisAndDecodeTextResultItem(const LayoutElement& element)
{
location = element.quad;
type = LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED;
}
LayoutAnalysisAndDecodeTextResultItem(const CQuadrilateral& quad, const std::string& text)
{
location = quad;
this->text = text;
type = LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODED;
}
LayoutAnalysisAndDecodeTextResultItem() = default;
CQuadrilateral location;
std::string text;
LayoutAnalysisAndDecodeTextResultType type{ LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED };
};
class MyLayoutAnalysisResult
{
public:
MyLayoutAnalysisResult() : layoutResultInner(nullptr) {}
MyLayoutAnalysisResult(const MyLayoutAnalysisResult& other) = delete;
MyLayoutAnalysisResult(LayoutAnalysisResult* pResult)
: layoutResultInner(pResult) {
}
~MyLayoutAnalysisResult()
{
if (layoutResultInner)
{
CLayoutAnalyzer::ReleaseResult(layoutResultInner);
layoutResultInner = nullptr;
}
}
public:
LayoutAnalysisResult* layoutResultInner;
};
class LayoutAnalysisAndDecodeTextResult
{
public:
template <typename T>
using Sparse2DArray = std::unordered_map<int, std::unordered_map<int, T>>;
LayoutAnalysisAndDecodeTextResult(const CommonDecodeResult& commonResult, const MyLayoutAnalysisResult& layoutResult)
{
for (int row_i = 0; row_i < layoutResult.layoutResultInner->rowCount; ++row_i)
{
for (int col_i = 0; col_i < layoutResult.layoutResultInner->colCount; ++col_i)
{
const LayoutElement& element = layoutResult.layoutResultInner->elements[row_i][col_i];
switch (element.source)
{
case LES_INPUT:
{
try
{
std::string text = commonResult.getText(element.quad.id);
LayoutAnalysisAndDecodeTextResultItem item(element.quad, text);
items[row_i][col_i] = std::move(item);
}
catch (const std::exception& e)
{
std::cerr << "Error retrieving text for quad id " << element.quad.id << ": " << e.what() << std::endl;
}
break;
}
case LES_INFERRED:
{
LayoutAnalysisAndDecodeTextResultItem item(element);
items[row_i][col_i] = std::move(item);
break;
}
case LES_NONE:
default:
break;
}
}
}
}
void updateItem(int row, int col, const std::string& text, const CQuadrilateral& quad)
{
std::lock_guard<std::mutex> lock(mtx);
auto rowIt = items.find(row);
if (rowIt == items.end())
throw std::out_of_range("Row " + std::to_string(row) + " not found.");
auto colIt = rowIt->second.find(col);
if (colIt == rowIt->second.end())
throw std::out_of_range("Column " + std::to_string(col) + " not found in row " + std::to_string(row) + ".");
colIt->second.text = text;
colIt->second.location = quad;
}
void updateItemWithDecodeFailed(int row, int col)
{
std::lock_guard<std::mutex> lock(mtx);
auto rowIt = items.find(row);
if (rowIt == items.end())
throw std::out_of_range("Row " + std::to_string(row) + " not found.");
auto colIt = rowIt->second.find(col);
if (colIt == rowIt->second.end())
throw std::out_of_range("Column " + std::to_string(col) + " not found in row " + std::to_string(row) + ".");
colIt->second.text = "";
colIt->second.type = LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODE_FAILED;
}
std::string toJson() const
{
int totalDecoded = 0;
int totalInferred = 0;
std::string gridJson = "";
// Use a map to sort items by row and column for consistent output
std::map<int, std::map<int, const LayoutAnalysisAndDecodeTextResultItem*>> sortedItems;
for (const auto& rowPair : items)
{
for (const auto& colPair : rowPair.second)
{
sortedItems[rowPair.first][colPair.first] = &colPair.second;
}
}
for (auto const& [rowIdx, colMap] : sortedItems)
{
for (auto const& [colIdx, itemPtr] : colMap)
{
const auto& item = *itemPtr;
std::string status;
if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODED || item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED) {
status = "Decoded";
totalDecoded++;
}
else if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODE_FAILED) {
status = "Inferred";
totalInferred++;
}
else {
status = "Failed";
}
if (!gridJson.empty()) gridJson += ",";
gridJson += "\n\t\t{ \"row\": " + std::to_string(rowIdx + 1) +
", \"col\": " + std::to_string(colIdx + 1) +
", \"status\": \"" + status +
"\", \"text\": \"" + escapeJsonString(item.text) + "\" }";
}
}
std::string json = "{\n";
json += "\t\"totalDecoded\": " + std::to_string(totalDecoded) + ",\n";
json += "\t\"totalInferred\": " + std::to_string(totalInferred) + ",\n";
json += "\t\"grid\": [" + gridJson + "\n\t]\n";
json += "}";
return json;
}
private:
static std::string escapeJsonString(const std::string& input)
{
std::string output;
output.reserve(input.length());
for (char c : input)
{
switch (c)
{
case '\"': output += "\\\""; break;
case '\\': output += "\\\\"; break;
case '\b': output += "\\b"; break;
case '\f': output += "\\f"; break;
case '\n': output += "\\n"; break;
case '\r': output += "\\r"; break;
case '\t': output += "\\t"; break;
default:
if (static_cast<unsigned char>(c) < 32)
{
char buf[10];
sprintf(buf, "\\u%04x", static_cast<int>(c));
output += buf;
}
else
{
output += c;
}
}
}
return output;
}
public:
Sparse2DArray<LayoutAnalysisAndDecodeTextResultItem> items;
private:
std::mutex mtx;
};
#if defined(_WIN32)
class ImageShower
{
public:
static ImageShower& instance()
{
static ImageShower inst("Grid Barcode Scanner [1/4] Fast Scan", 30);
return inst;
}
ImageShower(const ImageShower&) = delete;
ImageShower& operator=(const ImageShower&) = delete;
void update(const cv::Mat& img, const std::string& title)
{
std::lock_guard<std::mutex> lock(m_mutex);
img.copyTo(m_img);
windowTitle = title;
}
void stop()
{
if (running.exchange(false))
{
if (m_thread.joinable())
m_thread.join();
}
}
void setDelay(int ms)
{
delayMs = ms;
}
private:
ImageShower(const std::string& windowName, int delayMs)
: winName(windowName),
delayMs(delayMs),
windowTitle(windowName),
running(true)
{
m_thread = std::thread(&ImageShower::loop, this);
}
~ImageShower()
{
stop();
}
void loop()
{
cv::namedWindow(winName, cv::WINDOW_NORMAL);
cv::resizeWindow(winName, cv::Size(800, 600));
cv::moveWindow(winName, 100, 100);
std::string lastTitle = windowTitle;
while (running)
{
cv::Mat img;
std::string title;
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_img.empty())
img = m_img.clone();
m_img = cv::Mat();
title = windowTitle;
}
if (!img.empty())
{
if (title != lastTitle)
{
cv::setWindowTitle(winName, title);
lastTitle = title;
}
cv::Rect rect = cv::getWindowImageRect(winName);
int winW = rect.width;
int winH = rect.height;
double scale = std::min(
(double)winW / img.cols,
(double)winH / img.rows
);
int newW = (int)(img.cols * scale);
int newH = (int)(img.rows * scale);
cv::resizeWindow(winName, cv::Size(newW, newH));
cv::imshow(winName, img);
}
else
{
int key = cv::waitKey(delayMs);
if (key == 27)
running = false;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
int key = cv::waitKey(delayMs);
if (key == 27)
running = false;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
private:
std::string winName;
std::string windowTitle;
int delayMs;
std::thread m_thread;
std::mutex m_mutex;
cv::Mat m_img;
std::atomic<bool> running;
};
#else
class ImageShower
{
public:
static ImageShower& instance()
{
static ImageShower inst;
return inst;
}
ImageShower(const ImageShower&) = delete;
ImageShower& operator=(const ImageShower&) = delete;
void update(const cv::Mat&, const std::string&) {}
void stop() {}
void setDelay(int) {}
private:
ImageShower() = default;
~ImageShower() = default;
};
#endif
class ImageHelper
{
public:
ImageHelper(const std::string& path)
{
img = cv::imread(path, cv::IMREAD_COLOR);
if (img.empty())
throw std::runtime_error("Failed to read image from file: " + path);
imagePath = path;
}
~ImageHelper() = default;
int width() const
{
return img.cols;
}
int height() const
{
return img.rows;
}
int saveResultImage(CommonDecodeResult* result)
{
ImageHelper resultImage = drawSolidQuads(result->locations, cv::Scalar(0, 255, 0, 255), 2);
ImageShower::instance().update(resultImage.img,"Grid Barcode Scanner [1/4] Fast Scan");
return resultImage.saveToResultFile("_phase1");
}
int saveResultImage(const MyLayoutAnalysisResult& layoutResult)
{
std::vector<CQuadrilateral> decodeQuads;
std::vector<CQuadrilateral> inferredQuads;
for (int i = 0; i < layoutResult.layoutResultInner->rowCount; ++i)
{
for (int j = 0; j < layoutResult.layoutResultInner->colCount; ++j)
{
const LayoutElement& element = layoutResult.layoutResultInner->elements[i][j];
if (element.source == LES_INPUT)
decodeQuads.push_back(element.quad);
else if (element.source == LES_INFERRED)
inferredQuads.push_back(element.quad);
}
}
ImageHelper decodeImage = drawSolidQuads(decodeQuads, cv::Scalar(0, 255, 0, 255), 2);
ImageHelper inferredImage = decodeImage.drawDashedQuad(inferredQuads, cv::Scalar(0, 0, 255, 255), 10, 4);
ImageShower::instance().update(inferredImage.img, "Grid Barcode Scanner [2/4] Layout Analysis");
return inferredImage.saveToResultFile("_phase2");
}
int saveResultImage(const LayoutAnalysisAndDecodeTextResult& result)
{
std::vector<CQuadrilateral> decodeQuads;
std::vector<CQuadrilateral> inferredQuads;
std::vector<CQuadrilateral> undecodedQuads;
int total{ 0 }, decoded{ 0 };
for (const auto& row : result.items)
{
for (const auto& col : row.second)
{
total++;
const LayoutAnalysisAndDecodeTextResultItem& item = col.second;
if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODED)
{
decoded++;
decodeQuads.push_back(item.location);
}
else if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED)
{
decoded++;
inferredQuads.push_back(item.location);
}
else if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODE_FAILED)
{
undecodedQuads.push_back(expandQuad(item.location, SCALE_FACTOR));
}
}
}
ImageHelper decodeImage = drawSolidQuads(decodeQuads, cv::Scalar(0, 255, 0, 255), 2);
ImageHelper inferredImage = decodeImage.drawSolidQuads(inferredQuads, cv::Scalar(0, 255, 0, 255), 2);
ImageHelper undecodedImage = inferredImage.drawDashedQuad(undecodedQuads, cv::Scalar(0, 0, 255, 255), 10, 4);
ImageShower::instance().update(undecodedImage.img, "Grid Barcode Scanner [3/4] Deep Decode");
return undecodedImage.saveToResultFile("_phase3");
}
int saveFinalResultImage(const LayoutAnalysisAndDecodeTextResult& result)
{
std::vector<CQuadrilateral> decodeQuads;
std::vector<CQuadrilateral> inferredQuads;
std::vector<CQuadrilateral> undecodedQuads;
for (const auto& row : result.items)
{
for (const auto& col : row.second)
{
const LayoutAnalysisAndDecodeTextResultItem& item = col.second;
if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODED)
{
decodeQuads.push_back(item.location);
}
else if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED)
{
inferredQuads.push_back(item.location);
}
else if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODE_FAILED)
{
undecodedQuads.push_back(expandQuad(item.location, SCALE_FACTOR));
}
}
}
ImageHelper decodeImage = drawSolidQuads(decodeQuads, cv::Scalar(0, 255, 0, 255), 2);
ImageHelper inferredImage = decodeImage.drawSolidQuads(inferredQuads, cv::Scalar(0, 255, 0, 255), 2);
ImageHelper undecodedImage = inferredImage.drawDashedQuad(undecodedQuads, cv::Scalar(0, 0, 255, 255), 10, 4);
for (const auto& row : result.items)
{
for (const auto& col : row.second)
{
const LayoutAnalysisAndDecodeTextResultItem& item = col.second;
if (item.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_DECODE_FAILED)
continue;
cv::Point topLeft = GetTopLeft(item.location);
undecodedImage.drawText(item.text, topLeft, cv::Scalar(0, 255, 0, 255), 0.6);
}
}
ImageShower::instance().update(undecodedImage.img, "Grid Barcode Scanner [4/4] Final Result");
return undecodedImage.saveToResultFile("_final");
}
std::shared_ptr<CImageData> image() const
{
return getCImageData(img);
}
static void createResultDir(const std::string& imgPath)
{
fs::path dir = RESULT_DIR / fs::path(imgPath).filename().stem();
fs::create_directories(dir);
}
std::string getResultDir() const
{
fs::path resultPath = RESULT_DIR / imagePath.filename().stem();
return resultPath.string();
}
private:
ImageHelper(cv::Mat mat, const std::string& path) : img(mat), imagePath(path) {}
ImageHelper clone()
{
cv::Mat mat = img.clone();
return { mat,imagePath.string() };
}
bool saveToResultFile(const std::string& fix) const
{
fs::path resultPath = RESULT_DIR / imagePath.stem() / (imagePath.stem().string() + fix + imagePath.extension().string());
return cv::imwrite(resultPath.string(), img);
}
ImageHelper drawSolidQuads(const std::vector<CQuadrilateral>& quads, cv::Scalar color = cv::Scalar(0, 0, 255), int thickness = 2)
{
cv::Mat dst = img.clone();
std::vector<std::vector<cv::Point>> cvQuads;
for (auto& quad : quads)
{
cvQuads.push_back({ cv::Point(quad.points[0][0], quad.points[0][1]),
cv::Point(quad.points[1][0], quad.points[1][1]),
cv::Point(quad.points[2][0], quad.points[2][1]),
cv::Point(quad.points[3][0], quad.points[3][1]) });
}
cv::polylines(dst, cvQuads, true, color, thickness, cv::LINE_8);
return { dst, imagePath.string() };
}
ImageHelper drawDashedQuad(const std::vector<CQuadrilateral>& quads, cv::Scalar color, int dashLength = 5, int thickness = 2)
{
cv::Mat dst = img.clone();
auto drawDashedLine = [&](cv::Point p1, cv::Point p2)
{
cv::LineIterator it(dst, p1, p2, 8);
bool draw = true;
int count = 0;
for (int i = 0; i < it.count; i++, ++it)
{
if (draw)
{
cv::circle(
dst,
it.pos(),
thickness / 2,
cv::Scalar(color[0], color[1], color[2]),
cv::FILLED,
cv::LINE_AA
);
}
count++;
if (count == dashLength)
{
count = 0;
draw = !draw;
}
}
};
for (auto& quad : quads)
{
for (int i = 0; i < 4; i++)
{
cv::Point p1(quad.points[i][0], quad.points[i][1]);
cv::Point p2(quad.points[(i + 1) % 4][0], quad.points[(i + 1) % 4][1]);
drawDashedLine(p1, p2);
}
}
return { dst, imagePath.string() };
}
static cv::Point GetTopLeft(const CQuadrilateral& quad)
{
int idx = 0;
cv::Point topLeft(quad.points[0][0], quad.points[0][1]);
for (int i = 1; i < 4; i++)
{
if (quad.points[i][0] < topLeft.x)
{
topLeft.x = quad.points[i][0];
}
if (quad.points[i][1] < topLeft.y)
{
topLeft.y = quad.points[i][1];
}
}
return topLeft;
}
void drawText(const std::string& text, cv::Point org, cv::Scalar color, double scale = 1.0)
{
cv::putText(img, text, org,
cv::FONT_HERSHEY_SIMPLEX,
scale,
color,
2,
cv::LINE_AA);
}
static std::shared_ptr<CImageData> getCImageData(const cv::Mat& mat)
{
cv::Mat src = mat;
if (!src.isContinuous())
src = src.clone();
unsigned long long bytesLength = src.total() * src.elemSize();
const unsigned char* bytes = src.data;
int width = src.cols;
int height = src.rows;
int stride = static_cast<int>(src.step);
return std::make_shared<CImageData>(
bytesLength,
bytes,
width,
height,
stride,
ImagePixelFormat::IPF_BGR_888
);
}
private:
cv::Mat img;
fs::path imagePath;
};
struct Task {
int row;
int col;
LayoutAnalysisAndDecodeTextResultItem* item;
};
InputParams welcome()
{
InputParams params;
std::cout << "Grid Barcode Scanner!" << std::endl
<< "===========================" << std::endl;
std::cout << std::endl << "Image path : [press Enter to use sample image (sample_grid.png)]" << std::endl
<< "'Q'/'q' to quit" << std::endl;
std::string input;
std::getline(std::cin, input);
if (input == "Q" || input == "q")
{
params.exit = true;
return params;
}
if (input.empty())
params.imagePath = SAMPLE_IMAGE_PATH;
else
{
if (input.length() >= 2 && input.front() == '"' && input.back() == '"')
input = input.substr(1, input.length() - 2);
params.imagePath = input;
}
return params;
}
CommonDecodeResult commonDecode(const ImageHelper& image, const std::string& templatePath, const std::string& templateName = std::string())
{
CCaptureVisionRouter cvRouter;
char error[512];
int errorCode = cvRouter.InitSettingsFromFile(templatePath.c_str(), error, 512);
if (errorCode != ErrorCode::EC_OK)
{
std::cout << "Failed to initialize settings from file: ErrorCode: " << errorCode << ", ErrorString: " << error << std::endl;
return { errorCode };
}
auto start = std::chrono::high_resolution_clock::now();
CCapturedResult* result = cvRouter.Capture(image.image().get(), templateName.c_str());
auto end = std::chrono::high_resolution_clock::now();
if (result->GetErrorCode() == ErrorCode::EC_UNSUPPORTED_JSON_KEY_WARNING)
{
std::cout << "Common decode warning: " << result->GetErrorCode() << ", " << result->GetErrorString() << std::endl;
}
else if (result->GetErrorCode() != ErrorCode::EC_OK && result->GetErrorCode() != ErrorCode::EC_TIMEOUT)
{
std::cout << "Common decode error: " << result->GetErrorCode() << "," << result->GetErrorString() << std::endl;
int ret = result->GetErrorCode();
result->Release();
return { ret };
}
CommonDecodeResult decodeResult;
for (int i = 0; i < result->GetItemsCount(); ++i)
{
const CCapturedResultItem* item = result->GetItem(i);
if (item && (item->GetType() == CapturedResultItemType::CRIT_BARCODE))
{
const CBarcodeResultItem* barcodeItem = static_cast<const CBarcodeResultItem*>(item);
decodeResult.locations.push_back(barcodeItem->GetLocation());
decodeResult.texts.push_back(barcodeItem->GetText());
}
}
std::cout << "[Phase 1] Fast scan: " << decodeResult.texts.size() << " barcodes decoded in " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms." << std::endl;
result->Release();
return decodeResult;
}
int getMedianFuncValue(const std::vector<CQuadrilateral>& vec)
{
if (vec.empty())
return 0;
std::vector<int> values;
values.reserve(vec.size());
for (const auto& quad : vec)
{
values.push_back(quad.GetArea());
}
std::sort(values.begin(), values.end());
size_t n = values.size();
if (n % 2 == 1)
{
return values[n / 2];
}
else
{
return (values[n / 2 - 1] + values[n / 2]) / 2;
}
}
int findDifferentLengthIndex(const std::vector<std::string>&vec)
{
std::unordered_map<size_t, int> lenCount;
for (const auto& s : vec)
{
lenCount[s.size()]++;
}
if (lenCount.size() != 2)
{
return -1;
}
size_t abnormalLen = 0;
int abnormalTypeCount = 0;
for (const auto& [len, count] : lenCount)
{
if (count == 1)
{
abnormalLen = len;
abnormalTypeCount++;
}
}
if (abnormalTypeCount != 1)
{
return -1;
}
for (int i = 0; i < vec.size(); i++)
{
if (vec[i].size() == abnormalLen)
{
return i;
}
}
return -1;
}
MyLayoutAnalysisResult Analyze(const ImageHelper& imageHelper, const CommonDecodeResult& result)
{
LayoutAnalysisParameter layoutParam;
layoutParam.pattern = LayoutPattern::LP_MATRIX;
layoutParam.inputImageHeight = imageHelper.height();
layoutParam.inputImageWidth = imageHelper.width();
LayoutAnalysisResult* layoutResult = CLayoutAnalyzer::Analyze(result.locations.data(), result.locations.size(), &layoutParam);
if (!layoutResult || layoutResult->errorCode != ErrorCode::EC_OK)
{
std::cout << "Layout analysis failed: ErrorCode: " << (layoutResult ? layoutResult->errorCode : -1) << std::endl;
if (layoutResult)
CLayoutAnalyzer::ReleaseResult(layoutResult);
return { };
}
std::cout << std::endl << "[Phase 2] Layout analysis";
std::cout << ": "
<< layoutResult->colCount * layoutResult->rowCount << " grid positions (" << layoutResult->rowCount << "x" << layoutResult->colCount << "). "
<< layoutResult->inferredQuadCount << " inferred regions.";
std::cout << std::endl;
return { layoutResult };
}
void deepDecodeInner(
const ImageHelper& image,
const std::string& templatePath,
LayoutAnalysisAndDecodeTextResult* laadtResult,
const Task& task,
const std::string& templateName = std::string())
{
CCaptureVisionRouter cvRouter;
char error[512];
int errorCode = cvRouter.InitSettingsFromFile(templatePath.c_str(), error, 512);
if (errorCode != ErrorCode::EC_OK)
{
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
return;
}
SimplifiedCaptureVisionSettings settings;
errorCode = cvRouter.GetSimplifiedSettings(templateName.c_str(), &settings);
if(errorCode != ErrorCode::EC_OK)
{
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
return;
}
settings.roi = expandQuad(task.item->location, SCALE_FACTOR);
settings.roiMeasuredInPercentage = 0;
errorCode = cvRouter.UpdateSettings(templateName.c_str(), &settings, error, 512);
if (errorCode != ErrorCode::EC_OK)
{
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
return;
}
CCapturedResult* result = cvRouter.Capture(image.image().get(), templateName.c_str());
if (result->GetErrorCode() != ErrorCode::EC_OK)
{
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
result->Release();
return;
}
for (int i = 0; i < result->GetItemsCount(); ++i)
{
const CCapturedResultItem* item = result->GetItem(i);
if (item && item->GetType() == CapturedResultItemType::CRIT_BARCODE)
{
const CBarcodeResultItem* barcodeItem = static_cast<const CBarcodeResultItem*>(item);
const char* text = barcodeItem->GetText();
if(text)
laadtResult->updateItem(task.row, task.col,text, barcodeItem->GetLocation());
else
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
result->Release();
return;
}
}
laadtResult->updateItemWithDecodeFailed(task.row, task.col);
result->Release();
}
void deepDecode(const ImageHelper& image, const std::string& templatePath, LayoutAnalysisAndDecodeTextResult* laadtResult, const std::string& templateName = std::string())
{
auto start = std::chrono::high_resolution_clock::now();
std::vector<Task> tasks;
for(auto &row:laadtResult->items)
{
for(auto &col:row.second)
{
if(col.second.type == LayoutAnalysisAndDecodeTextResultType::LAADTRT_INFERRED)
{
tasks.push_back({ row.first, col.first, &col.second });
}
}