2021-04-15 11:45:07 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
2021-10-28 15:43:09 -07:00
Purpose
2021-04-15 11:45:07 -07:00
Amazon Lookout for Vision project code examples for using projects.
Examples are used in the service documentation:
https://docs.aws.amazon.com/lookout-for-vision/latest/developer-guide/.
2021-10-28 15:43:09 -07:00
2021-05-04 13:49:38 -07:00
Shows how to create and delete a project. Also, how to get information
about your projects.
2021-04-15 11:45:07 -07:00
"""
2021-10-28 15:43:09 -07:00
2021-04-15 11:45:07 -07:00
import logging
from botocore . exceptions import ClientError
2021-10-28 15:43:09 -07:00
from models import Models
2021-04-15 11:45:07 -07:00
logger = logging . getLogger ( __name__ )
2021-10-28 15:43:09 -07:00
# snippet-start:[python.example_code.lookoutvision.Projects]
2021-04-15 11:45:07 -07:00
class Projects :
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.lookoutvision.Projects]
2021-04-15 11:45:07 -07:00
"""
2021-10-28 15:43:09 -07:00
Provides example functions for creating, listing, and deleting Lookout for Vision
projects
2021-04-15 11:45:07 -07:00
"""
2023-10-18 10:35:05 -07:00
# snippet-start:[python.example_code.lookoutvision.CreateProject]
2021-04-15 11:45:07 -07:00
@staticmethod
def create_project ( lookoutvision_client , project_name ) :
"""
2021-10-28 15:43:09 -07:00
Creates a new Lookout for Vision project.
:param lookoutvision_client: A Boto3 Lookout for Vision client.
2021-04-15 11:45:07 -07:00
: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 )
2021-10-28 15:43:09 -07:00
project_arn = response [ " ProjectMetadata " ] [ " ProjectArn " ]
logger . info ( " project ARN: %s " , project_arn )
except ClientError :
logger . exception ( " Couldn ' t create project %s . " , project_name )
2021-04-15 11:45:07 -07:00
raise
2021-10-28 15:43:09 -07:00
else :
return project_arn
2021-04-15 11:45:07 -07:00
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.lookoutvision.CreateProject]
# snippet-start:[python.example_code.lookoutvision.DeleteProject]
2021-04-15 11:45:07 -07:00
@staticmethod
def delete_project ( lookoutvision_client , project_name ) :
"""
2021-10-28 15:43:09 -07:00
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.
2021-04-15 11:45:07 -07:00
"""
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 :
2021-10-28 15:43:09 -07:00
logger . exception ( " Couldn ' t delete project %s . " , project_name )
2021-04-15 11:45:07 -07:00
raise
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.lookoutvision.DeleteProject]
# snippet-start:[python.example_code.lookoutvision.ListProjects]
2021-04-15 11:45:07 -07:00
@staticmethod
def list_projects ( lookoutvision_client ) :
"""
2022-04-05 17:25:32 -07:00
Lists information about the projects that are in in your AWS account
and in the current AWS Region.
2021-04-15 11:45:07 -07:00
2021-10-28 15:43:09 -07:00
:param lookoutvision_client: A Boto3 Lookout for Vision client.
"""
2021-04-15 11:45:07 -07:00
try :
response = lookoutvision_client . list_projects ( )
for project in response [ " Projects " ] :
print ( " Project: " + project [ " ProjectName " ] )
print ( " \t ARN: " + project [ " ProjectArn " ] )
print ( " \t Created: " + str ( [ " CreationTimestamp " ] ) )
print ( " Datasets " )
project_description = lookoutvision_client . describe_project (
2023-10-18 10:35:05 -07:00
ProjectName = project [ " ProjectName " ]
)
2021-05-04 13:49:38 -07:00
if not project_description [ " ProjectDescription " ] [ " Datasets " ] :
2021-04-15 11:45:07 -07:00
print ( " \t No datasets " )
else :
2023-10-18 10:35:05 -07:00
for dataset in project_description [ " ProjectDescription " ] [
" Datasets "
] :
2021-05-04 13:49:38 -07:00
print ( f " \t type: { dataset [ ' DatasetType ' ] } " )
print ( f " \t Status: { dataset [ ' StatusMessage ' ] } " )
2021-04-15 11:45:07 -07:00
print ( " Models " )
response_models = lookoutvision_client . list_models (
2023-10-18 10:35:05 -07:00
ProjectName = project [ " ProjectName " ]
)
2021-05-04 13:49:38 -07:00
if not response_models [ " Models " ] :
2021-04-15 11:45:07 -07:00
print ( " \t No models " )
else :
for model in response_models [ " Models " ] :
2021-10-28 15:43:09 -07:00
Models . describe_model (
2023-10-18 10:35:05 -07:00
lookoutvision_client ,
project [ " ProjectName " ] ,
model [ " ModelVersion " ] ,
)
2021-10-28 15:43:09 -07:00
print ( " ------------------------------------------------------------ \n " )
print ( " Done! " )
except ClientError :
logger . exception ( " Problem listing projects. " )
2021-04-15 11:45:07 -07:00
raise
2023-10-18 10:35:05 -07:00
2021-10-28 15:43:09 -07:00
# snippet-end:[python.example_code.lookoutvision.ListProjects]