Пример #1
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])
def add_classification_images(image, my_tag, project_id):
    # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, buf = cv2.imencode('.jpg', image)
    all_tags = trainer.get_tags(project_id)
    tag_id = [tag for tag in all_tags if tag.name == my_tag][0]
    img = ImageFileCreateEntry(name="test",
                               contents=buf.tobytes(),
                               tag_ids=[tag_id])
    result = trainer.create_images_from_files(project_id, images=[img])
    return (result)
Пример #3
0
def create_images_from_files(name, buffer, project_id):
    images = []
    images.append(ImageFileCreateEntry(name=name, contents=buffer.getvalue()))
    try:
        result = trainer.create_images_from_files(project_id, images=images)

        if not result.is_batch_successful:
            return 'Image status: {0}'.format(result.images[0].status)
        else:
            return ''
    except HttpOperationError as e:
        return e.response.text
def createImageList(tag, tag_id):
    # Set directory to current tag
    base_image_url = image_folder + "/" + tag + "/"
    photo_name_list = os.listdir(base_image_url)
    image_list = []
    for file_name in photo_name_list[0:49]:
        with open(base_image_url + file_name, "rb") as image_contents:
            image_list.append(
                ImageFileCreateEntry(name=base_image_url + file_name,
                                     contents=image_contents.read(),
                                     tag_ids=[tag_id]))
    return image_list
Пример #5
0
def uploadimage(picture, tagname):
    #with open(os.fsdecode(picture), mode="rb") as img_data:
    #v= trainer.create_images_from_data(project, img_data)
    my_tag = CreateOrGet(tagname)
    images_to_upload = []
    with open(picture, mode="rb") as image_contents:
        images_to_upload.append(
            ImageFileCreateEntry(name=picture, contents=image_contents.read()))
    v = trainer.create_images_from_files(project,
                                         images=images_to_upload,
                                         tag_ids=[my_tag])
    return v
Пример #6
0
def image_data_to_custom_vision_create_entry(image_data, image_path,
                                             map_tag_name_to_id):
    image_file = open(image_path, mode="rb")
    custom_vision_regions = [
        image_data_region_to_custom_vision_region(region, map_tag_name_to_id)
        for region in image_data.regions
    ]
    create_entry = ImageFileCreateEntry(name=Path(image_path).name,
                                        contents=image_file.read(),
                                        regions=custom_vision_regions)
    image_file.close()
    return create_entry
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])
Пример #8
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
Пример #9
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.")
Пример #10
0
    async def get_blob_asynchronous(self,batch_input,batch_output_list, tags):
        print("{0:<30} {1:>20}".format("File", "Completed at"))
        with ThreadPoolExecutor(max_workers=self._WORKER_CONCURRENCY) as executor:

            loop = asyncio.get_event_loop()
            # self.START_TIME = default_timer()
            tasks = [
                loop.run_in_executor(
                    executor,
                    self.get_blob,
                    *(self._block_blob_service, blob)
                )
                for blob in batch_input
            ]
            for response in await asyncio.gather(*tasks):
                customvision_image = ImageFileCreateEntry(name=self.get_blob_filename(response.name),contents=response.content,tag_ids=[self.tags_dict.get(tag).id for tag in tags])
                batch_output_list.append(customvision_image)
Пример #11
0
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.")
Пример #12
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
def create_classifier_model(classifier_folder):
    trainer = CustomVisionTrainingClient(TRAINING_KEY, endpoint=ENDPOINT)

    # Create a new project
    project_name = "Seal ID Classifier-" + ITERATION + '-' + str(ACCURACY)
    print ("Creating project... " + project_name)
    project = trainer.create_project(project_name)

    tags = {}
    image_list = []

    for subdir, dirs, files in os.walk(classifier_folder):

        # Make tags for seals
        for subdirname in dirs:
            seal_name = subdirname
            print seal_name

            tags[seal_name] = trainer.create_tag(project.id, seal_name)

        for file in files:

            print("Adding images...")

            path_name = os.path.join(subdir, file)
            seal_folder_name = subdir.split('/')[3]
            with open(path_name, "rb") as image_contents:
                print path_name
                print seal_folder_name
                image_list.append(ImageFileCreateEntry(
                    name=file, contents=image_contents.read(), tag_ids=[tags[seal_folder_name].id]))

            # batch the requests
            if len(image_list) == 60:
                send_images(trainer, project, image_list)

                image_list = []

    # send last images not hitting the 60 limit
    send_images(trainer, project, image_list)
Пример #14
0
    def main(self) -> None:
        dataset_path = os.path.join(os.path.dirname(__file__),
                                    "../../../dataset")

        existing_image_count = self.trainer.get_image_count(
            project_id=self.project.id)
        file_number = existing_image_count
        self.files_to_upload = self.files_to_upload[file_number:]

        for file_name in self.files_to_upload:
            tagged_images_with_regions = []

            annotations: list = self._read_annotation_file(
                annotation_path=os.path.join(dataset_path, "annotations",
                                             file_name + ".txt"), )
            image_bytes: bytes = read_and_resize_image(
                image_path=os.path.join(dataset_path, "images",
                                        file_name + ".JPG"),
                max_byte_size=self.max_byte_size,
            )
            print(f"Image {file_name} is {len(image_bytes)} bytes")
            tagged_images_with_regions.append(
                ImageFileCreateEntry(name=file_name,
                                     contents=image_bytes,
                                     regions=annotations))
            print("Upload images...")
            upload_result = self.trainer.create_images_from_files(
                self.project.id,
                ImageFileCreateBatch(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)
            print(
                f"Uploaded file number {file_number+1} of {len(self.files_to_upload)}"
            )
            file_number += 1
def upload_images():
    """ To upload images to Azure CV services """

    print("Uploading images ...")

    img_count = 0
    for tag in list_tags:
        print("Tag name:", tag)
        #print("Tag id:", dict_tag_ids[tag])
        print("dict_data:", dict_data[tag])

        image_list = []
        files = dict_data[tag]
        for file in files:
            file_name = file
            #print("Filename:", file_name)
            with open(file_name, "rb") as image_contents:
                #print(image_contents.read())
                image_list.append(
                    ImageFileCreateEntry(name=file_name,
                                         contents=image_contents.read(),
                                         tag_ids=[dict_tag_ids[tag]
                                                  ]))  #azure api
            img_count += 1

        print("Image_list:", image_list)

        upload_result = trainer.create_images_from_files(
            project_id, images=image_list)  #azure api
        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)

    print(img_count, "images uploaded.")
    return None
Пример #16
0
def add_training_image_with_tag(image_file, tag):
    f = image_file.read()
    b = bytearray(f)

    trainer.create_images_from_files(
        project.id, [ImageFileCreateEntry(contents=b, tag_ids=[tag.id])])
Пример #17
0
# To add the sample images to the project, insert the following code after the tag creation.
# This code uploads each image with its corresponding tag.
# You can upload up to 64 images in a single batch.
base_image_url = "train_img/"

print("Adding images...")

image_list = []

for image_num in range(1, 12):
    file_name = "mol/{}.jpg".format(image_num)
    with open(base_image_url + "mol/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[mol_tag.id]))

for image_num in range(1, 20):
    file_name = "curve/{}.jpg".format(image_num)
    with open(base_image_url + "curve/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[curve_tag.id]))

for image_num in range(1, 15):
    file_name = "imaging/{}.jpg".format(image_num)
    with open(base_image_url + "imaging/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
Пример #18
0
        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,
                                     raw=True)
    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!")
Пример #19
0
# Image list to save images
image_list = []

# Add path in from the base_image_url where you have the cruise ship images folder
directory = os.fsencode(base_image_url + "cruise/")

# Loop through image file folder
for file in os.listdir(directory):

    file_name = os.fsdecode(file)
    print("FILE NAME cruise: ", file_name)
    with open(base_image_url + "cruise/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[cruise.id]))

# Add path in from the base_image_url where you have the other images folder
directory = os.fsencode(base_image_url + "other/")

# Loop through image file folder
for file in os.listdir(directory):
    file_name = os.fsdecode(file)
    print("FILE NAME other", file_name)
    with open(base_image_url + "other/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[other.id]))
canetoad_tag = trainer.create_tag(project.id, "cane toad")
frog_tag = trainer.create_tag(project.id, "frog")

base_image_url = "alaPhotos/"

print("Adding images...")

image_list = []

for image_num in range(0, 5):
    file_name = "canetoad_{}.jpg".format(image_num)
    with open(base_image_url + "canetoad/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[canetoad_tag.id]))

for image_num in range(0, 5):
    file_name = "frog_{}.jpg".format(image_num)
    with open(base_image_url + "frog/" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[frog_tag.id]))

upload_result = trainer.create_images_from_files(
    project.id, ImageFileCreateBatch(images=image_list))
if not upload_result.is_batch_successful:
    print("Image batch upload failed.")
    for image in upload_result.images:
Пример #21
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)
Пример #22
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    logging.info(f'Method: {req.method}')
    if req.method == "OPTIONS":
        return func.HttpResponse(status_code=204,
                                 headers={
                                     'Access-Control-Allow-Headers':
                                     'content-type',
                                     'Access-Control-Allow-Methods': 'POST',
                                     'Access-Control-Max-Age': '180',
                                     'Access-Control-Allow-Origin': '*'
                                 })

    body = req.get_json()

    blob_service = BlockBlobService(account_name=storageAccount,
                                    account_key=storageAccountKey)

    # prep trainer
    trainer = CustomVisionTrainingClient(trainingKey, apiEndpoint)
    #check tags
    check_tags(trainer)

    records = {'images': []}
    image_list = []

    try:
        for item in body['items']:
            # sign
            sign = item['type'].strip()

            # image bits
            img = base64.b64decode(item['image'].replace(
                'data:image/png;base64,', ''))
            stream = BytesIO(img)

            # storage path + save
            image_name = f'{str(uuid.uuid4())}.png'
            blob_name = f'{base_folder}/{sign}/{image_name}'
            sresponse = blob_service.create_blob_from_stream(
                storageContainer, blob_name, stream)

            logging.info(f'Storage Response: {sresponse}')

            # save to custom vision
            image_list.append(
                ImageFileCreateEntry(name=image_name,
                                     contents=img,
                                     tag_ids=[tags[sign].id]))

            # return image
            path = f'{blob_service.protocol}://{blob_service.primary_endpoint}/{storageContainer}/{blob_name}'
            records['images'].append({'sign': sign, 'path': path})

        # save list
        upload_result = trainer.create_images_from_files(projectId,
                                                         images=image_list)
        if not upload_result.is_batch_successful:
            records['error'] = {'type': 'CustomVision Error', 'items': []}
            for image in upload_result.images:
                records['error']['items'].append(
                    {image.source_url: image.status})
        else:
            records['error'] = {}
    except Exception as error:
        logging.exception('Python Error')
        records['error'] = {
            'code': '500',
            'message': f'{type(error).__name__}: {str(error)}',
            'type': 'Python Error'
        }

    return func.HttpResponse(body=json.dumps(records),
                             status_code=200,
                             headers={
                                 'Content-Type': 'application/json',
                                 'Access-Control-Allow-Origin': '*'
                             })
withMask_tag = trainer.create_tag(project.id, "With Mask")
withoutMask_tag = trainer.create_tag(project.id, "Without Mask")

base_image_url = "./images/"

print("Adding images...")

image_list = []

for image_num in range(1, 6):
    file_name = "withmask_{}.jpg".format(image_num)
    with open(base_image_url + "withmask/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[withMask_tag.id]))

for image_num in range(1, 6):
    file_name = "withoutmask_{}.jpg".format(image_num)
    with open(base_image_url + "withoutmask/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[withoutMask_tag.id]))

upload_result = trainer.create_images_from_files(
    project.id, ImageFileCreateBatch(images=image_list))
if not upload_result.is_batch_successful:
    print("Image batch upload failed.")
Пример #24
0
for label_ in labels:
    images[label_] = {}
    filenames[label_] = {}
    tag_ = trainer.create_tag(project.id, label_)
    for mode_ in modes:
        images[label_][mode_] = []
        dir_ = os.path.join(base_image_location, label_, mode_)
        names_ = os.listdir(dir_)
        names_ = [os.path.join(dir_, nm_) for nm_ in names_]
        names_ = [nm_ for nm_ in names_ if os.path.isfile(nm_)]
        filenames[label_][mode_] = names_
        for nm_ in names_:
            with open(nm_, 'rb') as img_:
                images[label_][mode_].append(
                    ImageFileCreateEntry(name=nm_,
                                         contents=img_.read(),
                                         tag_ids=[tag_.id]))

#upload the training data to azure
upload_result = trainer.create_images_from_files(
    project.id,
    ImageFileCreateBatch(images=images['seamus']['train'] +
                         images['finley']['train']))
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)

#train! It's as simple as that. Might take a few minutes...
print("Training...")
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
Пример #26
0
Glass_tag = trainer.create_tag(project.id, "Glass")
GG_tag = trainer.create_tag(project.id, "GG")

base_image_url = "./treasure-free/"

print("Adding images...")

image_list = []

for image_num in range(1, 11):
    file_name = "a{}.jpg".format(image_num)
    with open(base_image_url + "picture-training/Glass/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[Glass_tag.id]))

for image_num in range(1, 11):
    file_name = "b{}.jpg".format(image_num)
    with open(base_image_url + "picture-training/Plastic/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[Plastic_tag.id]))

for image_num in range(1, 11):
    file_name = "c{}.jpg".format(image_num)
    with open(base_image_url + "picture-training/PC/" + file_name,
              "rb") as image_contents:
# </snippet_tags>

# <snippet_upload>
base_image_location = "<path to repo directory>/cognitive-services-python-sdk-samples/samples/vision/"

print("Adding images...")

image_list = []

for image_num in range(1, 11):
    file_name = "hemlock_{}.jpg".format(image_num)
    with open(base_image_location + "images/Hemlock/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[hemlock_tag.id]))

for image_num in range(1, 11):
    file_name = "japanese_cherry_{}.jpg".format(image_num)
    with open(base_image_location + "images/Japanese Cherry/" + file_name,
              "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 tag_ids=[cherry_tag.id]))

upload_result = trainer.create_images_from_files(
    project.id, ImageFileCreateBatch(images=image_list))
if not upload_result.is_batch_successful:
    print("Image batch upload failed.")
Пример #28
0
def tag_images(metadata_df,
               blob_service_client,
               container_name,
               trainer,
               project,
               base_image_url="ExampleDataset1/",
               multi_label='off'):
    '''
    Convert images to format which can be uploaded to Custom Vision portal, and add tags/labels to each image

    Parameters
    ----------
    metadata_df (Pandas DataFrame) : DataFrame mapping image filenames to grain-size labels
    blob_service_client (BlobServiceClient object): client to interact with blob storage at account level
    container_name (str) : name of the blob storage container for relevant images
    trainer (CustomVisionTrainingClient object) : client for interacting with Custom Vision
    project (Project object) : relevant Custom Vision project
    base_image_url (str) : path to desired images in blob storage
    multi_label (str) : on/off to allow for more than one label per image

    Returns
    -------
    image_list (list of ImageFileCreateEntry objects) : List of tagged image objects
    '''

    # Constant values -- change in production
    file_column = 'filename'
    target_variable = 'label'

    # Initialize list and tags which are already in project
    image_list = []
    tags = get_tags(trainer, project)

    # Loop over all images and add tags
    for index, row in metadata_df.iterrows():

        # Single tag case
        if '/' not in row[target_variable]:
            grain_sizes = [row[target_variable]]

        # Multi-label case
        elif '/' in row[target_variable] and multi_label == 'on':

            # Process values for target variable
            grain_sizes_temp = row[target_variable].split('/')
            grain_sizes = [s.strip() for s in grain_sizes_temp]

        else:
            continue

        tag_ids = []

        # Get tag objects for this image, or create a new one
        for size in grain_sizes:

            if size not in tags:
                try:
                    new_tag = trainer.create_tag(project.id, size)
                    tags[size] = new_tag
                except:
                    pass

            tag_ids.append(tags[size].id)

        filename = base_image_url + row[file_column]
        print(filename)

        try:
            # Create a blob client using the local file name as the name for the blob
            blob_client = blob_service_client.get_blob_client(
                container=container_name, blob=filename)

            # Next we retrieve and tag images for uploading, holding them in memory
            stream = io.BytesIO()

            # Read image into memory stream
            image = blob_client.download_blob().readinto(stream)

            # Tag and add to image list
            image_list.append(
                ImageFileCreateEntry(name=filename,
                                     contents=bytearray(stream.getvalue()),
                                     tag_ids=tag_ids))

            stream.close()

        except:
            print('Trouble with this file')

    return image_list
Пример #29
0
# 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",
              mode="rb") as image_contents:
        tagged_images_with_regions.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
                                 regions=regions))
yellow = coach_Rives.create_tag(legoProject.id, "yellow")
plane = coach_Rives.create_tag(legoProject.id, "plane")
car = coach_Rives.create_tag(legoProject.id, "car")
racecar = coach_Rives.create_tag(legoProject.id, "racecar")
f1car = coach_Rives.create_tag(legoProject.id, "f1car")
crane = coach_Rives.create_tag(legoProject.id, "crane")
building = coach_Rives.create_tag(legoProject.id, "building")
station = coach_Rives.create_tag(legoProject.id, "station")
orange = coach_Rives.create_tag(legoProject.id, "orange")
'''

file_name = "Images/technic/choper.jpg"
with open(file_name, mode="rb") as image_contents:
    coach_Rives.create_images_from_files(legoProject.id, [
        ImageFileCreateEntry(name=file_name,
                             contents=image_contents.read(),
                             tag_ids=[technic.id])
    ])

file_name = "Images/technic/f1car.jpg"
with open(file_name, mode="rb") as image_contents:
    coach_Rives.create_images_from_files(legoProject.id, [
        ImageFileCreateEntry(name=file_name,
                             contents=image_contents.read(),
                             tag_ids=[technic.id])
    ])

file_name = "Images/technic/truck.jpg"
with open(file_name, mode="rb") as image_contents:
    coach_Rives.create_images_from_files(legoProject.id, [
        ImageFileCreateEntry(name=file_name,