경계 상자 표시 - Amazon Rekognition

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

경계 상자 표시

Amazon Rekognition Image 작업은 이미지에서 감지된 항목에 대해 경계 상자 좌표를 반환할 수 있습니다. 예를 들어, 이 DetectFaces작업을 수행하면 이미지에서 감지된 각 얼굴에 대해 경계 상자 (BoundingBox) 가 반환됩니다. 경계 상자 좌표를 사용하여 감지된 항목 주위에 상자를 표시할 수 있습니다. 예를 들어 다음 이미지에는 얼굴을 둘러싼 경계 상자가 표시되어 있습니다.

안경을 쓰고 주위에 거품이 묻어 웃고 있는 젊은 여성.

BoundingBox에는 다음과 같은 속성이 있습니다.

  • 높이 - 전체 이미지 높이에 대한 비율로서 경계 상자의 높이입니다.

  • 좌측 - 전체 이미지 너비에 대한 비율로서 경계 상자의 좌측 좌표입니다.

  • 상단 - 전체 이미지 높이에 대한 비율로서 경계 상자의 상단 좌표입니다.

  • 너비 - 전체 이미지 너비에 대한 비율로서 경계 상자의 너비입니다.

각 BoundingBox 속성의 값은 0에서 1 사이입니다. 각 속성 값은 전체 이미지 너비(LeftWidth) 또는 높이(HeightTop)에 대한 비율입니다. 예를 들어 입력 이미지가 700x200픽셀이고, 경계 상자의 상단-좌측 좌표가 350x50픽셀일 경우 API는 Left 값 0.5(350/700)와 Top 값 0.25(50/200)를 반환합니다.

다음 다이어그램에는 각 경계 상자가 커버하는 이미지 범위가 나와 있습니다.

테두리 상자가 이미지 크기와 어떻게 관련되는지를 보여주는 그래픽입니다.

테두리 상자를 올바른 위치와 크기로 표시하려면 값에 이미지 너비 또는 높이 (원하는 BoundingBox 값에 따라 다름) 를 곱하여 픽셀 값을 구해야 합니다. 이 픽셀 값을 사용하여 경계 상자를 표시합니다. 예를 들어 이전 이미지의 픽셀 치수는 608(너비) x 588(높이)입니다. 얼굴의 경계 상자 값은 다음과 같습니다.

BoundingBox.Left: 0.3922065 BoundingBox.Top: 0.15567766 BoundingBox.Width: 0.284666 BoundingBox.Height: 0.2930403

얼굴 경계 상자의 위치는 다음과 같이 계산됩니다.

Left coordinate = BoundingBox.Left (0.3922065) * image width (608) = 238

Top coordinate = BoundingBox.Top (0.15567766) * image height (588) = 91

Face width = BoundingBox.Width (0.284666) * image width (608) = 173

Face height = BoundingBox.Height (0.2930403) * image height (588) = 172

이들 값을 사용하여 얼굴 주위에 경계 상자를 표시합니다.

참고

이미지 방향을 여러 방법으로 설정할 수 있습니다. 애플리케이션이 이미지를 올바르게 표시하기 위해 이미지를 회전해야 할 수 있습니다. 경계 상자 좌표는 이미지의 방향에 영향을 받습니다. 경계 상자를 올바른 위치에 표시하기 위해 좌표를 변환해야 할 수 있습니다. 자세한 정보는 이미지 방향 및 경계 상자 좌표 가져오기을 참조하세요.

다음 예제는 호출로 감지된 얼굴 주위에 경계 상자를 표시하는 방법을 보여줍니다. DetectFaces 예제에서는 이미지 방향이 0도로 설정된 것으로 가정합니다. 또한 Amazon S3 버킷에서 이미지를 다운로드하는 방법도 보여줍니다.

경계 상자를 표시하는 방법
  1. 아직 설정하지 않았다면 다음과 같이 하세요.

    1. AmazonRekognitionFullAccess 권한과 AmazonS3ReadOnlyAccess 권한을 가진 사용자를 생성하거나 업데이트합니다. 자세한 정보는 1단계: AWS 계정 설정 및 사용자 생성을 참조하세요.

    2. 및 AWS SDK를 설치 AWS CLI 및 구성합니다. 자세한 정보는 2단계: 설정 AWS CLI 그리고 AWS SDKs을 참조하세요.

  2. 다음 예제를 사용하여 DetectFaces 작업을 호출합니다.

    Java

    bucket의 값을 이미지 파일이 저장된 Amazon S3 버킷으로 바꿉니다. photo의 값을 이미지 파일(.jpg 또는 .png 형식)의 파일 이름으로 바꿉니다.

    //Loads images, detects faces and draws bounding boxes.Determines exif orientation, if necessary. package com.amazonaws.samples; //Import the basic graphics classes. import java.awt.*; import java.awt.image.BufferedImage; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.BoundingBox; import com.amazonaws.services.rekognition.model.DetectFacesRequest; import com.amazonaws.services.rekognition.model.DetectFacesResult; import com.amazonaws.services.rekognition.model.FaceDetail; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.S3Object; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.S3ObjectInputStream; // Calls DetectFaces and displays a bounding box around each detected image. public class DisplayFaces extends JPanel { private static final long serialVersionUID = 1L; BufferedImage image; static int scale; DetectFacesResult result; public DisplayFaces(DetectFacesResult facesResult, BufferedImage bufImage) throws Exception { super(); scale = 1; // increase to shrink image size. result = facesResult; image = bufImage; } // Draws the bounding box around the detected faces. public void paintComponent(Graphics g) { float left = 0; float top = 0; int height = image.getHeight(this); int width = image.getWidth(this); Graphics2D g2d = (Graphics2D) g; // Create a Java2D version of g. // Draw the image. g2d.drawImage(image, 0, 0, width / scale, height / scale, this); g2d.setColor(new Color(0, 212, 0)); // Iterate through faces and display bounding boxes. List<FaceDetail> faceDetails = result.getFaceDetails(); for (FaceDetail face : faceDetails) { BoundingBox box = face.getBoundingBox(); left = width * box.getLeft(); top = height * box.getTop(); g2d.drawRect(Math.round(left / scale), Math.round(top / scale), Math.round((width * box.getWidth()) / scale), Math.round((height * box.getHeight())) / scale); } } public static void main(String arg[]) throws Exception { String photo = "photo.png"; String bucket = "bucket"; int height = 0; int width = 0; // Get the image from an S3 Bucket AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient(); com.amazonaws.services.s3.model.S3Object s3object = s3client.getObject(bucket, photo); S3ObjectInputStream inputStream = s3object.getObjectContent(); BufferedImage image = ImageIO.read(inputStream); DetectFacesRequest request = new DetectFacesRequest() .withImage(new Image().withS3Object(new S3Object().withName(photo).withBucket(bucket))); width = image.getWidth(); height = image.getHeight(); // Call DetectFaces AmazonRekognition amazonRekognition = AmazonRekognitionClientBuilder.defaultClient(); DetectFacesResult result = amazonRekognition.detectFaces(request); //Show the bounding box info for each face. List<FaceDetail> faceDetails = result.getFaceDetails(); for (FaceDetail face : faceDetails) { BoundingBox box = face.getBoundingBox(); float left = width * box.getLeft(); float top = height * box.getTop(); System.out.println("Face:"); System.out.println("Left: " + String.valueOf((int) left)); System.out.println("Top: " + String.valueOf((int) top)); System.out.println("Face Width: " + String.valueOf((int) (width * box.getWidth()))); System.out.println("Face Height: " + String.valueOf((int) (height * box.getHeight()))); System.out.println(); } // Create frame and panel. JFrame frame = new JFrame("RotateImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DisplayFaces panel = new DisplayFaces(result, image); panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale)); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } }
    Python

    bucket의 값을 이미지 파일이 저장된 Amazon S3 버킷으로 바꿉니다. photo의 값을 이미지 파일(.jpg 또는 .png 형식)의 파일 이름으로 바꿉니다. Rekognition 세션을 생성하는 라인에서 profile_name의 값을 개발자 프로필의 이름으로 대체합니다.

    import boto3 import io from PIL import Image, ImageDraw def show_faces(photo, bucket): session = boto3.Session(profile_name='profile-name') client = session.client('rekognition') # Load image from S3 bucket s3_connection = boto3.resource('s3') s3_object = s3_connection.Object(bucket, photo) s3_response = s3_object.get() stream = io.BytesIO(s3_response['Body'].read()) image = Image.open(stream) # Call DetectFaces response = client.detect_faces(Image={'S3Object': {'Bucket': bucket, 'Name': photo}}, Attributes=['ALL']) imgWidth, imgHeight = image.size draw = ImageDraw.Draw(image) # calculate and display bounding boxes for each detected face print('Detected faces for ' + photo) for faceDetail in response['FaceDetails']: print('The detected face is between ' + str(faceDetail['AgeRange']['Low']) + ' and ' + str(faceDetail['AgeRange']['High']) + ' years old') box = faceDetail['BoundingBox'] left = imgWidth * box['Left'] top = imgHeight * box['Top'] width = imgWidth * box['Width'] height = imgHeight * box['Height'] print('Left: ' + '{0:.0f}'.format(left)) print('Top: ' + '{0:.0f}'.format(top)) print('Face Width: ' + "{0:.0f}".format(width)) print('Face Height: ' + "{0:.0f}".format(height)) points = ( (left, top), (left + width, top), (left + width, top + height), (left, top + height), (left, top) ) draw.line(points, fill='#00d400', width=2) # Alternatively can draw rectangle. However you can't set line width. # draw.rectangle([left,top, left + width, top + height], outline='#00d400') image.show() return len(response['FaceDetails']) def main(): bucket = "bucket-name" photo = "photo-name" faces_count = show_faces(photo, bucket) print("faces detected: " + str(faces_count)) if __name__ == "__main__": main()
    Java V2

    이 코드는 AWS 문서 SDK 예제 GitHub 저장소에서 가져왔습니다. 전체 예제는 여기에서 확인하세요.

    참고로, s3은 AWS SDK Amazon S3 클라이언트를 가리키며 rekClient는 AWS SDK Amazon Rekognition 클라이언트를 가리킵니다.

    //snippet-start:[rekognition.java2.detect_labels.import] import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.model.Attribute; import software.amazon.awssdk.services.rekognition.model.BoundingBox; import software.amazon.awssdk.services.rekognition.model.DetectFacesRequest; import software.amazon.awssdk.services.rekognition.model.DetectFacesResponse; import software.amazon.awssdk.services.rekognition.model.FaceDetail; import software.amazon.awssdk.services.rekognition.model.Image; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; //snippet-end:[rekognition.java2.detect_labels.import] /** * Before running this Java V2 code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DisplayFaces extends JPanel { static DetectFacesResponse result; static BufferedImage image; static int scale; public static void main(String[] args) throws Exception { final String usage = "\n" + "Usage: " + " <sourceImage> <bucketName>\n\n" + "Where:\n" + " sourceImage - The name of the image in an Amazon S3 bucket (for example, people.png). \n\n" + " bucketName - The name of the Amazon S3 bucket (for example, myBucket). \n\n"; if (args.length != 2) { System.out.println(usage); System.exit(1); } String sourceImage = args[0]; String bucketName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); RekognitionClient rekClient = RekognitionClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); displayAllFaces(s3, rekClient, sourceImage, bucketName); s3.close(); rekClient.close(); } // snippet-start:[rekognition.java2.display_faces.main] public static void displayAllFaces(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) { int height; int width; byte[] data = getObjectBytes (s3, bucketName, sourceImage); InputStream is = new ByteArrayInputStream(data); try { SdkBytes sourceBytes = SdkBytes.fromInputStream(is); image = ImageIO.read(sourceBytes.asInputStream()); width = image.getWidth(); height = image.getHeight(); // Create an Image object for the source image software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder() .bytes(sourceBytes) .build(); DetectFacesRequest facesRequest = DetectFacesRequest.builder() .attributes(Attribute.ALL) .image(souImage) .build(); result = rekClient.detectFaces(facesRequest); // Show the bounding box info for each face. List<FaceDetail> faceDetails = result.faceDetails(); for (FaceDetail face : faceDetails) { BoundingBox box = face.boundingBox(); float left = width * box.left(); float top = height * box.top(); System.out.println("Face:"); System.out.println("Left: " + (int) left); System.out.println("Top: " + (int) top); System.out.println("Face Width: " + (int) (width * box.width())); System.out.println("Face Height: " + (int) (height * box.height())); System.out.println(); } // Create the frame and panel. JFrame frame = new JFrame("RotateImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DisplayFaces panel = new DisplayFaces(image); panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale)); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } catch (RekognitionException | FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(1); } catch (IOException e) { e.printStackTrace(); } } public static byte[] getObjectBytes (S3Client s3, String bucketName, String keyName) { try { GetObjectRequest objectRequest = GetObjectRequest .builder() .key(keyName) .bucket(bucketName) .build(); ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest); return objectBytes.asByteArray(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } public DisplayFaces(BufferedImage bufImage) { super(); scale = 1; // increase to shrink image size. image = bufImage; } // Draws the bounding box around the detected faces. public void paintComponent(Graphics g) { float left; float top; int height = image.getHeight(this); int width = image.getWidth(this); Graphics2D g2d = (Graphics2D) g; // Create a Java2D version of g. // Draw the image g2d.drawImage(image, 0, 0, width / scale, height / scale, this); g2d.setColor(new Color(0, 212, 0)); // Iterate through the faces and display bounding boxes. List<FaceDetail> faceDetails = result.faceDetails(); for (FaceDetail face : faceDetails) { BoundingBox box = face.boundingBox(); left = width * box.left(); top = height * box.top(); g2d.drawRect(Math.round(left / scale), Math.round(top / scale), Math.round((width * box.width()) / scale), Math.round((height * box.height())) / scale); } } // snippet-end:[rekognition.java2.display_faces.main] }