test_camera.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Note: all tests here are DISABLED by default due specific requirements.
  5. // Don't use #if 0 - these tests should be tested for compilation at least.
  6. //
  7. // Usage: opencv_test_videoio --gtest_also_run_disabled_tests --gtest_filter=*videoio_camera*<tested case>*
  8. #include "test_precomp.hpp"
  9. #include <opencv2/core/utils/configuration.private.hpp>
  10. namespace opencv_test { namespace {
  11. static void test_readFrames(/*const*/ VideoCapture& capture, const int N = 100, Mat* lastFrame = NULL, bool testTimestamps = true)
  12. {
  13. Mat frame;
  14. int64 time0 = cv::getTickCount();
  15. int64 sysTimePrev = time0;
  16. const double cvTickFreq = cv::getTickFrequency();
  17. double camTimePrev = 0.0;
  18. const double fps = capture.get(cv::CAP_PROP_FPS);
  19. const double framePeriod = fps == 0.0 ? 1. : 1.0 / fps;
  20. const bool validTickAndFps = cvTickFreq != 0 && fps != 0.;
  21. testTimestamps &= validTickAndFps;
  22. double frame0ts = 0;
  23. for (int i = 0; i < N; i++)
  24. {
  25. SCOPED_TRACE(cv::format("frame=%d", i));
  26. capture >> frame;
  27. ASSERT_FALSE(frame.empty());
  28. const int64 sysTimeCurr = cv::getTickCount();
  29. double camTimeCurr = capture.get(cv::CAP_PROP_POS_MSEC);
  30. if (i == 0)
  31. frame0ts = camTimeCurr;
  32. camTimeCurr -= frame0ts; // normalized timestamp based on the first frame
  33. if (cvtest::debugLevel > 0)
  34. {
  35. std::cout << i << ": " << camTimeCurr << std::endl;
  36. }
  37. // Do we have a previous frame?
  38. if (i > 0 && testTimestamps)
  39. {
  40. const double sysTimeElapsedSecs = (sysTimeCurr - sysTimePrev) / cvTickFreq;
  41. const double camTimeElapsedSecs = (camTimeCurr - camTimePrev) / 1000.;
  42. // Check that the time between two camera frames and two system time calls
  43. // are within 1.5 frame periods of one another.
  44. //
  45. // 1.5x is chosen to accomodate for a dropped frame, and an additional 50%
  46. // to account for drift in the scale of the camera and system time domains.
  47. EXPECT_NEAR(sysTimeElapsedSecs, camTimeElapsedSecs, framePeriod * 1.5);
  48. }
  49. EXPECT_GT(cvtest::norm(frame, NORM_INF), 0) << "Complete black image has been received";
  50. sysTimePrev = sysTimeCurr;
  51. camTimePrev = camTimeCurr;
  52. }
  53. int64 time1 = cv::getTickCount();
  54. printf("Processed %d frames on %.2f FPS\n", N, (N * cvTickFreq) / (time1 - time0 + 1));
  55. if (lastFrame) *lastFrame = frame.clone();
  56. }
  57. TEST(DISABLED_videoio_camera, basic)
  58. {
  59. VideoCapture capture(0);
  60. ASSERT_TRUE(capture.isOpened());
  61. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  62. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  63. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  64. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  65. test_readFrames(capture);
  66. capture.release();
  67. }
  68. // Test that CAP_PROP_CONVERT_RGB remain to false (default is true) after other supported property are set.
  69. // The test use odd value to be almost sure to trigger code responsible for recreating the device.
  70. TEST(DISABLED_videoio_camera, dshow_convert_rgb_persistency)
  71. {
  72. VideoCapture capture(CAP_DSHOW);
  73. ASSERT_TRUE(capture.isOpened());
  74. ASSERT_TRUE(capture.set(CAP_PROP_CONVERT_RGB, 0));
  75. ASSERT_DOUBLE_EQ(capture.get(CAP_PROP_CONVERT_RGB), 0);
  76. capture.set(CAP_PROP_FRAME_WIDTH, 641);
  77. capture.set(CAP_PROP_FRAME_HEIGHT, 481);
  78. capture.set(CAP_PROP_FPS, 31);
  79. capture.set(CAP_PROP_CHANNEL, 1);
  80. capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('Y', '1', '6', ' '));
  81. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  82. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  83. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  84. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  85. ASSERT_DOUBLE_EQ(capture.get(CAP_PROP_CONVERT_RGB), 0);
  86. capture.release();
  87. }
  88. TEST(DISABLED_videoio_camera, v4l_read_mjpg)
  89. {
  90. VideoCapture capture(CAP_V4L2);
  91. ASSERT_TRUE(capture.isOpened());
  92. ASSERT_TRUE(capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')));
  93. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  94. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  95. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  96. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  97. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  98. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  99. test_readFrames(capture);
  100. capture.release();
  101. }
  102. TEST(DISABLED_videoio_camera, v4l_open_mjpg)
  103. {
  104. VideoCapture capture;
  105. capture.open(0, CAP_V4L2, {
  106. CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')
  107. });
  108. ASSERT_TRUE(capture.isOpened());
  109. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  110. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  111. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  112. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  113. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  114. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  115. test_readFrames(capture);
  116. capture.release();
  117. }
  118. TEST(DISABLED_videoio_camera, v4l_open_mjpg_1280x720)
  119. {
  120. VideoCapture capture(0, CAP_V4L2, {
  121. CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'),
  122. CAP_PROP_FRAME_WIDTH, 1280,
  123. CAP_PROP_FRAME_HEIGHT, 720,
  124. });
  125. ASSERT_TRUE(capture.isOpened());
  126. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  127. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  128. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  129. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  130. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  131. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  132. test_readFrames(capture);
  133. capture.release();
  134. }
  135. //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
  136. TEST(DISABLED_videoio_camera, channel6)
  137. {
  138. VideoCapture capture(0);
  139. ASSERT_TRUE(capture.isOpened());
  140. capture.set(CAP_PROP_CHANNEL, 6);
  141. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  142. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  143. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  144. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  145. test_readFrames(capture);
  146. capture.release();
  147. }
  148. TEST(DISABLED_videoio_camera, v4l_read_framesize)
  149. {
  150. VideoCapture capture(CAP_V4L2);
  151. ASSERT_TRUE(capture.isOpened());
  152. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  153. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  154. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  155. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  156. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  157. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  158. test_readFrames(capture, 30);
  159. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 640));
  160. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 480));
  161. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  162. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  163. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  164. Mat frame640x480;
  165. test_readFrames(capture, 30, &frame640x480);
  166. EXPECT_EQ(640, frame640x480.cols);
  167. EXPECT_EQ(480, frame640x480.rows);
  168. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 1280));
  169. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 720));
  170. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  171. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  172. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  173. Mat frame1280x720;
  174. test_readFrames(capture, 30, &frame1280x720);
  175. EXPECT_EQ(1280, frame1280x720.cols);
  176. EXPECT_EQ(720, frame1280x720.rows);
  177. capture.release();
  178. }
  179. static
  180. utils::Paths getTestCameras()
  181. {
  182. static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
  183. return cameras;
  184. }
  185. TEST(DISABLED_videoio_camera, waitAny_V4L)
  186. {
  187. auto cameraNames = getTestCameras();
  188. if (cameraNames.empty())
  189. throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
  190. const int totalFrames = 50; // number of expected frames (summary for all cameras)
  191. const int64 timeoutNS = 100 * 1000000;
  192. const Size frameSize(640, 480);
  193. const int fpsDefaultEven = 30;
  194. const int fpsDefaultOdd = 15;
  195. std::vector<VideoCapture> cameras;
  196. for (size_t i = 0; i < cameraNames.size(); ++i)
  197. {
  198. const auto& name = cameraNames[i];
  199. int fps = (int)utils::getConfigurationParameterSizeT(cv::format("OPENCV_TEST_CAMERA%d_FPS", (int)i).c_str(), (i & 1) ? fpsDefaultOdd : fpsDefaultEven);
  200. std::cout << "Camera[" << i << "] = '" << name << "', fps=" << fps << std::endl;
  201. VideoCapture cap(name, CAP_V4L);
  202. ASSERT_TRUE(cap.isOpened()) << name;
  203. EXPECT_TRUE(cap.set(CAP_PROP_FRAME_WIDTH, frameSize.width)) << name;
  204. EXPECT_TRUE(cap.set(CAP_PROP_FRAME_HEIGHT, frameSize.height)) << name;
  205. EXPECT_TRUE(cap.set(CAP_PROP_FPS, fps)) << name;
  206. //launch cameras
  207. Mat firstFrame;
  208. EXPECT_TRUE(cap.read(firstFrame));
  209. EXPECT_EQ(frameSize.width, firstFrame.cols);
  210. EXPECT_EQ(frameSize.height, firstFrame.rows);
  211. cameras.push_back(cap);
  212. }
  213. std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
  214. {
  215. int counter = 0;
  216. std::vector<int> cameraReady;
  217. do
  218. {
  219. EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
  220. EXPECT_FALSE(cameraReady.empty());
  221. for (int idx : cameraReady)
  222. {
  223. //std::cout << "Reading frame from camera: " << idx << std::endl;
  224. ASSERT_TRUE(idx >= 0 && (size_t)idx < cameras.size()) << idx;
  225. VideoCapture& c = cameras[idx];
  226. Mat frame;
  227. #if 1
  228. ASSERT_TRUE(c.retrieve(frame)) << idx;
  229. #else
  230. ASSERT_TRUE(c.read(frame)) << idx;
  231. #endif
  232. EXPECT_EQ(frameSize.width, frame.cols) << idx;
  233. EXPECT_EQ(frameSize.height, frame.rows) << idx;
  234. ++frameFromCamera[idx];
  235. ++counter;
  236. }
  237. }
  238. while(counter < totalFrames);
  239. }
  240. for (size_t i = 0; i < cameraNames.size(); ++i)
  241. {
  242. EXPECT_GT(frameFromCamera[i], (size_t)0) << i;
  243. }
  244. }
  245. }} // namespace