Skip to content

Commit

Permalink
.Net: Sample image to text - Small fix (#5492)
Browse files Browse the repository at this point in the history
### Motivation and Context

- ImageContent MimeType is not set in constructor, but in property.
- `ImageFormat` was not properly set when reading the File Stream.
  • Loading branch information
RogerBarreto committed Mar 15, 2024
1 parent a580225 commit bf64bdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 25 additions & 6 deletions dotnet/samples/HuggingFaceImageTextExample/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,25 @@ private void UpdateImageDescription(string description)
/// <param name="pictureBox">The target <see cref="PictureBox"/>.</param>
/// <returns>Returns a <see cref="ImageContent"/>.</returns>
private static ImageContent CreateImageContentFromPictureBox(PictureBox pictureBox)
=> new(ConvertImageToReadOnlyMemory(pictureBox.Image))
=> new(ConvertImageToReadOnlyMemory(pictureBox))
{
MimeType = GetMimeType(pictureBox.Tag?.ToString()!)
};

/// <summary>
/// Converts an <see cref="Image"/> to a <see cref="ReadOnlyMemory{T}"/>.
/// Gets the image binary array from a <see cref="PictureBox"/>.
/// </summary>
/// <param name="image">The target <see cref="Image"/>.</param>
/// <param name="pictureBox">The target <see cref="PictureBox"/>.</param>
/// <returns>Returns image binary array.</returns>
private static ReadOnlyMemory<byte> ConvertImageToReadOnlyMemory(Image image)
private static ReadOnlyMemory<byte> ConvertImageToReadOnlyMemory(PictureBox pictureBox)
{
var image = pictureBox.Image;
var fileName = pictureBox.Tag.ToString()!;

using var memoryStream = new MemoryStream();

// Save the image to the MemoryStream, using PNG format for example
image.Save(memoryStream, ImageFormat.Jpeg);
image.Save(memoryStream, GetImageFormat(fileName));

// Optionally, reset the position of the MemoryStream to the beginning
memoryStream.Position = 0;
Expand Down Expand Up @@ -189,7 +193,22 @@ private static string GetMimeType(string fileName)
".tiff" => "image/tiff",
".ico" => "image/x-icon",
".svg" => "image/svg+xml",
_ => "application/octet-stream"
_ => throw new NotSupportedException("Unsupported image format.")
};
}

private static ImageFormat GetImageFormat(string fileName)
{
return Path.GetExtension(fileName) switch
{
".jpg" or ".jpeg" => ImageFormat.Jpeg,
".png" => ImageFormat.Png,
".gif" => ImageFormat.Gif,
".bmp" => ImageFormat.Bmp,
".tiff" => ImageFormat.Tiff,
".ico" => ImageFormat.Icon,
".svg" => ImageFormat.MemoryBmp,
_ => throw new NotSupportedException("Unsupported image format.")
};
}

Expand Down
6 changes: 3 additions & 3 deletions dotnet/samples/HuggingFaceImageTextExample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ var service = this._kernel.GetRequiredService<IImageToTextService>();
Once one of the images is selected, the binary data of the image is retrieved and sent to the ImageToText Service. The service then returns the descriptive text of the image. The following code snippet demonstrates how to use the ImageToText Service to retrieve the descriptive text of an image:

```csharp
// Get the binary content of an image:
var imageBinary = File.ReadAllBytes("path/to/file");
// Get the binary content of a JPEG image:
var imageBinary = File.ReadAllBytes("path/to/file.jpg");

// Prepare the image to be sent to the LLM
var imageContent = new ImageContent(imageBinary, "image/jpeg"));
var imageContent = new ImageContent(imageBinary) { MimeType = "image/jpeg" };

// Retrieves the image description
var textContent = await service.GetTextContentAsync(imageContent);
Expand Down

0 comments on commit bf64bdd

Please sign in to comment.