test_camera.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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, msmf_read_yuyv)
  103. {
  104. VideoCapture capture(CAP_MSMF);
  105. ASSERT_TRUE(capture.isOpened());
  106. ASSERT_TRUE(capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('Y', 'U', 'Y', 'V')));
  107. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  108. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  109. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  110. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  111. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  112. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  113. cv::Mat frame;
  114. for (int i = 0; i < 10; i++)
  115. {
  116. capture >> frame;
  117. EXPECT_EQ(2, frame.channels());
  118. }
  119. capture.release();
  120. }
  121. TEST(DISABLED_videoio_camera, v4l_open_mjpg)
  122. {
  123. VideoCapture capture;
  124. capture.open(0, CAP_V4L2, {
  125. CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')
  126. });
  127. ASSERT_TRUE(capture.isOpened());
  128. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  129. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  130. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  131. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  132. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  133. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  134. test_readFrames(capture);
  135. capture.release();
  136. }
  137. TEST(DISABLED_videoio_camera, v4l_open_mjpg_1280x720)
  138. {
  139. VideoCapture capture(0, CAP_V4L2, {
  140. CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'),
  141. CAP_PROP_FRAME_WIDTH, 1280,
  142. CAP_PROP_FRAME_HEIGHT, 720,
  143. });
  144. ASSERT_TRUE(capture.isOpened());
  145. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  146. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  147. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  148. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  149. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  150. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  151. test_readFrames(capture);
  152. capture.release();
  153. }
  154. //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
  155. TEST(DISABLED_videoio_camera, channel6)
  156. {
  157. VideoCapture capture(0);
  158. ASSERT_TRUE(capture.isOpened());
  159. capture.set(CAP_PROP_CHANNEL, 6);
  160. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  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. test_readFrames(capture);
  165. capture.release();
  166. }
  167. TEST(DISABLED_videoio_camera, v4l_read_framesize)
  168. {
  169. VideoCapture capture(CAP_V4L2);
  170. ASSERT_TRUE(capture.isOpened());
  171. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  172. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  173. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  174. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  175. int fourcc = (int)capture.get(CAP_PROP_FOURCC);
  176. std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
  177. test_readFrames(capture, 30);
  178. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 640));
  179. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 480));
  180. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  181. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  182. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  183. Mat frame640x480;
  184. test_readFrames(capture, 30, &frame640x480);
  185. EXPECT_EQ(640, frame640x480.cols);
  186. EXPECT_EQ(480, frame640x480.rows);
  187. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 1280));
  188. EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 720));
  189. std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  190. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  191. std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
  192. Mat frame1280x720;
  193. test_readFrames(capture, 30, &frame1280x720);
  194. EXPECT_EQ(1280, frame1280x720.cols);
  195. EXPECT_EQ(720, frame1280x720.rows);
  196. capture.release();
  197. }
  198. TEST(DISABLED_videoio_camera, v4l_rgb_convert)
  199. {
  200. VideoCapture capture(CAP_V4L2);
  201. ASSERT_TRUE(capture.isOpened());
  202. std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
  203. std::cout << " Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
  204. std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
  205. std::cout << "Pixel format: " << capture.get(cv::CAP_PROP_FORMAT) << std::endl;
  206. if (capture.get(CAP_PROP_FOURCC) != VideoWriter::fourcc('Y', 'U', 'Y', 'V'))
  207. {
  208. throw SkipTestException("Camera does not support YUYV format");
  209. }
  210. capture.set(cv::CAP_PROP_CONVERT_RGB, 0);
  211. std::cout << "New pixel format: " << capture.get(cv::CAP_PROP_FORMAT) << std::endl;
  212. cv::Mat frame;
  213. for (int i = 0; i < 10; i++)
  214. {
  215. int pixel_type = (int)capture.get(cv::CAP_PROP_FORMAT);
  216. int channels = CV_MAT_CN(pixel_type);
  217. int pixel_bytes = CV_ELEM_SIZE(pixel_type);
  218. // YUYV is expected for most of popular USB cam (COLOR_YUV2BGR_YUYV conversion)
  219. EXPECT_EQ(2, channels);
  220. EXPECT_EQ(2, pixel_bytes);
  221. capture >> frame;
  222. }
  223. }
  224. static
  225. utils::Paths getTestCameras()
  226. {
  227. static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
  228. return cameras;
  229. }
  230. TEST(DISABLED_videoio_camera, waitAny_V4L)
  231. {
  232. auto cameraNames = getTestCameras();
  233. if (cameraNames.empty())
  234. throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
  235. const int totalFrames = 50; // number of expected frames (summary for all cameras)
  236. const int64 timeoutNS = 100 * 1000000;
  237. const Size frameSize(640, 480);
  238. const int fpsDefaultEven = 30;
  239. const int fpsDefaultOdd = 15;
  240. std::vector<VideoCapture> cameras;
  241. for (size_t i = 0; i < cameraNames.size(); ++i)
  242. {
  243. const auto& name = cameraNames[i];
  244. int fps = (int)utils::getConfigurationParameterSizeT(cv::format("OPENCV_TEST_CAMERA%d_FPS", (int)i).c_str(), (i & 1) ? fpsDefaultOdd : fpsDefaultEven);
  245. std::cout << "Camera[" << i << "] = '" << name << "', fps=" << fps << std::endl;
  246. VideoCapture cap(name, CAP_V4L);
  247. ASSERT_TRUE(cap.isOpened()) << name;
  248. EXPECT_TRUE(cap.set(CAP_PROP_FRAME_WIDTH, frameSize.width)) << name;
  249. EXPECT_TRUE(cap.set(CAP_PROP_FRAME_HEIGHT, frameSize.height)) << name;
  250. EXPECT_TRUE(cap.set(CAP_PROP_FPS, fps)) << name;
  251. //launch cameras
  252. Mat firstFrame;
  253. EXPECT_TRUE(cap.read(firstFrame));
  254. EXPECT_EQ(frameSize.width, firstFrame.cols);
  255. EXPECT_EQ(frameSize.height, firstFrame.rows);
  256. cameras.push_back(cap);
  257. }
  258. std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
  259. {
  260. int counter = 0;
  261. std::vector<int> cameraReady;
  262. do
  263. {
  264. EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
  265. EXPECT_FALSE(cameraReady.empty());
  266. for (int idx : cameraReady)
  267. {
  268. //std::cout << "Reading frame from camera: " << idx << std::endl;
  269. ASSERT_TRUE(idx >= 0 && (size_t)idx < cameras.size()) << idx;
  270. VideoCapture& c = cameras[idx];
  271. Mat frame;
  272. #if 1
  273. ASSERT_TRUE(c.retrieve(frame)) << idx;
  274. #else
  275. ASSERT_TRUE(c.read(frame)) << idx;
  276. #endif
  277. EXPECT_EQ(frameSize.width, frame.cols) << idx;
  278. EXPECT_EQ(frameSize.height, frame.rows) << idx;
  279. ++frameFromCamera[idx];
  280. ++counter;
  281. }
  282. }
  283. while(counter < totalFrames);
  284. }
  285. for (size_t i = 0; i < cameraNames.size(); ++i)
  286. {
  287. EXPECT_GT(frameFromCamera[i], (size_t)0) << i;
  288. }
  289. }
  290. TEST(DISABLED_videoio_camera, ffmpeg_index)
  291. {
  292. int idx = (int)utils::getConfigurationParameterSizeT("OPENCV_TEST_FFMPEG_DEVICE_IDX", (size_t)-1);
  293. if (idx == -1)
  294. {
  295. throw SkipTestException("OPENCV_TEST_FFMPEG_DEVICE_IDX is not set");
  296. }
  297. VideoCapture cap;
  298. ASSERT_TRUE(cap.open(idx, CAP_FFMPEG));
  299. Mat frame;
  300. ASSERT_TRUE(cap.read(frame));
  301. ASSERT_FALSE(frame.empty());
  302. }
  303. }} // namespace