forked from mikerr/piVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamtest.cpp
58 lines (47 loc) · 1.35 KB
/
camtest.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
/* Compile with
gcc -std=c++0 -lopencv_core -lopencv_highgui
-L/usr/lib/uv4l/uv4lext/armv6l -luv4lext -Wl,-rpath,'/usr/lib/uv4l/uv4lext/armv6l'
test.cpp -o test
*/
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;
using namespace cv;
int main(int argc, char** argv){
VideoCapture cap(-1);
if (!cap.isOpened())
{
cout << "Cannot open camera" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
namedWindow("Output",CV_WINDOW_AUTOSIZE);
unsigned long f = 0;
auto start = monotonic_clock::now();
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "Cannot read a frame from camera" << endl;
break;
}
auto elapse = duration_cast<seconds>(monotonic_clock::now() - start).count();
++f;
auto fps = elapse ? f / elapse : 0;
if (!(elapse % 5) && fps) // print the framerate every 5s, for 1s
cout << "fps: " << fps << ", total frames: " << f
<< " elapsed time: " << elapse << "s\n";
imshow("Output", frame);
if (waitKey(30) == 27)
{
cout << "Exit, fps: " << fps << endl;
break;
}
}
return 0;
}