Skip to content

Commit 5d5f3e9

Browse files
committed
Initial commit
0 parents  commit 5d5f3e9

7 files changed

+651
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

CMakeLists.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
project(alprstreamsample)
2+
3+
set(CMAKE_BUILD_TYPE DEBUG)
4+
5+
cmake_minimum_required (VERSION 2.6)
6+
7+
8+
ADD_DEFINITIONS(
9+
-std=c++11
10+
)
11+
12+
13+
14+
15+
ADD_EXECUTABLE( alprstream_sample_videofile
16+
alprstream_sample_videofile.cpp
17+
)
18+
TARGET_LINK_LIBRARIES(alprstream_sample_videofile
19+
alprstream
20+
openalpr
21+
)
22+
23+
24+
ADD_EXECUTABLE( alprstream_sample_videostream
25+
alprstream_sample_videostream.cpp
26+
)
27+
TARGET_LINK_LIBRARIES(alprstream_sample_videostream
28+
alprstream
29+
openalpr
30+
)
31+
32+
ADD_EXECUTABLE( alprstream_sample_rawframe
33+
alprstream_sample_rawframe.cpp
34+
)
35+
TARGET_LINK_LIBRARIES(alprstream_sample_rawframe
36+
alprstream
37+
openalpr
38+
alpropencv
39+
)

alprstream_images.cpp

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// OpenALPR sample for alprstreamgpu library
2+
// Copyright 2017, OpenALPR Technology, Inc.
3+
4+
// System imports
5+
#include <cstdlib>
6+
#include <vector>
7+
#include <string>
8+
#include <string.h>
9+
#include <sstream>
10+
11+
// Import used to list files in directory
12+
#include <dirent.h>
13+
14+
#include <opencv2/opencv.hpp>
15+
#include <opencv2/highgui/highgui.hpp>
16+
17+
// Import OpenALPR alprstreamgpu (also pulls in alprgpu.h and alpr.h)
18+
// Object definitions for individual results are found in alpr.h
19+
// Object definitions for group results are found in alprstreamgpu.h
20+
#include <alprstream.h>
21+
22+
using namespace std;
23+
using namespace alpr;
24+
25+
// helper prototypes
26+
std::vector<std::string> list_files_in_dir(const char* dirPath);
27+
void print_frame_results(vector<RecognizedFrame> rframes);
28+
void print_group_results(std::vector<AlprGroupResult> groups);
29+
30+
31+
int main(int argc, char** argv) {
32+
33+
cout << "Initializing" << endl;
34+
const long STARTING_EPOCH_TIME_MS = 1500294710000;
35+
const std::string LICENSEPLATE_COUNTRY = "us";
36+
37+
38+
// Size of image buffer to maintain in stream queue -- This only matters if you are feeding
39+
// images/video into the buffer faster than can be processed (i.e., on a background thread)
40+
// Setting this to the batch size since we're feeding in images synchronously, so it's only needed to
41+
// hold a single batch
42+
43+
// Batch size and GPU ID set in openalpr.conf
44+
// Video buffer frames controls the number of frames to buffer in memory. Must be >= gpu batch size
45+
const int VIDEO_BUFFER_SIZE = 15;
46+
47+
// The stream will assume sequential frames. If there is no motion from frame to frame, then
48+
// processing can be skipped for some frames
49+
const bool USE_MOTION_DETECTION = false;
50+
51+
AlprStream alpr_stream(VIDEO_BUFFER_SIZE, USE_MOTION_DETECTION);
52+
Alpr alpr(LICENSEPLATE_COUNTRY, "", "", "SEpKS0xNTkewsbKztLW2t7i5uru8vb7C2Nje36WgpaetqqOvpaqvqpORl5CdkZ+QAHddFHPee9CiEPnPDHq90vCB7TcEJPm0Gq7MdB/0jGqrJmBzTXii59+J12zZ7GfsRL+a1VqbuOWZM+fkI3PoXzw53kOuwEr0RcEnEfFu8kXh8546xlSRYQSwoKoq84/B");
53+
54+
cout << "Initialization complete" << endl;
55+
56+
// It's important that the image dimensions are consistent within a batch and that you
57+
// only drive OpenALPR with few various image sizes. The memory for each image size is
58+
// cached on the GPU for efficiency, and using many different image sizes will degrade performance
59+
vector<string> input_images = list_files_in_dir("/tmp/imagebatchtest");
60+
61+
for (uint32_t i = 0; i < input_images.size(); i++)
62+
{
63+
cout << "Batching image " << i << ": " << input_images[i] << endl;
64+
65+
cv::Mat img = cv::imread(input_images[i], 1);
66+
67+
// Push the raw BGR pixel data
68+
// Use the arbitrary starting epoch time + 100ms for each image
69+
alpr_stream.push_frame(img.data, img.elemSize(), img.cols, img.rows, STARTING_EPOCH_TIME_MS + (i * 100));
70+
71+
int BATCH_SIZE = 10;
72+
if (alpr_stream.get_queue_size() >= BATCH_SIZE || i == input_images.size()-1)
73+
{
74+
// Process a batch once the stream is full or it's the last image
75+
76+
vector<RecognizedFrame> frame_results = alpr_stream.process_batch(&alpr);
77+
print_frame_results(frame_results);
78+
79+
// After each batch processing, we can check to see if any groups are ready
80+
// "Groups" form based on their timestamp and plate numbers on each stream
81+
// The stream object has configurable options for how long to wait before
82+
// completing a plate group. You may peek at the active list without popping.
83+
cout << "After batching there are: " << alpr_stream.peek_active_groups().size() << " active groups" << endl;
84+
85+
std::vector<AlprGroupResult> group_results = alpr_stream.pop_completed_groups();
86+
print_group_results(group_results);
87+
}
88+
}
89+
90+
cout << "Done" << endl;
91+
92+
return 0;
93+
}
94+
95+
96+
97+
void print_frame_results(vector<RecognizedFrame> rframes)
98+
{
99+
for (uint32_t frame_index = 0; frame_index < rframes.size(); frame_index++)
100+
{
101+
RecognizedFrame rf = rframes[frame_index];
102+
for (uint32_t i = 0; i < rf.results.plates.size(); i++)
103+
{
104+
cout << "Frame " << rf.frame_number << " result: " << rf.results.plates[i].bestPlate.characters << endl;
105+
}
106+
}
107+
}
108+
109+
void print_group_results(std::vector<AlprGroupResult> groups)
110+
{
111+
for (uint32_t group_index = 0; group_index < groups.size(); group_index++)
112+
{
113+
AlprGroupResult group = groups[group_index];
114+
115+
cout << "Group (" << group.epoch_ms_time_start << " - " << group.epoch_ms_time_end << ") " << group.best_plate_number << endl;
116+
}
117+
}
118+
119+
std::vector<std::string> list_files_in_dir(const char* dirPath)
120+
{
121+
DIR *dir;
122+
123+
std::vector<std::string> files;
124+
125+
struct dirent *ent;
126+
if ((dir = opendir (dirPath)) != NULL)
127+
{
128+
/* print all the files and directories within directory */
129+
while ((ent = readdir (dir)) != NULL)
130+
{
131+
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
132+
{
133+
stringstream fullpath;
134+
fullpath << dirPath << "/" << ent->d_name;
135+
files.push_back(fullpath.str());
136+
}
137+
}
138+
closedir (dir);
139+
}
140+
else
141+
{
142+
/* could not open directory */
143+
perror ("");
144+
return files;
145+
}
146+
147+
return files;
148+
}

alprstream_sample_rawframe.cpp

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// OpenALPR sample for alprstreamgpu library
2+
// Copyright 2017, OpenALPR Technology, Inc.
3+
4+
// System imports
5+
#include <cstdlib>
6+
#include <vector>
7+
#include <string>
8+
#include <string.h>
9+
#include <sstream>
10+
11+
// Import used to list files in directory
12+
#include <dirent.h>
13+
14+
#include <opencv2/opencv.hpp>
15+
#include <opencv2/highgui/highgui.hpp>
16+
17+
// Import OpenALPR alprstreamgpu (also pulls in alprgpu.h and alpr.h)
18+
// Object definitions for individual results are found in alpr.h
19+
// Object definitions for group results are found in alprstreamgpu.h
20+
#include <alprstream.h>
21+
22+
using namespace std;
23+
using namespace alpr;
24+
25+
// helper prototypes
26+
std::vector<std::string> list_files_in_dir(const char* dirPath);
27+
void print_frame_results(vector<RecognizedFrame> rframes);
28+
void print_group_results(std::vector<AlprGroupResult> groups);
29+
30+
31+
int main(int argc, char** argv) {
32+
33+
cout << "Initializing" << endl;
34+
const long STARTING_EPOCH_TIME_MS = 1500294710000;
35+
const std::string LICENSEPLATE_COUNTRY = "us";
36+
const std::string LICENSE_KEY = "";
37+
38+
39+
// Size of image buffer to maintain in stream queue -- This only matters if you are feeding
40+
// images/video into the buffer faster than can be processed (i.e., on a background thread)
41+
// Setting this to the batch size since we're feeding in images synchronously, so it's only needed to
42+
// hold a single batch
43+
44+
// Batch size and GPU ID set in openalpr.conf
45+
// Video buffer frames controls the number of frames to buffer in memory. Must be >= gpu batch size
46+
const int VIDEO_BUFFER_SIZE = 15;
47+
48+
// The stream will assume sequential frames. If there is no motion from frame to frame, then
49+
// processing can be skipped for some frames
50+
const bool USE_MOTION_DETECTION = false;
51+
52+
AlprStream alpr_stream(VIDEO_BUFFER_SIZE, USE_MOTION_DETECTION);
53+
Alpr alpr(LICENSEPLATE_COUNTRY, "", "", LICENSE_KEY);
54+
55+
if (!alpr.isLoaded())
56+
{
57+
cout << "Error loading OpenALPR library." << endl;
58+
exit(1);
59+
}
60+
61+
cout << "Initialization complete" << endl;
62+
63+
// It's important that the image dimensions are consistent within a batch and that you
64+
// only drive OpenALPR with few various image sizes. The memory for each image size is
65+
// cached on the GPU for efficiency, and using many different image sizes will degrade performance
66+
vector<string> input_images = list_files_in_dir("/tmp/imagebatchtest");
67+
68+
for (uint32_t i = 0; i < input_images.size(); i++)
69+
{
70+
cout << "Batching image " << i << ": " << input_images[i] << endl;
71+
72+
cv::Mat img = cv::imread(input_images[i], 1);
73+
74+
// Push the raw BGR pixel data
75+
// Use the arbitrary starting epoch time + 100ms for each image
76+
alpr_stream.push_frame(img.data, img.elemSize(), img.cols, img.rows, STARTING_EPOCH_TIME_MS + (i * 100));
77+
78+
int BATCH_SIZE = 10;
79+
if (alpr_stream.get_queue_size() >= BATCH_SIZE || i == input_images.size()-1)
80+
{
81+
// Process a batch once the stream is full or it's the last image
82+
83+
vector<RecognizedFrame> frame_results = alpr_stream.process_batch(&alpr);
84+
print_frame_results(frame_results);
85+
86+
// After each batch processing, we can check to see if any groups are ready
87+
// "Groups" form based on their timestamp and plate numbers on each stream
88+
// The stream object has configurable options for how long to wait before
89+
// completing a plate group. You may peek at the active list without popping.
90+
cout << "After batching there are: " << alpr_stream.peek_active_groups().size() << " active groups" << endl;
91+
92+
std::vector<AlprGroupResult> group_results = alpr_stream.pop_completed_groups();
93+
print_group_results(group_results);
94+
}
95+
}
96+
97+
cout << "Done" << endl;
98+
99+
return 0;
100+
}
101+
102+
103+
104+
void print_frame_results(vector<RecognizedFrame> rframes)
105+
{
106+
for (uint32_t frame_index = 0; frame_index < rframes.size(); frame_index++)
107+
{
108+
RecognizedFrame rf = rframes[frame_index];
109+
for (uint32_t i = 0; i < rf.results.plates.size(); i++)
110+
{
111+
cout << "Frame " << rf.frame_number << " result: " << rf.results.plates[i].bestPlate.characters << endl;
112+
}
113+
}
114+
}
115+
116+
void print_group_results(std::vector<AlprGroupResult> groups)
117+
{
118+
for (uint32_t group_index = 0; group_index < groups.size(); group_index++)
119+
{
120+
AlprGroupResult group = groups[group_index];
121+
122+
cout << "Group (" << group.epoch_ms_time_start << " - " << group.epoch_ms_time_end << ") " << group.best_plate_number << endl;
123+
}
124+
}
125+
126+
std::vector<std::string> list_files_in_dir(const char* dirPath)
127+
{
128+
DIR *dir;
129+
130+
std::vector<std::string> files;
131+
132+
struct dirent *ent;
133+
if ((dir = opendir (dirPath)) != NULL)
134+
{
135+
/* print all the files and directories within directory */
136+
while ((ent = readdir (dir)) != NULL)
137+
{
138+
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
139+
{
140+
stringstream fullpath;
141+
fullpath << dirPath << "/" << ent->d_name;
142+
files.push_back(fullpath.str());
143+
}
144+
}
145+
closedir (dir);
146+
}
147+
else
148+
{
149+
/* could not open directory */
150+
perror ("");
151+
return files;
152+
}
153+
154+
return files;
155+
}

0 commit comments

Comments
 (0)