Skip to content

Commit 26a0ad9

Browse files
Moved LoadImageFile And LoadLabel to SharedLIb
1 parent c563979 commit 26a0ad9

File tree

9 files changed

+95
-161
lines changed

9 files changed

+95
-161
lines changed

Samples/AdapterSelection/AdapterSelection/cpp/AdapterSelection.vcxproj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@
130130
<ConformanceMode>false</ConformanceMode>
131131
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
132132
<LanguageStandard>stdcpp17</LanguageStandard>
133-
<AdditionalIncludeDirectories>$(SolutionDir)\packages\rapidjson.v110.1.1.0\build\native\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
133+
<AdditionalIncludeDirectories>..\..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
134134
<CompileAsWinRT>false</CompileAsWinRT>
135135
</ClCompile>
136136
<Link>
137137
<SubSystem>Console</SubSystem>
138138
<GenerateDebugInformation>true</GenerateDebugInformation>
139-
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
139+
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;..\..\..\SampleSharedLib\$(Configuration)\SampleSharedLib.lib;%(AdditionalDependencies)</AdditionalDependencies>
140140
</Link>
141141
</ItemDefinitionGroup>
142142
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -149,11 +149,12 @@
149149
<ConformanceMode>true</ConformanceMode>
150150
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
151151
<LanguageStandard>stdcpp17</LanguageStandard>
152+
<AdditionalIncludeDirectories>..\..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
152153
</ClCompile>
153154
<Link>
154155
<SubSystem>Console</SubSystem>
155156
<GenerateDebugInformation>true</GenerateDebugInformation>
156-
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
157+
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;..\..\..\SampleSharedLib\x64\$(Configuration)\SampleSharedLib.lib;%(AdditionalDependencies)</AdditionalDependencies>
157158
</Link>
158159
</ItemDefinitionGroup>
159160
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
@@ -166,6 +167,7 @@
166167
<ConformanceMode>true</ConformanceMode>
167168
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
168169
<LanguageStandard>stdcpp17</LanguageStandard>
170+
<AdditionalIncludeDirectories>..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
169171
</ClCompile>
170172
<Link>
171173
<SubSystem>Console</SubSystem>
@@ -185,13 +187,14 @@
185187
<ConformanceMode>true</ConformanceMode>
186188
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
187189
<LanguageStandard>stdcpp17</LanguageStandard>
190+
<AdditionalIncludeDirectories>..\..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
188191
</ClCompile>
189192
<Link>
190193
<SubSystem>Console</SubSystem>
191194
<EnableCOMDATFolding>true</EnableCOMDATFolding>
192195
<OptimizeReferences>true</OptimizeReferences>
193196
<GenerateDebugInformation>true</GenerateDebugInformation>
194-
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
197+
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;..\..\..\SampleSharedLib\$(Configuration)\SampleSharedLib.lib;%(AdditionalDependencies)</AdditionalDependencies>
195198
</Link>
196199
</ItemDefinitionGroup>
197200
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -206,13 +209,14 @@
206209
<ConformanceMode>true</ConformanceMode>
207210
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
208211
<LanguageStandard>stdcpp17</LanguageStandard>
212+
<AdditionalIncludeDirectories>..\..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
209213
</ClCompile>
210214
<Link>
211215
<SubSystem>Console</SubSystem>
212216
<EnableCOMDATFolding>true</EnableCOMDATFolding>
213217
<OptimizeReferences>true</OptimizeReferences>
214218
<GenerateDebugInformation>true</GenerateDebugInformation>
215-
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
219+
<AdditionalDependencies>DXGI.lib;windowsapp.lib;ole32.lib;kernel32.lib;user32.lib;..\..\..\SampleSharedLib\x64\$(Configuration)\SampleSharedLib.lib;%(AdditionalDependencies)</AdditionalDependencies>
216220
</Link>
217221
</ItemDefinitionGroup>
218222
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
@@ -227,6 +231,7 @@
227231
<ConformanceMode>true</ConformanceMode>
228232
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
229233
<LanguageStandard>stdcpp17</LanguageStandard>
234+
<AdditionalIncludeDirectories>..\..\SampleSharedLib\SampleSharedLib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
230235
</ClCompile>
231236
<Link>
232237
<SubSystem>Console</SubSystem>

Samples/AdapterSelection/AdapterSelection/cpp/main.cpp

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "pch.h"
2+
#include "FileHelper.h"
23

34
using namespace winrt;
45
using namespace Windows::Foundation::Collections;
@@ -15,9 +16,6 @@ hstring modelPath;
1516
hstring imagePath;
1617

1718
// helper functions
18-
string GetModulePath();
19-
void LoadLabels();
20-
VideoFrame LoadImageFile(hstring filePath);
2119
void PrintResults(IVectorView<float> results);
2220
bool ParseArgs(int argc, char* argv[]);
2321
LearningModelDevice getLearningModelDeviceFromAdapter(com_ptr<IDXGIAdapter1> spAdapter);
@@ -88,7 +86,7 @@ int main(int argc, char* argv[])
8886

8987
// load the image
9088
printf("Loading the image...\n");
91-
auto imageFrame = LoadImageFile(imagePath);
89+
auto imageFrame = FileHelper::LoadImageFile(imagePath);
9290

9391
// bind the input image
9492
printf("Binding...\n");
@@ -149,74 +147,12 @@ bool ParseArgs(int argc, char* argv[])
149147
return true;
150148
}
151149

152-
string GetModulePath()
153-
{
154-
string val;
155-
char modulePath[MAX_PATH] = {};
156-
GetModuleFileNameA(NULL, modulePath, ARRAYSIZE(modulePath));
157-
char drive[_MAX_DRIVE];
158-
char dir[_MAX_DIR];
159-
char filename[_MAX_FNAME];
160-
char ext[_MAX_EXT];
161-
_splitpath_s(modulePath, drive, _MAX_DRIVE, dir, _MAX_DIR, filename, _MAX_FNAME, ext, _MAX_EXT);
162-
163-
val = drive;
164-
val += dir;
165-
return val;
166-
}
167-
168-
void LoadLabels()
169-
{
170-
// Parse labels from labels file. We know the file's entries are already sorted in order.
171-
std::string labelsFilePath = GetModulePath() + labelsFileName;
172-
ifstream labelFile(labelsFilePath, ifstream::in);
173-
if (labelFile.fail())
174-
{
175-
printf("failed to load the %s file. Make sure it exists in the same folder as the app\r\n", labelsFileName.c_str());
176-
exit(EXIT_FAILURE);
177-
}
178-
179-
std::string s;
180-
while (std::getline(labelFile, s, ','))
181-
{
182-
int labelValue = atoi(s.c_str());
183-
if (labelValue >= static_cast<int>(labels.size()))
184-
{
185-
labels.resize(labelValue + 1);
186-
}
187-
std::getline(labelFile, s);
188-
labels[labelValue] = s;
189-
}
190-
}
191-
192-
VideoFrame LoadImageFile(hstring filePath)
193-
{
194-
try
195-
{
196-
// open the file
197-
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
198-
// get a stream on it
199-
auto stream = file.OpenAsync(FileAccessMode::Read).get();
200-
// Create the decoder from the stream
201-
BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get();
202-
// get the bitmap
203-
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
204-
// load a videoframe from it
205-
VideoFrame inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
206-
// all done
207-
return inputImage;
208-
}
209-
catch (...)
210-
{
211-
printf("failed to load the image file, make sure you are using fully qualified paths\r\n");
212-
exit(EXIT_FAILURE);
213-
}
214-
}
215-
216150
void PrintResults(IVectorView<float> results)
217151
{
218152
// load the labels
219-
LoadLabels();
153+
auto modulePath = FileHelper::GetModulePath();
154+
std::string labelsFilePath = std::string(modulePath.begin(), modulePath.end()) + labelsFileName;
155+
labels = FileHelper::LoadLabels(labelsFilePath);
220156

221157
vector<pair<float, uint32_t>> sortedResults;
222158
for (uint32_t i = 0; i < results.Size(); i++) {

Samples/BatchSupport/BatchSupport/SampleHelper.cpp

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,6 @@ SoftwareBitmapToFloatVector(SoftwareBitmap softwareBitmap) {
6565
return outputVector;
6666
}
6767

68-
VideoFrame LoadImageFile(hstring filePath) {
69-
VideoFrame inputImage = nullptr;
70-
try {
71-
// open the file
72-
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
73-
// get a stream on it
74-
auto stream = file.OpenAsync(FileAccessMode::Read).get();
75-
// Create the decoder from the stream
76-
BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get();
77-
// get the bitmap
78-
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
79-
// load a videoframe from it
80-
inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
81-
} catch (...) {
82-
printf("failed to load the image file, make sure you are using fully "
83-
"qualified paths\r\n");
84-
exit(EXIT_FAILURE);
85-
}
86-
// all done
87-
return inputImage;
88-
}
89-
9068
hstring GetModelPath(std::string modelType) {
9169
hstring modelPath;
9270
if (modelType == "fixedBatchSize") {
@@ -104,7 +82,7 @@ TensorFloat CreateInputTensorFloat() {
10482
std::vector<float> inputVector = {};
10583
for (hstring imageName : imageNames) {
10684
auto imagePath = static_cast<hstring>(FileHelper::GetModulePath().c_str()) + imageName;
107-
auto imageFrame = LoadImageFile(imagePath);
85+
auto imageFrame = FileHelper::LoadImageFile(imagePath);
10886
std::vector<float> imageVector =
10987
SoftwareBitmapToFloatVector(imageFrame.SoftwareBitmap());
11088
inputVector.insert(inputVector.end(), imageVector.begin(), imageVector.end());
@@ -124,44 +102,19 @@ IVector<VideoFrame> CreateVideoFrames() {
124102
std::vector<VideoFrame> inputFrames = {};
125103
for (hstring imageName : imageNames) {
126104
auto imagePath = static_cast<hstring>(FileHelper::GetModulePath().c_str()) + imageName;
127-
auto imageFrame = LoadImageFile(imagePath);
105+
auto imageFrame = FileHelper::LoadImageFile(imagePath);
128106
inputFrames.emplace_back(imageFrame);
129107
}
130108
auto videoFrames = winrt::single_threaded_vector(std::move(inputFrames));
131109
return videoFrames;
132110
}
133111

134-
std::vector<std::string> LoadLabels(std::string labelsFilePath) {
135-
// Parse labels from labels file. We know the file's entries are already
136-
// sorted in order.
137-
std::vector<std::string> labels;
138-
std::ifstream labelFile{labelsFilePath, std::ifstream::in};
139-
if (labelFile.fail()) {
140-
printf("failed to load the %s file. Make sure it exists in the same "
141-
"folder as the app\r\n",
142-
labelsFilePath.c_str());
143-
exit(EXIT_FAILURE);
144-
}
145-
146-
std::string s;
147-
while (std::getline(labelFile, s, ',')) {
148-
int labelValue = atoi(s.c_str());
149-
if (labelValue >= labels.size()) {
150-
labels.resize(labelValue + 1);
151-
}
152-
std::getline(labelFile, s);
153-
labels[labelValue] = s;
154-
}
155-
156-
return labels;
157-
}
158-
159112
void PrintResults(IVectorView<float> results) {
160113
// load the labels
161114
auto modulePath = FileHelper::GetModulePath();
162115
std::string labelsFilePath =
163116
std::string(modulePath.begin(), modulePath.end()) + "Labels.txt";
164-
std::vector<std::string> labels = LoadLabels(labelsFilePath);
117+
std::vector<std::string> labels = FileHelper::LoadLabels(labelsFilePath);
165118
// SqueezeNet returns a list of 1000 options, with probabilities for each,
166119
// loop through all
167120
for (uint32_t batchId = 0; batchId < BATCH_SIZE; ++batchId) {

Samples/BatchSupport/BatchSupport/SampleHelper.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ namespace SampleHelper
1212
std::vector<float> SoftwareBitmapToFloatVector(
1313
winrt::Windows::Graphics::Imaging::SoftwareBitmap softwareBitmap);
1414

15-
// Load Image File as VideoFrame
16-
winrt::Windows::Media::VideoFrame LoadImageFile(
17-
winrt::hstring filePath);
18-
19-
// Load object detection labels
20-
std::vector<std::string> LoadLabels(std::string labelsFilePath);
21-
2215
// Create input Tensorfloats with 3 images.
2316
winrt::Windows::AI::MachineLearning::TensorFloat CreateInputTensorFloat();
2417

Samples/SampleSharedLib/SampleSharedLib/FileHelper.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
55

6+
using namespace winrt;
7+
using namespace winrt::Windows::Media;
8+
using namespace winrt::Windows::Graphics::Imaging;
9+
using namespace winrt::Windows::Storage;
10+
11+
612
namespace FileHelper {
713
std::wstring GetModulePath() {
814
std::wstring val;
@@ -20,4 +26,52 @@ namespace FileHelper {
2026

2127
return val;
2228
}
29+
30+
VideoFrame LoadImageFile(hstring filePath) {
31+
VideoFrame inputImage = nullptr;
32+
try {
33+
// open the file
34+
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
35+
// get a stream on it
36+
auto stream = file.OpenAsync(FileAccessMode::Read).get();
37+
// Create the decoder from the stream
38+
BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get();
39+
// get the bitmap
40+
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
41+
// load a videoframe from it
42+
inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
43+
}
44+
catch (...) {
45+
printf("failed to load the image file, make sure you are using fully "
46+
"qualified paths\r\n");
47+
exit(EXIT_FAILURE);
48+
}
49+
// all done
50+
return inputImage;
51+
}
52+
53+
std::vector<std::string> LoadLabels(std::string labelsFilePath) {
54+
// Parse labels from labels file. We know the file's entries are already
55+
// sorted in order.
56+
std::vector<std::string> labels;
57+
std::ifstream labelFile{ labelsFilePath, std::ifstream::in };
58+
if (labelFile.fail()) {
59+
printf("failed to load the %s file. Make sure it exists in the same "
60+
"folder as the app\r\n",
61+
labelsFilePath.c_str());
62+
exit(EXIT_FAILURE);
63+
}
64+
65+
std::string s;
66+
while (std::getline(labelFile, s, ',')) {
67+
int labelValue = atoi(s.c_str());
68+
if (labelValue >= labels.size()) {
69+
labels.resize(labelValue + 1);
70+
}
71+
std::getline(labelFile, s);
72+
labels[labelValue] = s;
73+
}
74+
75+
return labels;
76+
}
2377
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#pragma once
22
#include <string>
3+
#include <winrt/Windows.Media.h>
34
#include <Windows.h>
5+
46
namespace FileHelper
57
{
8+
// Get the Path of Executable
69
std::wstring GetModulePath();
10+
11+
// Load Image File as VideoFrame
12+
winrt::Windows::Media::VideoFrame LoadImageFile(
13+
winrt::hstring filePath);
14+
15+
// Load object detection labels
16+
std::vector<std::string> LoadLabels(std::string labelsFilePath);
717
}

Samples/SampleSharedLib/SampleSharedLib/dllload.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ int32_t __stdcall WINRT_RoGetActivationFactory(void* classId, winrt::guid const&
3535
}
3636
else
3737
{
38-
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), iid, factory);
38+
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), reinterpret_cast<GUID const&>(iid), factory);
3939
}
4040

4141
// If the library is not found, get the default one
4242
if (!library)
4343
{
44-
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), iid, factory);
44+
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), reinterpret_cast<GUID const&>(iid), factory);
4545
}
4646

4747
using DllGetActivationFactory = HRESULT __stdcall(HSTRING classId, void** factory);

Samples/SampleSharedLib/SampleSharedLib/pch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#ifndef PCH_H
22
#define PCH_H
33

4+
#include <winrt/Windows.Graphics.Imaging.h>
5+
#include <winrt/Windows.Storage.h>
6+
#include "winrt/Windows.Storage.Streams.h"
7+
8+
#include <Windows.h>
9+
410
#include <string>
511
#include <fstream>
612

0 commit comments

Comments
 (0)