Skip to content

Commit 5b8b7c7

Browse files
author
Sheil Kumar
committed
make random access stream impl load files
1 parent 3fb581b commit 5b8b7c7

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/OpenCVImage.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,46 @@ namespace abi_wss = ABI::Windows::Storage::Streams;
1919

2020
namespace winrt::WinMLSamplesGalleryNative::implementation
2121
{
22-
OpenCVImage::OpenCVImage(winrt::hstring path)
23-
{
24-
#ifdef USE_OPENCV
25-
image_ = cv::imread(winrt::to_string(path), cv::IMREAD_COLOR);
26-
#endif
27-
std::ifstream infile("C:\\Users\\sheil\\source\\repos\\App1\\SqueezeNet.onnx");
22+
static winrt::Microsoft::AI::MachineLearning::LearningModel CreateModelFromRandomAccessStreamReferenceFromPath(
23+
const char* path) {
24+
std::ifstream infile(path, std::ios_base::binary);
2825

2926
//get length of file
3027
infile.seekg(0, std::ios::end);
3128
size_t length = infile.tellg();
3229
infile.seekg(0, std::ios::beg);
3330

34-
auto buffer = std::unique_ptr<char[]>(new char[length]);
31+
// create buffer
32+
auto buffer = std::unique_ptr<byte[]>(new byte[length]);
3533

36-
//read file
37-
infile.read(buffer.get(), length);
34+
// read file
35+
infile.read(reinterpret_cast<char*>(buffer.get()), length);
3836

39-
winrt::com_ptr<abi_wss::IBuffer> ptr;
37+
// get start and end pointers
4038
auto start = reinterpret_cast<uint8_t*>(buffer.get());
4139
auto end = reinterpret_cast<uint8_t*>(buffer.get() + length);
40+
41+
// wrap bytes in weak buffer
42+
winrt::com_ptr<abi_wss::IBuffer> ptr;
4243
wrl::MakeAndInitialize<details::WeakBuffer<uint8_t>>(ptr.put(), start, end);
43-
winrt::com_ptr<abi_wss::IRandomAccessStreamReference> ras_ref;
44-
wrl::MakeAndInitialize<::WinMLSamplesGalleryNative::RandomAccessStreamReference>(ras_ref.put(), ptr.get());
4544

46-
winrt::Windows::Storage::Streams::IRandomAccessStreamReference stream_ref;
47-
winrt::attach_abi(stream_ref, ras_ref.get());
48-
winrt::Microsoft::AI::MachineLearning::LearningModel::LoadFromStream(stream_ref);
45+
// wrap buffer in random access stream
46+
wrl::ComPtr<ABI::Windows::Storage::Streams::IRandomAccessStreamReference> reference;
47+
wrl::MakeAndInitialize<::WinMLSamplesGalleryNative::RandomAccessStreamReference>(&reference, ptr.get());
48+
49+
winrt::Windows::Storage::Streams::IRandomAccessStreamReference random_access_stream;
50+
winrt::attach_abi(random_access_stream, reference.Detach());
51+
return winrt::Microsoft::AI::MachineLearning::LearningModel::LoadFromStream(random_access_stream);
52+
}
53+
54+
OpenCVImage::OpenCVImage(winrt::hstring path)
55+
{
56+
#ifdef USE_OPENCV
57+
image_ = cv::imread(winrt::to_string(path), cv::IMREAD_COLOR);
58+
#endif
59+
winrt::Microsoft::AI::MachineLearning::LearningModel::LoadFromFilePath(L"C:\\Users\\sheil\\source\\repos\\App1\\SqueezeNet.onnx");
60+
auto file_path = "C:\\Users\\sheil\\source\\repos\\App1\\SqueezeNet.onnx";
61+
auto model = CreateModelFromRandomAccessStreamReferenceFromPath(file_path);
4962
}
5063

5164
#ifdef USE_OPENCV

0 commit comments

Comments
 (0)