Azure Cognitive Services는 이미지 분석, 음성 인식, 번역 등 다양한 AI 기능을 API 형태로 제공하여 애플리케이션에 손쉽게 적용할 수 있습니다. 이번 포스트에서는 C#에서 Azure Cognitive Services 중 가장 많이 활용되는 Computer Vision API를 호출하는 방법을 간단히 살펴보겠습니다.
1. 사전 준비
1) Azure 포털에서 Computer Vision 리소스를 생성하고 Endpoint와 Key 값을 확인합니다.
2) .NET 프로젝트에 Microsoft.Azure.CognitiveServices.Vision.ComputerVision NuGet 패키지를 추가합니다.
dotnet add package Microsoft.Azure.CognitiveServices.Vision.ComputerVision
2. 기본 코드 구조
아래 예제는 이미지 URL을 전달받아 객체 탐지(Object Detection) 결과를 콘솔에 출력하는 간단한 프로그램입니다.
using System;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
namespace AzureCognitiveDemo
{
class Program
{
// Azure 포털에서 받은 값 입력
private const string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
private const string endpoint = "YOUR_ENDPOINT_URL";
static async Task Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("이미지 URL을 인수로 전달해주세요.");
return;
}
string imageUrl = args[0];
var client = Authenticate(endpoint, subscriptionKey);
await DetectObjectsAsync(client, imageUrl);
}
// 인증 및 클라이언트 생성
private static ComputerVisionClient Authenticate(string endpoint, string key)
{
var credentials = new ApiKeyServiceClientCredentials(key);
var client = new ComputerVisionClient(credentials)
{
Endpoint = endpoint
};
return client;
}
// 객체 탐지 실행
private static async Task DetectObjectsAsync(ComputerVisionClient client, string imageUrl)
{
Console.WriteLine($"이미지 분석 중: {imageUrl}\n");
// ObjectDetection 모델은 최신 API 버전에서 지원됩니다.
var result = await client.DetectObjectsAsync(imageUrl);
if (result.Objects.Count == 0)
{
Console.WriteLine("탐지된 객체가 없습니다.");
return;
}
foreach (var obj in result.Objects)
{
Console.WriteLine($"객체: {obj.ObjectProperty}\n신뢰도: {obj.Confidence:P2}\n위치: ({obj.Rectangle.X}, {obj.Rectangle.Y}), {obj.Rectangle.W}x{obj.Rectangle.H}\n");
}
}
}
}
3. 실전 활용 팁
• 비동기 처리를 적극 활용해 UI 차단을 방지합니다.
• 이미지가 로컬 파일인 경우 FileStream을 열어 client.ReadInStreamAsync 등 다른 메서드와 조합하면 됩니다.
• 비용 절감을 위해 Batch 요청이나 Rate Limiting을 구현하세요.
4. 마무리
Azure Cognitive Services는 REST API뿐 아니라 .NET SDK를 제공하므로, 위와 같은 짧은 코드로 강력한 AI 기능을 애플리케이션에 삽입할 수 있습니다. 필요에 따라 Vision, Speech, Language 등 다른 서비스와 조합해 보세요. 다음 포스트에서는 Speech to Text 연동 방법을 다루겠습니다.
'C#' 카테고리의 다른 글
| C# Record 구조체와 값 기반 레코드 설계 (0) | 2026.09.07 |
|---|---|
| C# Source Generator로 자동 매핑 코드 생성하기 (0) | 2026.09.07 |
| C#에서 자연어 처리와 텍스트 분석 (1) | 2026.09.05 |
| ML.NET을 사용한 기계 학습 모델 구현과 통합 (0) | 2026.09.04 |
| C#에서 그래프와 네트워크 알고리즘 구현 (0) | 2026.09.04 |