Object detection in camera stream with Azure Custom Vision service.
First of all you need active Azure Subscription. In case you don't have one yet just follow this instruction: Create your Azure free account today
The solution based on Create your Azure free account today. You need to create custom vision resource to run the example:
The solution uses the following nuget packages:
- Microsoft.Azure.CognitiveServices.Vision.ComputerVision
- Newtonsoft.Json
- OpenCvSharp3
Clone the repository:
git clone https://github.com/vzhukov/azure-bootcamp-2019.git
Open solution azure-cognitive-csharp\AzureLiveVideoStreamAnalysis.sln
in Visual Studio.
Restore nuget-packages:
Open App.config
and edit update app setting AzureCognitiveEndpoint
with your value from Azure Custom Vision service:
Update app settings AzureCognitiveKey
with your key:
Update app settings CameraIndex
with the device index wich you will use (default web camera index - 0).
Start the application to detect objects:
Init video capture for default device (index = 0)
car reader = new VideoCapture(0);
Init Azure custom vision client
var visionClient = new ComputerVisionClient("AzureCognitiveKey")
{
Endpoint = "AzureCognitiveEndpoint"
};
Read frame from camera:
var image = new Mat();
reader.Read(image);
Send image to Azure to recognize objects:
var imageBytes = image.ToBytes(".jpg");
using (var ms = new MemoryStream(imageBytes))
{
var task = await visionClient.DetectObjectsInStreamAsync(ms);
}
Parse the result:
var items = task.Objects;
// item.ObjectProperty
// item.Rectangle