microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

operators/cv2/imgcodecs/imread.hpp

50lines · modecode

1#include <opencv2/core.hpp>
2#include <opencv2/imgcodecs.hpp>
3
4#include "string_tensor.h"
5
6struct KernelImageReader : BaseKernel {
7 KernelImageReader(const OrtApi& api, const OrtKernelInfo& info) : BaseKernel(api, info) {
8 }
9
10 void Compute(OrtKernelContext* context) {
11 const OrtValue* input_data = ort_.KernelContext_GetInput(context, 0);
12 OrtTensorDimensions input_data_dimensions(ort_, input_data);
13
14 int n = input_data_dimensions[0];
15 if (n != 1) {
16 ORTX_CXX_API_THROW("[ImageReader]: the dimension of input value can only be 1 now.", ORT_INVALID_ARGUMENT);
17 }
18
19 std::vector<std::string> image_paths;
20 GetTensorMutableDataString(api_, ort_, context, input_data, image_paths);
21
22 cv::Mat img = cv::imread(image_paths[0], cv::IMREAD_COLOR);
23 std::vector<int64_t> output_dimensions = {1, img.size[0], img.size[1], static_cast<int64_t>(img.elemSize())};
24 OrtValue* output_image = ort_.KernelContext_GetOutput(context, 0, output_dimensions.data(), output_dimensions.size());
25 std::uint8_t* p_output_image = ort_.GetTensorMutableData<uint8_t>(output_image);
26 memcpy(p_output_image, img.data, img.total() * img.elemSize());
27 }
28};
29
30struct CustomOpImageReader : OrtW::CustomOpBase<CustomOpImageReader, KernelImageReader> {
31 size_t GetInputTypeCount() const {
32 return 1;
33 }
34
35 size_t GetOutputTypeCount() const {
36 return 1;
37 }
38
39 ONNXTensorElementDataType GetInputType(size_t index) const {
40 return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
41 }
42
43 ONNXTensorElementDataType GetOutputType(size_t index) const {
44 return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8;
45 }
46
47 const char* GetName() const {
48 return "ImageReader";
49 }
50};
51