623ce29f708d8bd1d7b74d3cfa11c1746e702185
[platform/upstream/opencv.git] / modules / videoio / test / test_camera.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 // Note: all tests here are DISABLED by default due specific requirements.
6 // Don't use #if 0 - these tests should be tested for compilation at least.
7 //
8 // Usage: opencv_test_videoio --gtest_also_run_disabled_tests --gtest_filter=*videoio_camera*<tested case>*
9
10 #include "test_precomp.hpp"
11 #include <opencv2/core/utils/configuration.private.hpp>
12
13 namespace opencv_test { namespace {
14
15 static void test_readFrames(/*const*/ VideoCapture& capture, const int N = 100, Mat* lastFrame = NULL)
16 {
17     Mat frame;
18     int64 time0 = cv::getTickCount();
19     for (int i = 0; i < N; i++)
20     {
21         SCOPED_TRACE(cv::format("frame=%d", i));
22
23         capture >> frame;
24         ASSERT_FALSE(frame.empty());
25
26         EXPECT_GT(cvtest::norm(frame, NORM_INF), 0) << "Complete black image has been received";
27     }
28     int64 time1 = cv::getTickCount();
29     printf("Processed %d frames on %.2f FPS\n", N, (N * cv::getTickFrequency()) / (time1 - time0 + 1));
30     if (lastFrame) *lastFrame = frame.clone();
31 }
32
33 TEST(DISABLED_videoio_camera, basic)
34 {
35     VideoCapture capture(0);
36     ASSERT_TRUE(capture.isOpened());
37     std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
38     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
39     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
40     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
41     test_readFrames(capture);
42     capture.release();
43 }
44
45 TEST(DISABLED_videoio_camera, v4l_read_mjpg)
46 {
47     VideoCapture capture(CAP_V4L2);
48     ASSERT_TRUE(capture.isOpened());
49     ASSERT_TRUE(capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')));
50     std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
51     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
52     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
53     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
54     int fourcc = (int)capture.get(CAP_PROP_FOURCC);
55     std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
56     test_readFrames(capture);
57     capture.release();
58 }
59
60 //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
61 TEST(DISABLED_videoio_camera, channel6)
62 {
63     VideoCapture capture(0);
64     ASSERT_TRUE(capture.isOpened());
65     capture.set(CAP_PROP_CHANNEL, 6);
66     std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
67     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
68     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
69     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
70     test_readFrames(capture);
71     capture.release();
72 }
73
74 TEST(DISABLED_videoio_camera, v4l_read_framesize)
75 {
76     VideoCapture capture(CAP_V4L2);
77     ASSERT_TRUE(capture.isOpened());
78     std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
79     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
80     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
81     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
82     int fourcc = (int)capture.get(CAP_PROP_FOURCC);
83     std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
84     test_readFrames(capture, 30);
85
86     EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 640));
87     EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 480));
88     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
89     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
90     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
91     Mat frame640x480;
92     test_readFrames(capture, 30, &frame640x480);
93     EXPECT_EQ(640, frame640x480.cols);
94     EXPECT_EQ(480, frame640x480.rows);
95
96     EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 1280));
97     EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 720));
98     std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
99     std::cout << "     height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
100     std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
101     Mat frame1280x720;
102     test_readFrames(capture, 30, &frame1280x720);
103     EXPECT_EQ(1280, frame1280x720.cols);
104     EXPECT_EQ(720, frame1280x720.rows);
105
106     capture.release();
107 }
108
109
110 static
111 utils::Paths getTestCameras()
112 {
113     static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
114     return cameras;
115 }
116
117 TEST(DISABLED_videoio_camera, waitAny_V4L)
118 {
119     auto cameraNames = getTestCameras();
120     if (cameraNames.empty())
121        throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
122
123     const int totalFrames = 50; // number of expected frames (summary for all cameras)
124     const int64 timeoutNS = 100 * 1000000;
125
126     const Size frameSize(640, 480);
127     const int fpsDefaultEven = 30;
128     const int fpsDefaultOdd = 15;
129
130     std::vector<VideoCapture> cameras;
131     for (size_t i = 0; i < cameraNames.size(); ++i)
132     {
133         const auto& name = cameraNames[i];
134         int fps = (int)utils::getConfigurationParameterSizeT(cv::format("OPENCV_TEST_CAMERA%d_FPS", (int)i).c_str(), (i & 1) ? fpsDefaultOdd : fpsDefaultEven);
135         std::cout << "Camera[" << i << "] = '" << name << "', fps=" << fps << std::endl;
136         VideoCapture cap(name, CAP_V4L);
137         ASSERT_TRUE(cap.isOpened()) << name;
138         EXPECT_TRUE(cap.set(CAP_PROP_FRAME_WIDTH, frameSize.width)) << name;
139         EXPECT_TRUE(cap.set(CAP_PROP_FRAME_HEIGHT, frameSize.height)) << name;
140         EXPECT_TRUE(cap.set(CAP_PROP_FPS, fps)) << name;
141         //launch cameras
142         Mat firstFrame;
143         EXPECT_TRUE(cap.read(firstFrame));
144         EXPECT_EQ(frameSize.width, firstFrame.cols);
145         EXPECT_EQ(frameSize.height, firstFrame.rows);
146         cameras.push_back(cap);
147     }
148
149     std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
150     {
151         int counter = 0;
152         std::vector<int> cameraReady;
153         do
154         {
155             EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
156             EXPECT_FALSE(cameraReady.empty());
157             for (int idx : cameraReady)
158             {
159                 //std::cout << "Reading frame from camera: " << idx << std::endl;
160                 ASSERT_TRUE(idx >= 0 && (size_t)idx < cameras.size()) << idx;
161                 VideoCapture& c = cameras[idx];
162                 Mat frame;
163 #if 1
164                 ASSERT_TRUE(c.retrieve(frame)) << idx;
165 #else
166                 ASSERT_TRUE(c.read(frame)) << idx;
167 #endif
168                 EXPECT_EQ(frameSize.width, frame.cols) << idx;
169                 EXPECT_EQ(frameSize.height, frame.rows) << idx;
170
171                 ++frameFromCamera[idx];
172                 ++counter;
173             }
174         }
175         while(counter < totalFrames);
176     }
177
178     for (size_t i = 0; i < cameraNames.size(); ++i)
179     {
180         EXPECT_GT(frameFromCamera[i], (size_t)0) << i;
181     }
182 }
183
184 }} // namespace