@@ -22,9 +22,10 @@ hstring imagePath;
2222// helper functions
2323string GetModulePath ();
2424void LoadLabels ();
25- VideoFrame LoadImageFile (hstring filePath);
25+ VideoFrame LoadImageFile (hstring filePath, ColorManagementMode colorManagementMode );
2626void PrintResults (IVectorView<float > results);
2727bool ParseArgs (int argc, char * argv[]);
28+ ColorManagementMode GetColorManagementMode (const LearningModel& model);
2829
2930wstring GetModelPath ()
3031{
@@ -57,9 +58,13 @@ int main(int argc, char* argv[])
5758 ticks = GetTickCount () - ticks;
5859 printf (" model file loaded in %d ticks\n " , ticks);
5960
61+ // get model color management mode
62+ printf (" Getting model color management mode...\n " );
63+ ColorManagementMode colorManagementMode = GetColorManagementMode (model);
64+
6065 // load the image
6166 printf (" Loading the image...\n " );
62- auto imageFrame = LoadImageFile (imagePath);
67+ auto imageFrame = LoadImageFile (imagePath, colorManagementMode );
6368 // now create a session and binding
6469 LearningModelSession session (model, LearningModelDevice (deviceKind));
6570 LearningModelBinding binding (session);
@@ -153,28 +158,76 @@ void LoadLabels()
153158 }
154159}
155160
156- VideoFrame LoadImageFile (hstring filePath)
161+ ColorManagementMode GetColorManagementMode (const LearningModel& model)
162+ {
163+ // Get model color space gamma
164+ hstring gammaSpace = L" " ;
165+ try
166+ {
167+ gammaSpace = model.Metadata ().Lookup (L" Image.ColorSpaceGamma" );
168+ }
169+ catch (...)
170+ {
171+ printf (" Model does not have color space gamma information. Will color manage to sRGB by default...\n " );
172+ }
173+ if (gammaSpace == L" " || _wcsicmp (gammaSpace.c_str (), L" SRGB" ) == 0 )
174+ {
175+ return ColorManagementMode::ColorManageToSRgb;
176+ }
177+ // Due diligence should be done to make sure that the input image is within the model's colorspace. There are multiple non-sRGB color spaces.
178+ printf (" Model metadata indicates that color gamma space is : %ws. Will not manage color space to sRGB...\n " , gammaSpace.c_str ());
179+ return ColorManagementMode::DoNotColorManage;
180+ }
181+
182+ VideoFrame LoadImageFile (hstring filePath, ColorManagementMode colorManagementMode)
157183{
184+ BitmapDecoder decoder = NULL ;
158185 try
159186 {
160187 // open the file
161188 StorageFile file = StorageFile::GetFileFromPathAsync (filePath).get ();
162189 // get a stream on it
163190 auto stream = file.OpenAsync (FileAccessMode::Read).get ();
164191 // Create the decoder from the stream
165- BitmapDecoder decoder = BitmapDecoder::CreateAsync (stream).get ();
166- // get the bitmap
167- SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync ().get ();
168- // load a videoframe from it
169- VideoFrame inputImage = VideoFrame::CreateWithSoftwareBitmap (softwareBitmap);
170- // all done
171- return inputImage;
192+ decoder = BitmapDecoder::CreateAsync (stream).get ();
172193 }
173194 catch (...)
174195 {
175- printf (" failed to load the image file, make sure you are using fully qualified paths\r\n " );
196+ printf (" Failed to load the image file, make sure you are using fully qualified paths\r\n " );
176197 exit (EXIT_FAILURE);
177198 }
199+ SoftwareBitmap softwareBitmap = NULL ;
200+ try
201+ {
202+ softwareBitmap = decoder.GetSoftwareBitmapAsync (
203+ decoder.BitmapPixelFormat (),
204+ decoder.BitmapAlphaMode (),
205+ BitmapTransform (),
206+ ExifOrientationMode::RespectExifOrientation,
207+ colorManagementMode
208+ ).get ();
209+ }
210+ catch (hresult_error hr)
211+ {
212+ printf (" Failed to create SoftwareBitmap! Please make sure that input image is within the model's colorspace.\n " );
213+ printf (" %ws\n " , hr.message ().c_str ());
214+ exit (hr.code ());
215+ }
216+ VideoFrame inputImage = NULL ;
217+ try
218+ {
219+ // load a videoframe from it
220+ inputImage = VideoFrame::CreateWithSoftwareBitmap (softwareBitmap);
221+ }
222+ catch (hresult_error hr)
223+ {
224+ printf (" Failed to create videoframe from software bitmap." );
225+ printf (" %ws\n " , hr.message ().c_str ());
226+ exit (hr.code ());
227+ }
228+ // all done
229+ return inputImage;
230+
178231}
179232
180233void PrintResults (IVectorView<float > results)
0 commit comments