I have around 1000 labeled images with me. How can I upload it in Azure custom vision instead of uploading the images and retagging them from the portal?
Asked
Active
Viewed 309 times
0
-
So you want to find a way that uploads images by code instead of on portal manually? If so just let me know your programming language and I'll provide you with a demo – Stanley Gong Mar 02 '21 at 04:47
-
yes, I want to upload using code. Python – Namita Menon Mar 03 '21 at 07:24
-
Have you tried this ?https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python#upload-and-tag-images – Stanley Gong Mar 03 '21 at 07:28
-
No, I will try this. Thanks for sharing. – Namita Menon Mar 03 '21 at 11:12
-
@NamitaMenon Any luck here? – Bhargavi Annadevara May 01 '21 at 11:12
1 Answers
0
I built a proof of concept using a package that I developed called PyLabel to upload annotations to Azure Custom Vision. You can see it here https://github.com/pylabel-project/samples/blob/main/pylabel2azure_custom_vision.ipynb.
PyLabel can read annotations from COCO, YOLO, or VOC format into a dataframe. Once they are in the data frame you can loop through the dataframe of annotations and use the Custom Vision APIs to upload the images and annotations.
The annotation format used by Custom Vision similar to the YOLO format because they both used normalized coordinated between 0-1.
Here is a snippet of the code from the notebook mentioned above:
#Iterate the rows for each image in the dataframe
for img_filename, img_df in dataset.df.groupby('img_filename'):
img_path = str(PurePath(dataset.path_to_annotations, str(img_df.iloc[0].img_folder), img_filename))
assert exists(img_path), f"File does not exist: {img_path}"
#Create a region object for each bounding box in the dataset
regions = []
for index, row in img_df.iterrows():
#Normalize the boundings box coordinates between 0 and 1
x = Decimal(row.ann_bbox_xmin / row.img_width).min(1)
y = Decimal(row.ann_bbox_ymin / row.img_height).min(1)
w = Decimal(row.ann_bbox_width / row.img_width).min(1-x)
h = Decimal(row.ann_bbox_height / row.img_height).min(1-y)
regions.append(Region(
tag_id=tags[row.cat_name].id,
left=x,
top=y,
width=w,
height=h
)
)
#Create an object with the image and all of the annotations for that image
with open(img_path, mode="rb") as image_contents:
image_and_annotations = [ImageFileCreateEntry(name=img_filename, contents=image_contents.read(), regions=regions)]
#Upload the image and all annnotations for that image
upload_result = trainer.create_images_from_files(
project.id,
ImageFileCreateBatch(images=image_and_annotations)
)
#If upload is not successful, print details about that image for debugging
if not upload_result.is_batch_successful:
print("Image upload failed.")
for image in upload_result.images:
print(img_path)
print("Image status: ", image.status)
print(regions)
#This will take a few minutes
print("Upload complete")

alexheat
- 479
- 5
- 9