Search⌘ K
AI Features

Object Detector Using SDK - Upload, Train and Test the Model

Explore how to upload tagged images with object coordinates and train a custom object detection model using Azure Custom Vision SDK. This lesson guides you through preparing image data, uploading in batches, and initiating model training to build a detector for objects like forks and scissors.

We'll cover the following...

Let’s continue the implementation. Just to recap, in the previous lesson, we’ve created the training and prediction client objects. We’ve also created a project and added the two image tags that we want our model to identify.

Now, we’re going to upload the images with their coordinates and tags to our Custom Vision project and then we’ll train our custom model.

Uploading the images

When we build an object detection using the Web portal, we’ve tagged the location of the object to train the model. In a similar way, we need to specify the region of each tagged object. Below is the file that contains the coordinates of the two objects “Fork” and “Scissor” in the images. The coordinates are given in the following order: left, top, width, height.

object_data.json

Now let’s upload the images and their corresponding tags to the Custom Vision project.

C++
import json
base_image_location = "CourseAssets/ObjectDetection/Images"
base_data_location = "CourseAssets/ObjectDetection/"
with open(base_data_location + "object_data.json") as data:
obj_data = json.loads(data.read())
tagged_images_with_regions = []
for file_name in obj_data["fork_image_regions"].keys():
left, top, width, height = obj_data["fork_image_regions"][file_name]
regions = [Region(tag_id = fork_tag.id, left = left, top = top, width = width, height = height)]
with open(base_image_location + "/fork/" + file_name + ".jpg", mode="rb") as fork_image:
tagged_images_with_regions.append(
ImageFileCreateEntry(
name = file_name,
contents = fork_image.read(),
regions = regions
)
)
for file_name in obj_data["scissors_image_regions"].keys():
left, top, width, height = obj_data["scissors_image_regions"][file_name]
regions = [Region(tag_id = scissors_tag.id, left = left, top = top, width = width, height = height)]
with open(base_image_location + "/scissors/" + file_name + ".jpg", mode = "rb") as scissor_image:
tagged_images_with_regions.append(
ImageFileCreateEntry(
name = file_name,
contents = scissor_image.read(),
regions = regions
)
)
upload_result = trainer.create_images_from_files(
project.id,
ImageFileCreateBatch(
images = tagged_images_with_regions
)
)
if not upload_result.is_batch_successful:
print("Image batch upload failed.")
for image in upload_result.images:
print("Image status: ", image.status)
else:
print("Images uploaded Successfully.")
  • In line 1, we import the required package to read the JSON file.

  • In ...