99namespace WindowsAISample
1010{
1111 // This is a sample application that demonstrates how to use the Windows AI APIs
12- // to perform text recognition and summarization on an image.
12+ // to perform text recognition and image description on an image.
1313 // To learn more about the Windows AI API usage, visit https://learn.microsoft.com/windows/ai/apis/
1414 public partial class MainForm : Form
1515 {
1616 private string pathToImage = string . Empty ;
17- private LanguageModel ? languageModel = null ;
17+ private ImageDescriptionGenerator ? imageDescriptionGenerator = null ;
1818 private TextRecognizer ? textRecognizer = null ;
1919
2020 public MainForm ( )
@@ -89,14 +89,14 @@ private async void ProcessButton_Click(object sender, EventArgs e)
8989
9090 try
9191 {
92- richTextBoxForImageSummary . Text = "Summarizing image text ..." ;
93- await SummarizeImageText ( textInImage ) ;
92+ richTextBoxForImageSummary . Text = "Describing image..." ;
93+ await DescribeImage ( ) ;
9494 }
9595 catch ( Exception ex )
9696 {
9797 TaskDialog . ShowDialog ( this , new TaskDialogPage {
9898 Caption = "Windows AI WinForms Sample Error" ,
99- Heading = "An error occurred while summarizing the text in the image" ,
99+ Heading = "An error occurred while describing the image" ,
100100 Text = ex . Message ,
101101 Icon = TaskDialogIcon . Error ,
102102 Buttons = new TaskDialogButtonCollection
@@ -111,52 +111,41 @@ private async void ProcessButton_Click(object sender, EventArgs e)
111111 private async Task LoadAIModels ( )
112112 {
113113 // Load the AI models needed for image processing
114- switch ( LanguageModel . GetReadyState ( ) )
114+ switch ( ImageDescriptionGenerator . GetReadyState ( ) )
115115 {
116116 case Microsoft . Windows . AI . AIFeatureReadyState . NotReady :
117- System . Diagnostics . Debug . WriteLine ( "Ensure LanguageModel is ready" ) ;
118- var op = await LanguageModel . EnsureReadyAsync ( ) ;
119- System . Diagnostics . Debug . WriteLine ( $ "LanguageModel .EnsureReadyAsync completed with status: { op . Status } ") ;
120- if ( op . Status != Microsoft . Windows . AI . AIFeatureReadyResultState . Success )
117+ System . Diagnostics . Debug . WriteLine ( "Ensure ImageDescriptionGenerator is ready" ) ;
118+ var opImage = await ImageDescriptionGenerator . EnsureReadyAsync ( ) ;
119+ System . Diagnostics . Debug . WriteLine ( $ "ImageDescriptionGenerator .EnsureReadyAsync completed with status: { opImage . Status } ") ;
120+ if ( opImage . Status != Microsoft . Windows . AI . AIFeatureReadyResultState . Success )
121121 {
122- richTextBoxForImageSummary . Text = "Language model not ready for use" ;
123- throw new Exception ( "Language model not ready for use" ) ;
122+ richTextBoxForImageSummary . Text = "Image description generator not ready for use" ;
123+ throw new Exception ( "Image description generator not ready for use" ) ;
124124 }
125125 break ;
126126 case Microsoft . Windows . AI . AIFeatureReadyState . DisabledByUser :
127- System . Diagnostics . Debug . WriteLine ( "Language model disabled by user" ) ;
128- richTextBoxForImageSummary . Text = "Language model disabled by user" ;
127+ System . Diagnostics . Debug . WriteLine ( "Image Description Generator disabled by user" ) ;
128+ richTextBoxForImageSummary . Text = "Image description generator disabled by user" ;
129129 return ;
130130 case Microsoft . Windows . AI . AIFeatureReadyState . NotSupportedOnCurrentSystem :
131- System . Diagnostics . Debug . WriteLine ( "Language model not supported on current system" ) ;
132- richTextBoxForImageSummary . Text = "Language model not supported on current system" ;
131+ System . Diagnostics . Debug . WriteLine ( "Image Description Generator not supported on current system" ) ;
132+ richTextBoxForImageSummary . Text = "Image description generator not supported on current system" ;
133133 return ;
134134 }
135135
136- try
136+ imageDescriptionGenerator = await ImageDescriptionGenerator . CreateAsync ( ) ;
137+ if ( imageDescriptionGenerator == null )
137138 {
138- // There is a bug in 1.8 where the LanguageModel.GetReadyState() is not
139- // returning the correct state. The call to CreateAsync() will throw
140- // an exception if the state is not ready.The sample application
141- // will log the error and continue for now.
142- languageModel = await LanguageModel . CreateAsync ( ) ;
143- if ( languageModel == null )
144- {
145- throw new Exception ( "Failed to create LanguageModel instance." ) ;
146- }
147- }
148- catch ( Exception ex )
149- {
150- System . Diagnostics . Debug . WriteLine ( $ "Error creating LanguageModel: { ex . Message } ") ;
139+ throw new Exception ( "Failed to create ImageDescriptionGenerator instance." ) ;
151140 }
152141
153142 switch ( TextRecognizer . GetReadyState ( ) )
154143 {
155144 case Microsoft . Windows . AI . AIFeatureReadyState . NotReady :
156145 System . Diagnostics . Debug . WriteLine ( "Ensure TextRecognizer is ready" ) ;
157- var op = await TextRecognizer . EnsureReadyAsync ( ) ;
158- System . Diagnostics . Debug . WriteLine ( $ "TextRecognizer.EnsureReadyAsync completed with status: { op . Status } ") ;
159- if ( op . Status != Microsoft . Windows . AI . AIFeatureReadyResultState . Success )
146+ var opText = await TextRecognizer . EnsureReadyAsync ( ) ;
147+ System . Diagnostics . Debug . WriteLine ( $ "TextRecognizer.EnsureReadyAsync completed with status: { opText . Status } ") ;
148+ if ( opText . Status != Microsoft . Windows . AI . AIFeatureReadyResultState . Success )
160149 {
161150 richTextBoxForImageText . Text = "Text recognizer not ready for use" ;
162151 throw new Exception ( "Text recognizer not ready for use" ) ;
@@ -197,6 +186,26 @@ private async Task<string> PerformTextRecognition()
197186 return text ;
198187 }
199188
189+ private async Task DescribeImage ( )
190+ {
191+ ImageBuffer ? imageBuffer = await LoadImageBufferFromFileAsync ( pathToImage ) ;
192+
193+ if ( imageBuffer == null )
194+ {
195+ throw new Exception ( "Failed to load image buffer." ) ;
196+ }
197+
198+ ContentFilterOptions filterOptions = new ( ) ;
199+ var result = await imageDescriptionGenerator ! . DescribeAsync ( imageBuffer , ImageDescriptionKind . BriefDescription , filterOptions ) ;
200+
201+ if ( result . Status != ImageDescriptionResultStatus . Complete )
202+ {
203+ throw new Exception ( $ "Image description failed with status: { result . Status } ") ;
204+ }
205+
206+ richTextBoxForImageSummary . Text = result . Description ;
207+ }
208+
200209 private async Task < ImageBuffer ? > LoadImageBufferFromFileAsync ( string filePath )
201210 {
202211 StorageFile file = await StorageFile . GetFileFromPathAsync ( filePath ) ;
@@ -208,45 +217,5 @@ private async Task<string> PerformTextRecognition()
208217 return bitmap != null ? ImageBuffer . CreateForSoftwareBitmap ( bitmap ) : null ;
209218 }
210219 }
211-
212- private async Task SummarizeImageText ( string text )
213- {
214- string systemPrompt = "You summarize user-provided text to a software developer audience." +
215- "Respond only with the summary and no additional text." ;
216-
217- // Update the property names to match the correct ones based on the provided type signature.
218- var promptMaxAllowedSeverityLevel = new TextContentFilterSeverity {
219- Hate = SeverityLevel . Low ,
220- Sexual = SeverityLevel . Low ,
221- Violent = SeverityLevel . Low ,
222- SelfHarm = SeverityLevel . Low
223- } ;
224-
225- var responseMaxAllowedSeverityLevel = new TextContentFilterSeverity {
226- Hate = SeverityLevel . Low ,
227- Sexual = SeverityLevel . Low ,
228- Violent = SeverityLevel . Low ,
229- SelfHarm = SeverityLevel . Low
230- } ;
231-
232- var contentFilterOptions = new ContentFilterOptions {
233- PromptMaxAllowedSeverityLevel = promptMaxAllowedSeverityLevel ,
234- ResponseMaxAllowedSeverityLevel = responseMaxAllowedSeverityLevel
235- } ;
236-
237- if ( languageModel != null )
238- {
239- // Create a context for the language model
240- var languageModelContext = languageModel ! . CreateContext ( systemPrompt , contentFilterOptions ) ;
241- string prompt = "Summarize the following text: " + text ;
242- var output = await languageModel . GenerateResponseAsync ( languageModelContext , prompt , new LanguageModelOptions ( ) ) ;
243- richTextBoxForImageSummary . Text = output . Text ;
244- }
245- else
246- {
247- System . Diagnostics . Debug . WriteLine ( "Error: LanguageModel is null but should have been created during LoadAIModels()" ) ;
248- richTextBoxForImageSummary . Text = "Error: LanguageModel is null" ;
249- }
250- }
251220 }
252221}
0 commit comments