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
Exemplo n.º 2
0
def coco_to_custom_vision(stream, project_id, trainer, data_dir):
    stream = stream | mp.as_list
    tags = stream | mp.select_field('class_id') | mp.dedup() | mp.as_list
    cv_tags = {tag: trainer.create_tag(project_id, tag) for tag in tags}

    stream = (
        stream
        | mp.apply(['width', 'height', 'ground_truth'], 'ground_truth',
                   lambda x: x[2]
                   | mp.where(lambda box: (box['width'] >= x[0] * 0.1) and
                              (box['height'] >= x[1] * 0.1))
                   | mp.as_list)
        | mp.filter('ground_truth', lambda x: len(x) > 0)
        | mp.apply(
            ['width', 'height', 'ground_truth'], 'regions', lambda x: x[2]
            | mp.select(lambda box: Region(tag_id=cv_tags[box['tag']].id,
                                           left=box['x1'] / x[0],
                                           top=box['y1'] / x[1],
                                           width=box['width'] / x[0],
                                           height=box['height'] / x[1]))
            | mp.as_list)
        | mp.apply(['filename', 'regions'], 'tagged_img',
                   lambda x: ImageFileCreateEntry(
                       name=x[0],
                       contents=open(join(data_dir, x[0]), mode="rb").read(),
                       regions=x[1]))
        | mp.as_list)
    tagged_images_with_regions = stream | mp.select_field(
        'tagged_img') | mp.as_list
    for i in range(0, len(tagged_images_with_regions), 50):
        trainer.create_images_from_files(
            project_id, images=tagged_images_with_regions[i:i + 50])
Exemplo n.º 3
0
def convert_row_to_region(tag_map, row):
    tag = tag_map[row[TAG_NAME_LOCATION]]
    x = float(row[X_MIN_LOCATION])
    y = float(row[Y_MIN_LOCATION])
    width = float(row[X_MAX_LOCATION]) - x
    height = float(row[Y_MAX_LOCATION]) - y
    return Region(tag_id=tag.id, left=x, top=y, width=width, height=height)
Exemplo n.º 4
0
 def add_superfluous_region_to_regions(regions, map_tag_name_to_id):
     tag_id = map_tag_name_to_id.get(superfluous_tag_name)
     if tag_id == None:
         raise Exception(
             f"No Custom Vision tag ID found for superfluous region with tag name {superfluous_tag_name}."
         )
     regions.append(Region(tag_id=tag_id, left=0, top=0, width=1, height=1))
Exemplo n.º 5
0
def image_data_region_to_custom_vision_region(region, map_tag_name_to_id):
    tag_id = map_tag_name_to_id.get(region.tag_name)
    if tag_id == None:
        raise Exception(
            f"No Custom Vision tag ID found for locally-defined region with tag name \"{region.tag_name}\"."
        )
    return Region(tag_id=tag_id,
                  left=region.left,
                  top=region.top,
                  width=region.width,
                  height=region.height)
def add_detection_images(image, coordinates, project_id):
    ret, buf = cv2.imencode('.jpg', image)
    tag_id = trainer.get_tags(project_id)[0].id
    region = Region(tag_id=tag_id,
                    left=coordinates[0],
                    top=coordinates[1],
                    width=coordinates[2],
                    height=coordinates[3])
    img = ImageFileCreateEntry(name="test",
                               contents=buf.tobytes(),
                               regions=[region],
                               tag_ids=[tag_id])
    return trainer.create_images_from_files(project_id, images=[img])
Exemplo n.º 7
0
    def upload_training_images(self, training_labeled_images):
        """
        Upload training images to Azure Custom Vision Object Detection.
        
        Args:
        ----
        training_lableded_images: list of labeledImage
        """
        assert (self.project_id is not None)

        print("Adding images...")
        tagged_images_with_regions = []
        batch = 0

        for i in range(len(training_labeled_images)):
            if i > 0 and (i % 64) == 0:
                batch += 1
                print("Adding images: batch ", batch)
                self._upload_one_batch_training_images(
                    tagged_images_with_regions)
                tagged_images_with_regions = []

            # accumulating labels within one batch
            labeled_img = training_labeled_images[i]

            for t, labels in labeled_img.labels.items():

                if t not in self.tags.keys(): self.create_tag(t)

                tag_id = self.tags[t]

                regions = []
                for m in labels:
                    x, y, w, h = normalize_coordinates(m, labeled_img.shape)
                    regions.append(
                        Region(tag_id=tag_id, left=x, top=y, width=w,
                               height=h))

            with open(labeled_img.path, mode="rb") as image_contents:
                tagged_images_with_regions.append(
                    ImageFileCreateEntry(name=labeled_img.name,
                                         contents=image_contents.read(),
                                         regions=regions))

        batch += 1
        if len(tagged_images_with_regions) > 0:
            print("Adding images: batch ", batch)
            self._upload_one_batch_training_images(tagged_images_with_regions)

        return
Exemplo n.º 8
0
def Upload_Images(folder):
    print("Uploading images...")

    # Get the tags defined in the project
    tags = training_client.get_tags(custom_vision_project.id)

    # Create a list of images with tagged regions
    tagged_images_with_regions = []

    # Get the images and tagged regions from the JSON file
    with open('tagged-images.json', 'r') as json_file:
        tagged_images = json.load(json_file)
        for image in tagged_images['files']:
            # Get the filename
            file = image['filename']
            # Get the tagged regions
            regions = []
            for tag in image['tags']:
                tag_name = tag['tag']
                # Look up the tag ID for this tag name
                tag_id = next(t for t in tags if t.name == tag_name).id
                # Add a region for this tag using the coordinates and dimensions in the JSON
                regions.append(
                    Region(tag_id=tag_id,
                           left=tag['left'],
                           top=tag['top'],
                           width=tag['width'],
                           height=tag['height']))
            # Add the image and its regions to the list
            with open(os.path.join(folder, file), mode="rb") as image_data:
                tagged_images_with_regions.append(
                    ImageFileCreateEntry(name=file,
                                         contents=image_data.read(),
                                         regions=regions))

    # Upload the list of images as a batch
    upload_result = training_client.create_images_from_files(
        custom_vision_project.id,
        ImageFileCreateBatch(images=tagged_images_with_regions))
    # Check for failure
    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.")
def upload_images(training_key):
    trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)

    # Find the object detection domain
    obj_detection_domain = next(domain for domain in trainer.get_domains() if domain.type == "ObjectDetection")

    print("Creating project...")
    try:
        project = trainer.create_project("LEGO Vision", domain_id=obj_detection_domain.id)
    except HttpOperationError:
        print("Project already exists. Using this one.")
        project = trainer.get_project(project_id="71548120-925d-4e59-ba7e-32f99de50240")

    classes = os.path.join(BASE_DIRECTORY, "class_map.txt")
    tags = dict()
    # Make two tags in the new project
    for _class in list(map(lambda line: line.split('\t')[0], open(classes).readlines())):
        try:
            tags[_class] = trainer.create_tag(project.id, _class)
        except HttpOperationError:
            print("Tag already created, continuing...")
            for tag in trainer.get_tags(project_id="71548120-925d-4e59-ba7e-32f99de50240"):
                tags[tag.name] = tag

    # Go through the data table above and create the images
    print("Adding images...")
    tagged_images_with_regions = []

    for image_path in glob.glob(os.path.join(IMAGES_FOLDER, "*.jpg")):
        file_id, extension = image_path.split(".", 1)
        image = cv2.imread(image_path)
        bboxes = read_bboxes(os.path.join(IMAGES_FOLDER, file_id + ".bboxes.tsv"), scale=1, padding=(0, 0, 0, 0))
        labels = read_labels(os.path.join(IMAGES_FOLDER, file_id + ".bboxes.labels.tsv"))
        regions = [Region(tag_id=tags[_class].id, left=bbox[0] / image.shape[1], top=bbox[1] / image.shape[0],
                          width=abs(bbox[0] - bbox[2]) / image.shape[1], height=abs(bbox[1] - bbox[3]) / image.shape[0])
                   for _class, bbox in zip(labels,
                                           bboxes)]
        with open(image_path, mode="rb") as image_contents:
            tagged_images_with_regions.append(ImageFileCreateEntry(name=file_id, contents=image_contents.read(),
                                                                   regions=regions))
    print("Azure Custom Vision can only accept images in batches of max 64 per batch. Cutting list up in batches..")
    for batch in chunks(tagged_images_with_regions, 64):
        trainer.create_images_from_files(project.id, images=batch, tag_ids=[tag.id for tag in tags.values()])
    print("Finished adding images. Visit customvision.ai to start training via the GUI.")
Exemplo n.º 10
0
def add_image(trainer, label, project_id, annotation, image_folder):
    """
    Add images with labels and bounding box
    """
    tagged_images_with_regions = []
    tag = trainer.create_tag(project_id, label)
    for file_name in annotation.keys():
        left, top, width, height = annotation[file_name]
        regions = [
            Region(tag_id=tag.id, left=left, top=top, width=width, height=height)
        ]
        file_path = os.path.join(image_folder, label, file_name + ".jpg")
        with open(file_path, "rb") as image_contents:
            tagged_images_with_regions.append(
                ImageFileCreateEntry(
                    name=file_name, contents=image_contents.read(), regions=regions
                )
            )
        image_contents.close()
    return tagged_images_with_regions
Exemplo n.º 11
0
    def _read_annotation_file(self, annotation_path: str) -> list:
        annotations = []
        with open(annotation_path, "r") as f:

            for line in f:
                line = line.strip()
                parameter_list = line.split(" ")
                label = int(parameter_list[0])
                x, y, w, h = list(map(float, parameter_list[1:]))

                left = x - w / 2
                if left < 0:  # Accounting for previous rounding error
                    left = 0
                top = y - h / 2
                if top < 0:  # Accounting for previous rounding error
                    top = 0

                if left + w > 1:  # Accounting for previous rounding error
                    w = 1 - left
                if top + h > 1:  # Accounting for previous rounding error
                    h = 1 - top

                try:
                    tag_id = self.label_to_tag_id[label]
                except:
                    raise ValueError(
                        f"Wrong label {label} at {annotation_path}")

                annotations.append(
                    Region(
                        tag_id=tag_id,
                        left=left,
                        top=top,
                        width=w,
                        height=h,
                    ))
        return annotations
def train_project(training_key):
    trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)

    # Find the object detection domain
    obj_detection_domain = next(domain for domain in trainer.get_domains()
                                if domain.type == "ObjectDetection")

    # Create a new project
    print("Creating project...")
    project = trainer.create_project("My Detection Project",
                                     domain_id=obj_detection_domain.id)

    # Make two tags in the new project
    fork_tag = trainer.create_tag(project.id, "fork")
    scissors_tag = trainer.create_tag(project.id, "scissors")

    fork_image_regions = {
        "fork_1": [0.145833328, 0.3509314, 0.5894608, 0.238562092],
        "fork_2": [0.294117659, 0.216944471, 0.534313738, 0.5980392],
        "fork_3": [0.09191177, 0.0682516545, 0.757352948, 0.6143791],
        "fork_4": [0.254901975, 0.185898721, 0.5232843, 0.594771266],
        "fork_5": [0.2365196, 0.128709182, 0.5845588, 0.71405226],
        "fork_6": [0.115196079, 0.133611143, 0.676470637, 0.6993464],
        "fork_7": [0.164215669, 0.31008172, 0.767156839, 0.410130739],
        "fork_8": [0.118872553, 0.318251669, 0.817401946, 0.225490168],
        "fork_9": [0.18259804, 0.2136765, 0.6335784, 0.643790841],
        "fork_10": [0.05269608, 0.282303959, 0.8088235, 0.452614367],
        "fork_11": [0.05759804, 0.0894935, 0.9007353, 0.3251634],
        "fork_12": [0.3345588, 0.07315363, 0.375, 0.9150327],
        "fork_13": [0.269607842, 0.194068655, 0.4093137, 0.6732026],
        "fork_14": [0.143382356, 0.218578458, 0.7977941, 0.295751631],
        "fork_15": [0.19240196, 0.0633497, 0.5710784, 0.8398692],
        "fork_16": [0.140931368, 0.480016381, 0.6838235, 0.240196079],
        "fork_17": [0.305147052, 0.2512582, 0.4791667, 0.5408496],
        "fork_18": [0.234068632, 0.445702642, 0.6127451, 0.344771236],
        "fork_19": [0.219362751, 0.141781077, 0.5919118, 0.6683006],
        "fork_20": [0.180147052, 0.239820287, 0.6887255, 0.235294119]
    }

    scissors_image_regions = {
        "scissors_1": [0.4007353, 0.194068655, 0.259803921, 0.6617647],
        "scissors_2": [0.426470578, 0.185898721, 0.172794119, 0.5539216],
        "scissors_3": [0.289215684, 0.259428144, 0.403186262, 0.421568632],
        "scissors_4": [0.343137264, 0.105833367, 0.332107842, 0.8055556],
        "scissors_5": [0.3125, 0.09766343, 0.435049027, 0.71405226],
        "scissors_6": [0.379901975, 0.24308826, 0.32107842, 0.5718954],
        "scissors_7": [0.341911763, 0.20714055, 0.3137255, 0.6356209],
        "scissors_8": [0.231617644, 0.08459154, 0.504901946, 0.8480392],
        "scissors_9": [0.170343131, 0.332957536, 0.767156839, 0.403594762],
        "scissors_10": [0.204656869, 0.120539248, 0.5245098, 0.743464053],
        "scissors_11": [0.05514706, 0.159754932, 0.799019635, 0.730392158],
        "scissors_12": [0.265931368, 0.169558853, 0.5061275, 0.606209159],
        "scissors_13": [0.241421565, 0.184264734, 0.448529422, 0.6830065],
        "scissors_14": [0.05759804, 0.05027781, 0.75, 0.882352948],
        "scissors_15": [0.191176474, 0.169558853, 0.6936275, 0.6748366],
        "scissors_16": [0.1004902, 0.279036, 0.6911765, 0.477124184],
        "scissors_17": [0.2720588, 0.131977156, 0.4987745, 0.6911765],
        "scissors_18": [0.180147052, 0.112369314, 0.6262255, 0.6666667],
        "scissors_19": [0.333333343, 0.0274019931, 0.443627447, 0.852941155],
        "scissors_20": [0.158088237, 0.04047389, 0.6691176, 0.843137264]
    }

    # Go through the data table above and create the images
    print("Adding images...")
    tagged_images_with_regions = []

    for file_name in fork_image_regions.keys():
        x, y, w, h = fork_image_regions[file_name]
        regions = [
            Region(tag_id=fork_tag.id, left=x, top=y, width=w, height=h)
        ]

        with open(os.path.join(IMAGES_FOLDER, "fork", file_name + ".jpg"),
                  mode="rb") as image_contents:
            tagged_images_with_regions.append(
                ImageFileCreateEntry(name=file_name,
                                     contents=image_contents.read(),
                                     regions=regions))

    for file_name in scissors_image_regions.keys():
        x, y, w, h = scissors_image_regions[file_name]
        regions = [
            Region(tag_id=scissors_tag.id, left=x, top=y, width=w, height=h)
        ]

        with open(os.path.join(IMAGES_FOLDER, "scissors", file_name + ".jpg"),
                  mode="rb") as image_contents:
            tagged_images_with_regions.append(
                ImageFileCreateEntry(name=file_name,
                                     contents=image_contents.read(),
                                     regions=regions))

    trainer.create_images_from_files(project.id,
                                     images=tagged_images_with_regions)

    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)

    # The iteration is now trained. Make it the default project endpoint
    trainer.update_iteration(project.id, iteration.id, is_default=True)
    print("Done!")
    return project, iteration
Exemplo n.º 13
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
Exemplo n.º 14
0
        balloon_image_regions[jpgName] = regions

print(balloon_image_regions)

# Update this with the path to where you downloaded the images.
base_image_url = "./images/"

# Go through the data table above and create the images
print("Adding images...")
tagged_images_with_regions = []

for filename in balloon_image_regions.keys():
    regions = []
    for region in balloon_image_regions[filename]:
        x, y, w, h = region
        regions.append(
            Region(tag_id=balloon_tag.id, left=x, top=y, width=w, height=h))

    with open(base_image_url + filename, mode="rb") as image_contents:
        tagged_images_with_regions.append(
            ImageFileCreateEntry(name=filename,
                                 contents=image_contents.read(),
                                 regions=regions))

upload_result = trainer.create_images_from_files(
    project.id, 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)
    exit(-1)
Exemplo n.º 15
0
def upload_images_to_customvision_helper(project_id,
                                         part_id,
                                         batch_size: int = 10) -> bool:
    """upload_images_to_customvision_helper.

    Helper function for uploading images to Custom Vision.
    Make sure part already upload to Custom Vision (
    customvision_id not null or blank).

    Args:
        project_id:
        part_id:
        batch_size (int): batch_size

    Returns:
        bool:
    """

    logger.info("Uploading images with part_id %s", part_id)

    has_new_images = False
    project_obj = Project.objects.get(pk=project_id)
    trainer = project_obj.setting.get_trainer_obj()
    part_obj: Part = Part.objects.get(pk=part_id)
    tag_id = part_obj.customvision_id
    images = Image.objects.filter(part_id=part_id,
                                  is_relabel=False,
                                  uploaded=False)
    logger.info("Tag id %s", tag_id)
    count = 0
    img_entries = []
    img_objs = []

    logger.info("Image length: %s", len(images))

    for index, image_obj in enumerate(images):
        logger.info("*** image %s, %s", index + 1, image_obj)
        has_new_images = True

        img_name = "img-" + datetime.datetime.utcnow().isoformat()

        regions = []
        width = image_obj.image.width
        height = image_obj.image.height
        try:
            labels = json.loads(image_obj.labels)
            if len(labels) == 0:
                continue
            for label in labels:
                label_x = label["x1"] / width
                label_y = label["y1"] / height
                label_w = (label["x2"] - label["x1"]) / width
                label_h = (label["y2"] - label["y1"]) / height
                region = Region(tag_id=tag_id,
                                left=label_x,
                                top=label_y,
                                width=label_w,
                                height=label_h)
                regions.append(region)

            image = image_obj.image
            image.open()
            img_entry = ImageFileCreateEntry(name=img_name,
                                             contents=image.read(),
                                             regions=regions)
            img_objs.append(image_obj)
            img_entries.append(img_entry)
            count += 1
        except:
            logger.exception("unexpected error")

        if len(img_entries) >= batch_size:
            logger.info("Uploading %s images", len(img_entries))
            upload_result = trainer.create_images_from_files(
                project_id=project_obj.customvision_project_id,
                images=img_entries)
            logger.info(
                "Uploading images... Is batch success: %s",
                upload_result.is_batch_successful,
            )
            img_entries = []
            for i, img_obj in enumerate(img_objs):
                img_obj.customvision_id = upload_result.images[i].image.id
                img_obj.uploaded = True
                img_obj.save()
            img_objs = []

    if len(img_entries) >= 1:
        logger.info("Uploading %s images", len(img_entries))
        upload_result = trainer.create_images_from_files(
            project_id=project_obj.customvision_project_id, images=img_entries)
        logger.info(
            "Uploading images... Is batch success: %s",
            upload_result.is_batch_successful,
        )
        for i, img_obj in enumerate(img_objs):
            img_obj.customvision_id = upload_result.images[i].image.id
            img_obj.remote_url = (
                upload_result.images[i].image.original_image_uri)
            img_obj.uploaded = True
            img_obj.save()
    logger.info("Uploading images... Done")
    logger.info("Has new images: %s", has_new_images)
    return has_new_images
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
    def upload_directory(self,
                         data_dir,
                         img_ext="*.jpg",
                         img_dir="images",
                         lbl_file="labels.csv",
                         default_tag_name="important"):
        """
        upload_directory - Upload images from a given directory into the CV workspace

        :param str data_dir: Source folder of the files.
        :param str img_ext: image extension.
        :param str img_dir: image folder.
        :param str lbl_file: labels file.
        :param str default_tag_name: default tag name.

        :returns: None

        """
        label_fn = os.path.join(data_dir, lbl_file)
        img_folder = os.path.join(data_dir, img_dir)

        # Check if required folders exist.
        if not (os.path.isdir(img_folder) and os.path.exists(label_fn)):
            print("Input data not found")
            return

        # Read labels and image list.
        labels_df = pd.read_csv(os.path.join(label_fn))
        image_list = glob.glob(os.path.join(img_folder, img_ext))

        # Upload each image with regions
        for _, path in enumerate(image_list):
            tagged_images_with_regions = []
            regions = []

            file_name = path.split("\\")[-1]
            img = Image.open(path)
            img_width, img_height = img.size

            for _, row in labels_df[labels_df.FileName ==
                                    file_name].iterrows():
                x, y, w, h = row.XMin, row.YMin, row.XMax - row.XMin, row.YMax - row.YMin
                x = x / img_width
                w = w / img_width
                y = y / img_height
                h = h / img_height

                if "DefectType" in row:
                    default_tag_name = row.DefectType

                tag = None
                for a_tag in self.tags:
                    if a_tag.name == default_tag_name:
                        tag = a_tag

                if not tag:
                    tag = self.client.create_tag(self.project_id,
                                                 default_tag_name)
                    self.tags = self.client.get_tags(self.project_id)

                regions.append(
                    Region(tag_id=tag.id, left=x, top=y, width=w, height=h))

            with open(path, mode="rb") as image_contents:
                tagged_images_with_regions.append(
                    ImageFileCreateEntry(name=file_name,
                                         contents=image_contents.read(),
                                         regions=regions))

            upload_result = self.client.create_images_from_files(
                self.project.id, 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)
Exemplo n.º 18
0
def detect(
        cfg,
        data,
        weights,
        images='data/samples',  # input folder
        output='output',  # output folder
        fourcc='mp4v',  # video codec
        img_size=416,
        conf_thres=0.8,
        nms_thres=0.8,
        save_txt=False,
        save_images=True):
    # Initialize
    device = torch_utils.select_device(force_cpu=ONNX_EXPORT)
    torch.backends.cudnn.benchmark = False  # set False for reproducible results
    if os.path.exists(output):
        shutil.rmtree(output)  # delete output folder
    os.makedirs(output)  # make new output folder

    # Initialize model
    if ONNX_EXPORT:
        s = (
            320, 192
        )  # (320, 192) or (416, 256) or (608, 352) onnx model image size (height, width)
        model = Darknet(cfg, s)
    else:
        model = Darknet(cfg, img_size)

    # Load weights
    if weights.endswith('.pt'):  # pytorch format
        model.load_state_dict(
            torch.load(weights, map_location=device)['model'])
    else:  # darknet format
        _ = load_darknet_weights(model, weights)

    # Eval mode
    model.to(device).eval()

    # Export mode
    if ONNX_EXPORT:
        img = torch.zeros((1, 3, s[0], s[1]))
        torch.onnx.export(model, img, 'weights/export.onnx', verbose=True)
        return

    # Half precision
    opt.half = opt.half and device.type != 'cpu'  # half precision only supported on CUDA
    if opt.half:
        model.half()

    # Set Dataloader
    vid_path, vid_writer = None, None
    if opt.webcam:
        save_images = False
        dataloader = LoadWebcam(img_size=img_size, half=opt.half)
    else:
        save_img = True
        dataloader = LoadImages(images, img_size=img_size, half=opt.half)

    # Get classes and colors
    classes = load_classes(parse_data_cfg(data)['names'])
    colors = [[random.randint(0, 255) for _ in range(3)]
              for _ in range(len(classes))]
    #opvragen van de tag id van het custom vision project
    auto_tag = next(
        filter(lambda t: t.name == "auto", trainer.get_tags(project.id)), None)
    # Run inference
    t0 = time.time()
    for i, (path, img, im0, vid_cap) in enumerate(dataloader):
        t = time.time()
        save_path = str(Path(output) / Path(path).name)
        foto = cv2.imread(path)
        h, w = foto.shape[:2]

        # Get detections
        img = torch.from_numpy(img).unsqueeze(0).to(device)
        pred, _ = model(img)
        det = non_max_suppression(pred.float(), conf_thres, nms_thres)[0]
        #

        if det is not None and len(det) > 0:
            # Rescale boxes from 416 to true image size
            det[:, :4] = scale_coords(img.shape[2:], det[:, :4],
                                      im0.shape).round()

            # Print results to screen
            print('%gx%g ' % img.shape[2:], end='')  # print image size
            for c in det[:, -1].unique():
                n = (det[:, -1] == c).sum()
                print('%g %ss' % (n, classes[int(c)]), end=', ')
            lijst = []
            # Draw bounding boxes and labels of detections
            for *xyxy, conf, cls_conf, cls in det:
                if save_img:  # Add bbox to image
                    label = '%s %.2f' % (classes[int(cls)], conf)
                    plot_one_box(xyxy,
                                 im0,
                                 label=label,
                                 color=colors[int(cls)])

                #if save_txt:  # Write to file
                if int(cls) == 2:
                    x1 = float(('{0}').format(*xyxy, )) / w
                    y1 = float(('{1}').format(*xyxy, )) / h
                    x2 = float(('{2}').format(*xyxy, )) / w
                    y2 = float(('{3}').format(*xyxy, )) / h
                    breedte = (x2 - x1)
                    hoogte = (y2 - y1)

                    coordinaten = [x1, y1, breedte, hoogte]
                    kleinelijst = [os.path.basename(path), coordinaten]
                    lijst.append(kleinelijst)

            base_image_url = Path("data/samples/")

            # Go through the data table above and create the images
            print("Adding images...")
            tagged_images_with_regions = []
            auto_image_regions = {}
            dictitem = {auto_tag.id: lijst}
            auto_image_regions.update(dictitem)

            for tag_id in auto_image_regions:
                for filename, [x, y, w, h] in auto_image_regions[tag_id]:
                    print("filename = " + str(filename))
                    #x,y,w,h = auto_image_regions[filename]
                    regions = [
                        Region(tag_id=auto_tag.id,
                               left=x,
                               top=y,
                               width=w,
                               height=h)
                    ]

                    with open(str(base_image_url) + "/" + filename,
                              mode="rb") as image_contents:
                        tagged_images_with_regions.append(
                            ImageFileCreateEntry(
                                name=filename,
                                contents=image_contents.read(),
                                regions=regions))

            upload_result = trainer.create_images_from_files(
                project.id, 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)

        print('Done. (%.3fs)' % (time.time() - t))

        if opt.webcam:  # Show live webcam
            cv2.imshow(weights, im0)

        if save_images:  # Save image with detections
            if dataloader.mode == 'images':
                cv2.imwrite(save_path, im0)
            else:
                if vid_path != save_path:  # new video
                    vid_path = save_path
                    if isinstance(vid_writer, cv2.VideoWriter):
                        vid_writer.release()  # release previous video writer

                    fps = vid_cap.get(cv2.CAP_PROP_FPS)
                    width = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                    height = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                    vid_writer = cv2.VideoWriter(
                        save_path, cv2.VideoWriter_fourcc(*fourcc), fps,
                        (width, height))
                vid_writer.write(im0)

    if save_images:
        print('Results saved to %s' % os.getcwd() + os.sep + output)
        if platform == 'darwin':  # macos
            os.system('open ' + output + ' ' + save_path)

    print('Done. (%.3fs)' % (time.time() - t0))

    path = "data/samples/"
    path2 = "data/finished/"
    print("we kopiëren alle foto's nu")
    src_files = os.listdir(path)
    for file_name in src_files:
        full_file_name = os.path.join(path, file_name)
        if os.path.isfile(full_file_name):
            shutil.copy(full_file_name, path2)

    print("we verwijderen alle foto's nu")
    for the_file in os.listdir(path):
        file_path = os.path.join(path, the_file)
        if os.path.isfile(file_path):
            os.unlink(file_path)
Exemplo n.º 19
0
def _train(project_id):

    project_obj = Project.objects.get(pk=project_id)
    customvision_project_id = project_obj.customvision_project_id

    # @FIXME (Hugh): wrap it up
    obj, created = Train.objects.update_or_create(
        project=project_obj,
        defaults={
            'status': 'Status: preparing data (images and annotations)',
            'log': '',
            'project': project_obj
        })

    Trainer.dequeue_iterations(
        trainer=trainer, custom_vision_project_id=customvision_project_id)

    try:
        count = 10
        while count > 0:
            part_ids = [part.id for part in project_obj.parts.all()]
            if len(part_ids) > 0: break
            print('waiting parts...')
            time.sleep(1)
            count -= 1

        # @FIXME (Hugh): wrap it up
        obj, created = Train.objects.update_or_create(
            project=project_obj,
            defaults={
                'status': 'sending',
                'log': 'Status : sending data (images and annotations)',
                'project': project_obj
            })

        print(project_obj.id)
        print('Part ids:', part_ids, flush=True)
        images = Image.objects.filter(part_id__in=part_ids,
                                      is_relabel=False,
                                      uploaded=False).all()
        img_entries = []
        img_objs = []

        tags = trainer.get_tags(customvision_project_id)
        tag_dict = {}
        for tag in tags:
            tag_dict[tag.name] = tag.id

        print('[INFO] Submit images and do the Training...')
        count = 0
        for index, image_obj in enumerate(images):
            print('*** image', index + 1, image_obj, flush=True)

            part = image_obj.part
            part_name = part.name
            if part_name not in tag_dict:
                print('new part name', part_name, flush=True)
                print('creating new tag (part name)', flush=True)
                tag = trainer.create_tag(customvision_project_id, part_name)
                tag_dict[tag.name] = tag.id

            tag_id = tag_dict[part_name]

            name = 'img-' + datetime.datetime.utcnow().isoformat()
            regions = []
            width = image_obj.image.width
            height = image_obj.image.height
            try:
                labels = json.loads(image_obj.labels)
                if len(labels) == 0: continue
                for label in labels:
                    x = label['x1'] / width
                    y = label['y1'] / height
                    w = (label['x2'] - label['x1']) / width
                    h = (label['y2'] - label['y1']) / height
                    region = Region(tag_id=tag_id,
                                    left=x,
                                    top=y,
                                    width=w,
                                    height=h)
                    regions.append(region)

                image = image_obj.image
                image.open()
                img_entry = ImageFileCreateEntry(name=name,
                                                 contents=image.read(),
                                                 regions=regions)
                img_objs.append(image_obj)
                img_entries.append(img_entry)
                count += 1
            except:
                print("[ERROR] Unexpected error:",
                      sys.exc_info()[0],
                      flush=True)

            if len(img_entries) >= 5:
                print('uploading...', flush=True)
                upload_result = trainer.create_images_from_files(
                    customvision_project_id, images=img_entries)
                print('batch success:',
                      upload_result.is_batch_successful,
                      flush=True)
                img_entries = []
                for img_obj in img_objs:
                    img_obj.uploaded = True
                    img_obj.save()
                img_objs = []

        if len(img_entries) >= 1:
            print('uploading...', flush=True)
            upload_result = trainer.create_images_from_files(
                customvision_project_id, images=img_entries)
            print('batch success:',
                  upload_result.is_batch_successful,
                  flush=True)
            for img_obj in img_objs:
                img_obj.uploaded = True
                img_obj.save()

        if count == 0:
            print('Nothing changed, no training', flush=True)
            # @FIXME (Hugh): wrap it up
            obj, created = Train.objects.update_or_create(
                project=project_obj,
                defaults={
                    'status': 'ok',
                    'log': 'Status: Nothing changed, no training',
                    'project': project_obj
                })

        else:
            print('training...', flush=True)
            try:
                trainer.train_project(customvision_project_id)
                project_obj.deployed = False
                project_obj.save(update_fields=['deployed'])
                update_train_status(project_id)
                print('[INFO] set deployed = False')
            except CustomVisionErrorException:
                print(
                    '[ERROR] From Custom Vision: Nothing changed since last training',
                    flush=True)
                raise

        return JsonResponse({'status': 'ok'})

    except Exception as e:
        err_msg = traceback.format_exc()
        print(f'Exception: {err_msg}')

        # @FIXME (Hugh): wrap it up
        obj, created = Train.objects.update_or_create(
            project=project_obj,
            defaults={
                'status': 'failed',
                'log': f'Status : failed {str(err_msg)}',
                'project': project_obj
            })

        return JsonResponse({
            'status': 'failed',
            'log': f'Status : failed {str(err_msg)}'
        })
Exemplo n.º 20
0
    fullpath = os.path.join(basePath, "cvjson.json")
    print("Loading Json: %s " % (fullpath))

    with open(fullpath) as f:
        imgList = json.load(f)

    print("Adding Images")
    for data in imgList:
        filePath = os.path.join(basePath, "img", data.get("Name"))

        print("Processing %s" % (data.get("Name")))

        regions = [
            Region(tag_id=tag.id,
                   left=data.get("x"),
                   top=data.get("y"),
                   width=data.get("width"),
                   height=data.get("height"))
        ]
        #regions = [ Region(tag_id=fork_tag.id, left=x,top=y,width=w,height=h) ]

        with open(filePath, mode="rb") as image_contents:
            #There is a limit of 64 images and 20 tags.

            tagged_images_with_regions.append(
                ImageFileCreateEntry(name=data.get("Name"),
                                     contents=image_contents.read(),
                                     regions=regions))

    trainer.create_images_from_files(project.id,
                                     images=tagged_images_with_regions,
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
Exemplo n.º 22
0
start = 0
for stop in range(50, len(file_list), 50):

    #11312
    #2771

    filelist = file_list[start:stop]
    tagged_images_with_regions = []
    for file in filelist:
        regions_ = []
        for _, row in df[df.filename == file].iterrows():
            #print(row)
            regions_.append(
                Region(tag_id=label_dict[row['class']],
                       left=row.left,
                       top=row.top,
                       width=row.width,
                       height=row.height))

        with open(base_image_url + row.filename, mode='rb') as image_contents:
            tagged_images_with_regions.append(
                ImageFileCreateEntry(name=row.filename,
                                     contents=image_contents.read(),
                                     regions=regions_))

    ## Upload results
    upload_result = trainer.create_images_from_files(
        project.id, images=tagged_images_with_regions)
    if not upload_result.is_batch_successful:
        print("Image batch upload failed.")
        for image in upload_result.images:
Exemplo n.º 23
0
            regions_lis.append((ind, x, y, w, h))
        images.append((k, regions_lis))

    print("Adding images...")
    print("%d images in total" % len(images))
    SEND_BY_BATCH = 64
    print("Send by batch =", SEND_BY_BATCH)
    tagged_images_with_regions = []
    BATCH_NUM = math.ceil(len(images) / SEND_BY_BATCH)
    for i in range(BATCH_NUM):
        for file_name, regions in images[i * SEND_BY_BATCH:(i + 1) *
                                         SEND_BY_BATCH]:
            regions_saved = []
            for r in regions:
                x, y, w, h = r[1:]
                tag_id = tag_index_to_id[r[0]]
                regions_saved.append(
                    Region(tag_id=tag_id, left=x, top=y, width=w, height=h))
            with open(IMAGES_PATH + "/" + file_name + "." + FILE_EXT,
                      mode="rb") as image_contents:
                tagged_images_with_regions.append(
                    ImageFileCreateEntry(name=file_name,
                                         contents=image_contents.read(),
                                         regions=regions_saved))
        print("Batch no.", i)
        trainer.create_images_from_files(project_id,
                                         images=tagged_images_with_regions)
        tagged_images_with_regions = []

# try to upload generated images
Exemplo n.º 24
0
tag_name_to_id = {}
for t in all_tags:
    tag_name_to_id[t.name] = t.id

# each element is (image file name, list of regions)
# each region is a tuple where the first element is the tag and the remaining are x, y, w, h.
images = [
    ("apple",[("Apple", 0.15492957746478872, 0.030985915492957747, 0.6633802816901408, 0.8802816901408451)]),
    ("banana",[("Banana", 0.034739454094292806, 0.04084507042253521, 0.9255583126550869, 0.919718309859155)]),
    ("fruits",[("Apple", 0.200845665961945, 0.09859154929577464, 0.2706131078224101, 0.347887323943662),
               ("Apple", 0.5190274841437632, 0.11549295774647887, 0.2420718816067653, 0.3225352112676056),
               ("Banana", 0.19978858350951373, 0.476056338028169, 0.595137420718816, 0.4169014084507043),
               ]),
]

print ("Adding images...")
tagged_images_with_regions = []
for file_name, regions in images:
    regions_saved = []
    for r in regions:
        x,y,w,h = r[1:]
        tag_id = tag_name_to_id[r[0]]
        regions_saved.append(Region(tag_id=tag_id, left=x,top=y,width=w,height=h))
    with open("test_upload_images/" + file_name + ".jpg", mode="rb") as image_contents:
        tagged_images_with_regions.append(
            ImageFileCreateEntry(name=file_name, contents=image_contents.read(), regions=regions_saved)
            )

trainer.create_images_from_files(project.id, images=tagged_images_with_regions)
Exemplo n.º 25
0
        region_count =0
        for region in vehicle_regions[image]:
            if region_count >= 200:
                break

            img = Image.open(BytesIO(response.content))
            width = img.size[0]
            height = img.size[1]

            #normalize region coordinates
            x = float((region['coordinates']['x']/width))
            y = float((region['coordinates']['y']/height))
            w = float((region['coordinates']['w']/width))
            h = float((region['coordinates']['h']/height))

            regions.append( Region(tag_id=tags[0].id, left=x, top=y, width=w, height=h)   )
            region_count+=1
        tagged_images_with_regions.append(ImageFileCreateEntry(name=image, contents=response.content, regions=regions))
      
         
    

#uploading labeled images to custom vision portal
print('Uploading images to trainer')
for i in range(0,len(tagged_images_with_regions),63):
    batch =  tagged_images_with_regions[i:i+63]
    upload_result = trainer.create_images_from_files(project.id, images=batch)
    if not upload_result.is_batch_successful:
       print("Image batch upload failed.")
       for image in upload_result.images:
            print("Image status: ", image.status)
Exemplo n.º 26
0
print(tagtoidmapper)

tagged_images_with_regions = []
z = 1

for image in images:

    if z % 50 == 0:
        upload_result = trainAPI.create_images_from_files(
            project.id, images=tagged_images_with_regions)
        tagged_images_with_regions = []
    regions = []

    for region in image['regions']:
        regions.append(
            Region(tag_id=tagtoidmapper[region['tagName']],
                   left=region['left'],
                   top=region['top'],
                   width=region['width'],
                   height=region['height']))
    image_file = open(os.path.join(imagesPath, image['id'] + '.png'), 'rb')
    tagged_images_with_regions.append(
        ImageFileCreateEntry(name=image['id'],
                             contents=image_file.read(),
                             regions=regions))
    image_file.close()
    z = z + 1

upload_result = trainAPI.create_images_from_files(
    project.id, images=tagged_images_with_regions)
def _train(project_id):
    """Actually do uplaod, train and deploy"""
    project_obj = Project.objects.get(pk=project_id)
    trainer = project_obj.setting.revalidate_and_get_trainer_obj()
    customvision_project_id = project_obj.customvision_project_id

    # Invalid Endpoint + Training Key
    if not trainer:
        project_obj.upcreate_training_status(
            status="failed", log=error_messages.CUSTOM_VISION_ACCESS_ERROR)
        return JsonResponse(
            {
                "status": "failed",
                "log": error_messages.CUSTOM_VISION_ACCESS_ERROR
            },
            status=503,
        )

    project_obj.upcreate_training_status(
        status="Status: preparing data (images and annotations)", log="")

    project_obj.dequeue_iterations()

    try:
        count = 10
        while count > 0:
            part_ids = [part.id for part in project_obj.parts.all()]
            if len(part_ids) > 0:
                break
            logging.info("waiting parts...")
            time.sleep(1)
            count -= 1

        logger.info("Project id: %s", project_obj.id)
        logger.info("Part ids: %s", part_ids)
        try:
            trainer.get_project(customvision_project_id)
            project_obj.upcreate_training_status(
                status="preparing",
                log=(
                    f"Status : Project {project_obj.customvision_project_name}"
                    + "found on Custom Vision"),
            )
        except:
            project_obj.create_project()
            project_obj.upcreate_training_status(
                status="preparing",
                log=("Status : Project created on CustomVision. " +
                     f"Name: {project_obj.customvision_project_name}"),
            )

            logger.info("Project created on CustomVision.")
            logger.info("Project Id: %s", project_obj.customvision_project_id)
            logger.info("Project Name: %s",
                        project_obj.customvision_project_name)
            customvision_project_id = project_obj.customvision_project_id

        project_obj.upcreate_training_status(
            status="sending",
            log="Status : sending data (images and annotations)")
        tags = trainer.get_tags(customvision_project_id)
        tag_dict = {}
        project_partnames = {}
        project_changed = False
        has_new_parts = False
        has_new_images = False
        for tag in tags:
            tag_dict[tag.name] = tag.id
        parts_last_train = len(tags)
        images_last_train = trainer.get_tagged_image_count(
            project_obj.customvision_project_id)
        # Update existing tags
        # TODO: update tags
        # trainer.update_tags(project_id, tag_id, new_tag)

        # Create tags on CustomVisioin Project
        # Maybe move to Project Model?
        logger.info("Creating tags before training...")
        counter = 0
        for part_id in part_ids:
            part_name = Part.objects.get(id=part_id).name
            part_description = Part.objects.get(id=part_id).description
            project_partnames[part_name] = "foo"
            if part_name not in tag_dict:
                logger.info("Creating tag: %s. Description: %s", part_name,
                            part_description)
                tag = trainer.create_tag(
                    project_id=customvision_project_id,
                    name=part_name,
                    description=part_description,
                )
                has_new_parts = True
                tag_dict[tag.name] = tag.id
                counter += 1
        project_changed = project_changed or (counter > 0)
        logger.info("Created %s tags", counter)
        logger.info("Creating tags... Done")

        # Upload images to CustomVisioin Project
        images = Image.objects.filter(part_id__in=part_ids,
                                      is_relabel=False,
                                      uploaded=False).all()
        logger.info("Uploading images before training...")
        count = 0
        img_entries = []
        img_objs = []
        logger.info("Image length: %s", len(images))

        for index, image_obj in enumerate(images):
            logger.info("*** image %s, %s", index + 1, image_obj)
            has_new_images = True
            part = image_obj.part
            part_name = part.name
            tag_id = tag_dict[part_name]
            img_name = "img-" + datetime.datetime.utcnow().isoformat()

            regions = []
            width = image_obj.image.width
            height = image_obj.image.height
            try:
                labels = json.loads(image_obj.labels)
                if len(labels) == 0:
                    continue
                for label in labels:
                    x = label["x1"] / width
                    y = label["y1"] / height
                    w = (label["x2"] - label["x1"]) / width
                    h = (label["y2"] - label["y1"]) / height
                    region = Region(tag_id=tag_id,
                                    left=x,
                                    top=y,
                                    width=w,
                                    height=h)
                    regions.append(region)

                image = image_obj.image
                image.open()
                img_entry = ImageFileCreateEntry(name=img_name,
                                                 contents=image.read(),
                                                 regions=regions)
                img_objs.append(image_obj)
                img_entries.append(img_entry)
                project_changed = project_changed or (not image_obj.uploaded)
                if project_changed:
                    logger.info("project_changed: %s", project_changed)
                count += 1
            except:
                logger.exception("unexpected error")

            if len(img_entries) >= 5:
                logger.info("Uploading %s images", len(img_entries))
                upload_result = trainer.create_images_from_files(
                    customvision_project_id, images=img_entries)
                logger.info(
                    "Uploading images... Is batch success: %s",
                    upload_result.is_batch_successful,
                )
                img_entries = []
                for img_obj in img_objs:
                    img_obj.uploaded = True
                    img_obj.save()
                img_objs = []

        if len(img_entries) >= 1:
            logger.info("Uploading %s images", len(img_entries))
            upload_result = trainer.create_images_from_files(
                customvision_project_id, images=img_entries)
            logger.info(
                "Uploading images... Is batch success: %s",
                upload_result.is_batch_successful,
            )
            for img_obj in img_objs:
                img_obj.uploaded = True
                img_obj.save()
        logger.info("Uploading images... Done")

        # Submit training task to Custom Vision
        if not project_changed:
            project_obj.upcreate_training_status(
                status="ok", log="Status: Nothing changed. Not training")
        else:
            project_obj.upcreate_training_status(
                status="training",
                log="Status: Project changed. Submitting training task...")
            training_task_submit_success = project_obj.train_project()
            if training_task_submit_success:
                project_obj.update_app_insight_counter(
                    has_new_parts=has_new_parts,
                    has_new_images=has_new_images,
                    parts_last_train=parts_last_train,
                    images_last_train=images_last_train,
                )
        # A Thread/Task to keep updating the status
        update_train_status(project_id)
        return JsonResponse({"status": "ok"})

    except CustomVisionErrorException as customvision_err:
        logger.error("CustomVisionErrorException: %s", customvision_err)
        if customvision_err.message == \
                "Operation returned an invalid status code 'Access Denied'":
            project_obj.upcreate_training_status(
                status="failed",
                log=
                "Training key or Endpoint is invalid. Please change the settings",
            )
            return JsonResponse(
                {
                    "status":
                    "failed",
                    "log":
                    "Training key or Endpoint is invalid. Please change the settings",
                },
                status=status.HTTP_503_SERVICE_UNAVAILABLE,
            )

        project_obj.upcreate_training_status(status="failed",
                                             log=customvision_err.message)
        return JsonResponse(
            {
                "status": "failed",
                "log": customvision_err.message
            },
            status=status.HTTP_400_BAD_REQUEST,
        )

    except Exception as e:
        # TODO: Remove in production
        err_msg = traceback.format_exc()
        logger.exception("Exception: %s", err_msg)
        project_obj.upcreate_training_status(
            status="failed", log=f"Status : failed {str(err_msg)}")
        return JsonResponse({
            "status": "failed",
            "log": f"Status : failed {str(err_msg)}"
        })
Exemplo n.º 28
0
    "scissors_17": [0.2720588, 0.131977156, 0.4987745, 0.6911765],
    "scissors_18": [0.180147052, 0.112369314, 0.6262255, 0.6666667],
    "scissors_19": [0.333333343, 0.0274019931, 0.443627447, 0.852941155],
    "scissors_20": [0.158088237, 0.04047389, 0.6691176, 0.843137264]
}

# Update this with the path to where you downloaded the images.
base_image_location = "<path to directory"

# Go through the data table above and create the images
print("Adding images...")
tagged_images_with_regions = []

for file_name in fork_image_regions.keys():
    x, y, w, h = fork_image_regions[file_name]
    regions = [Region(tag_id=fork_tag.id, left=x, top=y, width=w, height=h)]

    with open(base_image_location + "images/fork/" + file_name + ".jpg",
              mode="rb") as image_contents:
        tagged_images_with_regions.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 regions=regions))

for file_name in scissors_image_regions.keys():
    x, y, w, h = scissors_image_regions[file_name]
    regions = [
        Region(tag_id=scissors_tag.id, left=x, top=y, width=w, height=h)
    ]

    with open(base_image_location + "images/scissors/" + file_name + ".jpg",
Exemplo n.º 29
0
    tagged_images_with_regions = []

    for index, row in group.iterrows():

        width = row['x2'] - row['x1']
        height = row['y2'] - row['y1']

        x_scaled = row['x1'] / image_width
        y_scaled = row['y1'] / image_height
        width_scaled = width / image_width
        height_scaled = height / image_height

        regions.append(
            Region(tag_id=tags[row['defect']].id,
                   left=x_scaled,
                   top=y_scaled,
                   width=width_scaled,
                   height=height_scaled))

    with open(os.path.join('./data', "{0}.png".format(name)),
              mode="rb") as image_contents:
        tagged_images_with_regions.append(
            ImageFileCreateEntry(name=name,
                                 contents=image_contents.read(),
                                 regions=regions))

    upload_result = trainer.create_images_from_files(
        project.id, images=tagged_images_with_regions)
    if not upload_result.is_batch_successful:
        print("Image batch upload failed.")
        for image in upload_result.images: