쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

AWS SDK와 DetectAnomalies 함께 사용 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK와 DetectAnomalies 함께 사용

다음 코드 예시에서는 DetectAnomalies을 사용하는 방법을 보여 줍니다.

자세한 내용은 이미지에서 이상 감지를 참조하십시오.

Python
SDK for Python (Boto3)
참고

GitHub에 더 많은 내용이 있습니다. 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()
  • API 세부 정보는 AWS SDK for Python (Boto3) API 참조DetectAnomalies를 참조하십시오.

SDK for Python (Boto3)
참고

GitHub에 더 많은 내용이 있습니다. 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()
  • API 세부 정보는 AWS SDK for Python (Boto3) API 참조DetectAnomalies를 참조하십시오.

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.