11#include " pch.h"
2-
2+ # include " FileHelper.h "
33#include " operators/customoperatorprovider.h"
44
55using namespace winrt ;
@@ -11,15 +11,6 @@ using namespace winrt::Windows::Storage;
1111using namespace winrt ::Windows::AI::MachineLearning;
1212using namespace std ;
1313
14- wstring GetModulePath () {
15- wchar_t wzModuleFilePath[MAX_PATH + 1 ];
16- GetModuleFileName (NULL , wzModuleFilePath, MAX_PATH + 1 );
17- wstring moduleFilePath (wzModuleFilePath);
18- return wstring (
19- moduleFilePath.begin (),
20- moduleFilePath.begin () + moduleFilePath.find_last_of (L" \\ " ));;
21- }
22-
2314struct CommandLineInterpreter
2415{
2516 vector<wstring> m_commandLineArgs;
@@ -32,7 +23,7 @@ struct CommandLineInterpreter
3223 {
3324 wchar_t wzModuleFilePath[MAX_PATH + 1 ];
3425 GetModuleFileName (NULL , wzModuleFilePath, MAX_PATH + 1 );
35- return GetModulePath () + L" \\ " + pName;
26+ return FileHelper:: GetModulePath () + L" \\ " + pName;
3627 }
3728
3829 wstring TryGetModelPath ()
@@ -90,59 +81,13 @@ using Session = LearningModelSession;
9081using Kind = LearningModelDeviceKind;
9182vector<string> labels;
9283
93- void LoadLabels ()
94- {
95- wstring labelsFileName = L" labels.txt" ;
96-
97- // Parse labels from labels file. We know the file's entries are already sorted in order.
98- wstring labelsFilePath = GetModulePath () + L" \\ " + labelsFileName;
99- ifstream labelFile (labelsFilePath, ifstream::in);
100- if (labelFile.fail ())
101- {
102- printf (" failed to load the %ls file. Make sure it exists in the same folder as the app\r\n " , labelsFileName.c_str ());
103- exit (EXIT_FAILURE);
104- }
105- string s;
106- while (getline (labelFile, s, ' ,' ))
107- {
108- int labelValue = atoi (s.c_str ());
109- if (static_cast <size_t >(labelValue) >= labels.size ())
110- {
111- labels.resize (labelValue + 1 );
112- }
113- getline (labelFile, s);
114- labels[labelValue] = s;
115- }
116- }
117-
118- VideoFrame LoadImageFile (hstring filePath)
119- {
120- try
121- {
122- // open the file
123- StorageFile file = StorageFile::GetFileFromPathAsync (filePath).get ();
124- // get a stream on it
125- auto stream = file.OpenAsync (FileAccessMode::Read).get ();
126- // Create the decoder from the stream
127- BitmapDecoder decoder = BitmapDecoder::CreateAsync (stream).get ();
128- // get the bitmap
129- SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync ().get ();
130- // load a videoframe from it
131- VideoFrame inputImage = VideoFrame::CreateWithSoftwareBitmap (softwareBitmap);
132- // all done
133- return inputImage;
134- }
135- catch (...)
136- {
137- printf (" failed to load the image file, make sure you are using fully qualified paths\r\n " );
138- exit (EXIT_FAILURE);
139- }
140- }
141-
14284void PrintResults (IVectorView<float > results)
14385{
14486 // load the labels
145- LoadLabels ();
87+ auto modulePath = FileHelper::GetModulePath ();
88+ std::string labelsFilePath =
89+ std::string (modulePath.begin (), modulePath.end ()) + " Labels.txt" ;
90+ labels = FileHelper::LoadLabels (labelsFilePath);
14691
14792 vector<pair<float , uint32_t >> sortedResults;
14893 for (uint32_t i = 0 ; i < results.Size (); i++) {
@@ -167,7 +112,7 @@ void RunSqueezeNet(Session session, Model model, hstring imagePath) {
167112
168113 // load the image
169114 printf (" Loading the image...\n " );
170- auto imageFrame = LoadImageFile (imagePath);
115+ auto imageFrame = FileHelper:: LoadImageFile (imagePath);
171116
172117 // bind the input image
173118 printf (" Binding...\n " );
0 commit comments