Object detection with Azure Custom Vision
Tags: cloud, azure, ai, tutorial
Published: 2023-01-24 14:21:10

Object detection in images allows us to know the coordinates of one or multiple objects (or labels) previously established. 

In this tutorial, we will learn how to create a computer vision model with the _Azure Custom Vision_ resource, for the recognition of objects (Bottles, Faces, Guitars, and Hats) in images.

**Important things:**  

To customize our model, this will be the process we must follow:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkealagumjbnveq84wqy.png)
 
_Access to Custom Vision:_ https://www.customvision.ai/

_Sample image dataset:_ [General Dataset](https://github.com/esdanielgomez/ObjectDetection/tree/main/General%20Dataset).

## **Previous process: Create a Custom Vision project.**

First, we will need to log in to the Custom Vision Low-Code portal with our Azure account credentials, and create a new _Object Detection project_. 

**Note:** We will need an Azure Custom Vision resource. This resource can be created from the Azure portal, or from this configuration wizard directly.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nifwlyxpex61vcz78phw.png)

## **Step 1: Upload images.**

As a first step, the first thing we will do is load the images (preferably in order, according to the objects that we are going to tag for object detection).

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rcoq2u562nh7sef3rpt.png)
 
By selecting the _Add Images_ option, we can select the images we want to upload. In this example, we will load the images to tag hat objects:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijk5pe8boroqpykgcipa.png)
 
Once the image upload is finished, we can see something like this:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo75xdnyszt1ahqob75g.png)
 
Now, with each image loaded, we must open each image, and select the area where the object we want to tag is located.

![Select object in the image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7owv1fvcmmfh1uuwf6sr.png)
 
**Note:** in an image we can select one or more areas to tag several objects.

![Image areas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w205r4xk9gz2q8bm1rrc.png)

In order to complete this image tagging process, it is important that we have tagged at least 15 images for each category. For our example, this is the number of images we have used on each tag: Bottle - 18, Face - 26, Guitar - 18, and Hat - 19.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugf0sshyior6g7mgqa1e.png)

## **Step 2. Train the model.**

Now that we have the most important thing (as in any other Machine Learning model) – _the data_, now we can do the training from this option:

![Train option](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9drx69oxf567ey3q729t.png) 

Here we can consider two types of training:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fgvc2mcgejkrm1qsus5.png)

On the one hand, we can perform the training in the shortest possible time according to the number of images that have been uploaded and the number of tags we have; And on the other hand, we can do an advanced training, in which we can find the best possible model, considering a maximum time that we can specify:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhe7u812swf9ptbynfvg.png)
 
When the training is finished, we can evaluate the model.

## **Step 3. Evaluate the model.**

In this step, in the _Performance_ section, we can analyze the model with three metrics: _Precision_, _Recall_, and _mAP_ (Mean Average Precision).

![General performance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5bruhmdpr9szww5fnft.png) 

![Performance per object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8abdfcc11dgwcbovptgy.png)
 
In general, these metrics help us analyze the following: 

> **Accuracy:** indicates the fraction of identified images that were correct. For example, if the model identified 100 images as hats and 99 of them were actually hats, the accuracy would be 99%.

> **Recall:** indicates the fraction of actual classifications that were correctly identified. For example, if there were actually 100 images with hats and the model identified 80 as hats, the recall would be 80%.

> **mAP:** is the mean value of the mean accuracy (AP). AP is the area under the accuracy/recovery curve (accuracy plotted versus recovery for each prediction made).

**Quick test:**

With our Azure Custom Vision model ready, from the _Quick Test_ option we can make a quick example:

![Quick Test option](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1yfqb8xru290isb6yy6t.png) 
 
Here we can upload an image from our computer or use the link of an online image. Likewise, we can establish the probability threshold, that is, show all the detected objects that have a probability greater than the one we specify.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j25dcw5vns49ncdfluu8.png)
 
**Example:** in the previous image we can see that there is a 99.8% probability that a Guitar is located in the specified area.

## **Plus: Publish the model.**

Now that we have an established and tested Custom Vision model, we can now publish it from the _Performance_ section, and select an _Iteration/Training_. 

To publish the model, we must specify a name, and the Custom Vision resource in Azure that allows us to make evaluations:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc9w4atleduiaj1hwghj.png)
 
When publishing the model, we can use this resource as a web API:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd041q06baavii67fm5a.png)
 
We could also export the model considering TensorFlow, CoreML, Docker container, among others. 

Here we can learn more about it: [Export your model for use with mobile devices](https://learn.microsoft.com/en-US/azure/cognitive-services/custom-vision-service/export-your-model).

## **Thanks for reading!**

If you have any questions or ideas in mind, it will be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.

See you on [Twitter](https://twitter.com/esDanielGomez) / [esDanielGomez.com](https://esdanielgomez.com).