-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
161 lines (144 loc) · 5.93 KB
/
Copy pathcamera.cpp
File metadata and controls
161 lines (144 loc) · 5.93 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp> // cv::Canny()
#include <iostream>
using namespace cv;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
int main(int, char**)
{
Mat frame;
cout << "Opening camera..." << endl;
// GStreamer string, Viewed through the "gst-inspect-1.0 v4l2src" command, my device don't support video/x-jpeg
// use vaapi dec, need gpu support, install gstreamer1.0-vaapi
// std::string gst_pipeline = "v4l2src device=/dev/video0 ! image/jpeg, width=1920, height=1080, framerate=30/1 ! vaapijpegdec ! videoconvert ! appsink";
// next incorrect at sw831 !?
// std::string gst_pipeline = "v4l2src device=/dev/video0 ! image/jpeg, width=1920, height=1080, framerate=30/1 ! decodebin ! videoconvert n-threads=8 ! appsink sync=false";
// or soft jpeg dec
std::string gst_pipeline =
"v4l2src device=/dev/video0 do-timestamp=true ! image/jpeg, width=1920, height=1080, framerate=30/1 ! jpegdec ! videoconvert n-threads=8 ! video/x-raw, format=BGR ! queue leaky=downstream ! appsink max-buffers=30 sync=false";
// or local file ?
// std::string gst_pipeline = "/home/lixc/192.mp4";
VideoCapture capture;
// v4l2
// capture.open(0, CAP_V4L2); // open the first camera
// GStreamer
// use vaapi dec, need opencv >= 4.5.2
//capture.open(gst_pipeline, CAP_GSTREAMER, {CAP_PROP_HW_ACCELERATION, VIDEO_ACCELERATION_VAAPI});
// try all acceleration
// capture.open(gst_pipeline, CAP_GSTREAMER, {CAP_PROP_HW_ACCELERATION, VIDEO_ACCELERATION_ANY});
capture.open(gst_pipeline, CAP_GSTREAMER);
if (!capture.isOpened())
{
cerr << "ERROR: Can't initialize camera capture" << endl;
return 1;
}
// FFmpeg
// capture.open("/home/lixc/192.mp4", CAP_FFMPEG);
// if (capture.isOpened())
//{
// capture.set(CAP_PROP_HW_ACCELERATION, VIDEO_ACCELERATION_ANY);
//}
//else
// {
// cerr << "ERROR: Can't initialize camera capture" << endl;
// return 1;
// }
// incorrect usage!
// capture.open(0, CAP_FFMPEG);
// capture.open("/dev/video0", CAP_FFMPEG);
// capture.open(gst_pipeline, CAP_FFMPEG);
// std::string video_file = "v4l2src device=/dev/video0 ! image/jpeg, width=1920, height=1080, framerate=30/1 ! vaapijpegdec ! videoconvert ! filesink";
// capture.open(video_file, CAP_FFMPEG);
// FFmpeg end
// for v4l2
// bool fourcc_set = capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'));
// #ifndef NDEBUG
// cout << "fourcc set: " << fourcc_set << endl;
// #endif
//
// capture.set(CAP_PROP_FRAME_WIDTH, 1920);
// capture.set(CAP_PROP_FRAME_HEIGHT, 1080);
// capture.set(CAP_PROP_FPS, 30);
// v4l2 end
int frame_width = static_cast<int>(capture.get(CAP_PROP_FRAME_WIDTH));
int frame_height = static_cast<int>(capture.get(CAP_PROP_FRAME_HEIGHT));
double fps = capture.get(CAP_PROP_FPS);
cout << "Frame width: " << frame_width << endl;
cout << " height: " << frame_height << endl;
cout << "Capturing FPS: " << fps << endl;
cout << endl << "Press 'ESC' to quit, 'space' to toggle frame processing" << endl;
cout << endl << "Start grabbing..." << endl;
size_t nFrames = 0;
bool enableProcessing = false;
int64 t0 = cv::getTickCount();
int64 processingTime = 0;
double avg_fps = 0.0;
char fps_text[32];
char res_text[32];
double avg_time_per_frame = 0.0;
double avg_process_time = 0.0;
const string window_name("camera_window");
namedWindow(window_name, WINDOW_OPENGL | WINDOW_AUTOSIZE);
setOpenGlContext(window_name);
for (;;)
{
capture >> frame; // read the next frame from camera
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
nFrames++;
if (nFrames % 10 == 0)
{
constexpr int N = 10;
int64 t1 = cv::getTickCount();
avg_fps = static_cast<double>(getTickFrequency()) * N / (static_cast<double>(t1 - t0));
#ifndef NDEBUG
avg_time_per_frame = static_cast<double>(t1 - t0) * 1000.0f / (N * getTickFrequency());
avg_process_time = static_cast<double>(processingTime) * 1000.0f / (N * getTickFrequency());
cout << "Frames captured: " << cv::format("%5lld", (long long int)nFrames)
<< " Average FPS: " << cv::format("%9.1f", avg_fps)
<< " Average time per frame: " << cv::format("%9.2f ms", avg_time_per_frame)
<< " Average processing time: " << cv::format("%9.2f ms", avg_process_time)
<< std::endl;
#endif
t0 = t1;
processingTime = 0;
}
if (!enableProcessing)
{
sprintf(fps_text, "FPS: %.2f", avg_fps);
sprintf(res_text, "Resolution:%d*%d", frame_width, frame_height);
cv::putText(frame, fps_text, cv::Point(10, 30),
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 255, 0), 2);
cv::putText(frame, res_text, cv::Point(10, 70),
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 255, 0), 2);
imshow("Frame", frame);
}
else
{
int64 tp0 = cv::getTickCount();
Mat processed;
cv::Canny(frame, processed, 400, 1000, 5);
processingTime += cv::getTickCount() - tp0;
imshow("Frame", processed);
}
// for local file
// int key = waitKey(30);
int key = waitKey(1);
if (key == 27/*ESC*/)
break;
if (key == 32/*SPACE*/)
{
enableProcessing = !enableProcessing;
cout << "Enable frame processing ('space' key): " << enableProcessing << endl;
}
}
std::cout << "Number of captured frames: " << nFrames << endl;
return nFrames > 0 ? 0 : 1;
}