Skip to content

Commit 6ca35bb

Browse files
committed
Fixed Stabilizer and created Object Detector tests
1 parent 58d2e8f commit 6ca35bb

File tree

13 files changed

+354
-222
lines changed

13 files changed

+354
-222
lines changed

include/CVObjectDetection.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ class CVObjectDetection{
110110

111111
/// Protobuf Save and Load methods
112112
// Save protobuf file
113-
bool SaveTrackedData();
113+
bool SaveObjDetectedData();
114114
// Add frame object detection data into protobuf message.
115115
void AddFrameDataToProto(libopenshotobjdetect::Frame* pbFrameData, CVDetectionData& dData);
116-
// Load protobuf file
117-
bool LoadTrackedData();
118116

119117
/// Get and Set JSON methods
120118
void SetJson(const std::string value); ///< Load JSON string into this object
121119
void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
120+
121+
// Load protobuf file (ONLY FOR MAKE TEST)
122+
bool _LoadObjDetectdData();
122123
};

include/CVStabilization.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class CVStabilization {
8484

8585
size_t start;
8686
size_t end;
87+
double avr_dx, avr_dy, avr_da, max_dx, max_dy, max_da;
8788

8889
cv::Mat last_T;
8990
cv::Mat prev_grey;
@@ -120,8 +121,6 @@ class CVStabilization {
120121
bool SaveStabilizedData();
121122
// Add frame stabilization data into protobuf message
122123
void AddFrameDataToProto(libopenshotstabilize::Frame* pbFrameData, CamTrajectory& trajData, TransformParam& transData, size_t frame_number);
123-
// Load protobuf data file
124-
bool LoadStabilizedData();
125124

126125
// Return requested struct info for a given frame
127126
TransformParam GetTransformParamData(size_t frameId);
@@ -131,6 +130,8 @@ class CVStabilization {
131130
void SetJson(const std::string value); ///< Load JSON string into this object
132131
void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
133132

133+
// Load protobuf data file (ONLY FOR MAKE TEST)
134+
bool _LoadStabilizedData();
134135
};
135136

136137
#endif

include/CVTracker.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ class CVTracker {
9898
bool SaveTrackedData();
9999
// Add frame tracked data into protobuf message.
100100
void AddFrameDataToProto(libopenshottracker::Frame* pbFrameData, FrameData& fData);
101-
// Load protobuf file
102-
bool LoadTrackedData();
103101

104102
/// Get and Set JSON methods
105103
void SetJson(const std::string value); ///< Load JSON string into this object
106104
void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
105+
106+
// Load protobuf file (ONLY FOR MAKE TEST)
107+
bool _LoadTrackedData();
107108
};
108109

109110

src/CVObjectDetection.cpp

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void CVObjectDetection::DetectObjects(const cv::Mat &frame, size_t frameId){
118118
// Runs the forward pass to get output of the output layers
119119
std::vector<cv::Mat> outs;
120120
net.forward(outs, getOutputsNames(net));
121-
121+
122122
// Remove the bounding boxes with low confidence
123123
postprocess(frame.size(), outs, frameId);
124124

@@ -239,8 +239,8 @@ void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector
239239
cv::Rect_<float> normalized_box;
240240
normalized_box.x = (box.x)/(float)frameDims.width;
241241
normalized_box.y = (box.y)/(float)frameDims.height;
242-
normalized_box.width = (box.x+box.width)/(float)frameDims.width;
243-
normalized_box.height = (box.y+box.height)/(float)frameDims.height;
242+
normalized_box.width = (box.width)/(float)frameDims.width;
243+
normalized_box.height = (box.height)/(float)frameDims.height;
244244
normalized_boxes.push_back(normalized_box);
245245
}
246246

@@ -300,7 +300,7 @@ CVDetectionData CVObjectDetection::GetDetectionData(size_t frameId){
300300
}
301301
}
302302

303-
bool CVObjectDetection::SaveTrackedData(){
303+
bool CVObjectDetection::SaveObjDetectedData(){
304304
// Create tracker message
305305
libopenshotobjdetect::ObjDetect objMessage;
306306

@@ -346,10 +346,10 @@ void CVObjectDetection::AddFrameDataToProto(libopenshotobjdetect::Frame* pbFrame
346346
libopenshotobjdetect::Frame_Box* box = pbFrameData->add_bounding_box();
347347

348348
// Save bounding box data
349-
box->set_x1(dData.boxes.at(i).x);
350-
box->set_y1(dData.boxes.at(i).y);
351-
box->set_x2(dData.boxes.at(i).x + dData.boxes.at(i).width);
352-
box->set_y2(dData.boxes.at(i).y + dData.boxes.at(i).height);
349+
box->set_x(dData.boxes.at(i).x);
350+
box->set_y(dData.boxes.at(i).y);
351+
box->set_w(dData.boxes.at(i).width);
352+
box->set_h(dData.boxes.at(i).height);
353353
box->set_classid(dData.classIds.at(i));
354354
box->set_confidence(dData.confidences.at(i));
355355

@@ -394,3 +394,77 @@ void CVObjectDetection::SetJsonValue(const Json::Value root) {
394394
classesFile = (root["classes_file"].asString());
395395
}
396396
}
397+
398+
399+
400+
/*
401+
||||||||||||||||||||||||||||||||||||||||||||||||||
402+
ONLY FOR MAKE TEST
403+
||||||||||||||||||||||||||||||||||||||||||||||||||
404+
*/
405+
406+
407+
408+
// Load protobuf data file
409+
bool CVObjectDetection::_LoadObjDetectdData(){
410+
// Create tracker message
411+
libopenshotobjdetect::ObjDetect objMessage;
412+
413+
{
414+
// Read the existing tracker message.
415+
fstream input(protobuf_data_path, ios::in | ios::binary);
416+
if (!objMessage.ParseFromIstream(&input)) {
417+
cerr << "Failed to parse protobuf message." << endl;
418+
return false;
419+
}
420+
}
421+
422+
// Make sure classNames and detectionsData are empty
423+
classNames.clear(); detectionsData.clear();
424+
425+
// Get all classes names and assign a color to them
426+
for(int i = 0; i < objMessage.classnames_size(); i++){
427+
classNames.push_back(objMessage.classnames(i));
428+
}
429+
430+
// Iterate over all frames of the saved message
431+
for (size_t i = 0; i < objMessage.frame_size(); i++) {
432+
// Create protobuf message reader
433+
const libopenshotobjdetect::Frame& pbFrameData = objMessage.frame(i);
434+
435+
// Get frame Id
436+
size_t id = pbFrameData.id();
437+
438+
// Load bounding box data
439+
const google::protobuf::RepeatedPtrField<libopenshotobjdetect::Frame_Box > &pBox = pbFrameData.bounding_box();
440+
441+
// Construct data vectors related to detections in the current frame
442+
std::vector<int> classIds; std::vector<float> confidences; std::vector<cv::Rect_<float>> boxes;
443+
444+
for(int i = 0; i < pbFrameData.bounding_box_size(); i++){
445+
// Get bounding box coordinates
446+
float x = pBox.Get(i).x(); float y = pBox.Get(i).y();
447+
float w = pBox.Get(i).w(); float h = pBox.Get(i).h();
448+
// Create OpenCV rectangle with the bouding box info
449+
cv::Rect_<float> box(x, y, w, h);
450+
451+
// Get class Id (which will be assign to a class name) and prediction confidence
452+
int classId = pBox.Get(i).classid(); float confidence = pBox.Get(i).confidence();
453+
454+
// Push back data into vectors
455+
boxes.push_back(box); classIds.push_back(classId); confidences.push_back(confidence);
456+
}
457+
458+
// Assign data to object detector map
459+
detectionsData[id] = CVDetectionData(classIds, confidences, boxes, id);
460+
}
461+
462+
// Show the time stamp from the last update in object detector data file
463+
if (objMessage.has_last_updated())
464+
cout << " Loaded Data. Saved Time Stamp: " << TimeUtil::ToString(objMessage.last_updated()) << endl;
465+
466+
// Delete all global objects allocated by libprotobuf.
467+
google::protobuf::ShutdownProtobufLibrary();
468+
469+
return true;
470+
}

0 commit comments

Comments
 (0)