-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlgroundparsing.cpp
126 lines (95 loc) · 4.48 KB
/
xmlgroundparsing.cpp
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
// Function to convert rotated rect to a bounding box (cv::Rect)
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include "pugixml.hpp"
using namespace std;
using namespace cv;
//funciton to convet to the normal rectangle
cv::Rect rotatedRectToBoundingBox(const RotatedRect& rRect) {
return rRect.boundingRect();
}
// function to parse the XML and extract ground truth rectangles
std::vector<cv::Rect> parseXMLGroundTruth(const std::string& xmlPath) {
std::vector<cv::Rect> groundTruthRects;
// loading the XML file
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(xmlPath.c_str());
if (!result) {
cerr << "Failed to load XML file: " << result.description() << endl;
return groundTruthRects;
}
// Get the root node (<parking>)
pugi::xml_node parking = doc.child("parking");
if (!parking) {
cerr << "Invalid XML structure: <parking> not found." << endl;
return groundTruthRects;
}
// Iterate over each <space> element
for (pugi::xml_node space : parking.children("space")) {
pugi::xml_node rotatedRect = space.child("rotatedRect");
if (!rotatedRect) continue;
// Extract <center>, <size>, and <angle>
pugi::xml_node center = rotatedRect.child("center");
pugi::xml_node size = rotatedRect.child("size");
pugi::xml_node angle = rotatedRect.child("angle");
if (!center || !size || !angle) continue;
// Read values from XML
int centerX = center.attribute("x").as_int();
int centerY = center.attribute("y").as_int();
int width = size.attribute("w").as_int();
int height = size.attribute("h").as_int();
float angleDeg = angle.attribute("d").as_float();
// Create the RotatedRect
Point2f centerPoint(centerX, centerY);
Size2f rectSize(width, height);
RotatedRect rotatedRectObj(centerPoint, rectSize, angleDeg);
// Convert the rotated rectangle to a bounding box
cv::Rect boundingBox = rotatedRectToBoundingBox(rotatedRectObj);
groundTruthRects.push_back(boundingBox);
}
return groundTruthRects;
}
//------------------------------------------------------------Funnction fot the creating the occupied veector rectanfles
// we need to check this function with the nbounding boxes around the contours to make sure that they intersect and they are the cars, then we find the rectangledd from total rectangels that make this intersection and we change their color
std::vector<cv::Rect> parseOccupiedSpaces(const std::string& xmlPath) {
std::vector<cv::Rect> occupiedSpaces; // the vector of rectangles that is going to store the occupied rectangles
// Load the XML file
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(xmlPath.c_str());
if (!result) {
cerr << "Failed to load XML file: " << result.description() << endl;
return occupiedSpaces;
}
// Get the root node (<parking>)
pugi::xml_node parking = doc.child("parking");
if (!parking) {
cerr << "Invalid XML structure: <parking> not found." << endl;
return occupiedSpaces;
}
// Iterate over each <space> element
for (pugi::xml_node space : parking.children("space")) {
// Check the occupied attribute
int occupied = space.attribute("occupied").as_int();
if (occupied != 1) continue; // checking if it is set to one
pugi::xml_node rotatedRect = space.child("rotatedRect");
if (!rotatedRect) continue;
// Extract <center>, <size>, and <angle>
pugi::xml_node center = rotatedRect.child("center");
pugi::xml_node size = rotatedRect.child("size");
pugi::xml_node angle = rotatedRect.child("angle");
if (!center || !size || !angle) continue;
// reading values from XML
int centerX = center.attribute("x").as_int();
int centerY = center.attribute("y").as_int();
int width = size.attribute("w").as_int();
int height = size.attribute("h").as_int();
float angleDeg = angle.attribute("d").as_float();
Point2f centerPoint(centerX, centerY);
Size2f rectSize(width, height);
RotatedRect rotatedRectObj(centerPoint, rectSize, angleDeg);// createing the RotatedRect
cv::Rect boundingBox = rotatedRectToBoundingBox(rotatedRectObj);// Convert the rotated rectangle to a bounding box
occupiedSpaces.push_back(boundingBox);
}
return occupiedSpaces;
}