aboutsummaryrefslogtreecommitdiff
path: root/samples/common/src/CVUtils
diff options
context:
space:
mode:
Diffstat (limited to 'samples/common/src/CVUtils')
-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
3 files changed, 169 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