Load protobuf data file
| 156 | |
| 157 | // Load protobuf data file |
| 158 | bool ObjectDetection::LoadObjDetectdData(std::string inputFilePath) |
| 159 | { |
| 160 | // Parse the file |
| 161 | pb_objdetect::ObjDetect objMessage; |
| 162 | std::fstream input(inputFilePath, std::ios::in | std::ios::binary); |
| 163 | if (!objMessage.ParseFromIstream(&input)) { |
| 164 | std::cerr << "Failed to parse protobuf message." << std::endl; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // Clear out any old state |
| 169 | classNames.clear(); |
| 170 | detectionsData.clear(); |
| 171 | trackedObjects.clear(); |
| 172 | |
| 173 | // Seed colors for each class |
| 174 | std::srand(1); |
| 175 | for (int i = 0; i < objMessage.classnames_size(); ++i) { |
| 176 | classNames.push_back(objMessage.classnames(i)); |
| 177 | classesColor.push_back(cv::Scalar( |
| 178 | std::rand() % 205 + 50, |
| 179 | std::rand() % 205 + 50, |
| 180 | std::rand() % 205 + 50 |
| 181 | )); |
| 182 | } |
| 183 | |
| 184 | // Walk every frame in the protobuf |
| 185 | for (size_t fi = 0; fi < objMessage.frame_size(); ++fi) { |
| 186 | const auto &pbFrame = objMessage.frame(fi); |
| 187 | size_t frameId = pbFrame.id(); |
| 188 | |
| 189 | // Buffers for DetectionData |
| 190 | std::vector<int> classIds; |
| 191 | std::vector<float> confidences; |
| 192 | std::vector<cv::Rect_<float>> boxes; |
| 193 | std::vector<int> objectIds; |
| 194 | |
| 195 | // For each bounding box in this frame |
| 196 | for (int di = 0; di < pbFrame.bounding_box_size(); ++di) { |
| 197 | const auto &b = pbFrame.bounding_box(di); |
| 198 | float x = b.x(), y = b.y(), w = b.w(), h = b.h(); |
| 199 | int classId = b.classid(); |
| 200 | float confidence= b.confidence(); |
| 201 | int objectId = b.objectid(); |
| 202 | |
| 203 | // Record for DetectionData |
| 204 | classIds.push_back(classId); |
| 205 | confidences.push_back(confidence); |
| 206 | boxes.emplace_back(x, y, w, h); |
| 207 | objectIds.push_back(objectId); |
| 208 | |
| 209 | // Either append to an existing TrackedObjectBBox… |
| 210 | auto it = trackedObjects.find(objectId); |
| 211 | if (it != trackedObjects.end()) { |
| 212 | it->second->AddBox(frameId, x + w/2, y + h/2, w, h, 0.0); |
| 213 | } |
| 214 | else { |
| 215 | // …or create a brand-new one |
nothing calls this directly
no test coverage detected