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.
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.
8 // Usage: opencv_test_videoio --gtest_also_run_disabled_tests --gtest_filter=*videoio_camera*<tested case>*
10 #include "test_precomp.hpp"
11 #include <opencv2/core/utils/configuration.private.hpp>
13 namespace opencv_test { namespace {
15 static void test_readFrames(/*const*/ VideoCapture& capture, const int N = 100, Mat* lastFrame = NULL)
18 int64 time0 = cv::getTickCount();
19 for (int i = 0; i < N; i++)
21 SCOPED_TRACE(cv::format("frame=%d", i));
24 ASSERT_FALSE(frame.empty());
26 EXPECT_GT(cvtest::norm(frame, NORM_INF), 0) << "Complete black image has been received";
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();
33 TEST(DISABLED_videoio_camera, basic)
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);
45 TEST(DISABLED_videoio_camera, v4l_read_mjpg)
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);
60 //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
61 TEST(DISABLED_videoio_camera, channel6)
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);
74 TEST(DISABLED_videoio_camera, v4l_read_framesize)
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);
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;
92 test_readFrames(capture, 30, &frame640x480);
93 EXPECT_EQ(640, frame640x480.cols);
94 EXPECT_EQ(480, frame640x480.rows);
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;
102 test_readFrames(capture, 30, &frame1280x720);
103 EXPECT_EQ(1280, frame1280x720.cols);
104 EXPECT_EQ(720, frame1280x720.rows);
111 utils::Paths getTestCameras()
113 static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
117 TEST(DISABLED_videoio_camera, waitAny_V4L)
119 auto cameraNames = getTestCameras();
120 if (cameraNames.empty())
121 throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
123 const int totalFrames = 50; // number of expected frames (summary for all cameras)
124 const int64 timeoutNS = 100 * 1000000;
126 const Size frameSize(640, 480);
127 const int fpsDefaultEven = 30;
128 const int fpsDefaultOdd = 15;
130 std::vector<VideoCapture> cameras;
131 for (size_t i = 0; i < cameraNames.size(); ++i)
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;
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);
149 std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
152 std::vector<int> cameraReady;
155 EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
156 EXPECT_FALSE(cameraReady.empty());
157 for (int idx : cameraReady)
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];
164 ASSERT_TRUE(c.retrieve(frame)) << idx;
166 ASSERT_TRUE(c.read(frame)) << idx;
168 EXPECT_EQ(frameSize.width, frame.cols) << idx;
169 EXPECT_EQ(frameSize.height, frame.rows) << idx;
171 ++frameFromCamera[idx];
175 while(counter < totalFrames);
178 for (size_t i = 0; i < cameraNames.size(); ++i)
180 EXPECT_GT(frameFromCamera[i], (size_t)0) << i;