aboutsummaryrefslogtreecommitdiff
path: root/samples/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'samples/common/src')
-rw-r--r--samples/common/src/CVUtils/CvVideoFileWriter.cpp38
-rw-r--r--samples/common/src/CVUtils/CvVideoFrameReader.cpp98
-rw-r--r--samples/common/src/CVUtils/CvWindowOutput.cpp33
-rw-r--r--samples/common/src/Utils/CmdArgsParser.cpp70
4 files changed, 239 insertions, 0 deletions
diff --git a/samples/common/src/CVUtils/CvVideoFileWriter.cpp b/samples/common/src/CVUtils/CvVideoFileWriter.cpp
new file mode 100644
index 0000000000..b76630049a
--- /dev/null
+++ b/samples/common/src/CVUtils/CvVideoFileWriter.cpp
@@ -0,0 +1,38 @@
+//
+// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "CvVideoFileWriter.hpp"
+
+namespace common
+{
+
+void CvVideoFileWriter::Init(const std::string& outputVideo, int encoding, double fps, int width, int height)
+{
+ m_ready = m_cvWriter.open(outputVideo, cv::CAP_FFMPEG,
+ encoding,
+ fps,
+ cv::Size(width, height), true);
+}
+
+
+void CvVideoFileWriter::WriteFrame(std::shared_ptr<cv::Mat>& frame)
+{
+ if(m_cvWriter.isOpened())
+ {
+ cv::cvtColor(*frame, *frame, cv::COLOR_RGB2BGR);
+ m_cvWriter.write(*frame);
+ }
+}
+
+bool CvVideoFileWriter::IsReady() const
+{
+ return m_ready;
+}
+
+void CvVideoFileWriter::Close()
+{
+ m_cvWriter.release();
+}
+}// namespace common
diff --git a/samples/common/src/CVUtils/CvVideoFrameReader.cpp b/samples/common/src/CVUtils/CvVideoFrameReader.cpp
new file mode 100644
index 0000000000..2bd92d2d81
--- /dev/null
+++ b/samples/common/src/CVUtils/CvVideoFrameReader.cpp
@@ -0,0 +1,98 @@
+//
+// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+
+#include "CvVideoFrameReader.hpp"
+
+namespace common
+{
+
+std::shared_ptr<cv::Mat> CvVideoFrameReader::ReadFrame()
+{
+ // opencv copies data anyway
+ cv::Mat captureFrame;
+ m_capture.read(captureFrame);
+ return std::make_shared<cv::Mat>(std::move(captureFrame));
+}
+
+bool CvVideoFrameReader::IsExhausted(const std::shared_ptr<cv::Mat>& frame) const
+{
+ assert(frame!=nullptr);
+ return frame->empty();
+}
+
+void CvVideoFrameReader::CheckIsOpen(const std::string& source)
+{
+ if (!m_capture.isOpened())
+ {
+ throw std::runtime_error("Failed to open video capture for the source = " + source);
+ }
+}
+
+void CvVideoFrameReader::Init(const std::string& source)
+{
+ m_capture.open(source);
+ CheckIsOpen(source);
+}
+
+int CvVideoFrameReader::GetSourceWidth() const
+{
+ return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_WIDTH)));
+}
+
+int CvVideoFrameReader::GetSourceHeight() const
+{
+ return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_HEIGHT)));
+}
+
+double CvVideoFrameReader::GetSourceFps() const
+{
+ return m_capture.get(cv::CAP_PROP_FPS);
+}
+
+bool CvVideoFrameReader::ConvertToRGB()
+{
+ m_capture.set(cv::CAP_PROP_CONVERT_RGB, 1.0);
+ return static_cast<bool>(m_capture.get(cv::CAP_PROP_CONVERT_RGB));
+}
+
+std::string CvVideoFrameReader::GetSourceEncoding() const
+{
+ char fourccStr[5];
+ auto fourcc = (int)m_capture.get(cv::CAP_PROP_FOURCC);
+ sprintf(fourccStr,"%c%c%c%c",fourcc & 0xFF, (fourcc >> 8) & 0xFF, (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
+ return fourccStr;
+}
+
+int CvVideoFrameReader::GetSourceEncodingInt() const
+{
+ return (int)m_capture.get(cv::CAP_PROP_FOURCC);
+}
+
+int CvVideoFrameReader::GetFrameCount() const
+{
+ return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_COUNT)));
+};
+
+std::shared_ptr<cv::Mat> CvVideoFrameReaderRgbWrapper::ReadFrame()
+{
+ auto framePtr = m_reader->ReadFrame();
+ if (!IsExhausted(framePtr))
+ {
+ cv::cvtColor(*framePtr, *framePtr, cv::COLOR_BGR2RGB);
+ }
+ return framePtr;
+}
+
+bool CvVideoFrameReaderRgbWrapper::IsExhausted(const std::shared_ptr<cv::Mat>& frame) const
+{
+ return m_reader->IsExhausted(frame);
+}
+
+CvVideoFrameReaderRgbWrapper::CvVideoFrameReaderRgbWrapper(std::unique_ptr<common::CvVideoFrameReader> reader):
+ m_reader(std::move(reader))
+{}
+
+}// namespace common \ No newline at end of file
diff --git a/samples/common/src/CVUtils/CvWindowOutput.cpp b/samples/common/src/CVUtils/CvWindowOutput.cpp
new file mode 100644
index 0000000000..190a7602e2
--- /dev/null
+++ b/samples/common/src/CVUtils/CvWindowOutput.cpp
@@ -0,0 +1,33 @@
+//
+// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "CvWindowOutput.hpp"
+
+namespace common
+{
+
+void CvWindowOutput::Init(const std::string& windowName)
+{
+ m_windowName = windowName;
+ cv::namedWindow(m_windowName, cv::WINDOW_AUTOSIZE);
+}
+
+void CvWindowOutput::WriteFrame(std::shared_ptr<cv::Mat>& frame)
+{
+ cv::cvtColor(*frame, *frame, cv::COLOR_RGB2BGR);
+ cv::imshow( m_windowName, *frame);
+ cv::waitKey(30);
+}
+
+void CvWindowOutput::Close()
+{
+ cv::destroyWindow(m_windowName);
+}
+
+bool CvWindowOutput::IsReady() const
+{
+ return true;
+}
+}// namespace common \ No newline at end of file
diff --git a/samples/common/src/Utils/CmdArgsParser.cpp b/samples/common/src/Utils/CmdArgsParser.cpp
new file mode 100644
index 0000000000..1f09826a8b
--- /dev/null
+++ b/samples/common/src/Utils/CmdArgsParser.cpp
@@ -0,0 +1,70 @@
+//
+// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "CmdArgsParser.hpp"
+#include <iostream>
+/*
+ * Checks that a particular option was specified by the user
+ */
+bool CheckOptionSpecified(const std::map<std::string, std::string>& options, const std::string& option)
+{
+ auto it = options.find(option);
+ return it!=options.end();
+}
+
+/*
+ * Retrieves the user provided option
+ */
+std::string GetSpecifiedOption(const std::map<std::string, std::string>& options, const std::string& option)
+{
+ if (CheckOptionSpecified(options, option)){
+ return options.at(option);
+ }
+ else
+ {
+ throw std::invalid_argument("Required option: " + option + " not defined.");
+ }
+}
+
+/*
+ * Parses all the command line options provided by the user and stores in a map.
+ */
+int ParseOptions(std::map<std::string, std::string>& options, std::map<std::string, std::string>& acceptedOptions,
+ char *argv[], int argc)
+{
+ for (int i = 1; i < argc; ++i)
+ {
+ std::string currentOption = std::string(argv[i]);
+ auto it = acceptedOptions.find(currentOption);
+ if (it != acceptedOptions.end())
+ {
+ if (i + 1 < argc && std::string(argv[i + 1]).rfind("--", 0) != 0)
+ {
+ std::string value = argv[++i];
+ options.insert({it->first, value});
+ }
+ else if (std::string(argv[i]) == "HELP")
+ {
+ std::cout << "Available options" << std::endl;
+ for (auto & acceptedOption : acceptedOptions)
+ {
+ std::cout << acceptedOption.first << " : " << acceptedOption.second << std::endl;
+ }
+ return 2;
+ }
+ else
+ {
+ std::cerr << std::string(argv[i]) << " option requires one argument." << std::endl;
+ return 1;
+ }
+ }
+ else
+ {
+ std::cerr << "Unrecognised option: " << std::string(argv[i]) << std::endl;
+ return 1;
+ }
+ }
+ return 0;
+}