Exemple #1
0
def train_project(subscription_key):

    trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)

    # Create a new project
    print ("Creating project...")
    project = trainer.create_project(SAMPLE_PROJECT_NAME)

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

    print ("Adding images...")
    hemlock_dir = os.path.join(IMAGES_FOLDER, "Hemlock")
    for image in os.listdir(hemlock_dir):
        with open(os.path.join(hemlock_dir, image), mode="rb") as img_data: 
            trainer.create_images_from_data(project.id, img_data.read(), [ hemlock_tag.id ])
    
    cherry_dir = os.path.join(IMAGES_FOLDER, "Japanese Cherry")
    for image in os.listdir(cherry_dir):
        with open(os.path.join(cherry_dir, image), mode="rb") as img_data: 
            trainer.create_images_from_data(project.id, img_data.read(), [ cherry_tag.id ])

    print ("Training...")
    iteration = trainer.train_project(project.id)
    while (iteration.status == "Training"):
        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
Exemple #2
0
def train_project(subscription_key):
    try:
        prediction_resource_id = os.environ[
            PREDICTION_RESOURCE_ID_KEY_ENV_NAME]
    except KeyError:
        raise PredictionResourceMissingError(
            "Didn't find a prediction resource to publish to. Please set the {} environment variable"
            .format(PREDICTION_RESOURCE_ID_KEY_ENV_NAME))

    trainer = CustomVisionTrainingClient(subscription_key, endpoint=ENDPOINT)

    # Create a new project
    print("Creating project...")
    project = trainer.create_project(SAMPLE_PROJECT_NAME,
                                     classification_type=Classifier.multiclass)

    # Make two tags in the new project
    hemlock_tag = trainer.create_tag(project.id, "Hemlock")
    cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
    pine_needle_tag = trainer.create_tag(project.id, "Pine Needle Leaves")
    flat_leaf_tag = trainer.create_tag(project.id, "Flat Leaves")

    print("Adding images...")
    hemlock_dir = os.path.join(IMAGES_FOLDER, "Hemlock")
    for image in os.listdir(hemlock_dir):
        with open(os.path.join(hemlock_dir, image), mode="rb") as img_data:
            trainer.create_images_from_data(
                project.id, img_data.read(),
                [hemlock_tag.id, pine_needle_tag.id])

    cherry_dir = os.path.join(IMAGES_FOLDER, "Japanese Cherry")
    for image in os.listdir(cherry_dir):
        with open(os.path.join(cherry_dir, image), mode="rb") as img_data:
            trainer.create_images_from_data(project.id, img_data.read(),
                                            [cherry_tag.id, flat_leaf_tag.id])

    print("Training...")
    iteration = trainer.train_project(project.id)
    while (iteration.status == "Training"):
        iteration = trainer.get_iteration(project.id, iteration.id)
        print("Training status: " + iteration.status)
        time.sleep(1)

    # The iteration is now trained. Name and publish this iteration to a prediciton endpoint
    trainer.publish_iteration(project.id, iteration.id, PUBLISH_ITERATION_NAME,
                              prediction_resource_id)
    print("Done!")

    return project
Exemple #3
0
def check_tags(trainer: CustomVisionTrainingClient) -> None:
    t = ['none', 'rock', 'paper', 'scissors', 'lizard', 'spock']
    etags = {t.name: t for t in trainer.get_tags(projectId)}
    for tag in t:
        if tag in etags:
            tags[tag] = etags[tag]
        else:
            tags[tag] = trainer.create_tag(projectId, tag)
Exemple #4
0
def check_tags(trainer: CustomVisionTrainingClient) -> None:
    t = ['touching-face', 'not-touching-face', 'no-face-present']
    etags = {t.name: t for t in trainer.get_tags(projectId)}
    for tag in t:
        if tag in etags:
            tags[tag] = etags[tag]
        else:
            tags[tag] = trainer.create_tag(projectId, tag)
Exemple #5
0
def check_tags(trainer: CustomVisionTrainingClient) -> None:
    t = ['none', 'Bit', 'Doug', 'Penny']
    etags = {t.name: t for t in trainer.get_tags(projectId)}
    for tag in t:
        if tag in etags:
            tags[tag] = etags[tag]
        else:
            tags[tag] = trainer.create_tag(projectId, tag)
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.")
    def add_label_if_not_exists(self, trainer: CustomVisionTrainingClient, project: Project, tag_name: str)->Tag:
        """
        Adds a tag to the cache

        :param trainer: The project trainer
        :type: azure.cognitiveservices.vision.customvision.training.CustomVisionTrainingClient
        :param project: the Custom Vision project
        :type: string
        :param tag_name: the name of the tag to add
        :type: azure.cognitiveservices.vision.customvision.training.models.Project
        :returns: string
        """
        if len(self.tag_dictionary) == 0:
            tags=trainer.get_tags(project.id)
            for tag in tags:
                self.tag_dictionary[tag.name] = tag

        if tag_name not in self.tag_dictionary:
            self.tag_dictionary[tag_name] = trainer.create_tag(project.id, tag_name)

        return self.tag_dictionary[tag_name]
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)
# Replace with a valid key
training_key = "cde7deba2d5d4df5b768b50b700c46b7"
prediction_key = "fb49a542a16a47e6b68b2983db158c32"
prediction_resource_id = "/subscriptions/baa59b08-5ec4-44ea-a907-b12782d8e2a0/resourceGroups/Canetoads/providers/Microsoft.CognitiveServices/accounts/CaneToadMachineLea-Prediction"

publish_iteration_name = "classifyModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project("Cane Toad Classifier Python")

# Make two tags in the new project
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(),
Exemple #10
0
from bs4 import BeautifulSoup
import requests
project = trainer.create_project("TrumpAnalytics")
prediction_resource_id = "/subscriptions/31dad0b5-b624-4e49-815e-51e72ad9f6e4/resourceGroups/GeorgianCollege/providers/Microsoft.CognitiveServices/accounts/DataMiningAsgFour-Prediction"
publish_iteration_name = "classifyModel"
image_list = []

#First we train our model to predict if Trump is in an image

#collecting and counting the number of training images
training_image_url = r"https://github.com/robertroutledge/DataMiningFinalAssignment/raw/master/Python%20Code%20for%20Analysis/TrumpImageLibrary/"
page = requests.get(training_image_url)
soup = BeautifulSoup(page.text,'html.parser')
soupstring = str(soup)
num_training_images = soupstring.count("Box-row Box-row--focus-gray py-2 d-flex position-relative js-navigation-item")
trump_tag = trainer.create_tag(project.id, "Trump")

#iterates through the images, adds a tag
for x in range(1,num_training_images+1):
    name = "image{}.jpg".format(x)
    imagelink = training_image_url + name
    try:
        resource = urllib.request.urlopen(imagelink)
    except:
        print("one of the files doesn't exists")
    image_list.append(ImageFileCreateEntry(name=name, contents=resource.read(), tag_ids=[trump_tag.id]))

#uploads tagged images to Azure Custom Vision
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.")
Exemple #11
0
class LabelUtility:
    """ Utility for interacting with the Custom Vision label tool.
    """
    def __init__(self, ws_name, project_id, project_key):
        endpoint_url = "https://{}.cognitiveservices.azure.com/".format(
            ws_name)
        self.project_id = project_id
        self.client = CustomVisionTrainingClient(project_key,
                                                 endpoint=endpoint_url)
        self.project = self.client.get_project(project_id=project_id)
        self.tags = self.client.get_tags(project_id=project_id)

    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)

    def export_images(self, data_dir, img_dir="images", lbl_file="labels.csv"):
        """
        export_images - Export any tagged images that may exist
        and preserve their tags and regions.

        :param str data_dir: Output folder.
        :param str img_ext: image extension.
        :param str img_dir: image folder.
        :param str lbl_file: labels file.

        :returns: None

        """
        img_folder = os.path.join(data_dir, img_dir)
        # Check if required folders exist.
        if not os.path.isdir(img_folder):
            print("Output folder not found")
            return
        count = self.client.get_tagged_image_count(self.project_id)
        print("Found: ", count, " tagged images.")
        exported, idx = 0, 0
        data = []
        while count > 0:
            count_to_export = min(count, 256)
            print("Getting", count_to_export, "images")
            images = self.client.get_tagged_images(self.project_id,
                                                   take=count_to_export,
                                                   skip=exported)
            for image in images:
                file_name = f'file_{idx}.jpg'
                img_fname = os.path.join(img_folder, file_name)
                data += self.download_image(image, img_fname)
                idx += 1

            exported += count_to_export
            count -= count_to_export
        df = pd.DataFrame(data,
                          columns=[
                              "image_name", "DefectName", "xmin", "xmax",
                              "ymin", "ymax"
                          ])
        classes = sorted(list(set(df['DefectName'])))
        class_ids = {}
        f = open(os.path.join(data_dir, 'label_map.pbtxt'), "w+")
        for i, clas in enumerate(classes):
            class_ids[clas] = i + 1
            f.write('item {\n')
            f.write('\tid: ' + str(i + 1) + '\n')
            f.write('\tname: \'' + clas + '\'\n')
            f.write('}\n')
            f.write('\n')
        f.close()
        df['classid'] = [
            class_ids[the_defect] for the_defect in df['DefectName']
        ]
        df.to_csv(os.path.join(data_dir, lbl_file), index=False)

    @staticmethod
    def download_image(image, img_fname):
        """
        download_image - Export an image.

        :param pyImg3 image: Image object.
        :param str img_fname: Filename of the image.
        :returns: None

        """
        regions = []
        if hasattr(image, "regions"):
            regions = image.regions
        url = image.original_image_uri
        width = image.width
        height = image.height

        # Download the image
        responseFromURL = req.get(url).content
        with open(img_fname, 'wb') as f:
            f.write(responseFromURL)
            f.close()

        # Format the regions
        data = []
        for r in regions:
            left, top, wide, high = r.left, r.top, r.width, r.height
            left = left * width
            top = top * height
            wide = wide * width
            high = high * height
            data.append([
                img_fname.split("\\")[-1], r.tag_name,
                int(left),
                int(left + wide),
                int(top),
                int(top + high)
            ])
        return data
Exemple #12
0
        obj_detection_last_domain = domains[-1]

        # Create a new project
        print("Creating project...")
        try:
            project = trainer.create_project(
                "My Detection Project", domain_id=obj_detection_last_domain.id)
            project_id = project.id
        except HttpOperationError as err:
            sys.exit(
                "It seems that you have the project already. (or it can be other error.)"
            )

        # Make tags in the new project
        all_tags = [
            trainer.create_tag(project.id, "Apple"),
            trainer.create_tag(project.id, "Banana"),
            trainer.create_tag(project.id, "Mango"),
            trainer.create_tag(project.id, "Pear"),
            trainer.create_tag(project.id, "Pomelo")
        ]
    else:
        all_tags = trainer.get_tags(project_id)

    with open('../id.json', 'r') as f:
        index_to_class = json.load(f)
    tag_name_to_id = {t.name.lower(): t.id for t in all_tags}
    tag_index_to_id = {
        index: tag_name_to_id[v.lower()]
        for index, v in index_to_class.items()
    }
training_key = "1d09912649e14a70a777687237b4e39d"
prediction_key = "8a6e3ffc93d84b6ab547d944612e8b9c"
prediction_resource_id = "/subscriptions/ba66f898-5c3d-473b-953e-fefd816b4264/resourceGroups/packing/providers/Microsoft.CognitiveServices/accounts/packimagerecogniti-Prediction"

publish_iteration_name = "classifyMyChildren"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project("ClassifyMyChildren")

# Make two tags in the new project

Danny_tag = trainer.create_tag(project.id, "Danny")
Joshua_tag = trainer.create_tag(project.id, "Joshua")

base_image_url = "C:\\Users\\blessonj\\Python\\"

print("Adding images...")

image_list = []
batching_list = []

for image_num in range(1, 12):
    file_name = "Danny_{}.jpg".format(image_num)
    with open(base_image_url + "Danny\\" + file_name, "rb") as image_contents:
        image_list.append(
            ImageFileCreateEntry(name=file_name,
                                 contents=image_contents.read(),
Exemple #14
0
ENDPOINT = "https://australiaeast.api.cognitive.microsoft.com/"

# Replace with a valid key
training_key = "2382a5811bf34c099f3d8385d64d13da"
prediction_key = "dcf92e4f48224955a770e3e6e7390e3d"
prediction_resource_id = "/subscriptions/90ac3555-f817-4728-8e02-c5e28a30f349/resourceGroups/test/providers/Microsoft.CognitiveServices/accounts/test_prediction"

publish_iteration_name = "classifyModel"

trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)

# Create a new project
print("Creating project...")
project = trainer.create_project("AR-lab")

oscilloscope_tag = trainer.create_tag(project.id, "oscilloscope")
powersupply_tag = trainer.create_tag(project.id, "powersupply")
multimeter_tag = trainer.create_tag(project.id, "multimeter")
waveformgenerator_tag = trainer.create_tag(project.id, "waveformgenerator")
phoebe_tag = trainer.create_tag(project.id, "phoebe")
base_image_url = os.getcwd() + "/"

print("Adding images...")

print("Uploading oscilloscope and power supply")
image_list = []

for image_num in range(0, 16):
    file_name = "oscilloscope{}.jpg".format(image_num)
    with open(base_image_url + "images/oscilloscope/" + file_name,
              "rb") as image_contents:
# Replace with a valid key
training_key = "Train key"
prediction_key = "Pred key"
prediction_resource_id = "Prediction resource id"

publish_iteration_name = "classifyModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

#Create a new project
print("Creating project...")
project = trainer.create_project("My Project")

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(),
publish_iteration_name = "detectModel"

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" and domain.name == "General")

# Create a new project
print("Creating project...")
project = trainer.create_project("IA-Apple-Orange",
                                 domain_id=obj_detection_domain.id)

# Make two tags in the new project
fork_tag = trainer.create_tag(project.id, "maça")
scissors_tag = trainer.create_tag(project.id, "laranja")

fork_image_regions = {
    "maca_1": [0.55, 0.46, 0.472, 0.505],
    #    "maca_2": [ 0.294117659, 0.216944471, 0.534313738, 0.5980392 ],
    #    "maca_3": [ 0.09191177, 0.0682516545, 0.757352948, 0.6143791 ],
    #    "maca_4": [ 0.254901975, 0.185898721, 0.5232843, 0.594771266 ],
    #    "maca_5": [ 0.2365196, 0.128709182, 0.5845588, 0.71405226 ],
    #    "maca_6": [ 0.115196079, 0.133611143, 0.676470637, 0.6993464 ],
    #    "maca_7": [ 0.164215669, 0.31008172, 0.767156839, 0.410130739 ],
    #    "maca_8": [ 0.118872553, 0.318251669, 0.817401946, 0.225490168 ],
    #    "maca_9": [ 0.18259804, 0.2136765, 0.6335784, 0.643790841 ],
    #    "maca_10": [ 0.05269608, 0.282303959, 0.8088235, 0.452614367 ],
    #    "maca_11": [ 0.05759804, 0.0894935, 0.9007353, 0.3251634 ],
    #    "maca_12": [ 0.3345588, 0.07315363, 0.375, 0.9150327 ],
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
Exemple #18
0
prediction_key = data['prediction_key']
prediction_resource_id = data['prediction_resource_id']

publish_iteration_name = "detectModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)


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

# Create a new project
print ("Creating project...")
project = trainer.create_project(data["project_name"] ,domain_id=obj_detection_domain.id)
vehicle_tag = trainer.create_tag(project.id, data["tag"])
print("New project created")













# </snippet_auth>

# <snippet_create>
publish_iteration_name = "classifyModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project("My New Project")
# </snippet_create>

# <snippet_tags>
# Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
# </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(
Exemple #20
0
        domains = [domain for domain in trainer.get_domains()]
        obj_detection_last_domain = domains[-1]

        # Create a new project
        print("Creating project...")
        try:
            project = trainer.create_project(
                "My Detection Project", domain_id=obj_detection_last_domain.id)
            project_id = project.id
        except HttpOperationError as err:
            sys.exit(
                "It seems that you have the project already. (or it can be other error.)"
            )

        # Make tags in the new project
        all_tags = [trainer.create_tag(project.id, s) for s in all_tag_names]
    else:
        all_tags = trainer.get_tags(project_id)

    tag_name_to_id = {t.name.lower(): t.id for t in all_tags}
    tag_index_to_id = {
        index: tag_name_to_id[v.lower()]
        for index, v in index_to_class.items()
    }

    with open(IMAGES_PATH + "/anno.json") as data_file:
        data = json.load(data_file)

    images = []
    for k, v in data.items():
        regions_lis = []
# print(connect_str)
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

base_image_url = "https://originaldataset.blob.core.windows.net/"

# print(list(blob_service_client.list_containers()))
ambulance_container = blob_service_client.get_container_client("ambulance")
bench_container = blob_service_client.get_container_client("bench")

# Create a new project
print("Creating project...")
project = trainer.create_project("drawings")

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

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

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

for blob in bench_container.list_blobs():
    blob_name = blob.name
    blob_url = f"{base_image_url}bench/{blob_name}"
tags = os.environ["INPUT_TAGS"]
tagsVar = os.environ["INPUT_TAGSVAR"]
trainSize = int(os.environ["INPUT_TRAINSIZE"])

tags_str = (str(tags).strip('[]')).split(",")
tagsVar_str = (str(tagsVar).strip('[]')).split(",")

#List of tag variables
tag_list = []

num_tags = len(tags_str)

#Make tags in the new project
for tag in tags_str:
    tag_list.append(trainer.create_tag(project.id, tag))

base_image_url = "https://github.com/" + os.environ[
    "GITHUB_REPOSITORY"] + "/raw/master/"

print("Adding images...")

image_list = []

for i in range(num_tags):
    for image_num in range(1, trainSize + 1):
        file_name = tagsVar_str[i] + "{}.jpg".format(image_num)
        response = requests.get(base_image_url + "images/" + tags_str[i] +
                                "/" + file_name)
        print(base_image_url + "images/" + tags_str[i] + "/" + file_name)
        image_file = io.BytesIO(response.content)
# ENDPOINT is just the value of training endpoint with /customvision/v2.2/Training/ removed
# e.g. https://eastcentralus.api.cognitive.microsoft.com

ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
training_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)

# Create a new project
print("Creating project...")
project = trainer.create_project("ElephantNoElephant")
print("Project created")

# Make two tags in the new project
print("Creating tags")
elephant_tag = trainer.create_tag(project.id, "Elephant")
giraffe_tag = trainer.create_tag(project.id, "Giraffe")

print("Adding images...")
# Add all images in Elephant folder to your project with the tag "elephant"
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             "ElephantGiraffeTrainingImages")
elephant_dir = os.path.join(IMAGES_FOLDER, "Elephant")
for image in os.listdir(elephant_dir):
    with open(os.path.join(elephant_dir, image), mode="rb") as img_data:
        trainer.create_images_from_data(project.id, img_data.read(),
                                        [elephant_tag.id])
print("added elephants")

# Add all images in Giraffe folder to your project with the tag "giraffe"
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)),
Exemple #24
0
base_image_location = '/Users/nickhaber/Downloads/teachable_machine_demo'

print("Adding images...")

#Need to create a list of all ImageFileCreateEntry objects
#with images tagged according to the label
#the control flow here reflects my directory structure
images = {}
filenames = {}
labels = ['seamus', 'finley']
modes = ['train', 'test']

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
vision_credentials = CognitiveServicesCredentials(COGSVCS_KEY)
vision_client = ComputerVisionClient(COGSVCS_CLIENTURL, vision_credentials)

person_group_id = 'reactor'

publish_iteration_name = "classifyModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(custom_vision_endpoint, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project("CV Project")

# Make two tags in the new project
cruise = trainer.create_tag(project.id, "cruise")
other = trainer.create_tag(project.id, "other")

# Add the base path to the downloaded image files
base_image_url = ""

print("Adding images...")

# 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):
Exemple #26
0
    def trainModel(self, database, modelID, parameters, onMessage, onFinished):
        onMessage("Trainer fetching model settings.")
        session = database.cursor()
        session.execute(
            "SELECT remote_id, training_data, extra_info FROM " +
            self._datatableName + " WHERE id = %s", (modelID, ))
        result = session.fetchone()
        session.close()

        if result:
            projectID, trainingData, _ = result

            onMessage("Training starting...")

            onMessage("Retrieving model...")
            trainer = CustomVisionTrainingClient(self._trainingKey,
                                                 endpoint=self._endPoint)
            project = trainer.get_project(projectID)

            onMessage("Downloading/Caching and Analyzing training data...")

            imageList = []
            dataClassList = {}

            try:
                start = time.time()
                # retrieve information of created tags
                createdTags = trainer.get_tags(projectID)
                for tag in createdTags:
                    dataClassList[tag.name] = tag

                imageOK = 0
                imageFailed = 0
                imageTotal = len(trainingData)

                def visualizeImageDownload():
                    return "(" + str(imageOK) + "/" + str(
                        imageFailed) + "/" + str(imageTotal) + ")"

                for photoID in trainingData:
                    image, _, err = self._serverAPI.getResource(
                        database, photoID)

                    if err:
                        imageFailed += 1
                        onMessage("Failed to download image " + str(photoID) +
                                  ". Error: " + err + " " +
                                  visualizeImageDownload())
                    else:
                        imageOK += 1

                        classOfData = str(trainingData[photoID])

                        # create tag if not exists
                        if classOfData not in dataClassList:
                            dataClassList[classOfData] = trainer.create_tag(
                                project.id, classOfData)

                        isOK, encodedImage = cv2.imencode('.png', image)
                        imageList.append(
                            ImageFileCreateEntry(
                                name=str(photoID) + ".png",
                                contents=encodedImage,
                                tag_ids=[dataClassList[classOfData].id]))

                        onMessage(visualizeImageDownload())
                end = time.time()
                onMessage("Image caching done. Used: " + str(end - start))

                start = time.time()
                for i in range(0, len(imageList), 64):
                    batch = imageList[i:i + 64]
                    upload_result = trainer.create_images_from_files(
                        project.id, images=batch)

                    if not upload_result.is_batch_successful:
                        onMessage("Image batch upload failed.")

                        for image in upload_result.images:
                            onMessage("Image status: ", image.status)

                        onFinished(False)
                        return
                end = time.time()
                onMessage("Image upload done. Used: " + str(end - start))

                onMessage("Training model with " + str(imageOK) + " photos...")

                iteration = trainer.train_project(project.id)
                while (iteration.status != "Completed"):
                    iteration = trainer.get_iteration(project.id, iteration.id)
                    onMessage("Training status: " + iteration.status)
                    time.sleep(3)

                # The iteration is now trained. Publish it to the project endpoint
                trainer.publish_iteration(project.id, iteration.id, projectID,
                                          self._resourceID)
                onMessage("Training done.")
                onFinished(True)

            except Exception as err:
                onMessage("Failed to train.")
                onMessage("Error Message: " + str(err))
                onFinished(False)
        else:
            onMessage("The trainer can't recognize the given model any more.")
            onFinished(False)
Exemple #27
0
# Replace with a valid key
training_key = "edb35a52aa04460d8edcffc78893ec9c3"
prediction_key = "<your prediction key>"
prediction_resource_id = "/subscriptions/b580d1b7-163e-4a91-bb43-1d260d3753a5/resourceGroups/mygroup/providers/Microsoft.CognitiveServices/accounts/Mol_Images"

publish_iteration_name = "model1"

trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT)

# Create a new project
print("Creating project...")
project = trainer.create_project("OPVDB_classification")

# Make two tags in the new project
mol_tag = trainer.create_tag(project.id, "mol")
curve_tag = trainer.create_tag(project.id, "curve")
# uvvis_tag = trainer.create_tag(project.id, "uvvis")
imaging_tag = trainer.create_tag(project.id, "imaging")

# 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)
apiEndpoint = "https://southcentralus.api.cognitive.microsoft.com"

# Bizim için üretiken traning ve prediction key değerleri
tKey = "c3a53a4fb5f24137a179f0bcaf7754a5"
pKey = "bf7571576405446782543f832b038891"

# Eğitmen istemci nesnesi tanımlanıyor. İlk parametre traning_key
# ikinci parametre Cognitive servis adresi
coach_Rives = CustomVisionTrainingClient(tKey, endpoint=apiEndpoint)

# Projeyi oluşturuyoruz
print("Lego projesi oluşturuluyor")
legoProject = coach_Rives.create_project("Agent_Leggooo")  # projemizin adı

# Şimdi deneme amaçlı tag'ler oluşturup bu tag'lere çeşitli fotoğraflar yükleyeceğiz
technic = coach_Rives.create_tag(legoProject.id, "technic")
city = coach_Rives.create_tag(legoProject.id, "city")

# Aşağıdaki tag'ler şu anda yorum satırı. Bunları açıp, create_images_from_files metodlarındaki tag_ids dizisine ekleyebiliriz.
# Ancak Vision servisi her tag için en az beş adete fotoğraf olmasını istiyor. Bu kümeyi örnekleyemediğim için sadece iki tag ile ilerledim.
'''
helicopter = coach_Rives.create_tag(legoProject.id, "helicopter")
truck = coach_Rives.create_tag(legoProject.id, "truck")
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")
Exemple #29
0
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)

publish_iteration_name = "detectModel"

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

# 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],
Exemple #30
0
# Replace with a valid key
training_key = "f14a2b199e614fafbe96e23b17fd58b1"
prediction_key = "8d30ba3f687644c582153ad00394cd9c"
prediction_resource_id = "/subscriptions/b94ca22d-fa8d-4e70-9231-3224c99a1bcf/resourceGroups/treasure_ntub/providers/Microsoft.CognitiveServices/accounts/TreasureTrain-Prediction"

publish_iteration_name = "classifyModel"

credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print("Creating project...")
project = trainer.create_project("treasure-free")

# Make two tags in the new project
Plastic_tag = trainer.create_tag(project.id, "Plastic")
PC_tag = trainer.create_tag(project.id, "PC")
IAC_tag = trainer.create_tag(project.id, "IAC")
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: