xref: /aosp_15_r20/external/armnn/samples/ObjectDetection/test/FrameReaderTest.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #define CATCH_CONFIG_MAIN
7 
8 #include <catch.hpp>
9 #include <opencv2/opencv.hpp>
10 
11 #include "IFrameReader.hpp"
12 #include "CvVideoFrameReader.hpp"
13 
14 SCENARIO("Read frames from video file using CV frame reader", "[framereader]") {
15 
16     GIVEN("a valid video file") {
17 
18         std::string testResources = TEST_RESOURCE_DIR;
19         REQUIRE(testResources != "");
20         std::string file =  testResources + "/" + "Megamind.avi";
21         WHEN("Frame reader is initialised") {
22 
23             common::CvVideoFrameReader reader;
24             THEN("no exception is thrown") {
25                 reader.Init(file);
26 
27                 AND_WHEN("when source parameters are read") {
28 
29                     auto fps = reader.GetSourceFps();
30                     auto height = reader.GetSourceHeight();
31                     auto width = reader.GetSourceWidth();
32                     auto encoding = reader.GetSourceEncoding();
33                     auto framesCount = reader.GetFrameCount();
34 
35                     THEN("they are aligned with video file") {
36 
37                         REQUIRE(height == 528);
38                         REQUIRE(width == 720);
39                         REQUIRE(encoding == "XVID");
40                         REQUIRE(fps == 23.976);
41                         REQUIRE(framesCount == 270);
42                     }
43 
44                 }
45 
46                 AND_WHEN("frame is read") {
47                     auto framePtr = reader.ReadFrame();
48 
49                     THEN("it is not a NULL pointer") {
50                         REQUIRE(framePtr != nullptr);
51                     }
52 
53                     AND_THEN("it is not empty") {
54                         REQUIRE(!framePtr->empty());
55                         REQUIRE(!reader.IsExhausted(framePtr));
56                     }
57                 }
58 
59                 AND_WHEN("all frames were read from the file") {
60 
61                     for (int i = 0; i < 270; i++) {
62                         auto framePtr = reader.ReadFrame();
63                     }
64 
65                     THEN("last + 1 frame is empty") {
66                         auto framePtr = reader.ReadFrame();
67 
68                         REQUIRE(framePtr->empty());
69                         REQUIRE(reader.IsExhausted(framePtr));
70                     }
71 
72                 }
73 
74                 AND_WHEN("frames are read from the file, pointers point to the different objects") {
75 
76                     auto framePtr = reader.ReadFrame();
77 
78                     cv::Mat *frame = framePtr.get();
79 
80                     for (int i = 0; i < 30; i++) {
81                         REQUIRE(frame != reader.ReadFrame().get());
82                     }
83 
84                 }
85             }
86         }
87     }
88 
89     GIVEN("an invalid video file") {
90 
91         std::string file = "nosuchfile.avi";
92 
93         WHEN("Frame reader is initialised") {
94 
95             common::CvVideoFrameReader reader;
96 
97             THEN("exception is thrown") {
98                 REQUIRE_THROWS(reader.Init(file));
99             }
100         }
101 
102     }
103 }