Hello there,
I am trying to upload images to custom vision portal using python.
but when i am running the below code it is always throwing TypeError: create_images_from_files() missing 1 required positional argument: 'batch'
My code :
print ("Creating project...")
project = trainer.create_project("Bird Classification")
print("Project created!")
import os
os.chdir(r'D:\Custom Vision Sample\bird_photos')
tags = [name for name in os.listdir('.') if os.path.isdir(name)]
print(tags)
def createTag(tag):
result = trainer.create_tag(project.id, tag)
print(f'{tag} create with id: {result}')
return result.id
def createImageList(tag, tag_id):
base_image_url = fr"D:/Custom Vision Sample/bird_photos/{tag}/"
photo_name_list = os.listdir(base_image_url)
image_list = []
for file_name in photo_name_list:
with open(base_image_url+file_name, "rb") as image_contents:
image_list.append(ImageFileCreateEntry(name=base_image_url+file_name, contents=image_contents.read(), tag_ids=[tag_id]))
return image_list
def uploadImageList(image_list):
upload_result = trainer.create_images_from_files(project.id, images=image_list)
if not upload_result.is_batch_successful:
print("Image batch upload failed.")
for image in upload_result.images:
print("Image status: ", image.status)
exit(-1)
for tag in tags:
tag_id = createTag(tag)
print(f"tag creation done with tag id {tag_id}")
image_list = createImageList(tag, tag_id)
print("image_list created with length " + str(len(image_list)))
# Break list into lists of 25 and upload in batches
for i in range(0, len(image_list), 25):
batchofimages = image_list[i:i + 25]
print(f'Upload started for batch {i} total items {len(batchofimages)} for tag {tag}...')
uploadImageList(batchofimages)
print(f"Batch {i} Image upload completed. Total uploaded {len(batchofimages)} for tag {tag}")
@Lal Balaji Govind Krishna I think the issue is in the call to image_list.append()
While creating the list of images you are using the complete local file path for the file name instead of just the file name. This might be causing the batch of images to be incorrect before the call to uploadImageList() and its corresponding trainer.create_images_from_files()
Try using this for image_list.append()
image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[tag_id]))
Along with the following to catch any error exceptions for trainer.create_images_from_files()
trainer.create_images_from_files(project.id, images=image_list)
except HttpOperationError as e:
print(e.response.text)
exit(-1)