From f6aa472acccb510955c96382be2f98be72105b46 Mon Sep 17 00:00:00 2001 From: Skreg <85214856+shyama7004@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:25:48 +0530 Subject: [PATCH] Merge pull request #26800 from shyama7004:fix-cap-orientation-auto-default Fixed default cap_prop_orientation_auto behaviour #26800 Fixes : #26795 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/videoio/src/cap_interface.hpp | 2 +- modules/videoio/test/test_orientation.cpp | 121 ++++++++++++---------- 2 files changed, 67 insertions(+), 56 deletions(-) diff --git a/modules/videoio/src/cap_interface.hpp b/modules/videoio/src/cap_interface.hpp index 624da9358a..65427e2ab0 100644 --- a/modules/videoio/src/cap_interface.hpp +++ b/modules/videoio/src/cap_interface.hpp @@ -247,7 +247,7 @@ public: class VideoCaptureBase : public IVideoCapture { public: - VideoCaptureBase() : autorotate(false) {} + VideoCaptureBase() : autorotate(true) {} double getProperty(int propId) const CV_OVERRIDE { switch(propId) diff --git a/modules/videoio/test/test_orientation.cpp b/modules/videoio/test/test_orientation.cpp index 96530e2a03..ccff7391b0 100644 --- a/modules/videoio/test/test_orientation.cpp +++ b/modules/videoio/test/test_orientation.cpp @@ -8,69 +8,80 @@ using namespace std; namespace opencv_test { namespace { -typedef TestWithParam VideoCaptureAPITests; +// PR: https://github.com/opencv/opencv/pull/26800 +// TODO: Enable the tests back on Windows after FFmpeg plugin rebuild +#ifndef _WIN32 -// related issue: https://github.com/opencv/opencv/issues/15499 -TEST_P(VideoCaptureAPITests, mp4_orientation_meta_auto) +struct VideoCaptureAPITests: TestWithParam { - cv::VideoCaptureAPIs api = GetParam(); - if (!videoio_registry::hasBackend(api)) - throw SkipTestException("backend " + std::to_string(int(api)) + " was not found"); + void SetUp() + { + cv::VideoCaptureAPIs api = GetParam(); + if (!videoio_registry::hasBackend(api)) + throw SkipTestException("backend " + std::to_string(int(api)) + " was not found"); - string video_file = string(cvtest::TS::ptr()->get_data_path()) + "video/rotated_metadata.mp4"; + string video_file = string(cvtest::TS::ptr()->get_data_path()) + "video/rotated_metadata.mp4"; + + EXPECT_NO_THROW(cap.open(video_file, api)); + ASSERT_TRUE(cap.isOpened()) << "Can't open the video: " << video_file << " with backend " << api << std::endl; + } + + void tearDown() + { + cap.release(); + } + + void orientationCheck(double angle, int width, int height) + { + EXPECT_EQ(angle, cap.get(CAP_PROP_ORIENTATION_META)); + EXPECT_EQ(width, (int)cap.get(CAP_PROP_FRAME_WIDTH)); + EXPECT_EQ(height, (int)cap.get(CAP_PROP_FRAME_HEIGHT)); + + Mat frame; + cap >> frame; + + ASSERT_EQ(width, frame.cols); + ASSERT_EQ(height, frame.rows); + } VideoCapture cap; - EXPECT_NO_THROW(cap.open(video_file, api)); - ASSERT_TRUE(cap.isOpened()) << "Can't open the video: " << video_file << " with backend " << api << std::endl; +}; - // related issue: https://github.com/opencv/opencv/issues/22088 - EXPECT_EQ(90, cap.get(CAP_PROP_ORIENTATION_META)); - - EXPECT_TRUE(cap.set(CAP_PROP_ORIENTATION_AUTO, true)); - - Size actual; - EXPECT_NO_THROW(actual = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), - (int)cap.get(CAP_PROP_FRAME_HEIGHT))); - EXPECT_EQ(270, actual.width); - EXPECT_EQ(480, actual.height); - - Mat frame; - - cap >> frame; - - ASSERT_EQ(270, frame.cols); - ASSERT_EQ(480, frame.rows); -} - -// related issue: https://github.com/opencv/opencv/issues/15499 -TEST_P(VideoCaptureAPITests, mp4_orientation_no_rotation) +// Related issues: +// - https://github.com/opencv/opencv/issues/26795 +// - https://github.com/opencv/opencv/issues/15499 +TEST_P(VideoCaptureAPITests, mp4_orientation_default_auto) { - cv::VideoCaptureAPIs api = GetParam(); - if (!videoio_registry::hasBackend(api)) - throw SkipTestException("backend " + std::to_string(int(api)) + " was not found"); - - string video_file = string(cvtest::TS::ptr()->get_data_path()) + "video/rotated_metadata.mp4"; - - VideoCapture cap; - EXPECT_NO_THROW(cap.open(video_file, api)); - cap.set(CAP_PROP_ORIENTATION_AUTO, 0); - ASSERT_TRUE(cap.isOpened()) << "Can't open the video: " << video_file << " with backend " << api << std::endl; - ASSERT_FALSE(cap.get(CAP_PROP_ORIENTATION_AUTO)); - - Size actual; - EXPECT_NO_THROW(actual = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), - (int)cap.get(CAP_PROP_FRAME_HEIGHT))); - EXPECT_EQ(480, actual.width); - EXPECT_EQ(270, actual.height); - - Mat frame; - - cap >> frame; - - ASSERT_EQ(480, frame.cols); - ASSERT_EQ(270, frame.rows); + EXPECT_TRUE(cap.get(CAP_PROP_ORIENTATION_AUTO)); + orientationCheck(90., 270, 480); } -INSTANTIATE_TEST_CASE_P(videoio, VideoCaptureAPITests, testing::Values(CAP_FFMPEG, CAP_AVFOUNDATION)); +TEST_P(VideoCaptureAPITests, mp4_orientation_forced) +{ + EXPECT_TRUE(cap.set(CAP_PROP_ORIENTATION_AUTO, false)); + orientationCheck(90., 480, 270); +} + +TEST_P(VideoCaptureAPITests, mp4_orientation_switch) +{ + SCOPED_TRACE("Initial orientation with autorotation"); + orientationCheck(90., 270, 480); + SCOPED_TRACE("Disabled autorotation"); + EXPECT_TRUE(cap.set(CAP_PROP_ORIENTATION_AUTO, false)); + EXPECT_FALSE(cap.get(CAP_PROP_ORIENTATION_AUTO)); + orientationCheck(90., 480, 270); +} + + +static cv::VideoCaptureAPIs supported_backends[] = { +#ifdef HAVE_AVFOUNDATION + CAP_AVFOUNDATION, +#endif + CAP_FFMPEG +}; + +INSTANTIATE_TEST_CASE_P(videoio, VideoCaptureAPITests, testing::ValuesIn(supported_backends)); + +#endif // WIN32 }} // namespace