-
Notifications
You must be signed in to change notification settings - Fork 2
/
bounding_box.cpp
100 lines (77 loc) · 2.5 KB
/
bounding_box.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
/*
file: bounding_box.cpp
author: Ali Mirza
This script is part of post-processing module.
Its responsible for drawing bounding boxes around predictions made by CNN
*/
#include <iostream>
#include <opencv2/opencv.hpp>
// max number of predictions that need to be classified
#define MAX_PREDICTIONS 1
/* data model for bounding boxes */
typedef struct BoundingBox{
float x;
float y;
float width;
float height;
} BoundingBox;
/* data model for predictions */
typedef struct Prediction{
char* label;
float confidence;
BoundingBox bounds;
} Prediction;
/*
draws rectangle around images identified inside camera frame
*/
void draw_bounds(cv::Mat &cameraFrame, std::vector<Prediction> predictions) {
std::vector<cv::Rect> objects;
// create CvRect for all predicitons
for (size_t i = 0; i < predictions.size(); i++) {
objects.push_back(CvRect::CvRect(predictions[i].bounds.x,
predictions[i].bounds.y,
predictions[i].bounds.width,
predictions[i].bounds.height));
}
// draw rectangle on camera frame
for (size_t i = 0; i < objects.size(); i++)
cv::rectangle(cameraFrame, objects[i], cv::Scalar(255, 0, 0), 5);
}
/*
get_predictions populates predictions array with objects identified inside image
note: currently supports mock predictions for testing only
*/
void get_predictions(std::vector<Prediction> &predictions) {
int i;
struct Prediction prediction;
strcpy(prediction.label, "Person");
prediction.confidence = 50.5;
prediction.bounds.x = 50 + prediction.bounds.x;
prediction.bounds.y = 50;
prediction.bounds.width = 50;
prediction.bounds.height = 50;
for (i = 0; i < MAX_PREDICTIONS; i++) {
predictions.push_back(prediction);
}
}
int main(void) {
// prediction objects array
std::vector<Prediction> predictionObjects;
// capture stream from camera
cv::VideoCapture capture(0);
// check if camera stream is available
if(!capture.isOpened()) {
std::cout << "Failed to open camera stream" << std::endl;
return -1;
}
while (true) {
cv::Mat cameraFrame;
capture.read(cameraFrame);
get_predictions(predictionObjects);
draw_bounds(cameraFrame,predictionObjects);
cv::imshow("output", cameraFrame);
if (cv::waitKey(50) >= 0)
break;
}
return 0;
}