Skip to content

Commit 1c4a052

Browse files
committed
Moved frame capture from detectHumans to main to make it operate on single frame. Added method for loading network to memory
1 parent 84fb512 commit 1c4a052

File tree

3 files changed

+59
-43
lines changed

3 files changed

+59
-43
lines changed

app/main.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,44 @@ using std::endl;
1515

1616
int main() {
1717
cout << "Human Detector and Tracker" << endl;
18-
Robot bot(Eigen::Matrix4d::Random());
19-
cout << "Checking detectHuman functionality: "
20-
<< "\n" << bot.detectHumans() << endl;
18+
Eigen::Matrix4d test_matrix;
19+
test_matrix << 1, 2, 0, 0,
20+
4, 1, 0, 0,
21+
0, 0, 1, 100,
22+
0, 0, 0, 1;
23+
Robot bot(test_matrix);//Eigen::Matrix4d::Identity());
24+
// Load the network
25+
Net net = bot.loadNetwork("../network/yolov3.cfg",
26+
"../network/yolov3.weights");
27+
28+
// Start video capture by activating camera
29+
cv::VideoCapture cap(0);
30+
// Test if camera is actually active or not
31+
if (cap.isOpened() == false) {
32+
std::cout << "Cannot open the video camera" << std::endl;
33+
// Wait for any key press
34+
std::cin.get();
35+
return -1;
36+
}
37+
38+
// Create name for window frame
39+
static const string window_name = "Human Object Detector & Tracker";
40+
cv::namedWindow(window_name, cv::WINDOW_NORMAL);
41+
Mat frame;
42+
vector<Rect> positions;
43+
while (true) {
44+
cap >> frame;
45+
// const char* str = "../assets/test.jpeg"; // Lena image
46+
// Mat frame = cv::imread(str);
47+
bot.detectHumans(frame, net);
48+
// Show the frame captured on screen
49+
cv::imshow(window_name, frame);
50+
// To get continuous live video until ctrl+C is pressed
51+
if (cv::waitKey(1) == 27) {
52+
break;
53+
}
54+
}
55+
cap.release();
56+
cv::destroyAllWindows();
2157
return 0;
2258
}

include/Robot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class Robot {
4444
/**
4545
* @brief detectHumans : Main method for human detection
4646
*/
47-
int detectHumans();
47+
int detectHumans(Mat frame, Net net);
48+
Net loadNetwork(string model_config, string model_weights);
4849
// A constructor
4950
explicit Robot(Matrix4d transformation_matrix);
5051

src/Robot.cpp

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ Robot::Robot(Eigen::Matrix4d transformation_matrix) {
4343
* @param frame : Current Camera frame
4444
* @return Mat : processed camera frame, ready for detection
4545
*/
46+
Net Robot::loadNetwork(string model_config, string model_weights) {
47+
// Load the network
48+
Net net = cv::dnn::readNetFromDarknet(model_config,
49+
model_weights);
50+
// Define use of specific computation backend
51+
net.setPreferableBackend(cv::dnn::DNN_TARGET_CPU);
52+
return net;
53+
}
54+
4655
Mat Robot::prepFrame(Mat frame) {
4756
Mat blob_frame;
4857
blob_frame = cv::dnn::blobFromImage(frame, 1/255.0,
@@ -88,36 +97,15 @@ vector<double> Robot::transformToRobotFrame(vector<Rect> bbox_coords) {
8897
/**
8998
* @brief detectHumans : Main method for human detection
9099
*/
91-
int Robot::detectHumans() {
100+
int Robot::detectHumans(Mat frame, Net net) {
92101
HumanDetector hooman;
93-
// Load the network
94-
Net net = cv::dnn::readNetFromDarknet(path_to_model_congfiguration,
95-
path_to_model_weights);
96-
// Define use of specific computation backend
97-
net.setPreferableBackend(cv::dnn::DNN_TARGET_CPU);
98-
// Start video capture by activating camera
99-
cv::VideoCapture cap(0);
100-
// Test if camera is actually active or not
101-
if (cap.isOpened() == false) {
102-
std::cout << "Cannot open the video camera" << std::endl;
103-
// Wait for any key press
104-
std::cin.get();
105-
return -1;
106-
}
107-
// Create name for window frame
108-
static const string window_name = "Human Object Detector & Tracker";
109-
cv::namedWindow(window_name, cv::WINDOW_NORMAL);
110-
// Initialize the frame that will be analysed
111-
Mat frame, blob;
112-
// Initialize variable that stores data recieved from detection model
113-
vector<Mat> outs;
114102
vector<Rect> bbox;
115-
// Create a loop for capturing frames in real time
116-
while (true) {
117-
// Take one frame from live feed for processing
118-
cap >> frame;
119-
// Create a pre-processed frame for detection
120-
blob = prepFrame(frame);
103+
vector<Mat> outs;
104+
vector<Rect> human_locations;
105+
bbox.clear();
106+
outs.clear();
107+
human_locations.clear();
108+
Mat blob = prepFrame(frame);
121109
// Run the detection model and get the data for detected humans
122110
outs = hooman.detection(net, blob);
123111
// Apply confidence and NMS thresholding
@@ -126,15 +114,6 @@ int Robot::detectHumans() {
126114
// Testing out transformation matrix
127115
vector<Rect> dummy = {Rect(10, 20, 30, 1)};
128116
transformToRobotFrame(dummy);
129-
// Show the frame captured on screen
130-
cv::imshow(window_name, frame);
131-
// To get continuous live video until ctrl+C is pressed
132-
if (cv::waitKey(1) == 27) {
133-
break;
134-
}
135-
}
136-
// Deactivate camera and close window
137-
cap.release();
138-
cv::destroyAllWindows();
117+
139118
return 0;
140-
}
119+
}

0 commit comments

Comments
 (0)