如何在Azure自定义视觉ai中使用已标记的图像?



我有大约1000个带标签的图像。我如何在Azure自定义视觉中上传它,而不是从门户上传图像并重新标记它们?

我使用我开发的一个名为PyLabel的包构建了一个概念验证,用于将注释上传到Azure Custom Vision。你可以在这里看到https://github.com/pylabel-project/samples/blob/main/pylabel2azure_custom_vision.ipynb.

PyLabel可以从COCO, YOLO或VOC格式读取注解到数据框架中。一旦它们在数据框架中,你就可以遍历注释的数据框架,并使用自定义视觉api来上传图像和注释。

自定义视觉使用的注释格式类似于YOLO格式,因为它们都使用了0-1之间的标准化协调。

下面是上面提到的笔记本中的代码片段:

#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")

相关内容

  • 没有找到相关文章

最新更新