-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathParameterTuner.cpp
More file actions
273 lines (238 loc) · 9.86 KB
/
ParameterTuner.cpp
File metadata and controls
273 lines (238 loc) · 9.86 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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <ctime>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
using namespace dynamsoft::cvr;
using namespace dynamsoft::license;
using namespace dynamsoft::dbr;
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftLicensex64.lib")
#else
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftLicensex86.lib")
#endif
#endif
class LoopInfo {
public:
CCaptureVisionRouter* cvrInstance;
std::string imagePath;
std::string templatePath;
std::string matchedTemplate;
std::string description;
LoopInfo(CCaptureVisionRouter* cvr) : cvrInstance(cvr) {}
};
std::tuple<std::string, std::string, std::string> selectImage() {
std::map<std::string, std::tuple<std::string, std::string, std::string>> sampleImages = {
{"1", {"images/blurry.png", "ReadBlurry1DBarcode.json", "Suitable for blurred 1D barcode"}},
{"2", {"images/GeneralBarcodes.png", "ReadMultipleBarcode.json", "Suitable for multiple barcodes"}},
{"3", {"images/inverted-barcode.png", "ReadInvertedBarcode.json", "Suitable for colour inverted barcode"}},
{"4", {"images/DPM.png", "ReadDPM.json", "Suitable for Direct Part Marking barcode"}},
{"5", {"images/EAN-13.jpg", "ReadOneDRetail.json", "Suitable for retail 1D barcode such as EAN13, UPC-A"}},
{"6", {"images/OneDIndustrial.jpg", "ReadOneDIndustrial.json", "Suitable for industrial 1D barcode such as Code128, Code39"}}
};
std::cout << "Available Sample Scenarios:" << std::endl;
std::cout << "[1] Blurry 1D barcode" << std::endl;
std::cout << "[2] Multiple Barcodes" << std::endl;
std::cout << "[3] Colour Inverted Barcode" << std::endl;
std::cout << "[4] Direct Part Marking (DPM)" << std::endl;
std::cout << "[5] Retail 1D barcode" << std::endl;
std::cout << "[6] Industrial 1D barcode" << std::endl;
std::cout << "[7] Custom Image" << std::endl;
while (true) {
std::cout << "\nEnter the number of the image to test, or provide a full path to your own image:" << std::endl;
std::string choice;
std::getline(std::cin, choice);
auto it = sampleImages.find(choice);
if (it != sampleImages.end()) {
auto value = it->second;
std::string imagePath = std::get<0>(value);
std::string matchedTemplate = std::get<1>(value);
std::string description = std::get<2>(value);
imagePath.insert(0, "../../");
return std::make_tuple(imagePath, matchedTemplate, description);
}
else if (choice == "7") {
std::cout << "Enter full path to your own image:\n> ";
std::string customPath;
std::getline(std::cin, customPath);
if (customPath.length() >= 2 && customPath.front() == '"' && customPath.back() == '"')
customPath = customPath.substr(1, customPath.length() - 2);
if (std::ifstream(customPath).good()) {
return { customPath, "", "" };
}
}
else
{
if (choice.length() >= 2 && choice.front() == '"' && choice.back() == '"')
choice = choice.substr(1, choice.length() - 2);
if (std::ifstream(choice).good()) {
return{ choice, "", "" };
}
}
std::cout << "Invalid path input, please try again." << std::endl;
}
}
std::string selectTemplate(const std::string& matchedTemplate, const std::string& description) {
std::vector<std::pair<std::string, std::string>> options;
if (!matchedTemplate.empty()) {
options.emplace_back(matchedTemplate, description);
}
options.emplace_back("ReadBarcodes_Default.json", "General purpose settings");
options.emplace_back("Custom template", "Use your own template");
std::cout << "\nSelect template for this image:" << std::endl;
for (size_t i = 0; i < options.size(); ++i) {
std::cout << "[" << i + 1 << "] " << options[i].first << " (" << options[i].second << ")" << std::endl;
}
std::cout << "\nEnter the number of the template to test, or provide a full path to your own template:" << std::endl;
while (true) {
std::string choice;
std::getline(std::cin, choice);
if (choice.size()==1 && isdigit(choice[0])) {
int index = std::stoi(choice) - 1;
if (index >= 0 && index < options.size()) {
std::string selectedPath = options[index].first;
if (index == options.size() - 1) {
std::cout << "Enter full path to your custom template:\n> ";
std::getline(std::cin, selectedPath);
}
else if (index == options.size() - 2)
{
selectedPath = "../../Dist/Templates/DBR-PresetTemplates.json";
}
else
{
selectedPath.insert(0, "../../CustomTemplates/");
}
if (selectedPath.length() >= 2 && selectedPath.front() == '"' && selectedPath.back() == '"')
selectedPath = selectedPath.substr(1, selectedPath.length() - 2);
if (std::ifstream(selectedPath).good()) {
return selectedPath;
}
else {
std::cout << "Invalid template path, please try again." << std::endl;
}
}
else {
std::cout << "Invalid choice, please try again." << std::endl;
}
}
else if (std::ifstream(choice).good()) {
return choice;
}
else {
std::cout << "Invalid input, please try again." << std::endl;
}
}
}
void run(CCaptureVisionRouter* cvrInstance, const std::string& imagePath, const std::string& templatePath) {
std::cout << "\nRunning with:" << std::endl;
std::cout << "Image: " << imagePath << std::endl;
std::cout << "Template: " << templatePath << std::endl;
std::clock_t startTime = std::clock();
CCapturedResultArray* resultArray = cvrInstance->CaptureMultiPages(imagePath.c_str(), "");
int elapsedMs = static_cast<int>((std::clock() - startTime) * 1000 / CLOCKS_PER_SEC);
int count = resultArray->GetResultsCount();
for (int i = 0; i < count; ++i)
{
const CCapturedResult* result = resultArray->GetResult(i);
if (!result)
continue;
int pageNumber = i + 1;
// It is usually necessary to determine 'ImageTagType' based on the original image tag.
// Since imageFile is used, it is directly converted to 'const dynamsoft::basic_structures::CFileImageTag *'.
const CFileImageTag *tag = dynamic_cast<const CFileImageTag *>(result->GetOriginalImageTag());
if(tag != nullptr)
{
pageNumber = tag->GetPageNumber() + 1;
}
std::cout << "Result" << (count > 1 ? " Page-" + std::to_string(pageNumber) : "") << ":" << std::endl;
int errorCode = result->GetErrorCode();
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_UNSUPPORTED_JSON_KEY_WARNING) {
std::cout << "Error: " << errorCode << ", " << result->GetErrorString() << std::endl;
continue;
}
CDecodedBarcodesResult* barcodeResult = result->GetDecodedBarcodesResult();
if (barcodeResult) {
int count = barcodeResult->GetItemsCount();
for (int j = 0; j < count; ++j) {
const CBarcodeResultItem* item = barcodeResult->GetItem(j);
std::cout << "Barcode result [" << j << "]: " << item->GetText()
<< " (Format: " << item->GetFormatString() << ")" << std::endl;
}
}
else
{
std::cout << "No barcode decode." << std::endl;
}
std::cout << "Time used: " << elapsedMs << " ms...\n" << std::endl;
if (barcodeResult)
barcodeResult->Release();
}
if (resultArray)
resultArray->Release();
}
void loopInner(LoopInfo& loopInfo, bool getImage = true, bool getTemplate = true) {
if (getImage) {
std::tuple<std::string, std::string, std::string> tuple = selectImage();
loopInfo.imagePath = std::get<0>(tuple);
loopInfo.matchedTemplate = std::get<1>(tuple);
loopInfo.description = std::get<2>(tuple);
}
if (getTemplate) {
loopInfo.templatePath = selectTemplate(loopInfo.matchedTemplate, loopInfo.description);
char errorString[1024]{ 0 };
int errorCode = loopInfo.cvrInstance->InitSettingsFromFile(loopInfo.templatePath.c_str(), errorString, 1024);
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_UNSUPPORTED_JSON_KEY_WARNING) {
std::cout << "Init template failed: " << errorCode << ", " << errorString << std::endl;
return;
}
}
run(loopInfo.cvrInstance, loopInfo.imagePath, loopInfo.templatePath);
}
int main() {
std::cout << "Welcome to ParameterTuner!" << std::endl;
char errorString[1024]{ 0 };
int errorCode{ 0 };
// Initialize license.
// The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
// You can also request a 30-day trial license in the customer portal: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=c_cpp
errorCode = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", errorString, 1024);
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_LICENSE_WARNING)
std::cout << "License initialization failed: ErrorCode: " << errorCode << ", ErrorString: " << errorString << std::endl;
CCaptureVisionRouter* cvRouter = new CCaptureVisionRouter();
LoopInfo loopInfo(cvRouter);
loopInner(loopInfo);
while (true) {
std::cout << "What would you like to do next?" << std::endl;
std::cout << "[1] Try a different template" << std::endl;
std::cout << "[2] Load another image" << std::endl;
std::cout << "[3] Exit" << std::endl;
std::cout << "Enter your choice:\n> ";
std::string choice;
std::getline(std::cin, choice);
if (choice == "1") {
loopInner(loopInfo, false, true);
}
else if (choice == "2") {
loopInner(loopInfo, true, false);
}
else if (choice == "3") {
std::cout << "Thank you for using ParameterTuner!" << std::endl;
break;
}
else {
std::cout << "Invalid input. Returning to main menu." << std::endl;
}
}
delete cvRouter, cvRouter = nullptr;
return 0;
}