def migrate_images(src_trainer, dest_trainer, project_id, dest_project_id,
                   created_tags):
    # Migrate any tagged images that may exist and preserve their tags and regions.
    count = src_trainer.get_tagged_image_count(project_id)
    print("Found:", count, "tagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_tagged_images(project_id,
                                               take=count_to_migrate,
                                               skip=migrated)
        for i in images:
            print("Migrating", i.id, i.image_uri)
            regions = []
            for r in i.regions:
                print("Found region:", r.region_id, r.tag_id, r.left, r.top,
                      r.width, r.height)
                regions.append(
                    Region(tag_id=created_tags[r.tag_id],
                           left=r.left,
                           top=r.top,
                           width=r.width,
                           height=r.height))

            entry = ImageUrlCreateEntry(url=i.image_uri, regions=regions)
            dest_trainer.create_images_from_urls(dest_project_id,
                                                 images=[entry])
        migrated += count_to_migrate
        count -= count_to_migrate

    # Migrate any untagged images that may exist.
    count = src_trainer.get_untagged_image_count(project_id)
    print("Found:", count, "untagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_untagged_images(project_id,
                                                 take=count_to_migrate,
                                                 skip=migrated)
        for i in images:
            print("Migrating", i.id, i.image_uri)
            regions = []
            for r in i.regions:
                print("Found region:", r.region_id, r.tag_id, r.left, r.top,
                      r.width, r.height)
                regions.append(
                    Region(tag_id=created_tags[r.tag_id],
                           left=r.left,
                           top=r.top,
                           width=r.width,
                           height=r.height))

            entry = ImageUrlCreateEntry(url=i.image_uri, regions=regions)
            dest_trainer.create_images_from_urls(dest_project_id,
                                                 images=[entry])
        migrated += count_to_migrate
        count -= count_to_migrate
    return images
Example #2
0
def add_train_dev_images(farmid, runid, gridlablel_list, tag_dict):
    SEP = '/'
    run_images_path = str(farmid) + SEP + str(
        runid) + SEP + "rgb"  #Change this if required
    train_image_list = []
    try:
        blob_service_client = BlobServiceClient.from_connection_string(
            BlobStorage.CONNECT_STR)
        container_client = blob_service_client.get_container_client(
            BlobStorage.TEST_CONTAINER)

        for feedback_grid in gridlablel_list:
            grid_id = feedback_grid["grid_id"]
            rel_image_path = run_images_path + SEP + grid_id + ".png"
            source_blob_url = container_client.primary_endpoint + "/" + rel_image_path  #Get Test Image URL
            dest_image_name = str(farmid) + SEP + str(
                grid_id
            ) + "-" + feedback_grid["label"] + ".png"  #Set Name for Dest Image
            dest_image_url = copy_blob_from_url(
                source_blob_url, dest_image_name, feedback_grid["split"]
            )  #Upload to Train Container with new name

            if feedback_grid["split"] == "train":
                image_tag_id = tag_dict[feedback_grid["label"]]
                train_image_list.append(
                    ImageUrlCreateEntry(url=dest_image_url,
                                        tag_ids=[image_tag_id]))

        return train_image_list

    except Exception as e:
        logging.error("Error in add_train_dev_images" + str(e))
Example #3
0
 def _transform(self, df: DataFrame) -> DataFrame:
     imagesDF = df.toPandas().set_index('urls').T.to_dict('list')
     for img in imagesDF:
       product_img_link = img
       product_img_link = product_img_link.replace("bloomingdales.com", "bloomingdalesassets.com")
       tagList = imagesDF[img]
       summary = trainer.create_images_from_urls(self.project_id, [ImageUrlCreateEntry(url=product_img_link,tag_ids=tagList)])
     return df
Example #4
0
 def create_image_url_list(self, project: Project, labelled_blobs: [LabelledBlob])-> [ImageUrlCreateEntry]:
     labeller = Labeller()
     image_url_create_list = []
     for labelled_blob in labelled_blobs:
         tag_ids = []
         for label in labelled_blob.labels:
             tag_ids.append(labeller.add_label_if_not_exists(self.training_client, project, label).id)
         image_url_create_list.append( ImageUrlCreateEntry(url=labelled_blob.download_url, tag_ids=tag_ids ))
     return image_url_create_list
Example #5
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(
        'ML Professoar HTTP trigger function AddLabeledDataClient processed a request.'
    )

    try:
        # retrieve the parameters from the multi-part form http request
        image_url = req.form.get('ImageUrl')
        image_labeling_json = req.form.get('DataLabels')
    except:
        return func.HttpResponse(
            "Please pass JSON containing the labeled regions associated with this image on the query string or in the request body.",
            status_code=400)

    # validate both parameters were passed into the function
    if image_url and image_labeling_json:
        labels = []
        count_of_labels_applied_to_image = 0

        endpoint = os.environ['ClientEndpoint']

        # Get Cognitive Services Environment Variables
        project_id = os.environ["ProjectID"]
        training_key = os.environ['TrainingKey']

        # load labeled image regions passed in request into dictionary
        image_labeling_data = json.loads(image_labeling_json)

        # instanciate custom vision client
        trainer = CustomVisionTrainingClient(training_key, endpoint=endpoint)

        # retrieve tags from project and loop through them to find tag ids for tags that need to be applied to the image
        tags = trainer.get_tags(project_id)
        image_label = image_labeling_data["label"]
        for tag in tags:
            if tag.name == image_label:
                labels.append(tag.id)
                count_of_labels_applied_to_image = count_of_labels_applied_to_image + 1
                break

        # create the image from a url and attach the appropriate tags as labels.
        upload_result = trainer.create_images_from_urls(
            project_id, [ImageUrlCreateEntry(url=image_url, tag_ids=labels)])
        if upload_result.is_batch_successful:
            return func.HttpResponse(
                str(count_of_labels_applied_to_image) +
                " Tag(s) applied to image at url: " + image_url)
        else:
            return func.HttpResponse("Upload of " + image_url + " failed.")

    else:
        return func.HttpResponse(
            "Please pass a URL to a blob file containing the image to be added to training in this request on the query string.",
            status_code=400)
Example #6
0
def store_raw_images(link, maxcount, project, tag):
    image_urls = urllib.request.urlopen(link).read().decode()
    pic_num = 1

    while (pic_num < maxcount):
        for i in image_urls.split('\n'):
            try:
                print("url: %s" % (i))
                request = urllib.request.urlopen(i, timeout=10)
                trainer.create_images_from_urls(
                    project.id, [ImageUrlCreateEntry(url=i, tag_ids=[tag.id])])
                pic_num += 1
            except Exception as e:
                print(str(e))
Example #7
0
    def upload_images(self, labels: List, container_name) -> None:
        """
            Takes as input a list of labels, uploads all assosiated images to Azure Custom Vision project.
            If label in input already exists in Custom Vision project, all images are uploaded directly.
            If label in input does not exist in Custom Vision project, new label (Tag object in Custom Vision) is created before uploading images

            Parameters:
            labels (str[]): List of labels

            Returns:
            None
        """
        url_list = []
        existing_tags = list(self.trainer.get_tags(self.project_id))

        try:
            container = self.blob_service_client.get_container_client(
                container_name)
        except Exception as e:
            print(
                "could not find container with CONTAINER_NAME name error: ",
                str(e),
            )

        for label in labels:
            # check if input has correct type
            if not isinstance(label, str):
                raise Exception("label " + str(label) + " must be a string")

            tag = [t for t in existing_tags if t.name == label]
            # check if tag already exists
            if len(tag) == 0:
                try:
                    tag = self.trainer.create_tag(self.project_id, label)
                    print("Created new label in project: " + label)
                except Exception as e:
                    print(e)
                    continue
            else:
                tag = tag[0]

            blob_prefix = f"{label}/"
            blob_list = container.list_blobs(name_starts_with=blob_prefix)

            if not blob_list:
                raise AttributeError("no images for this label")

            # build correct URLs and append to URL list
            for blob in blob_list:
                blob_url = f"{self.base_img_url}/{container_name}/{blob.name}"
                url_list.append(
                    ImageUrlCreateEntry(url=blob_url, tag_ids=[tag.id]))

        # upload URLs in chunks of 64
        print("Uploading images from blob to CV")
        img_f = 0
        img_s = 0
        img_d = 0
        itr_img = 0
        chunks = self.__chunks(url_list, setup.CV_MAX_IMAGES)
        num_imgs = len(url_list)
        error_messages = set()
        for url_chunk in chunks:
            upload_result = self.trainer.create_images_from_urls(
                self.project_id, images=url_chunk)
            if not upload_result.is_batch_successful:
                for image in upload_result.images:
                    if image.status == "OK":
                        img_s += 1
                    elif image.status == "OKDuplicate":
                        img_d += 1
                    else:
                        error_messages.add(image.status)
                        img_f += 1

                    itr_img += 1
            else:
                batch_size = len(upload_result.images)
                img_s += batch_size
                itr_img += batch_size

            prc = itr_img / num_imgs
            print(
                f"\t succesfull: \033[92m {img_s:5d} \033]92m \033[0m",
                f"\t duplicates: \033[33m {img_d:5d} \033]33m \033[0m",
                f"\t failed: \033[91m {img_f:5d} \033]91m \033[0m",
                f"\t [{prc:03.2%}]",
                sep="",
                end="\r",
                flush=True,
            )

        print()
        if len(error_messages) > 0:
            print("Error messages:")
            for error_message in error_messages:
                print(f"\t {error_message}")
Example #8
0
project = trainer.create_project("dtademo2")
print("Project ID is {0} name {1}".format(project.id, project.name))

# Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")

base_image_url = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/"

print("Adding images...")
for image_num in range(1, 10):
    image_url = base_image_url + "Images/Hemlock/hemlock_{}.jpg".format(
        image_num)
    trainer.create_images_from_urls(
        project.id,
        [ImageUrlCreateEntry(url=image_url, tag_ids=[hemlock_tag.id])])

for image_num in range(1, 10):
    image_url = base_image_url + "Images/Japanese Cherry/japanese_cherry_{}.jpg".format(
        image_num)
    trainer.create_images_from_urls(
        project.id,
        [ImageUrlCreateEntry(url=image_url, tag_ids=[cherry_tag.id])])

# Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
# they can be added by using the following:
#
#import os
#hemlock_dir = "Images\\Hemlock"
#for image in os.listdir(os.fsencode("Images\\Hemlock")):
#    with open(hemlock_dir + "\\" + os.fsdecode(image), mode="rb") as img_data:
Example #9
0
# Create a new project
for name in categoryList:
    #If the project already exists than no new models will be created
    if name not in existingProjects:
        arcpy.AddMessage("Creating Model {}...".format(name))
        project = trainer.create_project(name)

        #Negative Tag Name
        negTagname = "Not_{}".format(name)

        #Make two tags in the new project
        positive_tag = trainer.create_tag(project.id, name)
        negative_tag = trainer.create_tag(project.id, negTagname)

        imageEntryList = [ImageUrlCreateEntry(url=image_url, tag_ids=[positive_tag.id]) for image_url in imageList(name)]
        negEntryList = [ImageUrlCreateEntry(url=image_url, tag_ids=[negative_tag.id]) for image_url in imageList(negTagname)]

        arcpy.AddMessage("Loading training photos into model...")
        for imgChunk in imageListChunks(imageEntryList, 63):
            trainer.create_images_from_urls(project.id,imgChunk)
        for imgChunk in imageListChunks(negEntryList, 63):
            trainer.create_images_from_urls(project.id,imgChunk)
        arcpy.AddMessage("Training Model...")
        iteration = trainer.train_project(project.id)
        while iteration.status == "Training":
            iteration = trainer.get_iteration(project.id, iteration.id)
            time.sleep(3)

        iteration_name = name + "classifyModel"
        # The iteration is now trained. Make it the default project endpoint
Example #10
0
# Create a new project
print("Creating project...")
project = trainer.create_project("CodeRed")

# Make two tags in the new project
top_tag = trainer.create_tag(project.id, "Top")
bottom_tag = trainer.create_tag(project.id, "Bottom")

base_image_url = "https://raw.githubusercontent.com/insung3511/A2ZObject-Data/"

print("Adding images...")
for image_num in range(1, 10):
    image_url = base_image_url + "master/bottom/opencv{}.png".format(image_num)
    trainer.create_images_from_urls(
        project.id,
        [ImageUrlCreateEntry(url=image_url, tag_ids=[bottom_tag.id])])
    print("Image State Bottom : ", image_num)

for image_num in range(1, 10):
    image_url = base_image_url + "master/top/opencv{}.png".format(image_num)
    trainer.create_images_from_urls(
        project.id, [ImageUrlCreateEntry(url=image_url, tag_ids=[top_tag.id])])
    print("Image State Top : ", image_num)

print("Training...")
iteration = trainer.train_project(project.id)
while (iteration.status != "Completed"):
    iteration = trainer.get_iteration(project.id, iteration.id)
    print("Training status: " + iteration.status)
    time.sleep(1)
Example #11
0
def process_training_car_df(training_car_df):

    # gets all the tags in the project
    tag_list = trainer.get_tags(project_id)

    img_name_list = list()
    img_dict = dict()

    # instantiate a dictionary and fill up keys
    for row_index, row in training_car_df.iterrows():
        image_name = row['ImageFilename']
        img_dict[image_name] = list()
        img_name_list.append(image_name)

    # Divide images to appropriate tags <-- create df for each tag ***strategize this part***
    classifier_list = list(training_car_df.columns.values)[
        3:16]  # This gives us all the feature names

    #use this for loop to simply create tags and store them in img_dict
    for classifier in classifier_list:

        if classifier == 'Location':
            img_dict = three_answer_classifier(classifier, CONST_URBAN,
                                               CONST_SUBURBAN, CONST_RURAL,
                                               training_car_df, img_dict,
                                               tag_list)

        elif classifier == 'Time':
            img_dict = two_answer_classifier(classifier, CONST_DAY,
                                             CONST_NIGHT, training_car_df,
                                             img_dict, tag_list)

        elif classifier == 'Weather':
            img_dict = three_answer_classifier(classifier, CONST_CLEAR,
                                               CONST_RAINY, CONST_SNOWY,
                                               training_car_df, img_dict,
                                               tag_list)

        elif classifier == 'RoadCondition':
            img_dict = three_answer_classifier(classifier, CONST_DRY,
                                               CONST_WET, CONST_SNOWY,
                                               training_car_df, img_dict,
                                               tag_list)

        elif classifier == 'Severity':
            img_dict = five_answer_classifier(classifier, CONST_ZERO,
                                              CONST_ONE, CONST_TWO,
                                              CONST_THREE, CONST_FOUR,
                                              training_car_df, img_dict,
                                              tag_list)

        # Any classifier that has yes/no answer goes to else
        else:
            img_dict = yes_no_classifier(classifier, training_car_df, img_dict,
                                         tag_list)

    print("\nAdding images...")
    # this is for YesDamage
    for img_name in img_name_list:
        image_url = base_image_url + img_name
        tag_list = img_dict[img_name]

        trainer.create_images_from_urls(
            project_id, [ImageUrlCreateEntry(url=image_url, tag_ids=tag_list)])

        # print('uploading {} \t Tags: {}' .format(image_url, tag_list))
        print('uploading {} '.format(image_url))
Example #12
0
def migrate_images(src_trainer, dest_trainer, project_id, dest_project_id,
                   created_tags):
    # Migrate any tagged images that may exist and preserve their tags and regions.
    count = src_trainer.get_tagged_image_count(project_id)
    print("Found:", count, "tagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_tagged_images(project_id,
                                               take=count_to_migrate,
                                               skip=migrated)
        images_to_upload = []
        for i in images:
            print("Migrating", i.id, i.original_image_uri)
            if i.regions:
                regions = []
                for r in i.regions:
                    print("Found region:", r.region_id, r.tag_id, r.left,
                          r.top, r.width, r.height)
                    regions.append(
                        Region(tag_id=created_tags[r.tag_id],
                               left=r.left,
                               top=r.top,
                               width=r.width,
                               height=r.height))
                entry = ImageUrlCreateEntry(url=i.original_image_uri,
                                            regions=regions)
            else:
                tag_ids = []
                for t in i.tags:
                    print("Found tag:", t.tag_name, t.tag_id)
                    tag_ids.append(created_tags[t.tag_id])
                entry = ImageUrlCreateEntry(url=i.original_image_uri,
                                            tag_ids=tag_ids)

            images_to_upload.append(entry)

        upload_result = dest_trainer.create_images_from_urls(
            dest_project_id, images=images_to_upload)
        if not upload_result.is_batch_successful:
            print("ERROR: Failed to upload image batch")
            for i in upload_result.images:
                print("\tImage status:", i.id, i.status)
            exit(-1)

        migrated += count_to_migrate
        count -= count_to_migrate

    # Migrate any untagged images that may exist.
    count = src_trainer.get_untagged_image_count(project_id)
    print("Found:", count, "untagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_untagged_images(project_id,
                                                 take=count_to_migrate,
                                                 skip=migrated)
        images_to_upload = []
        for i in images:
            print("Migrating", i.id, i.original_image_uri)
            images_to_upload.append(
                ImageUrlCreateEntry(url=i.original_image_uri))

        upload_result = dest_trainer.create_images_from_urls(
            dest_project_id, images=images_to_upload)
        if not upload_result.is_batch_successful:
            print("ERROR: Failed to upload image batch")
            for i in upload_result.images:
                print("\tImage status:", i.id, i.status)
            exit(-1)
        migrated += count_to_migrate
        count -= count_to_migrate
    return images
# Make two tags in the new project (if the tags are not found)
if Hemlock_tag_count == 0:
	hemlock_tag = trainer.create_tag(project.id, "Hemlock")
	print("Creating Tag Hemlock")
if JapaneseCherry_tag_count == 0:
	cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
	print("Creating Tag Japanese Cherry")


###################################### 3. Upload images to the project ##########################################################
base_image_url = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/"

print ("Adding images...")
for image_num in range(1,10):
    image_url = base_image_url + "Images/Hemlock/hemlock_{}.jpg".format(image_num)
    trainer.create_images_from_urls(project.id, [ ImageUrlCreateEntry(url=image_url, tag_ids=[ hemlock_tag.id ] ) ])

for image_num in range(1,10):
    image_url = base_image_url + "Images/Japanese Cherry/japanese_cherry_{}.jpg".format(image_num)
    trainer.create_images_from_urls(project.id, [ ImageUrlCreateEntry(url=image_url, tag_ids=[ cherry_tag.id ] ) ])


# Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
# they can be added by using the following:
#
#import os
#hemlock_dir = "Images\\Hemlock"
#for image in os.listdir(os.fsencode("Images\\Hemlock")):
#    with open(hemlock_dir + "\\" + os.fsdecode(image), mode="rb") as img_data: 
#        trainer.create_images_from_data(project.id, img_data, [ hemlock_tag.id ])
#
Example #14
0
print("Creating project...")
project = trainer.create_project("drawings")

# Make two tags in the new project
bench_tag = trainer.create_tag(project.id, "bench")
ambulance_tag = trainer.create_tag(project.id, "ambulance")
print(ambulance_tag.id)

print("Adding images...")
url_list = []

for blob in ambulance_container.list_blobs():
    blob_name = blob.name
    blob_url = f"{base_image_url}ambulance/{blob_name}"
    url_list.append(
        ImageUrlCreateEntry(url=blob_url, tag_ids=[ambulance_tag.id]))

for blob in bench_container.list_blobs():
    blob_name = blob.name
    blob_url = f"{base_image_url}bench/{blob_name}"
    url_list.append(ImageUrlCreateEntry(url=blob_url, tag_ids=[bench_tag.id]))

for url_chunk in chunks(url_list, 64):
    upload_result = trainer.create_images_from_urls(project.id,
                                                    images=url_chunk)
    if not upload_result.is_batch_successful:
        print("Image batch upload failed.")
        for image in upload_result.images:
            if image.status != "OKDUPLICATE":
                print(image.source_url)
                print(image)
Example #15
0
    """
    using yahoo (this script can't use at google)
    """
    url = 'http://image.search.yahoo.co.jp/search?n=60&p={}&search.x=1'.format(quote(word))
    byte_content, _ = fetcher.fetch(url)
    structured_page = BeautifulSoup(byte_content.decode('UTF-8'), 'html.parser')
    img_link_elems = structured_page.find_all('a', attrs={'target': 'imagewin'})
    img_urls = [e.get('href') for e in img_link_elems if e.get('href').startswith('http')]
    img_urls = list(set(img_urls))
    return img_urls

if __name__ == '__main__': 

# Replace with a valid key
    trainer = training_api.TrainingApi(training_key)    
    project = trainer.create_project("FlowerNameProject")    
    
    df=pd.read_csv("flowerlist.csv", header=None)
    for i, rows in df.iterrows():
        word = rows[0] + " 花"
        print(word)
        tag = trainer.create_tag(project.id, rows[0])
        for j, img_url in enumerate(img_url_list(word)):
            if j < 15:
    	        print(img_url)
    	        trainer.create_images_from_urls(project.id, [ ImageUrlCreateEntry(url=img_url, tag_ids=[tag.id])])
    
    
    print ("Training...")
    iteration = trainer.train_project(project.id)
Example #16
0
for blob_name in bbs.list_blob_names(args.storage_container):
    if (counter < limit):
        print("attempting blob: {}".format(blob_name))
        now = datetime.utcnow()
        permission = BlobPermissions(read=True)
        sas_token = bbs.generate_blob_shared_access_signature(
            container_name=args.storage_container,
            blob_name=blob_name,
            start=now,
            expiry=now + timedelta(minutes=15),
            permission=permission)
        sas_url = bbs.make_blob_url(container_name=args.storage_container,
                                    blob_name=blob_name,
                                    sas_token=sas_token)
        print(sas_url)
        image_entry = ImageUrlCreateEntry(url=sas_url)
        image_block.append(image_entry)
        counter += 1
    else:
        print("attempting upload...")
        upload_result = train_client.create_images_from_urls(
            project_id=args.cv_project_id, images=image_block)
        check_print_failure(upload_result)
        image_block = []
        counter = 0
        print("COMPLETED BLOCK")

print(len(image_block))
upload_result = train_client.create_images_from_urls(
    project_id=args.cv_project_id, images=image_block)
check_print_failure(upload_result)
# Create a new project
print("Creating project...")
project = trainer.create_project("My Project")

# Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")

base_image_url = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/"

print("Adding images...")
for image_num in range(1, 10):
    image_url = base_image_url + "Images/Hemlock/hemlock_{}.jpg".format(
        image_num)
    trainer.create_images_from_urls(
        project.id, [ImageUrlCreateEntry(image_url, [hemlock_tag.id])])

for image_num in range(1, 10):
    image_url = base_image_url + "Images/Japanese Cherry/japanese_cherry_{}.jpg".format(
        image_num)
    trainer.create_images_from_urls(
        project.id, [ImageUrlCreateEntry(image_url, [cherry_tag.id])])

# Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
# they can be added by using the following:
#
#import os
#hemlock_dir = "Images\\Hemlock"
#for image in os.listdir(os.fsencode("Images\\Hemlock")):
#    with open(hemlock_dir + "\\" + os.fsdecode(image), mode="rb") as img_data:
#        trainer.create_images_from_data(project.id, img_data.read(), [ hemlock_tag.id ])
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(
        'ML Professoar HTTP trigger function AddLabeledDataClient processed a request.'
    )

    dataBlobUrl = req.params.get('dataBlobUrl')
    if not dataBlobUrl:
        return func.HttpResponse(
            "Please pass a URL to a blob containing the image to be added to training in this request on the query string.",
            status_code=400)

    if dataBlobUrl:
        ImageLabelsJson = req.params.get('imageLabels')
        if not ImageLabelsJson:
            try:
                ImageLabelsJson = req.get_json()
            except:
                return func.HttpResponse(
                    "Please pass JSON containing the labels associated with this image on the query string or in the request body.",
                    status_code=400)

        if ImageLabelsJson:
            # https://pypi.org/project/custom_vision_client/
            # https://github.com/Azure-Samples/cognitive-services-python-sdk-samples/blob/master/samples/vision/custom_vision_training_samples.py

            Labels = []
            CountOfTagsAppliedToTimage = 0
            Endpoint = os.environ['clientEndpoint']

            # Get Cognitive Services Environment Variables
            ProjectID = os.environ["projectID"]
            TrainingKey = os.environ['trainingKey']

            # strip out list of Image Labels passed in request
            ImageLabels = json.loads(ImageLabelsJson)

            # retrieve tags from project and loop through them to find tag ids for tags that need to be applied to the image
            Trainer = CustomVisionTrainingClient(TrainingKey,
                                                 endpoint=Endpoint)
            Tags = Trainer.get_tags(ProjectID)
            for ImageLabel in ImageLabels:
                for Tag in Tags:
                    if Tag.name == ImageLabel:
                        Labels.append(Tag.id)
                        CountOfTagsAppliedToTimage = CountOfTagsAppliedToTimage + 1
                        break

            # create the image from a url and attach the appropriate tags as labels.
            upload_result = Trainer.create_images_from_urls(
                ProjectID,
                [ImageUrlCreateEntry(url=dataBlobUrl, tag_ids=Labels)])
            if upload_result.is_batch_successful:
                return func.HttpResponse(
                    str(CountOfTagsAppliedToTimage) +
                    " Tag(s) applied to image at url: " + dataBlobUrl)
            else:
                return func.HttpResponse("Upload of " + dataBlobUrl +
                                         " failed.")

    else:
        return func.HttpResponse(
            "Please pass a URL to a blob file containing the image to be added to training in this request on the query string.",
            status_code=400)
def migrate_images(src_trainer, dest_trainer, project_id, dest_project,
                   created_tags):
    # Migrate any tagged images that may exist and preserve their tags and regions.
    count = src_trainer.get_tagged_image_count(project_id)
    print("Found:", count, "tagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_tagged_images(project_id,
                                               take=count_to_migrate,
                                               skip=migrated)
        images_to_upload = []
        for i in images:
            print("Migrating", i.id, i.original_image_uri)
            if i.regions:
                regions = []
                tag_ids = []
                for r in i.regions:
                    print("Found region:", r.region_id, r.tag_id, r.left,
                          r.top, r.width, r.height)
                    regions.append(
                        Region(tag_id=created_tags[r.tag_id],
                               left=r.left,
                               top=r.top,
                               width=r.width,
                               height=r.height))
                    tag_ids.append(created_tags[r.tag_id])
                entry = ImageUrlCreateEntry(url=i.original_image_uri,
                                            regions=regions)
            else:
                tag_ids = []
                for t in i.tags:
                    print("Found tag:", t.tag_name, t.tag_id)
                    tag_ids.append(created_tags[t.tag_id])
                entry = ImageUrlCreateEntry(url=i.original_image_uri,
                                            tag_ids=tag_ids)

            images_to_upload.append(entry)
            image_file = '{}-{}.jpg'.format(i.id, "_".join(tag_ids))
            xml_file = '{}-{}.xml'.format(i.id, "_".join(tag_ids))
            image = None
            if not os.path.exists('export/' + image_file):
                r = requests.get(i.original_image_uri)
                with open('export/' + image_file, 'wb') as f:
                    f.write(r.content)
                    image = Image.open(BytesIO(r.content))
            else:
                image = Image.open('export/' + image_file)
            w, h = image.size
            annotation = ET.Element('annotation')
            folder = ET.SubElement(annotation, 'folder')
            filename = ET.SubElement(annotation, 'filename')
            filename.text = image_file
            path = ET.SubElement(annotation, 'path')
            path.text = image_file
            source = ET.SubElement(annotation, 'source')
            database = ET.SubElement(source, 'database')
            database.text = 'Egge'
            size = ET.SubElement(annotation, 'size')
            ET.SubElement(size, 'width').text = str(w)
            ET.SubElement(size, 'height').text = str(h)
            ET.SubElement(size, 'depth').text = '3'
            ET.SubElement(annotation, 'segmented').text = '0'
            for r in i.regions:
                _object = ET.SubElement(annotation, 'object')
                ET.SubElement(_object, 'name').text = created_tags[r.tag_id]
                ET.SubElement(_object, 'pose').text = 'Unspecified'
                ET.SubElement(_object, 'truncated').text = '0'
                ET.SubElement(_object, 'difficult').text = '0'
                ET.SubElement(_object, 'occluded').text = '0'
                bndbox = ET.SubElement(_object, 'bndbox')
                ET.SubElement(bndbox, 'xmin').text = str(int(r.left * w))
                ET.SubElement(bndbox,
                              'xmax').text = str(int((r.left + r.width) * w))
                ET.SubElement(bndbox, 'ymin').text = str(int(r.top * h))
                ET.SubElement(bndbox,
                              'ymax').text = str(int((r.top + r.height) * h))
            xmlstr = minidom.parseString(
                ET.tostring(annotation)).toprettyxml(indent="   ")
            with open('export/' + xml_file, "wb") as f:
                f.write(xmlstr.encode('utf-8'))

        if dest_trainer:
            upload_result = dest_trainer.create_images_from_urls(
                dest_project.id, images=images_to_upload)
            if not upload_result.is_batch_successful:
                print("ERROR: Failed to upload image batch")
                for i in upload_result.images:
                    print("\tImage status:", i.id, i.status)
                exit(-1)

        migrated += count_to_migrate
        count -= count_to_migrate

    # Migrate any untagged images that may exist.
    count = src_trainer.get_untagged_image_count(project_id)
    print("Found:", count, "untagged images.")
    migrated = 0
    while (count > 0):
        count_to_migrate = min(count, 50)
        print("Getting", count_to_migrate, "images")
        images = src_trainer.get_untagged_images(project_id,
                                                 take=count_to_migrate,
                                                 skip=migrated)
        images_to_upload = []
        for i in images:
            print("Migrating", i.id, i.original_image_uri)
            images_to_upload.append(
                ImageUrlCreateEntry(url=i.original_image_uri))

        upload_result = dest_trainer.create_images_from_urls(
            dest_project.id, images=images_to_upload)
        if not upload_result.is_batch_successful:
            print("ERROR: Failed to upload image batch")
            for i in upload_result.images:
                print("\tImage status:", i.id, i.status)
            exit(-1)
        migrated += count_to_migrate
        count -= count_to_migrate
    return images
    if(batchMaxIndex > tagged_image_count):
        batchMaxIndex = tagged_image_count



# In[50]:


tagged_images_with_tags = []
for image in tagged_images: #for each tagged image on origin
    dest_tags_ids = []
    
    for tag in image.tags: #for each tag on the origin image
        dest_tags_ids.append(dest_tags_dict[tag.tag_name]) #append it to the image dest_tags_ids list
    #print(image)
    tagged_images_with_tags.append(ImageUrlCreateEntry(url=image.original_image_uri, tag_ids=dest_tags_ids))
    #tagged_images_with_tags.append(ImageUrlCreateEntry(url=image.image_uri, tag_ids=dest_tags_ids))
print("Done")


# ## Create the images with regions on destination project

# In[52]:


print(len(tagged_images))
print(len(tagged_images_with_tags))

print(len(dest_tags_ids))

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(
        'MLP Pbject Detection HTTP trigger function AddLabeledData processed a request.'
    )

    try:
        image_url = req.form.get('ImageUrl')
        image_labeling_json = req.form.get('DataLabels')
    except:
        return func.HttpResponse(
            "Please pass JSON containing the labeled regions associated with this image on the query string or in the request body.",
            status_code=400)

    if image_url and image_labeling_json:
        labels = []
        labeled_images_with_regions = []
        count_of_regions_applied_to_image = 0
        count_of_labels_applied_to_region = 0

        endpoint = os.environ['ClientEndpoint']

        # Get Cognitive Services Environment Variables
        project_id = os.environ["ProjectID"]
        training_key = os.environ['TrainingKey']

        # load labeled image regions passed in request into dictionary
        image_labeling_data = json.loads(image_labeling_json)

        # instanciate custom vision client
        trainer = CustomVisionTrainingClient(training_key, endpoint=endpoint)

        # get list of valid tags for this model
        tags = trainer.get_tags(project_id)

        # get the height and width of the image
        image_width = image_labeling_data['asset']['size']['width']
        image_height = image_labeling_data['asset']['size']['height']

        # for each labeled region in this asset get the map lables to tag ids and map boundaries formats
        # from vott to azure cognitive services then upload to the cognitive services project.
        for labeled_region in image_labeling_data['regions']:
            for label in labeled_region['tags']:
                for tag in tags:
                    if tag.name == label:
                        labels.append(tag.id)
                        count_of_labels_applied_to_region = count_of_labels_applied_to_region + 1
                        break
            if count_of_labels_applied_to_region > 0:
                count_of_regions_applied_to_image = count_of_regions_applied_to_image + 1
            else:
                return func.HttpResponse(
                    "This Azure Cognitive Services Object Detection project does not contain any labeling tags.  Please add tags to the project before attempting to add labeled data into the project.",
                    status_code=400)

            top_left_x = labeled_region['points'][0]['x']
            top_left_y = labeled_region['points'][0]['y']
            top_right_x = labeled_region['points'][1]['x']
            top_right_y = labeled_region['points'][1]['y']
            bottom_right_x = labeled_region['points'][2]['x']
            bottom_right_y = labeled_region['points'][2]['y']
            bottom_left_x = labeled_region['points'][3]['x']
            bottom_left_y = labeled_region['points'][3]['y']

            # calculate normalized coordinates.
            normalized_left = top_left_x / image_width
            normalized_top = top_left_y / image_height
            normalized_width = (top_right_x - top_left_x) / image_width
            normalized_height = (bottom_left_y - top_left_y) / image_height

            regions = [
                Region(tag_id=labels[0],
                       left=normalized_left,
                       top=normalized_top,
                       width=normalized_width,
                       height=normalized_height)
            ]

        labeled_images_with_regions.append(
            ImageUrlCreateEntry(url=image_url, regions=regions))
        upload_result = trainer.create_images_from_urls(
            project_id, images=labeled_images_with_regions)

        result = ""
        if upload_result.is_batch_successful:
            for image in upload_result.images:
                result = result + "Image " + image.source_url + " Status: " + image.status + ", "
            return func.HttpResponse(
                "Images successfully uploaded with " +
                str(count_of_regions_applied_to_image) + " regions and " +
                str(count_of_labels_applied_to_region) +
                " label(s) to project " + project_id + "with result: " +
                result,
                status_code=200)
        else:
            success = True
            for image in upload_result.images:
                result = result + "Image " + image.source_url + " Status: " + image.status + ", "
                if not "ok" in image.status.lower():
                    success = False

            if success:
                return func.HttpResponse(
                    "Image batch upload succeeded with result: " + result,
                    status_code=200)
            else:
                return func.HttpResponse(
                    "Image batch upload failed with result: " + result,
                    status_code=400)

    else:
        return func.HttpResponse(
            "Please pass valid a valid image url and labels in the request body using the parameter names: ImageUrl and DataLabels.",
            status_code=400)
import os
import unittest
from azure.cognitiveservices.vision.customvision.training.models import ImageUrlCreateEntry

from autotrainer.custom_vision.custom_vision_client import CustomVisionClient
from autotrainer.custom_vision.domain import Domain
from autotrainer.custom_vision.classification_type import ClassificationType

from autotrainer.custom_vision.balancer import Balancer
from azure.cognitiveservices.vision.customvision.training.models import ImageUrlCreateEntry

test_set = [
    ImageUrlCreateEntry(url="", tag_ids=["one"]),
    ImageUrlCreateEntry(url="", tag_ids=["two"]),
    ImageUrlCreateEntry(url="", tag_ids=["two"]),
    ImageUrlCreateEntry(url="", tag_ids=["three"]),
    ImageUrlCreateEntry(url="", tag_ids=["three"]),
    ImageUrlCreateEntry(url="", tag_ids=["three"]),
    ImageUrlCreateEntry(url="", tag_ids=["five"]),
    ImageUrlCreateEntry(url="", tag_ids=["five"]),
    ImageUrlCreateEntry(url="", tag_ids=["five"]),
    ImageUrlCreateEntry(url="", tag_ids=["five"]),
    ImageUrlCreateEntry(url="", tag_ids=["five"]),
]


class BalancerTests(unittest.TestCase):
    def test_balance_set(self):
        balancer = Balancer(test_set, 1)
        result = balancer.apply()
        self.assertEqual(1, sum("one" in p.tag_ids for p in result))
        if tag.name == species:
            break
    if tag.name != species:
        tag = trainer.create_tag(project.id, species)

    file_dir = 'ala image urls/' + species + 'RawFile.csv'
    image_url_list = GetALAimages.listOfAlaImageUrls(file_dir)

    # going through a small portion of url list as can only upload 64 at a time
    for batch_number in range(math.ceil(len(image_url_list) / 67)):
        image_list = []
        endIndex = (batch_number + 1) * 67
        if endIndex > len(image_url_list):
            endIndex = len(image_url_list)
        for url in image_url_list[batch_number * 67:endIndex - 3]:
            image_list.append(ImageUrlCreateEntry(url=url, tag_ids=[tag.id]))
        # every 67 items, add 3 to the testing images, along with their actual species
        for i in range(endIndex - 3, endIndex):
            if i >= 0 and i < len(image_url_list):
                test_list.append([image_url_list[i], species])

        # check that there are some images to train on, then upload these
        if len(image_list) > 0:
            upload_result = trainer.create_images_from_urls(
                project.id, ImageUrlCreateBatch(images=image_list))

            # gives error when there is a duplicate (which is possible with the ALA data) so code below is to ignore
            # duplicate error.
            while not upload_result.is_batch_successful:
                image_list = []
                for image in upload_result.images: