Selecione suas preferências de cookies

Usamos cookies essenciais e ferramentas semelhantes que são necessárias para fornecer nosso site e serviços. Usamos cookies de desempenho para coletar estatísticas anônimas, para que possamos entender como os clientes usam nosso site e fazer as devidas melhorias. Cookies essenciais não podem ser desativados, mas você pode clicar em “Personalizar” ou “Recusar” para recusar cookies de desempenho.

Se você concordar, a AWS e terceiros aprovados também usarão cookies para fornecer recursos úteis do site, lembrar suas preferências e exibir conteúdo relevante, incluindo publicidade relevante. Para aceitar ou recusar todos os cookies não essenciais, clique em “Aceitar” ou “Recusar”. Para fazer escolhas mais detalhadas, clique em “Personalizar”.

Lookout for Vision usando o SDK para Python (Boto3) - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Lookout for Vision usando o SDK para Python (Boto3)

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK for Python (Boto3) with Lookout for Vision.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar perfis de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Cenários são exemplos de código que mostram como realizar tarefas específicas chamando várias funções dentro de um serviço ou combinadas com outros Serviços da AWS.

Cada exemplo inclui um link para o código-fonte completo, em que você pode encontrar instruções sobre como configurar e executar o código.

Conceitos básicos

O exemplo de código a seguir mostra como começar a usar o Lookout for Vision.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

""" This example shows how to list your Amazon Lookout for Vision projects. If you haven't previously created a project in the current AWS Region, the response is an empty list, however it confirms that you can call the Lookout for Vision API. """ from botocore.exceptions import ClientError import boto3 class Hello: """Hello class for Amazon Lookout for Vision""" @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in your AWS account and in the current AWS Region. : param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError as err: print(f"Couldn't list projects. \n{err}") raise def main(): session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") Hello.list_projects(lookoutvision_client) if __name__ == "__main__": main()
  • Para obter detalhes da API, consulte a ListProjectsReferência da API AWS SDK for Python (Boto3).

O exemplo de código a seguir mostra como começar a usar o Lookout for Vision.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

""" This example shows how to list your Amazon Lookout for Vision projects. If you haven't previously created a project in the current AWS Region, the response is an empty list, however it confirms that you can call the Lookout for Vision API. """ from botocore.exceptions import ClientError import boto3 class Hello: """Hello class for Amazon Lookout for Vision""" @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in your AWS account and in the current AWS Region. : param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError as err: print(f"Couldn't list projects. \n{err}") raise def main(): session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") Hello.list_projects(lookoutvision_client) if __name__ == "__main__": main()
  • Para obter detalhes da API, consulte a ListProjectsReferência da API AWS SDK for Python (Boto3).

Ações

O código de exemplo a seguir mostra como usar CreateDataset.

Para obter mais informações, consulte Criar conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def create_dataset(lookoutvision_client, project_name, manifest_file, dataset_type): """ Creates a new Lookout for Vision dataset :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project_name: The name of the project in which you want to create a dataset. :param bucket: The bucket that contains the manifest file. :param manifest_file: The path and name of the manifest file. :param dataset_type: The type of the dataset (train or test). """ try: bucket, key = manifest_file.replace("s3://", "").split("/", 1) logger.info("Creating %s dataset type...", dataset_type) dataset = { "GroundTruthManifest": {"S3Object": {"Bucket": bucket, "Key": key}} } response = lookoutvision_client.create_dataset( ProjectName=project_name, DatasetType=dataset_type, DatasetSource=dataset, ) logger.info("Dataset Status: %s", response["DatasetMetadata"]["Status"]) logger.info( "Dataset Status Message: %s", response["DatasetMetadata"]["StatusMessage"], ) logger.info("Dataset Type: %s", response["DatasetMetadata"]["DatasetType"]) # Wait until either created or failed. finished = False status = "" dataset_description = {} while finished is False: dataset_description = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) status = dataset_description["DatasetDescription"]["Status"] if status == "CREATE_IN_PROGRESS": logger.info("Dataset creation in progress...") time.sleep(2) elif status == "CREATE_COMPLETE": logger.info("Dataset created.") finished = True else: logger.info( "Dataset creation failed: %s", dataset_description["DatasetDescription"]["StatusMessage"], ) finished = True if status != "CREATE_COMPLETE": message = dataset_description["DatasetDescription"]["StatusMessage"] logger.exception("Couldn't create dataset: %s", message) raise Exception(f"Couldn't create dataset: {message}") except ClientError: logger.exception("Service error: Couldn't create dataset.") raise
  • Para obter detalhes da API, consulte a CreateDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar CreateDataset.

Para obter mais informações, consulte Criar conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def create_dataset(lookoutvision_client, project_name, manifest_file, dataset_type): """ Creates a new Lookout for Vision dataset :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project_name: The name of the project in which you want to create a dataset. :param bucket: The bucket that contains the manifest file. :param manifest_file: The path and name of the manifest file. :param dataset_type: The type of the dataset (train or test). """ try: bucket, key = manifest_file.replace("s3://", "").split("/", 1) logger.info("Creating %s dataset type...", dataset_type) dataset = { "GroundTruthManifest": {"S3Object": {"Bucket": bucket, "Key": key}} } response = lookoutvision_client.create_dataset( ProjectName=project_name, DatasetType=dataset_type, DatasetSource=dataset, ) logger.info("Dataset Status: %s", response["DatasetMetadata"]["Status"]) logger.info( "Dataset Status Message: %s", response["DatasetMetadata"]["StatusMessage"], ) logger.info("Dataset Type: %s", response["DatasetMetadata"]["DatasetType"]) # Wait until either created or failed. finished = False status = "" dataset_description = {} while finished is False: dataset_description = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) status = dataset_description["DatasetDescription"]["Status"] if status == "CREATE_IN_PROGRESS": logger.info("Dataset creation in progress...") time.sleep(2) elif status == "CREATE_COMPLETE": logger.info("Dataset created.") finished = True else: logger.info( "Dataset creation failed: %s", dataset_description["DatasetDescription"]["StatusMessage"], ) finished = True if status != "CREATE_COMPLETE": message = dataset_description["DatasetDescription"]["StatusMessage"] logger.exception("Couldn't create dataset: %s", message) raise Exception(f"Couldn't create dataset: {message}") except ClientError: logger.exception("Service error: Couldn't create dataset.") raise
  • Para obter detalhes da API, consulte a CreateDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar CreateModel.

Para obter mais informações, consulte Como treinar o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def create_model( lookoutvision_client, project_name, training_results, tag_key=None, tag_key_value=None, ): """ Creates a version of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project in which you want to create a model. :param training_results: The Amazon S3 location where training results are stored. :param tag_key: The key for a tag to add to the model. :param tag_key_value - A value associated with the tag_key. return: The model status and version. """ try: logger.info("Training model...") output_bucket, output_folder = training_results.replace("s3://", "").split( "/", 1 ) output_config = { "S3Location": {"Bucket": output_bucket, "Prefix": output_folder} } tags = [] if tag_key is not None: tags = [{"Key": tag_key, "Value": tag_key_value}] response = lookoutvision_client.create_model( ProjectName=project_name, OutputConfig=output_config, Tags=tags ) logger.info("ARN: %s", response["ModelMetadata"]["ModelArn"]) logger.info("Version: %s", response["ModelMetadata"]["ModelVersion"]) logger.info("Started training...") print("Training started. Training might take several hours to complete.") # Wait until training completes. finished = False status = "UNKNOWN" while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=response["ModelMetadata"]["ModelVersion"], ) status = model_description["ModelDescription"]["Status"] if status == "TRAINING": logger.info("Model training in progress...") time.sleep(600) continue if status == "TRAINED": logger.info("Model was successfully trained.") else: logger.info( "Model training failed: %s ", model_description["ModelDescription"]["StatusMessage"], ) finished = True except ClientError: logger.exception("Couldn't train model.") raise else: return status, response["ModelMetadata"]["ModelVersion"]
  • Para obter detalhes da API, consulte a CreateModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar CreateModel.

Para obter mais informações, consulte Como treinar o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def create_model( lookoutvision_client, project_name, training_results, tag_key=None, tag_key_value=None, ): """ Creates a version of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project in which you want to create a model. :param training_results: The Amazon S3 location where training results are stored. :param tag_key: The key for a tag to add to the model. :param tag_key_value - A value associated with the tag_key. return: The model status and version. """ try: logger.info("Training model...") output_bucket, output_folder = training_results.replace("s3://", "").split( "/", 1 ) output_config = { "S3Location": {"Bucket": output_bucket, "Prefix": output_folder} } tags = [] if tag_key is not None: tags = [{"Key": tag_key, "Value": tag_key_value}] response = lookoutvision_client.create_model( ProjectName=project_name, OutputConfig=output_config, Tags=tags ) logger.info("ARN: %s", response["ModelMetadata"]["ModelArn"]) logger.info("Version: %s", response["ModelMetadata"]["ModelVersion"]) logger.info("Started training...") print("Training started. Training might take several hours to complete.") # Wait until training completes. finished = False status = "UNKNOWN" while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=response["ModelMetadata"]["ModelVersion"], ) status = model_description["ModelDescription"]["Status"] if status == "TRAINING": logger.info("Model training in progress...") time.sleep(600) continue if status == "TRAINED": logger.info("Model was successfully trained.") else: logger.info( "Model training failed: %s ", model_description["ModelDescription"]["StatusMessage"], ) finished = True except ClientError: logger.exception("Couldn't train model.") raise else: return status, response["ModelMetadata"]["ModelVersion"]
  • Para obter detalhes da API, consulte a CreateModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar CreateProject.

Para obter mais informações, consulte Criar o projeto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def create_project(lookoutvision_client, project_name): """ Creates a new Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name for the new project. :return project_arn: The ARN of the new project. """ try: logger.info("Creating project: %s", project_name) response = lookoutvision_client.create_project(ProjectName=project_name) project_arn = response["ProjectMetadata"]["ProjectArn"] logger.info("project ARN: %s", project_arn) except ClientError: logger.exception("Couldn't create project %s.", project_name) raise else: return project_arn
  • Para obter detalhes da API, consulte a CreateProjectReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar CreateProject.

Para obter mais informações, consulte Criar o projeto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def create_project(lookoutvision_client, project_name): """ Creates a new Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name for the new project. :return project_arn: The ARN of the new project. """ try: logger.info("Creating project: %s", project_name) response = lookoutvision_client.create_project(ProjectName=project_name) project_arn = response["ProjectMetadata"]["ProjectArn"] logger.info("project ARN: %s", project_arn) except ClientError: logger.exception("Couldn't create project %s.", project_name) raise else: return project_arn
  • Para obter detalhes da API, consulte a CreateProjectReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteDataset.

Para obter mais informações, consulte Excluindo um conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def delete_dataset(lookoutvision_client, project_name, dataset_type): """ Deletes a Lookout for Vision dataset :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to delete. :param dataset_type: The type (train or test) of the dataset that you want to delete. """ try: logger.info( "Deleting the %s dataset for project %s.", dataset_type, project_name ) lookoutvision_client.delete_dataset( ProjectName=project_name, DatasetType=dataset_type ) logger.info("Dataset deleted.") except ClientError: logger.exception("Service error: Couldn't delete dataset.") raise
  • Para obter detalhes da API, consulte a DeleteDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteDataset.

Para obter mais informações, consulte Excluindo um conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def delete_dataset(lookoutvision_client, project_name, dataset_type): """ Deletes a Lookout for Vision dataset :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to delete. :param dataset_type: The type (train or test) of the dataset that you want to delete. """ try: logger.info( "Deleting the %s dataset for project %s.", dataset_type, project_name ) lookoutvision_client.delete_dataset( ProjectName=project_name, DatasetType=dataset_type ) logger.info("Dataset deleted.") except ClientError: logger.exception("Service error: Couldn't delete dataset.") raise
  • Para obter detalhes da API, consulte a DeleteDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteModel.

Para obter mais informações, consulte Excluindo um modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def delete_model(lookoutvision_client, project_name, model_version): """ Deletes a Lookout for Vision model. The model must first be stopped and can't be in training. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model that you want to delete. """ try: logger.info("Deleting model: %s", model_version) lookoutvision_client.delete_model( ProjectName=project_name, ModelVersion=model_version ) model_exists = True while model_exists: response = lookoutvision_client.list_models(ProjectName=project_name) model_exists = False for model in response["Models"]: if model["ModelVersion"] == model_version: model_exists = True if model_exists is False: logger.info("Model deleted") else: logger.info("Model is being deleted...") time.sleep(2) logger.info("Deleted Model: %s", model_version) except ClientError: logger.exception("Couldn't delete model.") raise
  • Para obter detalhes da API, consulte a DeleteModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteModel.

Para obter mais informações, consulte Excluindo um modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def delete_model(lookoutvision_client, project_name, model_version): """ Deletes a Lookout for Vision model. The model must first be stopped and can't be in training. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model that you want to delete. """ try: logger.info("Deleting model: %s", model_version) lookoutvision_client.delete_model( ProjectName=project_name, ModelVersion=model_version ) model_exists = True while model_exists: response = lookoutvision_client.list_models(ProjectName=project_name) model_exists = False for model in response["Models"]: if model["ModelVersion"] == model_version: model_exists = True if model_exists is False: logger.info("Model deleted") else: logger.info("Model is being deleted...") time.sleep(2) logger.info("Deleted Model: %s", model_version) except ClientError: logger.exception("Couldn't delete model.") raise
  • Para obter detalhes da API, consulte a DeleteModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteProject.

Para obter mais informações, consulte Excluindo um projeto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def delete_project(lookoutvision_client, project_name): """ Deletes a Lookout for Vision Model :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to delete. """ try: logger.info("Deleting project: %s", project_name) response = lookoutvision_client.delete_project(ProjectName=project_name) logger.info("Deleted project ARN: %s ", response["ProjectArn"]) except ClientError as err: logger.exception("Couldn't delete project %s.", project_name) raise
  • Para obter detalhes da API, consulte a DeleteProjectReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DeleteProject.

Para obter mais informações, consulte Excluindo um projeto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def delete_project(lookoutvision_client, project_name): """ Deletes a Lookout for Vision Model :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to delete. """ try: logger.info("Deleting project: %s", project_name) response = lookoutvision_client.delete_project(ProjectName=project_name) logger.info("Deleted project ARN: %s ", response["ProjectArn"]) except ClientError as err: logger.exception("Couldn't delete project %s.", project_name) raise
  • Para obter detalhes da API, consulte a DeleteProjectReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DescribeDataset.

Para obter mais informações, consulte Visualizar conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def describe_dataset(lookoutvision_client, project_name, dataset_type): """ Gets information about a Lookout for Vision dataset. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to describe. :param dataset_type: The type (train or test) of the dataset that you want to describe. """ try: response = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) print(f"Name: {response['DatasetDescription']['ProjectName']}") print(f"Type: {response['DatasetDescription']['DatasetType']}") print(f"Status: {response['DatasetDescription']['Status']}") print(f"Message: {response['DatasetDescription']['StatusMessage']}") print(f"Images: {response['DatasetDescription']['ImageStats']['Total']}") print(f"Labeled: {response['DatasetDescription']['ImageStats']['Labeled']}") print(f"Normal: {response['DatasetDescription']['ImageStats']['Normal']}") print(f"Anomaly: {response['DatasetDescription']['ImageStats']['Anomaly']}") except ClientError: logger.exception("Service error: problem listing datasets.") raise print("Done.")
  • Para obter detalhes da API, consulte a DescribeDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DescribeDataset.

Para obter mais informações, consulte Visualizar conjunto de dados.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def describe_dataset(lookoutvision_client, project_name, dataset_type): """ Gets information about a Lookout for Vision dataset. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to describe. :param dataset_type: The type (train or test) of the dataset that you want to describe. """ try: response = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) print(f"Name: {response['DatasetDescription']['ProjectName']}") print(f"Type: {response['DatasetDescription']['DatasetType']}") print(f"Status: {response['DatasetDescription']['Status']}") print(f"Message: {response['DatasetDescription']['StatusMessage']}") print(f"Images: {response['DatasetDescription']['ImageStats']['Total']}") print(f"Labeled: {response['DatasetDescription']['ImageStats']['Labeled']}") print(f"Normal: {response['DatasetDescription']['ImageStats']['Normal']}") print(f"Anomaly: {response['DatasetDescription']['ImageStats']['Anomaly']}") except ClientError: logger.exception("Service error: problem listing datasets.") raise print("Done.")
  • Para obter detalhes da API, consulte a DescribeDatasetReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DescribeModel.

Para obter mais informações, consulte Visualizar modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def describe_model(lookoutvision_client, project_name, model_version): """ Shows the performance metrics for a trained model. :param lookoutvision_client: A Boto3 Amazon Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model. """ response = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) model_description = response["ModelDescription"] print(f"\tModel version: {model_description['ModelVersion']}") print(f"\tARN: {model_description['ModelArn']}") if "Description" in model_description: print(f"\tDescription: {model_description['Description']}") print(f"\tStatus: {model_description['Status']}") print(f"\tMessage: {model_description['StatusMessage']}") print(f"\tCreated: {str(model_description['CreationTimestamp'])}") if model_description["Status"] in ("TRAINED", "HOSTED"): training_start = model_description["CreationTimestamp"] training_end = model_description["EvaluationEndTimestamp"] duration = training_end - training_start print(f"\tTraining duration: {duration}") print("\n\tPerformance metrics\n\t-------------------") print(f"\tRecall: {model_description['Performance']['Recall']}") print(f"\tPrecision: {model_description['Performance']['Precision']}") print(f"\tF1: {model_description['Performance']['F1Score']}") training_output_bucket = model_description["OutputConfig"]["S3Location"][ "Bucket" ] prefix = model_description["OutputConfig"]["S3Location"]["Prefix"] print(f"\tTraining output: s3://{training_output_bucket}/{prefix}")
  • Para obter detalhes da API, consulte a DescribeModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DescribeModel.

Para obter mais informações, consulte Visualizar modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def describe_model(lookoutvision_client, project_name, model_version): """ Shows the performance metrics for a trained model. :param lookoutvision_client: A Boto3 Amazon Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model. """ response = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) model_description = response["ModelDescription"] print(f"\tModel version: {model_description['ModelVersion']}") print(f"\tARN: {model_description['ModelArn']}") if "Description" in model_description: print(f"\tDescription: {model_description['Description']}") print(f"\tStatus: {model_description['Status']}") print(f"\tMessage: {model_description['StatusMessage']}") print(f"\tCreated: {str(model_description['CreationTimestamp'])}") if model_description["Status"] in ("TRAINED", "HOSTED"): training_start = model_description["CreationTimestamp"] training_end = model_description["EvaluationEndTimestamp"] duration = training_end - training_start print(f"\tTraining duration: {duration}") print("\n\tPerformance metrics\n\t-------------------") print(f"\tRecall: {model_description['Performance']['Recall']}") print(f"\tPrecision: {model_description['Performance']['Precision']}") print(f"\tF1: {model_description['Performance']['F1Score']}") training_output_bucket = model_description["OutputConfig"]["S3Location"][ "Bucket" ] prefix = model_description["OutputConfig"]["S3Location"]["Prefix"] print(f"\tTraining output: s3://{training_output_bucket}/{prefix}")
  • Para obter detalhes da API, consulte a DescribeModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DetectAnomalies.

Para obter mais informações, consulte Detectar anomalias em uma imagem.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Inference: """ Shows how to detect anomalies in an image using a trained Lookout for Vision model. """ @staticmethod def detect_anomalies(lookoutvision_client, project_name, model_version, photo): """ Calls DetectAnomalies using the supplied project, model version, and image. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The project that contains the model that you want to use. :param model_version: The version of the model that you want to use. :param photo: The photo that you want to analyze. :return: The DetectAnomalyResult object that contains the analysis results. """ image_type = imghdr.what(photo) if image_type == "jpeg": content_type = "image/jpeg" elif image_type == "png": content_type = "image/png" else: logger.info("Image type not valid for %s", photo) raise ValueError( f"File format not valid. Supply a jpeg or png format file: {photo}" ) # Get images bytes for call to detect_anomalies. with open(photo, "rb") as image: response = lookoutvision_client.detect_anomalies( ProjectName=project_name, ContentType=content_type, Body=image.read(), ModelVersion=model_version, ) return response["DetectAnomalyResult"] @staticmethod def download_from_s3(s3_resource, photo): """ Downloads an image from an S3 bucket. :param s3_resource: A Boto3 Amazon S3 resource. :param photo: The Amazon S3 path of a photo to download. return: The local path to the downloaded file. """ try: bucket, key = photo.replace("s3://", "").split("/", 1) local_file = os.path.basename(photo) except ValueError: logger.exception("Couldn't get S3 info for %s", photo) raise try: logger.info("Downloading %s", photo) s3_resource.Bucket(bucket).download_file(key, local_file) except ClientError: logger.exception("Couldn't download %s from S3.", photo) raise return local_file @staticmethod def reject_on_classification(image, prediction, confidence_limit): """ Returns True if the anomaly confidence is greater than or equal to the supplied confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0 - 1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking classification for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) is greater" f" than limit ({confidence_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ): """ Checks if the number of anomaly types is greater than the anomaly types limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence: The minimum acceptable confidence (float 0 - 1). :param anomaly_types_limit: The maximum number of allowable anomaly types (int). :return: True if the error condition indicates an anomaly, otherwise False. """ logger.info("Checking number of anomaly types for %s", image) reject = False if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: anomaly_types = { anomaly["Name"] for anomaly in prediction["Anomalies"] if anomaly["Name"] != "background" } if len(anomaly_types) > anomaly_types_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and " f"the number of anomaly types ({len(anomaly_types)-1}) is " f"greater than the limit ({anomaly_types_limit})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ): """ Checks if the coverage area of an anomaly is greater than the coverage limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0-1). :anomaly_label: The anomaly label for the type of anomaly that you want to check. :coverage_limit: The maximum acceptable percentage coverage of an anomaly (float 0-1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking coverage for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: for anomaly in prediction["Anomalies"]: if anomaly["Name"] == anomaly_label and anomaly["PixelAnomaly"][ "TotalPercentageArea" ] > (coverage_limit): reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and {anomaly['Name']} " f"coverage ({anomaly['PixelAnomaly']['TotalPercentageArea']:.2%}) " f"is greater than limit ({coverage_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def analyze_image(lookoutvision_client, image, config): """ Analyzes an image with an Amazon Lookout for Vision model. Also runs a series of checks to determine if the contents of an image should be rejected. :param lookoutvision_client: A Lookout for Vision Boto3 client. param image: A local image that you want to analyze. param config: Configuration information for the model and reject limits. """ project = config["project"] model_version = config["model_version"] confidence_limit = config["confidence_limit"] coverage_limit = config["coverage_limit"] anomaly_types_limit = config["anomaly_types_limit"] anomaly_label = config["anomaly_label"] # Get analysis results. print(f"Analyzing {image}.") prediction = Inference.detect_anomalies( lookoutvision_client, project, model_version, image ) anomalies = [] reject = Inference.reject_on_classification(image, prediction, confidence_limit) if reject: anomalies.append("Classification: An anomaly was found.") reject = Inference.reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ) if reject: anomalies.append("Coverage: Anomaly coverage too high.") reject = Inference.reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ) if reject: anomalies.append("Anomaly type count: Too many anomaly types found.") print() if len(anomalies) > 0: print(f"Anomalies found in {image}") for anomaly in anomalies: print(f"{anomaly}") else: print(f"No anomalies found in {image}") def main(): """ Detects anomalies in an image file. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser( description="Find anomalies with Amazon Lookout for Vision." ) parser.add_argument( "image", help="The file that you want to analyze. Supply a local file path or a " "path to an S3 object.", ) parser.add_argument( "config", help=( "The configuration JSON file to use. " "See https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/" "python/example_code/lookoutvision/README.md" ), ) args = parser.parse_args() session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") # Get configuration information. with open(args.config, encoding="utf-8") as config_file: config = json.load(config_file) # Download image if located in S3 bucket. if args.image.startswith("s3://"): image = Inference.download_from_s3(s3_resource, args.image) else: image = args.image Inference.analyze_image(lookoutvision_client, image, config) # Delete image, if downloaded from S3 bucket. if args.image.startswith("s3://"): os.remove(image) except ClientError as err: print(f"Service error: {err.response['Error']['Message']}") except FileNotFoundError as err: print(f"The supplied file couldn't be found: {err.filename}.") except ValueError as err: print(f"A value error occurred: {err}.") else: print("\nSuccessfully completed analysis.") if __name__ == "__main__": main()
  • Para obter detalhes da API, consulte a DetectAnomaliesReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar DetectAnomalies.

Para obter mais informações, consulte Detectar anomalias em uma imagem.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Inference: """ Shows how to detect anomalies in an image using a trained Lookout for Vision model. """ @staticmethod def detect_anomalies(lookoutvision_client, project_name, model_version, photo): """ Calls DetectAnomalies using the supplied project, model version, and image. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The project that contains the model that you want to use. :param model_version: The version of the model that you want to use. :param photo: The photo that you want to analyze. :return: The DetectAnomalyResult object that contains the analysis results. """ image_type = imghdr.what(photo) if image_type == "jpeg": content_type = "image/jpeg" elif image_type == "png": content_type = "image/png" else: logger.info("Image type not valid for %s", photo) raise ValueError( f"File format not valid. Supply a jpeg or png format file: {photo}" ) # Get images bytes for call to detect_anomalies. with open(photo, "rb") as image: response = lookoutvision_client.detect_anomalies( ProjectName=project_name, ContentType=content_type, Body=image.read(), ModelVersion=model_version, ) return response["DetectAnomalyResult"] @staticmethod def download_from_s3(s3_resource, photo): """ Downloads an image from an S3 bucket. :param s3_resource: A Boto3 Amazon S3 resource. :param photo: The Amazon S3 path of a photo to download. return: The local path to the downloaded file. """ try: bucket, key = photo.replace("s3://", "").split("/", 1) local_file = os.path.basename(photo) except ValueError: logger.exception("Couldn't get S3 info for %s", photo) raise try: logger.info("Downloading %s", photo) s3_resource.Bucket(bucket).download_file(key, local_file) except ClientError: logger.exception("Couldn't download %s from S3.", photo) raise return local_file @staticmethod def reject_on_classification(image, prediction, confidence_limit): """ Returns True if the anomaly confidence is greater than or equal to the supplied confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0 - 1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking classification for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) is greater" f" than limit ({confidence_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ): """ Checks if the number of anomaly types is greater than the anomaly types limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence: The minimum acceptable confidence (float 0 - 1). :param anomaly_types_limit: The maximum number of allowable anomaly types (int). :return: True if the error condition indicates an anomaly, otherwise False. """ logger.info("Checking number of anomaly types for %s", image) reject = False if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: anomaly_types = { anomaly["Name"] for anomaly in prediction["Anomalies"] if anomaly["Name"] != "background" } if len(anomaly_types) > anomaly_types_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and " f"the number of anomaly types ({len(anomaly_types)-1}) is " f"greater than the limit ({anomaly_types_limit})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ): """ Checks if the coverage area of an anomaly is greater than the coverage limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0-1). :anomaly_label: The anomaly label for the type of anomaly that you want to check. :coverage_limit: The maximum acceptable percentage coverage of an anomaly (float 0-1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking coverage for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: for anomaly in prediction["Anomalies"]: if anomaly["Name"] == anomaly_label and anomaly["PixelAnomaly"][ "TotalPercentageArea" ] > (coverage_limit): reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and {anomaly['Name']} " f"coverage ({anomaly['PixelAnomaly']['TotalPercentageArea']:.2%}) " f"is greater than limit ({coverage_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def analyze_image(lookoutvision_client, image, config): """ Analyzes an image with an Amazon Lookout for Vision model. Also runs a series of checks to determine if the contents of an image should be rejected. :param lookoutvision_client: A Lookout for Vision Boto3 client. param image: A local image that you want to analyze. param config: Configuration information for the model and reject limits. """ project = config["project"] model_version = config["model_version"] confidence_limit = config["confidence_limit"] coverage_limit = config["coverage_limit"] anomaly_types_limit = config["anomaly_types_limit"] anomaly_label = config["anomaly_label"] # Get analysis results. print(f"Analyzing {image}.") prediction = Inference.detect_anomalies( lookoutvision_client, project, model_version, image ) anomalies = [] reject = Inference.reject_on_classification(image, prediction, confidence_limit) if reject: anomalies.append("Classification: An anomaly was found.") reject = Inference.reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ) if reject: anomalies.append("Coverage: Anomaly coverage too high.") reject = Inference.reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ) if reject: anomalies.append("Anomaly type count: Too many anomaly types found.") print() if len(anomalies) > 0: print(f"Anomalies found in {image}") for anomaly in anomalies: print(f"{anomaly}") else: print(f"No anomalies found in {image}") def main(): """ Detects anomalies in an image file. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser( description="Find anomalies with Amazon Lookout for Vision." ) parser.add_argument( "image", help="The file that you want to analyze. Supply a local file path or a " "path to an S3 object.", ) parser.add_argument( "config", help=( "The configuration JSON file to use. " "See https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/" "python/example_code/lookoutvision/README.md" ), ) args = parser.parse_args() session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") # Get configuration information. with open(args.config, encoding="utf-8") as config_file: config = json.load(config_file) # Download image if located in S3 bucket. if args.image.startswith("s3://"): image = Inference.download_from_s3(s3_resource, args.image) else: image = args.image Inference.analyze_image(lookoutvision_client, image, config) # Delete image, if downloaded from S3 bucket. if args.image.startswith("s3://"): os.remove(image) except ClientError as err: print(f"Service error: {err.response['Error']['Message']}") except FileNotFoundError as err: print(f"The supplied file couldn't be found: {err.filename}.") except ValueError as err: print(f"A value error occurred: {err}.") else: print("\nSuccessfully completed analysis.") if __name__ == "__main__": main()
  • Para obter detalhes da API, consulte a DetectAnomaliesReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar ListModels.

Para obter mais informações, consulte Visualizar modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def describe_models(lookoutvision_client, project_name): """ Gets information about all models in a Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to use. """ try: response = lookoutvision_client.list_models(ProjectName=project_name) print("Project: " + project_name) for model in response["Models"]: Models.describe_model( lookoutvision_client, project_name, model["ModelVersion"] ) print() print("Done...") except ClientError: logger.exception("Couldn't list models.") raise
  • Para obter detalhes da API, consulte a ListModelsReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar ListModels.

Para obter mais informações, consulte Visualizar modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Models: @staticmethod def describe_models(lookoutvision_client, project_name): """ Gets information about all models in a Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to use. """ try: response = lookoutvision_client.list_models(ProjectName=project_name) print("Project: " + project_name) for model in response["Models"]: Models.describe_model( lookoutvision_client, project_name, model["ModelVersion"] ) print() print("Done...") except ClientError: logger.exception("Couldn't list models.") raise
  • Para obter detalhes da API, consulte a ListModelsReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar ListProjects.

Para obter mais informações, consulte Visualizar projetos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("\tARN: " + project["ProjectArn"]) print("\tCreated: " + str(["CreationTimestamp"])) print("Datasets") project_description = lookoutvision_client.describe_project( ProjectName=project["ProjectName"] ) if not project_description["ProjectDescription"]["Datasets"]: print("\tNo datasets") else: for dataset in project_description["ProjectDescription"][ "Datasets" ]: print(f"\ttype: {dataset['DatasetType']}") print(f"\tStatus: {dataset['StatusMessage']}") print("Models") response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) if not response_models["Models"]: print("\tNo models") else: for model in response_models["Models"]: Models.describe_model( lookoutvision_client, project["ProjectName"], model["ModelVersion"], ) print("------------------------------------------------------------\n") print("Done!") except ClientError: logger.exception("Problem listing projects.") raise
  • Para obter detalhes da API, consulte a ListProjectsReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar ListProjects.

Para obter mais informações, consulte Visualizar projetos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Projects: @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("\tARN: " + project["ProjectArn"]) print("\tCreated: " + str(["CreationTimestamp"])) print("Datasets") project_description = lookoutvision_client.describe_project( ProjectName=project["ProjectName"] ) if not project_description["ProjectDescription"]["Datasets"]: print("\tNo datasets") else: for dataset in project_description["ProjectDescription"][ "Datasets" ]: print(f"\ttype: {dataset['DatasetType']}") print(f"\tStatus: {dataset['StatusMessage']}") print("Models") response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) if not response_models["Models"]: print("\tNo models") else: for model in response_models["Models"]: Models.describe_model( lookoutvision_client, project["ProjectName"], model["ModelVersion"], ) print("------------------------------------------------------------\n") print("Done!") except ClientError: logger.exception("Problem listing projects.") raise
  • Para obter detalhes da API, consulte a ListProjectsReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar StartModel.

Para obter mais informações, consulte Iniciar o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def start_model( lookoutvision_client, project_name, model_version, min_inference_units ): """ Starts the hosting of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to start hosting. :param model_version: The version of the model that you want to start hosting. :param min_inference_units: The number of inference units to use for hosting. """ try: logger.info( "Starting model version %s for project %s", model_version, project_name ) lookoutvision_client.start_model( ProjectName=project_name, ModelVersion=model_version, MinInferenceUnits=min_inference_units, ) print("Starting hosting...") status = "" finished = False # Wait until hosted or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STARTING_HOSTING": logger.info("Host starting in progress...") time.sleep(10) continue if status == "HOSTED": logger.info("Model is hosted and ready for use.") finished = True continue logger.info("Model hosting failed and the model can't be used.") finished = True if status != "HOSTED": logger.error("Error hosting model: %s", status) raise Exception(f"Error hosting model: {status}") except ClientError: logger.exception("Couldn't host model.") raise
  • Para obter detalhes da API, consulte a StartModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar StartModel.

Para obter mais informações, consulte Iniciar o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def start_model( lookoutvision_client, project_name, model_version, min_inference_units ): """ Starts the hosting of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to start hosting. :param model_version: The version of the model that you want to start hosting. :param min_inference_units: The number of inference units to use for hosting. """ try: logger.info( "Starting model version %s for project %s", model_version, project_name ) lookoutvision_client.start_model( ProjectName=project_name, ModelVersion=model_version, MinInferenceUnits=min_inference_units, ) print("Starting hosting...") status = "" finished = False # Wait until hosted or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STARTING_HOSTING": logger.info("Host starting in progress...") time.sleep(10) continue if status == "HOSTED": logger.info("Model is hosted and ready for use.") finished = True continue logger.info("Model hosting failed and the model can't be used.") finished = True if status != "HOSTED": logger.error("Error hosting model: %s", status) raise Exception(f"Error hosting model: {status}") except ClientError: logger.exception("Couldn't host model.") raise
  • Para obter detalhes da API, consulte a StartModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar StopModel.

Para obter mais informações, consulte Interromper o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def stop_model(lookoutvision_client, project_name, model_version): """ Stops a running Lookout for Vision Model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to stop hosting. :param model_version: The version of the model that you want to stop hosting. """ try: logger.info("Stopping model version %s for %s", model_version, project_name) response = lookoutvision_client.stop_model( ProjectName=project_name, ModelVersion=model_version ) logger.info("Stopping hosting...") status = response["Status"] finished = False # Wait until stopped or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STOPPING_HOSTING": logger.info("Host stopping in progress...") time.sleep(10) continue if status == "TRAINED": logger.info("Model is no longer hosted.") finished = True continue logger.info("Failed to stop model: %s ", status) finished = True if status != "TRAINED": logger.error("Error stopping model: %s", status) raise Exception(f"Error stopping model: {status}") except ClientError: logger.exception("Couldn't stop hosting model.") raise
  • Para obter detalhes da API, consulte a StopModelReferência da API AWS SDK for Python (Boto3).

O código de exemplo a seguir mostra como usar StopModel.

Para obter mais informações, consulte Interromper o modelo.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def stop_model(lookoutvision_client, project_name, model_version): """ Stops a running Lookout for Vision Model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to stop hosting. :param model_version: The version of the model that you want to stop hosting. """ try: logger.info("Stopping model version %s for %s", model_version, project_name) response = lookoutvision_client.stop_model( ProjectName=project_name, ModelVersion=model_version ) logger.info("Stopping hosting...") status = response["Status"] finished = False # Wait until stopped or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STOPPING_HOSTING": logger.info("Host stopping in progress...") time.sleep(10) continue if status == "TRAINED": logger.info("Model is no longer hosted.") finished = True continue logger.info("Failed to stop model: %s ", status) finished = True if status != "TRAINED": logger.error("Error stopping model: %s", status) raise Exception(f"Error stopping model: {status}") except ClientError: logger.exception("Couldn't stop hosting model.") raise
  • Para obter detalhes da API, consulte a StopModelReferência da API AWS SDK for Python (Boto3).

Cenários

O exemplo de código a seguir mostra como criar um arquivo de manifesto do Lookout for Vision e carregá-lo para o Amazon S3.

Para obter mais informações, consulte Criar um arquivo de manifesto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def create_manifest_file_s3(s3_resource, image_s3_path, manifest_s3_path): """ Creates a manifest file and uploads to Amazon S3. :param s3_resource: A Boto3 Amazon S3 resource. :param image_s3_path: The Amazon S3 path to the images referenced by the manifest file. The images must be in an Amazon S3 bucket with the following folder structure. s3://amzn-s3-demo-bucket/<train or test>/ normal/ anomaly/ Place normal images in the normal folder and anomalous images in the anomaly folder. :param manifest_s3_path: The Amazon S3 location in which to store the created manifest file. """ output_manifest_file = "temp.manifest" try: # Current date and time in manifest file format. dttm = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") # Get bucket and folder from image and manifest file paths. bucket, prefix = image_s3_path.replace("s3://", "").split("/", 1) if prefix[-1] != "/": prefix += "/" manifest_bucket, manifest_prefix = manifest_s3_path.replace( "s3://", "" ).split("/", 1) with open(output_manifest_file, "w") as mfile: logger.info("Creating manifest file") src_bucket = s3_resource.Bucket(bucket) # Create JSON lines for anomalous images. for obj in src_bucket.objects.filter( Prefix=prefix + "anomaly/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "anomaly", dttm) mfile.write(json.dumps(manifest) + "\n") # Create json lines for normal images. for obj in src_bucket.objects.filter( Prefix=prefix + "normal/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "normal", dttm) mfile.write(json.dumps(manifest) + "\n") logger.info("Uploading manifest file to %s", manifest_s3_path) s3_resource.Bucket(manifest_bucket).upload_file( output_manifest_file, manifest_prefix ) except ClientError: logger.exception("Error uploading manifest.") raise except Exception: logger.exception("Error uploading manifest.") raise else: logger.info("Completed manifest file creation and upload.") finally: try: os.remove(output_manifest_file) except FileNotFoundError: pass @staticmethod def create_json_line(image, class_name, dttm): """ Creates a single JSON line for an image. :param image: The S3 location for the image. :param class_name: The class of the image (normal or anomaly) :param dttm: The date and time that the JSON is created. """ label = 0 if class_name == "normal": label = 0 elif class_name == "anomaly": label = 1 else: logger.error("Unexpected label value: %s for %s", label, image) raise Exception(f"Unexpected label value: {label} for {image}") manifest = { "source-ref": image, "anomaly-label": label, "anomaly-label-metadata": { "confidence": 1, "job-name": "labeling-job/anomaly-label", "class-name": class_name, "human-annotated": "yes", "creation-date": dttm, "type": "groundtruth/image-classification", }, } return manifest

O exemplo de código a seguir mostra como criar um arquivo de manifesto do Lookout for Vision e carregá-lo para o Amazon S3.

Para obter mais informações, consulte Criar um arquivo de manifesto.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Datasets: @staticmethod def create_manifest_file_s3(s3_resource, image_s3_path, manifest_s3_path): """ Creates a manifest file and uploads to Amazon S3. :param s3_resource: A Boto3 Amazon S3 resource. :param image_s3_path: The Amazon S3 path to the images referenced by the manifest file. The images must be in an Amazon S3 bucket with the following folder structure. s3://amzn-s3-demo-bucket/<train or test>/ normal/ anomaly/ Place normal images in the normal folder and anomalous images in the anomaly folder. :param manifest_s3_path: The Amazon S3 location in which to store the created manifest file. """ output_manifest_file = "temp.manifest" try: # Current date and time in manifest file format. dttm = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") # Get bucket and folder from image and manifest file paths. bucket, prefix = image_s3_path.replace("s3://", "").split("/", 1) if prefix[-1] != "/": prefix += "/" manifest_bucket, manifest_prefix = manifest_s3_path.replace( "s3://", "" ).split("/", 1) with open(output_manifest_file, "w") as mfile: logger.info("Creating manifest file") src_bucket = s3_resource.Bucket(bucket) # Create JSON lines for anomalous images. for obj in src_bucket.objects.filter( Prefix=prefix + "anomaly/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "anomaly", dttm) mfile.write(json.dumps(manifest) + "\n") # Create json lines for normal images. for obj in src_bucket.objects.filter( Prefix=prefix + "normal/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "normal", dttm) mfile.write(json.dumps(manifest) + "\n") logger.info("Uploading manifest file to %s", manifest_s3_path) s3_resource.Bucket(manifest_bucket).upload_file( output_manifest_file, manifest_prefix ) except ClientError: logger.exception("Error uploading manifest.") raise except Exception: logger.exception("Error uploading manifest.") raise else: logger.info("Completed manifest file creation and upload.") finally: try: os.remove(output_manifest_file) except FileNotFoundError: pass @staticmethod def create_json_line(image, class_name, dttm): """ Creates a single JSON line for an image. :param image: The S3 location for the image. :param class_name: The class of the image (normal or anomaly) :param dttm: The date and time that the JSON is created. """ label = 0 if class_name == "normal": label = 0 elif class_name == "anomaly": label = 1 else: logger.error("Unexpected label value: %s for %s", label, image) raise Exception(f"Unexpected label value: {label} for {image}") manifest = { "source-ref": image, "anomaly-label": label, "anomaly-label-metadata": { "confidence": 1, "job-name": "labeling-job/anomaly-label", "class-name": class_name, "human-annotated": "yes", "creation-date": dttm, "type": "groundtruth/image-classification", }, } return manifest

O exemplo de código a seguir mostra como criar, treinar e iniciar um modelo do Lookout for Vision.

SDK para Python (Boto3).

Criar e, opcionalmente, iniciar um modelo do Amazon Lookout for Vision usando argumentos de linha de comando. O código de exemplo cria um novo projeto, um conjunto de dados de treinamento, um conjunto de dados de teste opcional e um modelo. Depois que o treinamento do modelo for concluído, um script fornecido poderá ser usado para testar o modelo com uma imagem.

Este exemplo exige um conjunto de imagens para treinar o modelo. Você pode encontrar exemplos de imagens de placas de circuito GitHub que você pode usar para treinamento e teste. Para obter detalhes sobre como copiar essas imagens em um bucket do Amazon Simple Storage Service (Amazon S3), consulte Preparar imagens de exemplo.

Para obter o código-fonte completo e instruções sobre como configurar e executar, veja o exemplo completo em GitHub.

Serviços utilizados neste exemplo
  • Lookout for Vision

O exemplo de código a seguir mostra como criar, treinar e iniciar um modelo do Lookout for Vision.

SDK para Python (Boto3).

Criar e, opcionalmente, iniciar um modelo do Amazon Lookout for Vision usando argumentos de linha de comando. O código de exemplo cria um novo projeto, um conjunto de dados de treinamento, um conjunto de dados de teste opcional e um modelo. Depois que o treinamento do modelo for concluído, um script fornecido poderá ser usado para testar o modelo com uma imagem.

Este exemplo exige um conjunto de imagens para treinar o modelo. Você pode encontrar exemplos de imagens de placas de circuito GitHub que você pode usar para treinamento e teste. Para obter detalhes sobre como copiar essas imagens em um bucket do Amazon Simple Storage Service (Amazon S3), consulte Preparar imagens de exemplo.

Para obter o código-fonte completo e instruções sobre como configurar e executar, veja o exemplo completo em GitHub.

Serviços utilizados neste exemplo
  • Lookout for Vision

O exemplo de código a seguir mostra como exportar os conjuntos de dados de um projeto do Lookout for Vision.

Para obter mais informações, consulte Exportar conjuntos de dados de um projeto (SDK).

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

""" Purpose Shows how to export the datasets (manifest files and images) from an Amazon Lookout for Vision project to a new Amazon S3 location. """ import argparse import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def copy_file(s3_resource, source_file, destination_file): """ Copies a file from a source Amazon S3 folder to a destination Amazon S3 folder. The destination can be in a different S3 bucket. :param s3: An Amazon S3 Boto3 resource. :param source_file: The Amazon S3 path to the source file. :param destination_file: The destination Amazon S3 path for the copy operation. """ source_bucket, source_key = source_file.replace("s3://", "").split("/", 1) destination_bucket, destination_key = destination_file.replace("s3://", "").split( "/", 1 ) try: bucket = s3_resource.Bucket(destination_bucket) dest_object = bucket.Object(destination_key) dest_object.copy_from(CopySource={"Bucket": source_bucket, "Key": source_key}) dest_object.wait_until_exists() logger.info("Copied %s to %s", source_file, destination_file) except ClientError as error: if error.response["Error"]["Code"] == "404": error_message = ( f"Failed to copy {source_file} to " f"{destination_file}. : {error.response['Error']['Message']}" ) logger.warning(error_message) error.response["Error"]["Message"] = error_message raise def upload_manifest_file(s3_resource, manifest_file, destination): """ Uploads a manifest file to a destination Amazon S3 folder. :param s3: An Amazon S3 Boto3 resource. :param manifest_file: The manifest file that you want to upload. :destination: The Amazon S3 folder location to upload the manifest file to. """ destination_bucket, destination_key = destination.replace("s3://", "").split("/", 1) bucket = s3_resource.Bucket(destination_bucket) put_data = open(manifest_file, "rb") obj = bucket.Object(destination_key + manifest_file) try: obj.put(Body=put_data) obj.wait_until_exists() logger.info("Put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name) except ClientError: logger.exception( "Couldn't put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name ) raise finally: if getattr(put_data, "close", None): put_data.close() def get_dataset_types(lookoutvision_client, project): """ Determines the types of the datasets (train or test) in an Amazon Lookout for Vision project. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to check. :return: The dataset types in the project. """ try: response = lookoutvision_client.describe_project(ProjectName=project) datasets = [] for dataset in response["ProjectDescription"]["Datasets"]: if dataset["Status"] in ("CREATE_COMPLETE", "UPDATE_COMPLETE"): datasets.append(dataset["DatasetType"]) return datasets except lookoutvision_client.exceptions.ResourceNotFoundException: logger.exception("Project %s not found.", project) raise def process_json_line(s3_resource, entry, dataset_type, destination): """ Creates a JSON line for a new manifest file, copies image and mask to destination. :param s3_resource: An Amazon S3 Boto3 resource. :param entry: A JSON line from the manifest file. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. :return: A JSON line with details for the destination location. """ entry_json = json.loads(entry) print(f"source: {entry_json['source-ref']}") # Use existing folder paths to ensure console added image names don't clash. bucket, key = entry_json["source-ref"].replace("s3://", "").split("/", 1) logger.info("Source location: %s/%s", bucket, key) destination_image_location = destination + dataset_type + "/images/" + key copy_file(s3_resource, entry_json["source-ref"], destination_image_location) # Update JSON for writing. entry_json["source-ref"] = destination_image_location if "anomaly-mask-ref" in entry_json: source_anomaly_ref = entry_json["anomaly-mask-ref"] mask_bucket, mask_key = source_anomaly_ref.replace("s3://", "").split("/", 1) destination_mask_location = destination + dataset_type + "/masks/" + mask_key entry_json["anomaly-mask-ref"] = destination_mask_location copy_file(s3_resource, source_anomaly_ref, entry_json["anomaly-mask-ref"]) return entry_json def write_manifest_file( lookoutvision_client, s3_resource, project, dataset_type, destination ): """ Creates a manifest file for a dataset. Copies the manifest file and dataset images (and masks, if present) to the specified Amazon S3 destination. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to use. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. """ try: # Create a reusable Paginator paginator = lookoutvision_client.get_paginator("list_dataset_entries") # Create a PageIterator from the Paginator page_iterator = paginator.paginate( ProjectName=project, DatasetType=dataset_type, PaginationConfig={"PageSize": 100}, ) output_manifest_file = dataset_type + ".manifest" # Create manifest file then upload to Amazon S3 with images. with open(output_manifest_file, "w", encoding="utf-8") as manifest_file: for page in page_iterator: for entry in page["DatasetEntries"]: try: entry_json = process_json_line( s3_resource, entry, dataset_type, destination ) manifest_file.write(json.dumps(entry_json) + "\n") except ClientError as error: if error.response["Error"]["Code"] == "404": print(error.response["Error"]["Message"]) print(f"Excluded JSON line: {entry}") else: raise upload_manifest_file( s3_resource, output_manifest_file, destination + "datasets/" ) except ClientError: logger.exception("Problem getting dataset_entries") raise def export_datasets(lookoutvision_client, s3_resource, project, destination): """ Exports the datasets from an Amazon Lookout for Vision project to a specified Amazon S3 destination. :param project: The Lookout for Vision project that you want to use. :param destination: The destination Amazon S3 folder for the exported datasets. """ # Add trailing backslash, if missing. destination = destination if destination[-1] == "/" else destination + "/" print(f"Exporting project {project} datasets to {destination}.") # Get each dataset and export to destination. dataset_types = get_dataset_types(lookoutvision_client, project) for dataset in dataset_types: logger.info("Copying %s dataset to %s.", dataset, destination) write_manifest_file( lookoutvision_client, s3_resource, project, dataset, destination ) print("Exported dataset locations") for dataset in dataset_types: print(f" {dataset}: {destination}datasets/{dataset}.manifest") print("Done.") def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument("project", help="The project that contains the dataset.") parser.add_argument("destination", help="The destination Amazon S3 folder.") def main(): """ Exports the datasets from an Amazon Lookout for Vision project to a destination Amazon S3 location. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() try: session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") export_datasets( lookoutvision_client, s3_resource, args.project, args.destination ) except ClientError as err: logger.exception(err) print(f"Failed: {format(err)}") if __name__ == "__main__": main()

O exemplo de código a seguir mostra como exportar os conjuntos de dados de um projeto do Lookout for Vision.

Para obter mais informações, consulte Exportar conjuntos de dados de um projeto (SDK).

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

""" Purpose Shows how to export the datasets (manifest files and images) from an Amazon Lookout for Vision project to a new Amazon S3 location. """ import argparse import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def copy_file(s3_resource, source_file, destination_file): """ Copies a file from a source Amazon S3 folder to a destination Amazon S3 folder. The destination can be in a different S3 bucket. :param s3: An Amazon S3 Boto3 resource. :param source_file: The Amazon S3 path to the source file. :param destination_file: The destination Amazon S3 path for the copy operation. """ source_bucket, source_key = source_file.replace("s3://", "").split("/", 1) destination_bucket, destination_key = destination_file.replace("s3://", "").split( "/", 1 ) try: bucket = s3_resource.Bucket(destination_bucket) dest_object = bucket.Object(destination_key) dest_object.copy_from(CopySource={"Bucket": source_bucket, "Key": source_key}) dest_object.wait_until_exists() logger.info("Copied %s to %s", source_file, destination_file) except ClientError as error: if error.response["Error"]["Code"] == "404": error_message = ( f"Failed to copy {source_file} to " f"{destination_file}. : {error.response['Error']['Message']}" ) logger.warning(error_message) error.response["Error"]["Message"] = error_message raise def upload_manifest_file(s3_resource, manifest_file, destination): """ Uploads a manifest file to a destination Amazon S3 folder. :param s3: An Amazon S3 Boto3 resource. :param manifest_file: The manifest file that you want to upload. :destination: The Amazon S3 folder location to upload the manifest file to. """ destination_bucket, destination_key = destination.replace("s3://", "").split("/", 1) bucket = s3_resource.Bucket(destination_bucket) put_data = open(manifest_file, "rb") obj = bucket.Object(destination_key + manifest_file) try: obj.put(Body=put_data) obj.wait_until_exists() logger.info("Put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name) except ClientError: logger.exception( "Couldn't put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name ) raise finally: if getattr(put_data, "close", None): put_data.close() def get_dataset_types(lookoutvision_client, project): """ Determines the types of the datasets (train or test) in an Amazon Lookout for Vision project. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to check. :return: The dataset types in the project. """ try: response = lookoutvision_client.describe_project(ProjectName=project) datasets = [] for dataset in response["ProjectDescription"]["Datasets"]: if dataset["Status"] in ("CREATE_COMPLETE", "UPDATE_COMPLETE"): datasets.append(dataset["DatasetType"]) return datasets except lookoutvision_client.exceptions.ResourceNotFoundException: logger.exception("Project %s not found.", project) raise def process_json_line(s3_resource, entry, dataset_type, destination): """ Creates a JSON line for a new manifest file, copies image and mask to destination. :param s3_resource: An Amazon S3 Boto3 resource. :param entry: A JSON line from the manifest file. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. :return: A JSON line with details for the destination location. """ entry_json = json.loads(entry) print(f"source: {entry_json['source-ref']}") # Use existing folder paths to ensure console added image names don't clash. bucket, key = entry_json["source-ref"].replace("s3://", "").split("/", 1) logger.info("Source location: %s/%s", bucket, key) destination_image_location = destination + dataset_type + "/images/" + key copy_file(s3_resource, entry_json["source-ref"], destination_image_location) # Update JSON for writing. entry_json["source-ref"] = destination_image_location if "anomaly-mask-ref" in entry_json: source_anomaly_ref = entry_json["anomaly-mask-ref"] mask_bucket, mask_key = source_anomaly_ref.replace("s3://", "").split("/", 1) destination_mask_location = destination + dataset_type + "/masks/" + mask_key entry_json["anomaly-mask-ref"] = destination_mask_location copy_file(s3_resource, source_anomaly_ref, entry_json["anomaly-mask-ref"]) return entry_json def write_manifest_file( lookoutvision_client, s3_resource, project, dataset_type, destination ): """ Creates a manifest file for a dataset. Copies the manifest file and dataset images (and masks, if present) to the specified Amazon S3 destination. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to use. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. """ try: # Create a reusable Paginator paginator = lookoutvision_client.get_paginator("list_dataset_entries") # Create a PageIterator from the Paginator page_iterator = paginator.paginate( ProjectName=project, DatasetType=dataset_type, PaginationConfig={"PageSize": 100}, ) output_manifest_file = dataset_type + ".manifest" # Create manifest file then upload to Amazon S3 with images. with open(output_manifest_file, "w", encoding="utf-8") as manifest_file: for page in page_iterator: for entry in page["DatasetEntries"]: try: entry_json = process_json_line( s3_resource, entry, dataset_type, destination ) manifest_file.write(json.dumps(entry_json) + "\n") except ClientError as error: if error.response["Error"]["Code"] == "404": print(error.response["Error"]["Message"]) print(f"Excluded JSON line: {entry}") else: raise upload_manifest_file( s3_resource, output_manifest_file, destination + "datasets/" ) except ClientError: logger.exception("Problem getting dataset_entries") raise def export_datasets(lookoutvision_client, s3_resource, project, destination): """ Exports the datasets from an Amazon Lookout for Vision project to a specified Amazon S3 destination. :param project: The Lookout for Vision project that you want to use. :param destination: The destination Amazon S3 folder for the exported datasets. """ # Add trailing backslash, if missing. destination = destination if destination[-1] == "/" else destination + "/" print(f"Exporting project {project} datasets to {destination}.") # Get each dataset and export to destination. dataset_types = get_dataset_types(lookoutvision_client, project) for dataset in dataset_types: logger.info("Copying %s dataset to %s.", dataset, destination) write_manifest_file( lookoutvision_client, s3_resource, project, dataset, destination ) print("Exported dataset locations") for dataset in dataset_types: print(f" {dataset}: {destination}datasets/{dataset}.manifest") print("Done.") def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument("project", help="The project that contains the dataset.") parser.add_argument("destination", help="The destination Amazon S3 folder.") def main(): """ Exports the datasets from an Amazon Lookout for Vision project to a destination Amazon S3 location. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() try: session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") export_datasets( lookoutvision_client, s3_resource, args.project, args.destination ) except ClientError as err: logger.exception(err) print(f"Failed: {format(err)}") if __name__ == "__main__": main()

O exemplo de código a seguir mostra como encontrar um projeto do Lookout for Vision com uma tag específica.

Para obter mais informações, consulte Marcando modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import logging import argparse import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def find_tag(tags, key, value): """ Finds a tag in the supplied list of tags. :param tags: A list of tags associated with a Lookout for Vision model. :param key: The tag to search for. :param value: The tag key value to search for. :return: True if the tag value exists, otherwise False. """ found = False for tag in tags: if key == tag["Key"]: logger.info("\t\tMatch found for tag: %s value: %s.", key, value) found = True break return found def find_tag_in_projects(lookoutvision_client, key, value): """ Finds Lookout for Vision models tagged with the supplied key and value. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param key: The tag key to find. :param value: The value of the tag that you want to find. return: A list of matching model versions (and model projects) that were found. """ try: found_tags = [] found = False projects = lookoutvision_client.list_projects() # Iterate through each project and models within a project. for project in projects["Projects"]: logger.info("Searching project: %s ...", project["ProjectName"]) response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) tags = lookoutvision_client.list_tags_for_resource( ResourceArn=model_description["ModelDescription"]["ModelArn"] ) logger.info( "\tSearching model: %s for tag: %s value: %s.", model_description["ModelDescription"]["ModelArn"], key, value, ) if find_tag(tags["Tags"], key, value) is True: found = True logger.info( "\t\tMATCH: Project: %s: model version %s", project["ProjectName"], model_description["ModelDescription"]["ModelVersion"], ) found_tags.append( { "Project": project["ProjectName"], "ModelVersion": model_description["ModelDescription"][ "ModelVersion" ], } ) if found is False: logger.info("No match for tag %s with value %s.", key, value) except ClientError: logger.exception("Problem finding tags.") raise else: return found_tags def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) parser.add_argument("tag", help="The tag that you want to find.") parser.add_argument("value", help="The tag value that you want to find.") args = parser.parse_args() key = args.tag value = args.value session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") print(f"Searching your models for tag: {key} with value: {value}.") tagged_models = find_tag_in_projects(lookoutvision_client, key, value) print("Matched models\n--------------") if len(tagged_models) > 0: for model in tagged_models: print(f"Project: {model['Project']}. model version:{model['ModelVersion']}") else: print("No matches found.") if __name__ == "__main__": main()

O exemplo de código a seguir mostra como encontrar um projeto do Lookout for Vision com uma tag específica.

Para obter mais informações, consulte Marcando modelos.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import logging import argparse import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def find_tag(tags, key, value): """ Finds a tag in the supplied list of tags. :param tags: A list of tags associated with a Lookout for Vision model. :param key: The tag to search for. :param value: The tag key value to search for. :return: True if the tag value exists, otherwise False. """ found = False for tag in tags: if key == tag["Key"]: logger.info("\t\tMatch found for tag: %s value: %s.", key, value) found = True break return found def find_tag_in_projects(lookoutvision_client, key, value): """ Finds Lookout for Vision models tagged with the supplied key and value. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param key: The tag key to find. :param value: The value of the tag that you want to find. return: A list of matching model versions (and model projects) that were found. """ try: found_tags = [] found = False projects = lookoutvision_client.list_projects() # Iterate through each project and models within a project. for project in projects["Projects"]: logger.info("Searching project: %s ...", project["ProjectName"]) response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) tags = lookoutvision_client.list_tags_for_resource( ResourceArn=model_description["ModelDescription"]["ModelArn"] ) logger.info( "\tSearching model: %s for tag: %s value: %s.", model_description["ModelDescription"]["ModelArn"], key, value, ) if find_tag(tags["Tags"], key, value) is True: found = True logger.info( "\t\tMATCH: Project: %s: model version %s", project["ProjectName"], model_description["ModelDescription"]["ModelVersion"], ) found_tags.append( { "Project": project["ProjectName"], "ModelVersion": model_description["ModelDescription"][ "ModelVersion" ], } ) if found is False: logger.info("No match for tag %s with value %s.", key, value) except ClientError: logger.exception("Problem finding tags.") raise else: return found_tags def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) parser.add_argument("tag", help="The tag that you want to find.") parser.add_argument("value", help="The tag value that you want to find.") args = parser.parse_args() key = args.tag value = args.value session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") print(f"Searching your models for tag: {key} with value: {value}.") tagged_models = find_tag_in_projects(lookoutvision_client, key, value) print("Matched models\n--------------") if len(tagged_models) > 0: for model in tagged_models: print(f"Project: {model['Project']}. model version:{model['ModelVersion']}") else: print("No matches found.") if __name__ == "__main__": main()

O exemplo de código a seguir mostra como listar modelos do Lookout for Vision que estão hospedados atualmente.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def list_hosted(lookoutvision_client): """ Displays a list of models in your account that are currently hosted. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() hosted = 0 print("Hosted models\n-------------") for project in response["Projects"]: response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) if model_description["ModelDescription"]["Status"] == "HOSTED": print( f"Project: {project['ProjectName']} Model version: " f"{model['ModelVersion']}" ) hosted += 1 print(f"{hosted} model(s) hosted") except ClientError: logger.exception("Problem listing hosted models.") raise

O exemplo de código a seguir mostra como listar modelos do Lookout for Vision que estão hospedados atualmente.

SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class Hosting: @staticmethod def list_hosted(lookoutvision_client): """ Displays a list of models in your account that are currently hosted. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() hosted = 0 print("Hosted models\n-------------") for project in response["Projects"]: response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) if model_description["ModelDescription"]["Status"] == "HOSTED": print( f"Project: {project['ProjectName']} Model version: " f"{model['ModelVersion']}" ) hosted += 1 print(f"{hosted} model(s) hosted") except ClientError: logger.exception("Problem listing hosted models.") raise
PrivacidadeTermos do sitePreferências de cookies
© 2025, Amazon Web Services, Inc. ou suas afiliadas. Todos os direitos reservados.