|
| 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