Skip to content

Commit c7188cf

Browse files
committed
program w/o threading
1 parent 261a184 commit c7188cf

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

main2.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @file main.cpp
3+
* @brief opencv webcam stream and testing multithreading efficiency
4+
* @author Ari Nguyen
5+
*/
6+
#include <chrono>
7+
#include <iostream>
8+
#include <stdio.h>
9+
#include <string>
10+
#include <opencv2/opencv.hpp>
11+
12+
#include "src/utils.h"
13+
14+
int main(int argc, char *argv[]) {
15+
int numFrames = 100; // default
16+
int apiID = cv::CAP_ANY; // 0 = autodetect default API
17+
int device_id = 0;
18+
19+
cv::VideoCapture ws;
20+
FPS fps;
21+
cv::Mat frame;
22+
23+
// set numFrames;
24+
if (argc > 1) {
25+
try {
26+
numFrames = atoi(argv[1]);
27+
}
28+
catch (std::exception const & e) {
29+
std::cout<< "error: " << e.what() << std::endl;
30+
exit(1);
31+
}
32+
}
33+
34+
// start streaming video
35+
ws.open(device_id, apiID);
36+
if (!ws.isOpened()) {
37+
std::cerr << "(!)ERROR: Unable to open camera\n";
38+
exit(2);
39+
}
40+
41+
fps.start();
42+
while (fps.getNumFrames() < numFrames) {
43+
ws.read(frame);
44+
if (frame.empty()) { //check if succeeded
45+
std::cerr << "(!)Error2: Blank frame grabbed\n";
46+
exit(3);
47+
}
48+
49+
// show live and wait for a key with timeout long enough to show images
50+
cv::imshow("Live", frame);
51+
if (cv::waitKey(5) >= 0) {
52+
break;
53+
}
54+
fps.update();
55+
}
56+
57+
58+
// stop measuring fps
59+
fps.stop();
60+
61+
// display info
62+
std::cout << "[INFO] Total Frames: " << numFrames << std::endl;
63+
std::cout << "[INFO] Elasped time: " << fps.elapsed() << " seconds\n";
64+
std::cout << "[INFO] Approx. FPS: " << fps.fps() << std::endl;
65+
66+
// cleanup
67+
cv::destroyAllWindows();
68+
69+
exit(0);
70+
}

0 commit comments

Comments
 (0)