-
Notifications
You must be signed in to change notification settings - Fork 162
/
alpr_utils.h
121 lines (101 loc) · 3.01 KB
/
alpr_utils.h
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
#if !defined(_ULTIMATE_ALPR_SDK_SAMPLES_UTILS_H_)
#define _ULTIMATE_ALPR_SDK_SAMPLES_UTILS_H_
#include <ultimateALPR-SDK-API-PUBLIC.h>
#include <assert.h>
#include <stdlib.h>
#include <map>
#include <sys/stat.h>
#include <codecvt>
// Not part of the SDK, used to decode images -> https://github.com/nothings/stb
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#include "stb_image.h"
#if ULTALPR_SDK_OS_ANDROID
#include "alpr_utils.h"
#endif /* ULTALPR_SDK_OS_ANDROID */
using namespace ultimateAlprSdk;
/*
* File description
*/
struct AlprFile {
void* uncompressedData = nullptr;
size_t width = 0;
size_t height = 0;
ULTALPR_SDK_IMAGE_TYPE type;
virtual ~AlprFile() {
release();
}
void release() {
if (uncompressedData) {
free(uncompressedData);
uncompressedData = nullptr;
}
}
inline bool isValid() const {
return (uncompressedData != nullptr && width && height);
}
};
/*
* Decodes a JPEG/PNG/BMP file
* @param path
* @param type
* @param width
* @param height
* @returns
*/
static bool alprDecodeFile(const std::string& path, AlprFile& alprFile)
{
ULTALPR_SDK_ASSERT(!path.empty());
// Open file
FILE* file =
# if ULTALPR_SDK_OS_ANDROID
sdk_android_asset_fopen(path.c_str(), "rb");
# else
fopen(path.c_str(), "rb");
# endif
if (!file) {
ULTALPR_SDK_PRINT_ERROR("Failed to open file at: %s", path.c_str());
return false;
}
// Decode the file
int width, height, channels;
stbi_uc* uncompressedData = stbi_load_from_file(file, &width, &height, &channels, 0);
fclose(file);
if (!uncompressedData || width <= 0 || height <= 0 || (channels != 1 && channels != 3 && channels != 4)) {
ULTALPR_SDK_PRINT_ERROR("Invalid file(%s, %d, %d, %d)", path.c_str(), width, height, channels);
if (uncompressedData) {
free(uncompressedData);
}
return false;
}
// We expect RGB-family data from the JPEG/PNG/BMP file
// If you're using data from your camera then, it should be YUV-family and you don't need
// to convert to RGB-family.
// List of supported types: https://www.doubango.org/SDKs/anpr/docs/cpp-api.html#_CPPv4N15ultimateAlprSdk22ULTALPR_SDK_IMAGE_TYPEE
alprFile.type = (channels == 3) ? ULTALPR_SDK_IMAGE_TYPE_RGB24 : (channels == 1 ? ULTALPR_SDK_IMAGE_TYPE_Y : ULTALPR_SDK_IMAGE_TYPE_RGBA32);
alprFile.uncompressedData = uncompressedData;
alprFile.width = static_cast<size_t>(width);
alprFile.height = static_cast<size_t>(height);
return true;
}
static bool alprParseArgs(int argc, char *argv[], std::map<std::string, std::string >& values)
{
ULTALPR_SDK_ASSERT(argc > 0 && argv != nullptr);
values.clear();
// Make sure the number of arguments is even
if ((argc - 1) & 1) {
ULTALPR_SDK_PRINT_ERROR("Number of args must be even");
return false;
}
// Parsing
for (int index = 1; index < argc; index += 2) {
std::string key = argv[index];
if (key.size() < 2 || key[0] != '-' || key[1] != '-') {
ULTALPR_SDK_PRINT_ERROR("Invalid key: %s", key.c_str());
return false;
}
values[key] = argv[index + 1];
}
return true;
}
#endif /* _ULTIMATE_ALPR_SDK_SAMPLES_UTILS_H_ */