AI Image Recognition with .NET: A Practical Guide

Updated on Nov 11,2025

Table of Contents

Unlock the power of AI in your .NET applications! This guide walks you through building an image recognition system using Microsoft's cutting-edge AI libraries and large language models. Learn to configure services and analyze images with ease.

Key Points

Utilize Microsoft.Extensions.AI for AI integration in .NET.

Leverage large language models (LLMs) via Ollama for local image recognition.

Configure dependency injection for streamlined service management.

Process images and extract meaningful information with AI.

Employ structured responses to enhance data usability.

Adapt code for various image recognition tasks.

Getting Started with AI Image Recognition in .NET

Setting Up Your .NET Environment

Let's embark on creating an AI-powered Image Recognition application using .NET. Our starting point involves setting up a console application.

The first step is to install Microsoft.Extensions.Hosting. This package provides the necessary hosting infrastructure to configure services and leverage dependency injection containers effectively, streamlining the development process. Make sure you've installed this package to make your AI adventure smooth and stable. This will be the foundation for building intelligent applications using .NET and AI. By including pre-release versions, you gain access to the latest features and improvements, ensuring your application is on the cutting edge of AI technology.

Integrating Microsoft.Extensions.AI

The core of our image recognition system relies on the Microsoft.Extensions.AI library. Since it's currently in preview, enabling pre-release versions is essential. The library provides a range of tools and utilities for working with generative AI components.

To install, use the NuGet Package Manager, and make sure that the 'Include prerelease' box is checked, so that the correct package can be located. It simplifies interaction with AI models by offering abstractions for common tasks, like interacting with local Ollama models and other generative AI components.

Here's a breakdown of the NuGet packages we'll need to install:

Package Name Description
Microsoft.Extensions.AI Utilities for working with generative AI components.
Microsoft.Extensions.AI.Ollama Implementation of generative AI abstractions for Ollama, which supports local LLM interactions.

Make sure the versions you are using are the latest available pre-release versions to take advantage of the most recent bug fixes and features!

Configuring Dependency Injection for AI Services

With the necessary libraries installed, the next step is to configure dependency injection.

Here's how you can incorporate AI services into your .NET application:

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.AI.ChatClient;

var builder = Host.CreateApplicationBuilder();

builder.Services.AddChatClient(new OllamaChatClient(new Uri("http://localhost:11434"), "llava:7b"));

var app = builder.Build();

This code snippet configures your application to use an Ollama chat client, which will allow us to process images. The URL specifies where the Ollama service is hosted (in this case, locally), and 'llava:7b' denotes the specific large language model to be used. LLaVA is a large multimodal model. By adding these configurations, your application can smoothly interact with generative AI. By setting up the DI container this way, your application is primed to perform AI-driven tasks, including image recognition and analysis.

Harnessing Ollama and Vision Models for Image Recognition

Leveraging Ollama for Local LLM Hosting

Ollama presents a streamlined, lightweight framework that enables you to run significant language models directly on your local machine. This eliminates reliance on external servers or cloud services, providing benefits for development and experimentation.

You can access a comprehensive selection of available models that you can implement, including vision models like llava, by visiting the Ollama website. Selecting a smaller vision model, like the 7b version of llava, balances between precision and computational efficiency.

Before incorporating any of this code into your .NET application, first ensure that Ollama has been properly installed on your system. Now, let's get this 7b model running:

 ollama run llava:7b

After executing this command, Ollama will download (if necessary) and launch the 'llava:7b' vision model. At this stage, you can immediately begin interacting with this model via the command line interface. This direct interaction is invaluable for both testing the model and validating the configuration of your setup before incorporating it into the .NET project.

Crafting Prompts for Image Recognition

Here’s how to formulate prompts, and send your first request with a data URL:

  1. Open the command line for ollama

  2. Run the following command:

    /path/to/your/image.png
  3. Then, type your request

  4. Type the following command:

    Explain what can be seen in the image.

    For those familiar with AI prompts, it can be that simple.

Step-by-Step Guide to Image Recognition Implementation

Preparing Image Data for Processing

The initial step is to prepare your images for analysis. It shows how to read the image into the application. To achieve this, the image file should be converted into an array of bytes, making it compatible for transmission to the AI model.

File.ReadAllBytes("images/traffic_cam_1.png")

This snippet reads the contents of the image at the file path 'images/traffic_cam_1.png' and turns it into an array of bytes. This byte array is then encapsulated inside an AIChatContent object, which assigns the image's media type, like “image/png.”

Composing the Chat Message with Image

To communicate with the LLM, a ChatMessage object is created. It is important to set the ChatRole to user for clear direction. Here’s how to structure your message:

var message = new ChatMessage(ChatRole.User, "What's in this image?");

message.Contents.Add(new DataContent(File.ReadAllBytes("images/traffic_cam_1.png"), "image/png"));

This code constructs a chat message that includes both the textual Prompt (“What’s in this image?”) and the prepared image data as content. This structured message is ready for submission to the AI model.

Communicating and Extracting the Response

To ensure everything operates correctly, it is important to leverage dependency injection to retrieve your configured ChatClient, and then forward the constructed message. You can use the GetResponseAsync method:

var chatClient = app.Services.GetRequiredService<IChatClient>();
var response = await chatClient.GetResponseAsync(message);
Console.WriteLine(response.Message.Text);

This action involves retrieving the response from the language model and displaying it in the console. The resulting output shows what the AI detects in the given image, completing the cycle of image data processing.

Structuring the AI Response

Enhance the utility of AI-generated data by structuring the responses to better integrate into your projects. This involves defining a custom C# class that matches the anticipated structure of the response and using tools for parsing. Consider this approach for parsing more comprehensive response:

public record TrafficCamResult
{
    public string CameraName { get; set; }
    public TrafficStatus Status { get; set; }
    public int NumberOfCars { get; set; }
    public int NumberOfTrucks { get; set; }
    public int NumberOfPedestrians { get; set; }
}

By receiving data in a structured format, you simplify subsequent steps in data manipulation and integration. To leverage more complex or powerful tools, it is essential to consider the integration of the paid model from Open AI.

Advantages and Limitations of AI-Powered Image Recognition in .NET

👍 Pros

Enhanced automation and efficiency.

Improved data accessibility.

Extensibility to various business processes.

Local .net applications, using Ollama.

👎 Cons

High implementation costs and dependency on models.

Ethical considerations with the bias and AI.

Not always highly accurate if bad prompts and data provided.

Frequently Asked Questions

Can I use other models besides llava with Ollama?
Yes, Ollama supports a variety of models. You can use any vision-supported model that is available in the Ollama model library. You can find it at ollama.com
What if my images are not in PNG format?
You need to convert images to compatible formats if the model you are using cannot recognize your image format. Use standard image processing tools, or you may want to update your prompt.
How can I deploy this image recognition application to a server?
To deploy your app, containerize it with Docker and deploy using a CI/CD pipeline through Azure DevOps. Ensure your CI/CD pipeline is in place so that your production matches your staging.

Related Questions

How can I improve the accuracy of image recognition?
There are some techniques in the code that can be used to improve the accuracy of the image recognition. First, carefully curate the images, ensuring clear visibility. Second, experiment with different prompts to improve accuracy. Third, consider better, paid models from Open AI.

Most people like