summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/common/format_reader
diff options
context:
space:
mode:
authorEric Dao <eric@erickhangdao.com>2025-03-10 17:54:31 -0400
committerEric Dao <eric@erickhangdao.com>2025-03-10 17:54:31 -0400
commitab224e2e6ba65f5a369ec392f99cd8845ad06c98 (patch)
treea1e757e9341863ed52b8ad4c5a1c45933aab9da4 /python/openvino/runtime/common/format_reader
parent40da1752f2c8639186b72f6838aa415e854d0b1d (diff)
downloadthesis-master.tar.gz
thesis-master.tar.bz2
thesis-master.zip
completed thesisHEADmaster
Diffstat (limited to 'python/openvino/runtime/common/format_reader')
-rw-r--r--python/openvino/runtime/common/format_reader/CMakeLists.txt55
-rw-r--r--python/openvino/runtime/common/format_reader/MnistUbyte.cpp66
-rw-r--r--python/openvino/runtime/common/format_reader/MnistUbyte.h58
-rw-r--r--python/openvino/runtime/common/format_reader/bmp.cpp64
-rw-r--r--python/openvino/runtime/common/format_reader/bmp.h75
-rw-r--r--python/openvino/runtime/common/format_reader/format_reader.cpp44
-rw-r--r--python/openvino/runtime/common/format_reader/format_reader.h95
-rw-r--r--python/openvino/runtime/common/format_reader/format_reader_ptr.h43
-rw-r--r--python/openvino/runtime/common/format_reader/opencv_wrapper.cpp83
-rw-r--r--python/openvino/runtime/common/format_reader/opencv_wrapper.h58
-rw-r--r--python/openvino/runtime/common/format_reader/register.h58
-rw-r--r--python/openvino/runtime/common/format_reader/yuv_nv12.cpp36
-rw-r--r--python/openvino/runtime/common/format_reader/yuv_nv12.h57
13 files changed, 792 insertions, 0 deletions
diff --git a/python/openvino/runtime/common/format_reader/CMakeLists.txt b/python/openvino/runtime/common/format_reader/CMakeLists.txt
new file mode 100644
index 0000000..3daab96
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/CMakeLists.txt
@@ -0,0 +1,55 @@
+# Copyright (C) 2018-2022 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+#
+
+set (TARGET_NAME "format_reader")
+
+file (GLOB MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
+file (GLOB LIBRARY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
+
+# Create named folders for the sources within the .vcproj
+# Empty name lists them directly under the .vcproj
+source_group("src" FILES ${LIBRARY_SRC})
+source_group("include" FILES ${LIBRARY_HEADERS})
+
+# Create library file from sources.
+add_library(${TARGET_NAME} SHARED ${MAIN_SRC} ${LIBRARY_HEADERS})
+
+# Find OpenCV components if exist
+find_package(OpenCV QUIET COMPONENTS core imgproc imgcodecs)
+if(NOT OpenCV_FOUND)
+ message(WARNING "OpenCV is disabled or not found, ${TARGET_NAME} will be built without OpenCV support")
+else()
+ target_link_libraries(${TARGET_NAME} PRIVATE ${OpenCV_LIBRARIES} ie_samples_utils)
+ if(UNIX AND NOT APPLE)
+ # Workaround issue that rpath-link is missing for PRIVATE dependencies
+ # Fixed in cmake 3.16.0 https://gitlab.kitware.com/cmake/cmake/issues/19556
+ target_link_libraries(${TARGET_NAME} INTERFACE "-Wl,-rpath-link,${OpenCV_INSTALL_PATH}/lib")
+ endif()
+ # Make this definition public so that it's also seen by dla benchmark. As dla benchmark
+ # uses this macro to identify which image extensions are supported by the image reader
+ target_compile_definitions(${TARGET_NAME} PUBLIC USE_OPENCV)
+endif()
+
+target_compile_definitions(${TARGET_NAME} PRIVATE IMPLEMENT_FORMAT_READER)
+
+target_include_directories(${TARGET_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}"
+ "${CMAKE_CURRENT_SOURCE_DIR}/..")
+
+set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_PDB_NAME ${TARGET_NAME}
+ FOLDER cpp_samples)
+
+if(COMMAND add_clang_format_target)
+ add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
+endif()
+
+install(
+ TARGETS ${TARGET_NAME}
+ RUNTIME DESTINATION samples_bin/ COMPONENT samples_bin EXCLUDE_FROM_ALL
+ LIBRARY DESTINATION samples_bin/ COMPONENT samples_bin EXCLUDE_FROM_ALL
+)
+
+install(TARGETS ${TARGET_NAME}
+ RUNTIME DESTINATION "dla/bin" COMPONENT EMUTEST
+ LIBRARY DESTINATION "dla/lib" COMPONENT EMUTEST
+ ARCHIVE DESTINATION "dla/lib" COMPONENT EMUTEST)
diff --git a/python/openvino/runtime/common/format_reader/MnistUbyte.cpp b/python/openvino/runtime/common/format_reader/MnistUbyte.cpp
new file mode 100644
index 0000000..182ef99
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/MnistUbyte.cpp
@@ -0,0 +1,66 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+// clang-format off
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include "MnistUbyte.h"
+// clang-format on
+
+using namespace FormatReader;
+
+int MnistUbyte::reverseInt(int i) {
+ unsigned char ch1, ch2, ch3, ch4;
+ ch1 = (unsigned char)(i & 255);
+ ch2 = (unsigned char)((i >> 8) & 255);
+ ch3 = (unsigned char)((i >> 16) & 255);
+ ch4 = (unsigned char)((i >> 24) & 255);
+ return (static_cast<int>(ch1) << 24) + (static_cast<int>(ch2) << 16) + (static_cast<int>(ch3) << 8) + ch4;
+}
+
+MnistUbyte::MnistUbyte(const std::string& filename) {
+ std::ifstream file(filename, std::ios::binary);
+ if (!file.is_open()) {
+ return;
+ }
+ int magic_number = 0;
+ int number_of_images = 0;
+ int n_rows = 0;
+ int n_cols = 0;
+ file.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
+ magic_number = reverseInt(magic_number);
+ if (magic_number != 2051) {
+ return;
+ }
+ file.read(reinterpret_cast<char*>(&number_of_images), sizeof(number_of_images));
+ number_of_images = reverseInt(number_of_images);
+ file.read(reinterpret_cast<char*>(&n_rows), sizeof(n_rows));
+ n_rows = reverseInt(n_rows);
+ _height = (size_t)n_rows;
+ file.read(reinterpret_cast<char*>(&n_cols), sizeof(n_cols));
+ n_cols = reverseInt(n_cols);
+ _width = (size_t)n_cols;
+ if (number_of_images > 1) {
+ std::cout << "[MNIST] Warning: number_of_images in mnist file equals " << number_of_images
+ << ". Only a first image will be read." << std::endl;
+ }
+
+ size_t size = _width * _height * 1;
+
+ _data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());
+ size_t count = 0;
+ if (0 < number_of_images) {
+ for (int r = 0; r < n_rows; ++r) {
+ for (int c = 0; c < n_cols; ++c) {
+ unsigned char temp = 0;
+ file.read(reinterpret_cast<char*>(&temp), sizeof(temp));
+ _data.get()[count++] = temp;
+ }
+ }
+ }
+
+ file.close();
+}
diff --git a/python/openvino/runtime/common/format_reader/MnistUbyte.h b/python/openvino/runtime/common/format_reader/MnistUbyte.h
new file mode 100644
index 0000000..8991166
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/MnistUbyte.h
@@ -0,0 +1,58 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief Mnist reader
+ * \file MnistUbyte.h
+ */
+#pragma once
+
+#include <memory>
+#include <string>
+
+// clang-format off
+#include "format_reader.h"
+#include "register.h"
+// clang-format on
+
+namespace FormatReader {
+/**
+ * \class MnistUbyte
+ * \brief Reader for mnist db files
+ */
+class MnistUbyte : public Reader {
+private:
+ int reverseInt(int i);
+
+ static Register<MnistUbyte> reg;
+
+public:
+ /**
+ * \brief Constructor of Mnist reader
+ * @param filename - path to input data
+ * @return MnistUbyte reader object
+ */
+ explicit MnistUbyte(const std::string& filename);
+ virtual ~MnistUbyte() {}
+
+ /**
+ * \brief Get size
+ * @return size
+ */
+ size_t size() const override {
+ return _width * _height * 1;
+ }
+
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // format_reader is copied from openvino samples/cpp/common/format_reader/
+ // this might need special care when doing a OV uplift
+ std::shared_ptr<unsigned char> getData(size_t width, size_t height, ResizeType resize_type) override {
+ if ((width * height != 0) && (_width * _height != width * height)) {
+ std::cout << "[ WARNING ] Image won't be resized! Please use OpenCV.\n";
+ return nullptr;
+ }
+ return _data;
+ }
+};
+} // namespace FormatReader
diff --git a/python/openvino/runtime/common/format_reader/bmp.cpp b/python/openvino/runtime/common/format_reader/bmp.cpp
new file mode 100644
index 0000000..240d13f
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/bmp.cpp
@@ -0,0 +1,64 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+// clang-format off
+#include <fstream>
+#include <iostream>
+
+#include "bmp.h"
+// clang-format on
+
+using namespace std;
+using namespace FormatReader;
+
+BitMap::BitMap(const string& filename) {
+ BmpHeader header;
+ BmpInfoHeader infoHeader;
+
+ ifstream input(filename, ios::binary);
+ if (!input) {
+ return;
+ }
+
+ input.read(reinterpret_cast<char*>(&header.type), 2);
+
+ if (header.type != 'M' * 256 + 'B') {
+ std::cerr << "[BMP] file is not bmp type\n";
+ return;
+ }
+
+ input.read(reinterpret_cast<char*>(&header.size), 4);
+ input.read(reinterpret_cast<char*>(&header.reserved), 4);
+ input.read(reinterpret_cast<char*>(&header.offset), 4);
+
+ input.read(reinterpret_cast<char*>(&infoHeader), sizeof(BmpInfoHeader));
+
+ bool rowsReversed = infoHeader.height < 0;
+ _width = infoHeader.width;
+ _height = abs(infoHeader.height);
+
+ if (infoHeader.bits != 24) {
+ cerr << "[BMP] 24bpp only supported. But input has:" << infoHeader.bits << "\n";
+ return;
+ }
+
+ if (infoHeader.compression != 0) {
+ cerr << "[BMP] compression not supported\n";
+ }
+
+ int padSize = _width & 3;
+ char pad[3];
+ size_t size = _width * _height * 3;
+
+ _data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());
+
+ input.seekg(header.offset, ios::beg);
+
+ // reading by rows in invert vertically
+ for (uint32_t i = 0; i < _height; i++) {
+ uint32_t storeAt = rowsReversed ? i : (uint32_t)_height - 1 - i;
+ input.read(reinterpret_cast<char*>(_data.get()) + _width * 3 * storeAt, _width * 3);
+ input.read(pad, padSize);
+ }
+}
diff --git a/python/openvino/runtime/common/format_reader/bmp.h b/python/openvino/runtime/common/format_reader/bmp.h
new file mode 100644
index 0000000..ac3ff31
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/bmp.h
@@ -0,0 +1,75 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief BMP reader
+ * \file bmp.h
+ */
+#pragma once
+
+#include <memory>
+#include <string>
+
+// clang-format off
+#include "format_reader.h"
+#include "register.h"
+// clang-format on
+
+namespace FormatReader {
+/**
+ * \class BitMap
+ * \brief Reader for bmp files
+ */
+class BitMap : public Reader {
+private:
+ static Register<BitMap> reg;
+
+ typedef struct BmpHeaderType {
+ unsigned short type = 0u; /* Magic identifier */
+ unsigned int size = 0u; /* File size in bytes */
+ unsigned int reserved = 0u;
+ unsigned int offset = 0u; /* Offset to image data, bytes */
+ } BmpHeader;
+
+ typedef struct BmpInfoHeaderType {
+ unsigned int size = 0u; /* Header size in bytes */
+ int width = 0, height = 0; /* Width and height of image */
+ unsigned short planes = 0u; /* Number of colour planes */
+ unsigned short bits = 0u; /* Bits per pixel */
+ unsigned int compression = 0u; /* Compression type */
+ unsigned int imagesize = 0u; /* Image size in bytes */
+ int xresolution = 0, yresolution = 0; /* Pixels per meter */
+ unsigned int ncolours = 0u; /* Number of colours */
+ unsigned int importantcolours = 0u; /* Important colours */
+ } BmpInfoHeader;
+
+public:
+ /**
+ * \brief Constructor of BMP reader
+ * @param filename - path to input data
+ * @return BitMap reader object
+ */
+ explicit BitMap(const std::string& filename);
+ virtual ~BitMap() {}
+
+ /**
+ * \brief Get size
+ * @return size
+ */
+ size_t size() const override {
+ return _width * _height * 3;
+ }
+
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // format_reader is copied from openvino samples/cpp/common/format_reader/
+ // this might need special care when doing a OV uplift
+ std::shared_ptr<unsigned char> getData(size_t width, size_t height, ResizeType resize_type) override {
+ if ((width * height != 0) && (_width * _height != width * height)) {
+ std::cout << "[ WARNING ] Image won't be resized! Please use OpenCV.\n";
+ return nullptr;
+ }
+ return _data;
+ }
+};
+} // namespace FormatReader
diff --git a/python/openvino/runtime/common/format_reader/format_reader.cpp b/python/openvino/runtime/common/format_reader/format_reader.cpp
new file mode 100644
index 0000000..94a8441
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/format_reader.cpp
@@ -0,0 +1,44 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <iostream>
+
+// clang-format off
+#include "bmp.h"
+#include "MnistUbyte.h"
+#include "yuv_nv12.h"
+#include "opencv_wrapper.h"
+#include "format_reader.h"
+// clang-format on
+
+using namespace FormatReader;
+
+std::vector<Registry::CreatorFunction> Registry::_data;
+
+Register<MnistUbyte> MnistUbyte::reg;
+Register<YUV_NV12> YUV_NV12::reg;
+#ifdef USE_OPENCV
+Register<OCVReader> OCVReader::reg;
+#else
+Register<BitMap> BitMap::reg;
+#endif
+
+Reader* Registry::CreateReader(const char* filename) {
+ for (const auto &maker : _data) {
+ Reader* ol = maker(filename);
+ if (ol != nullptr && ol->size() != 0)
+ return ol;
+ if (ol != nullptr)
+ delete ol;
+ }
+ return nullptr;
+}
+
+void Registry::RegisterReader(CreatorFunction f) {
+ _data.push_back(f);
+}
+
+FORMAT_READER_API(Reader*) CreateFormatReader(const char* filename) {
+ return Registry::CreateReader(filename);
+}
diff --git a/python/openvino/runtime/common/format_reader/format_reader.h b/python/openvino/runtime/common/format_reader/format_reader.h
new file mode 100644
index 0000000..99fc573
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/format_reader.h
@@ -0,0 +1,95 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief Format reader abstract class implementation
+ * \file format_reader.h
+ */
+#pragma once
+
+#include <iostream>
+#include <memory>
+#include <string>
+#include <vector>
+
+#if defined(_WIN32)
+# ifdef IMPLEMENT_FORMAT_READER
+# define FORMAT_READER_API(type) extern "C" __declspec(dllexport) type
+# else
+# define FORMAT_READER_API(type) extern "C" type
+# endif
+#elif (__GNUC__ >= 4)
+# ifdef IMPLEMENT_FORMAT_READER
+# define FORMAT_READER_API(type) extern "C" __attribute__((visibility("default"))) type
+# else
+# define FORMAT_READER_API(type) extern "C" type
+# endif
+#else
+# define FORMAT_READER_API(TYPE) extern "C" TYPE
+#endif
+
+namespace FormatReader {
+/**
+ * \class FormatReader
+ * \brief This is an abstract class for reading input data
+ */
+class Reader {
+protected:
+ /// \brief height
+ size_t _height = 0;
+ /// \brief width
+ size_t _width = 0;
+ /// \brief data
+ std::shared_ptr<unsigned char> _data;
+
+public:
+ virtual ~Reader() = default;
+
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // format_reader is copied from openvino samples/cpp/common/format_reader/
+ // this might need special care when doing a OV uplift
+ enum ResizeType {
+ RESIZE, // resize the image to target (height, width)
+ PAD_RESIZE, // pad the image into a squared image and then resize the image to target (height, width)
+ };
+
+ /**
+ * \brief Get width
+ * @return width
+ */
+ size_t width() const {
+ return _width;
+ }
+
+ /**
+ * \brief Get height
+ * @return height
+ */
+ size_t height() const {
+ return _height;
+ }
+
+ /**
+ * \brief Get input data ptr
+ * @return shared pointer with input data
+ * @In case of using OpenCV, parameters width and height will be used for image resizing
+ */
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // Needs special care when doing a OV uplift
+ virtual std::shared_ptr<unsigned char> getData(size_t width = 0, size_t height = 0,
+ ResizeType resize_type = ResizeType::RESIZE) = 0;
+
+ /**
+ * \brief Get size
+ * @return size
+ */
+ virtual size_t size() const = 0;
+};
+} // namespace FormatReader
+
+/**
+ * \brief Function for create reader
+ * @return FormatReader pointer
+ */
+FORMAT_READER_API(FormatReader::Reader*) CreateFormatReader(const char* filename);
diff --git a/python/openvino/runtime/common/format_reader/format_reader_ptr.h b/python/openvino/runtime/common/format_reader/format_reader_ptr.h
new file mode 100644
index 0000000..eb9bf8e
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/format_reader_ptr.h
@@ -0,0 +1,43 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief Implementation of smart pointer for Reader class
+ * \file format_reader_ptr.h
+ */
+#pragma once
+
+#include <functional>
+#include <memory>
+
+#include "format_reader.h"
+
+namespace FormatReader {
+class ReaderPtr {
+public:
+ explicit ReaderPtr(const char* imageName) : reader(CreateFormatReader(imageName)) {}
+ /**
+ * @brief dereference operator overload
+ * @return Reader
+ */
+ Reader* operator->() const noexcept {
+ return reader.get();
+ }
+
+ /**
+ * @brief dereference operator overload
+ * @return Reader
+ */
+ Reader* operator*() const noexcept {
+ return reader.get();
+ }
+
+ Reader* get() {
+ return reader.get();
+ }
+
+protected:
+ std::unique_ptr<Reader> reader;
+};
+} // namespace FormatReader
diff --git a/python/openvino/runtime/common/format_reader/opencv_wrapper.cpp b/python/openvino/runtime/common/format_reader/opencv_wrapper.cpp
new file mode 100644
index 0000000..b8ebeef
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/opencv_wrapper.cpp
@@ -0,0 +1,83 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#ifdef USE_OPENCV
+# include <fstream>
+# include <iostream>
+
+// clang-format off
+# include <opencv2/opencv.hpp>
+
+# include "samples/slog.hpp"
+# include "opencv_wrapper.h"
+// clang-format on
+
+using namespace std;
+using namespace FormatReader;
+
+OCVReader::OCVReader(const string& filename) {
+ img = cv::imread(filename);
+ _size = 0;
+
+ if (img.empty()) {
+ return;
+ }
+
+ _size = img.size().width * img.size().height * img.channels();
+ _width = img.size().width;
+ _height = img.size().height;
+}
+
+// Set the maximum number of printed warnings; large image directories can otherwise be overwhelming
+static size_t resize_warning_count = 0;
+const size_t max_resize_warnings = 5;
+
+std::shared_ptr<unsigned char> OCVReader::getData(size_t width = 0, size_t height = 0, ResizeType resize_type = ResizeType::RESIZE) {
+ if (width == 0)
+ width = img.cols;
+
+ if (height == 0)
+ height = img.rows;
+
+ size_t size = width * height * img.channels();
+ _data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());
+
+ if (width != static_cast<size_t>(img.cols) || height != static_cast<size_t>(img.rows)) {
+ if (resize_warning_count < max_resize_warnings) {
+ slog::warn << "Image is resized from (" << img.cols << ", " << img.rows << ") to (" << width << ", " << height
+ << ")" << slog::endl;
+ resize_warning_count++;
+ } else if (resize_warning_count == max_resize_warnings) {
+ slog::warn << "Additional image resizing messages have been suppressed." << slog::endl;
+ resize_warning_count++;
+ }
+ }
+
+ cv::Mat resized;
+ if (resize_type == ResizeType::RESIZE) {
+ resized = cv::Mat(cv::Size(width, height), img.type(), _data.get());
+ // cv::resize() just copy data to output image if sizes are the same
+ cv::resize(img, resized, cv::Size(width, height));
+ } else if (resize_type == ResizeType::PAD_RESIZE)
+ {
+ cv::Mat padded;
+ // Find the larger side out of width and height of the image
+ int max_dim = std::max(img.rows, img.cols);
+ // Calculate padding for shorter dimension
+ int top = (max_dim - img.rows) / 2;
+ int bottom = (max_dim - img.rows + 1) / 2;
+ int left = (max_dim - img.cols) / 2;
+ int right = (max_dim - img.cols + 1) / 2;
+ // Add padding (0, i.e., black) to make the image a square
+ cv::copyMakeBorder(img, padded, top, bottom, left, right, cv::BORDER_CONSTANT, cv::Scalar());
+ cv::resize(padded, resized, cv::Size(width, height));
+ std::memcpy(_data.get(), resized.data, resized.total() * resized.elemSize());
+ } else {
+ slog::err << "Specified resize type is not implemented." << slog::endl;
+ std::exit(1);
+ }
+
+ return _data;
+}
+#endif
diff --git a/python/openvino/runtime/common/format_reader/opencv_wrapper.h b/python/openvino/runtime/common/format_reader/opencv_wrapper.h
new file mode 100644
index 0000000..c402e8d
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/opencv_wrapper.h
@@ -0,0 +1,58 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief Image reader
+ * \file opencv_wrapper.h
+ */
+#pragma once
+
+#ifdef USE_OPENCV
+# include <memory>
+# include <string>
+
+// clang-format off
+# include <opencv2/opencv.hpp>
+
+# include "format_reader.h"
+# include "register.h"
+// clang-format on
+
+namespace FormatReader {
+/**
+ * \class OCVMAT
+ * \brief OpenCV Wrapper
+ */
+class OCVReader : public Reader {
+private:
+ cv::Mat img;
+ size_t _size;
+ static Register<OCVReader> reg;
+
+public:
+ /**
+ * \brief Constructor of BMP reader
+ * @param filename - path to input data
+ * @return BitMap reader object
+ */
+ explicit OCVReader(const std::string& filename);
+ virtual ~OCVReader() {}
+
+ /**
+ * \brief Get size
+ * @return size
+ */
+ size_t size() const override {
+ return _size;
+ }
+
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // format_reader is copied from openvino samples/cpp/common/format_reader/
+ // this might need special care when doing a OV uplift
+ std::shared_ptr<unsigned char> getData(size_t width,
+ size_t height,
+ ResizeType resize_type) override;
+};
+} // namespace FormatReader
+#endif
diff --git a/python/openvino/runtime/common/format_reader/register.h b/python/openvino/runtime/common/format_reader/register.h
new file mode 100644
index 0000000..781eca3
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/register.h
@@ -0,0 +1,58 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+/**
+ * \brief Register for readers
+ * \file register.h
+ */
+#pragma once
+
+#include <functional>
+#include <string>
+#include <vector>
+
+#include "format_reader.h"
+
+namespace FormatReader {
+/**
+ * \class Registry
+ * \brief Create reader from fabric
+ */
+class Registry {
+private:
+ typedef std::function<Reader*(const std::string& filename)> CreatorFunction;
+ static std::vector<CreatorFunction> _data;
+
+public:
+ /**
+ * \brief Create reader
+ * @param filename - path to input data
+ * @return Reader for input data or nullptr
+ */
+ static Reader* CreateReader(const char* filename);
+
+ /**
+ * \brief Registers reader in fabric
+ * @param f - a creation function
+ */
+ static void RegisterReader(CreatorFunction f);
+};
+
+/**
+ * \class Register
+ * \brief Registers reader in fabric
+ */
+template <typename To>
+class Register {
+public:
+ /**
+ * \brief Constructor creates creation function for fabric
+ * @return Register object
+ */
+ Register() {
+ Registry::RegisterReader([](const std::string& filename) -> Reader* {
+ return new To(filename);
+ });
+ }
+};
+} // namespace FormatReader
diff --git a/python/openvino/runtime/common/format_reader/yuv_nv12.cpp b/python/openvino/runtime/common/format_reader/yuv_nv12.cpp
new file mode 100644
index 0000000..f25c5cb
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/yuv_nv12.cpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+// clang-format off
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include "yuv_nv12.h"
+// clang-format on
+
+using namespace FormatReader;
+
+YUV_NV12::YUV_NV12(const std::string& filename) {
+ auto pos = filename.rfind('.');
+ if (pos == std::string::npos)
+ return;
+ if (filename.substr(pos + 1) != "yuv")
+ return;
+
+ std::ifstream file(filename, std::ios::binary);
+ if (!file.is_open()) {
+ return;
+ }
+
+ file.seekg(0, file.end);
+ _size = file.tellg();
+ file.seekg(0, file.beg);
+
+ _data.reset(new unsigned char[_size], std::default_delete<unsigned char[]>());
+
+ file.read(reinterpret_cast<char*>(_data.get()), _size);
+
+ file.close();
+}
diff --git a/python/openvino/runtime/common/format_reader/yuv_nv12.h b/python/openvino/runtime/common/format_reader/yuv_nv12.h
new file mode 100644
index 0000000..dd74c7b
--- /dev/null
+++ b/python/openvino/runtime/common/format_reader/yuv_nv12.h
@@ -0,0 +1,57 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * \brief YUV NV12 reader
+ * \file yuv_nv12.h
+ */
+#pragma once
+
+#include <memory>
+#include <string>
+
+// clang-format off
+#include "format_reader.h"
+#include "register.h"
+// clang-format on
+
+namespace FormatReader {
+/**
+ * \class YUV_NV12
+ * \brief Reader for YUV NV12 files
+ */
+class YUV_NV12 : public Reader {
+private:
+ static Register<YUV_NV12> reg;
+ size_t _size = 0;
+
+public:
+ /**
+ * \brief Constructor of YUV NV12 reader
+ * @param filename - path to input data
+ * @return YUV_NV12 reader object
+ */
+ explicit YUV_NV12(const std::string& filename);
+ virtual ~YUV_NV12() {}
+
+ /**
+ * \brief Get size
+ * @return size
+ */
+ size_t size() const override {
+ return _size;
+ }
+
+ // langsu: ResizeType is a added by us to support custom resizing functions (only in opencv_wrapper).
+ // format_reader is copied from openvino samples/cpp/common/format_reader/
+ // this might need special care when doing a OV uplift
+ std::shared_ptr<unsigned char> getData(size_t width, size_t height, Reader::ResizeType resize_type) override {
+ if ((width * height * 3 / 2 != size())) {
+ std::cout << "Image dimensions not match with NV12 file size \n";
+ return nullptr;
+ }
+ return _data;
+ }
+};
+} // namespace FormatReader