aboutsummaryrefslogtreecommitdiff
path: root/samples/ObjectDetection/test/FrameReaderTest.cpp
blob: a02fa7fd4e04b3a0836547e9ce3c01ba5ff1c244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#define CATCH_CONFIG_MAIN

#include <catch.hpp>
#include <opencv2/opencv.hpp>

#include "IFrameReader.hpp"
#include "CvVideoFrameReader.hpp"

SCENARIO("Read frames from video file using CV frame reader", "[framereader]") {

    GIVEN("a valid video file") {

        std::string testResources = TEST_RESOURCE_DIR;
        REQUIRE(testResources != "");
        std::string file =  testResources + "/" + "Megamind.avi";
        WHEN("Frame reader is initialised") {

            common::CvVideoFrameReader reader;
            THEN("no exception is thrown") {
                reader.Init(file);

                AND_WHEN("when source parameters are read") {

                    auto fps = reader.GetSourceFps();
                    auto height = reader.GetSourceHeight();
                    auto width = reader.GetSourceWidth();
                    auto encoding = reader.GetSourceEncoding();
                    auto framesCount = reader.GetFrameCount();

                    THEN("they are aligned with video file") {

                        REQUIRE(height == 528);
                        REQUIRE(width == 720);
                        REQUIRE(encoding == "XVID");
                        REQUIRE(fps == 23.976);
                        REQUIRE(framesCount == 270);
                    }

                }

                AND_WHEN("frame is read") {
                    auto framePtr = reader.ReadFrame();

                    THEN("it is not a NULL pointer") {
                        REQUIRE(framePtr != nullptr);
                    }

                    AND_THEN("it is not empty") {
                        REQUIRE(!framePtr->empty());
                        REQUIRE(!reader.IsExhausted(framePtr));
                    }
                }

                AND_WHEN("all frames were read from the file") {

                    for (int i = 0; i < 270; i++) {
                        auto framePtr = reader.ReadFrame();
                    }

                    THEN("last + 1 frame is empty") {
                        auto framePtr = reader.ReadFrame();

                        REQUIRE(framePtr->empty());
                        REQUIRE(reader.IsExhausted(framePtr));
                    }

                }

                AND_WHEN("frames are read from the file, pointers point to the different objects") {

                    auto framePtr = reader.ReadFrame();

                    cv::Mat *frame = framePtr.get();

                    for (int i = 0; i < 30; i++) {
                        REQUIRE(frame != reader.ReadFrame().get());
                    }

                }
            }
        }
    }

    GIVEN("an invalid video file") {

        std::string file = "nosuchfile.avi";

        WHEN("Frame reader is initialised") {

            common::CvVideoFrameReader reader;

            THEN("exception is thrown") {
                REQUIRE_THROWS(reader.Init(file));
            }
        }

    }
}