static async Task Main(string[] args) { var key = Environment.GetEnvironmentVariable("COMPUTER_VISION_API_KEY"); var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = "https://visiondemosb.cognitiveservices.azure.com/" }; using var imageStream = File.OpenRead(args.FirstOrDefault() ?? "sample.jpg"); using var cts = new CancellationTokenSource(); _ = Task.Factory.StartNew(async() => { while (!cts.Token.IsCancellationRequested) { Console.Write("."); await Task.Delay(500); } }, cts.Token); var result = await client.DetectObjectsInStreamAsync(imageStream); cts.Cancel(); Console.WriteLine(); foreach (var obj in result.Objects) { Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence}"); } }
/* * END - DETECT OBJECTS - URL IMAGE */ /* * DETECT OBJECTS - LOCAL IMAGE * This is an alternative way to detect objects, instead of doing so through AnalyzeImage. */ public static async Task DetectObjectsLocal(ComputerVisionClient client, string localImage) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("DETECT OBJECTS - LOCAL IMAGE"); Console.WriteLine(); using (Stream stream = File.OpenRead(localImage)) { // Make a call to the Computer Vision service using the local file DetectResult results = await client.DetectObjectsInStreamAsync(stream); Console.WriteLine($"Detecting objects in local image {Path.GetFileName(localImage)}..."); Console.WriteLine(); // For each detected object in the picture, print out the bounding object detected, confidence of that detection and bounding box within the image Console.WriteLine("Detected objects:"); if (results.Objects != null) { foreach (var obj in results.Objects) { Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " + $"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}"); } } Console.WriteLine(); } }
/// <summary> /// Detect Shapes /// </summary> /// <param name="imageStream">Image Stream</param> /// <returns>Results Received from the Computer Vision Service</returns> public async Task <DetectResult> DetectShapes(Stream imageStream) { imageStream.ThrowIfNull(nameof(imageStream)); var result = await _client.DetectObjectsInStreamAsync(imageStream); return(result); }
// Analyze a local image private static async Task DetectObjectsFromStreamAsync(ComputerVisionClient computerVision, string imagePath) { if (!File.Exists(imagePath)) { Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { DetectResult analysis = await computerVision.DetectObjectsInStreamAsync(imageStream); Console.WriteLine(imagePath); DisplayObjects(analysis); } }
static async Task Main(string[] args) { var key = Environment.GetEnvironmentVariable("COMPUTER_VISION_API_KEY"); var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = "https://visiondemosb.cognitiveservices.azure.com/" }; using var imageStream = File.OpenRead(args.FirstOrDefault() ?? "sample.jpg"); var result = await client.DetectObjectsInStreamAsync(imageStream); foreach (var obj in result.Objects) { Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence}"); } }
/* * END - DETECT OBJECTS - URL IMAGE */ /* * DETECT OBJECTS - LOCAL IMAGE * This is an alternative way to detect objects, instead of doing so through AnalyzeImage. */ public static async Task <DetectResult> DetectObjectsLocal(ComputerVisionClient client, string localImage) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("DETECT OBJECTS - LOCAL IMAGE"); Console.WriteLine(); using (Stream stream = File.OpenRead(localImage)) { Console.WriteLine($"Detecting objects in local image {Path.GetFileName(localImage)}..."); Console.WriteLine(); // Make a call to the Computer Vision service using the local file DetectResult results = await client.DetectObjectsInStreamAsync(stream); // For each detected object in the picture, print out the bounding object detected, confidence of that detection and bounding box within the image Console.WriteLine("Detected objects:"); Console.WriteLine(); return(results); } }
private async Task <DetectResult> DetectObjectsAsync(string imageUrl) { Stream stream = File.OpenRead(imageUrl); return(await visionClient.DetectObjectsInStreamAsync(stream)); }