

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

# COCO 데이터 세트 변환
<a name="md-coco-transform-example"></a>

다음 Python 예제를 사용하여 COCO 형식 데이터 세트의 경계 상자 정보를 Amazon Rekognition Custom Labels 매니페스트 파일로 변환합니다. 해당 코드는 생성된 매니페스트 파일을 Amazon S3 버킷에 업로드합니다. 해당 코드는 이미지를 업로드하는 데 사용할 수 있는 AWS CLI 명령도 제공합니다.

**COCO 데이터 세트를 변환하려면(SDK)**

1. 아직 설정하지 않았다면 다음과 같이 하세요.

   1. `AmazonS3FullAccess` 권한이 있는지 확인합니다. 자세한 내용은 [SDK 권한 설정](su-sdk-permissions.md) 단원을 참조하십시오.

   1.  AWS CLI 및 AWS SDKs를 설치하고 구성합니다. 자세한 내용은 [4단계: AWS CLI 및 AWS SDKs 설정](su-awscli-sdk.md) 단원을 참조하십시오.

1. 다음 Python 코드를 사용하여 COCO 데이터 세트를 변환합니다. 다음 값을 설정하세요.
   + `s3_bucket`: 이미지 및 Amazon Rekognition Custom Labels 매니페스트 파일을 저장할 S3 버킷의 이름입니다.
   + `s3_key_path_images`: S3 버킷(`s3_bucket`) 내에서 이미지를 배치하려는 위치의 경로
   + `s3_key_path_manifest_file`: S3 버킷(`s3_bucket`) 내에서 사용자 지정 레이블 매니페스트 파일을 배치할 경로
   + `local_path`: 예제에서 입력 COCO 데이터 세트를 열고 새 사용자 지정 레이블 매니페스트 파일도 저장하는 로컬 경로
   + `local_images_path`: 훈련에 사용할 이미지의 로컬 경로
   + `coco_manifest`: 입력 COCO 데이터 세트 파일 이름
   + `cl_manifest_file`: 예제에서 만든 매니페스트 파일의 이름 `local_path`에서 지정한 위치에 파일이 저장됩니다. 일반적으로 파일에는 `.manifest` 확장자가 있지만 필수는 아닙니다.
   + `job_name`: 사용자 정의 레이블 작업의 이름

   ```
   import json
   import os
   import random
   import shutil
   import datetime
   import botocore
   import boto3
   import PIL.Image as Image
   import io
   
   #S3 location for images
   s3_bucket = 'bucket'
   s3_key_path_manifest_file = 'path to custom labels manifest file/'
   s3_key_path_images = 'path to images/'
   s3_path='s3://' + s3_bucket  + '/' + s3_key_path_images
   s3 = boto3.resource('s3')
   
   #Local file information
   local_path='path to input COCO dataset and output Custom Labels manifest/'
   local_images_path='path to COCO images/'
   coco_manifest = 'COCO dataset JSON file name'
   coco_json_file = local_path + coco_manifest
   job_name='Custom Labels job name'
   cl_manifest_file = 'custom_labels.manifest'
   
   label_attribute ='bounding-box'
   
   open(local_path + cl_manifest_file, 'w').close()
   
   # class representing a Custom Label JSON line for an image
   class cl_json_line:  
       def __init__(self,job, img):  
   
           #Get image info. Annotations are dealt with seperately
           sizes=[]
           image_size={}
           image_size["width"] = img["width"]
           image_size["depth"] = 3
           image_size["height"] = img["height"]
           sizes.append(image_size)
   
           bounding_box={}
           bounding_box["annotations"] = []
           bounding_box["image_size"] = sizes
   
           self.__dict__["source-ref"] = s3_path + img['file_name']
           self.__dict__[job] = bounding_box
   
           #get metadata
           metadata = {}
           metadata['job-name'] = job_name
           metadata['class-map'] = {}
           metadata['human-annotated']='yes'
           metadata['objects'] = [] 
           date_time_obj = datetime.datetime.strptime(img['date_captured'], '%Y-%m-%d %H:%M:%S')
           metadata['creation-date']= date_time_obj.strftime('%Y-%m-%dT%H:%M:%S') 
           metadata['type']='groundtruth/object-detection'
           
           self.__dict__[job + '-metadata'] = metadata
   
   
   print("Getting image, annotations, and categories from COCO file...")
   
   with open(coco_json_file) as f:
   
       #Get custom label compatible info    
       js = json.load(f)
       images = js['images']
       categories = js['categories']
       annotations = js['annotations']
   
       print('Images: ' + str(len(images)))
       print('annotations: ' + str(len(annotations)))
       print('categories: ' + str(len (categories)))
   
   
   print("Creating CL JSON lines...")
       
   images_dict = {image['id']: cl_json_line(label_attribute, image) for image in images}
   
   print('Parsing annotations...')
   for annotation in annotations:
   
       image=images_dict[annotation['image_id']]
   
       cl_annotation = {}
       cl_class_map={}
   
       # get bounding box information
       cl_bounding_box={}
       cl_bounding_box['left'] = annotation['bbox'][0]
       cl_bounding_box['top'] = annotation['bbox'][1]
    
       cl_bounding_box['width'] = annotation['bbox'][2]
       cl_bounding_box['height'] = annotation['bbox'][3]
       cl_bounding_box['class_id'] = annotation['category_id']
   
       getattr(image, label_attribute)['annotations'].append(cl_bounding_box)
   
   
       for category in categories:
            if annotation['category_id'] == category['id']:
               getattr(image, label_attribute + '-metadata')['class-map'][category['id']]=category['name']
           
       
       cl_object={}
       cl_object['confidence'] = int(1)  #not currently used by Custom Labels
       getattr(image, label_attribute + '-metadata')['objects'].append(cl_object)
   
   print('Done parsing annotations')
   
   # Create manifest file.
   print('Writing Custom Labels manifest...')
   
   for im in images_dict.values():
   
       with open(local_path+cl_manifest_file, 'a+') as outfile:
               json.dump(im.__dict__,outfile)
               outfile.write('\n')
               outfile.close()
   
   # Upload manifest file to S3 bucket.
   print ('Uploading Custom Labels manifest file to S3 bucket')
   print('Uploading'  + local_path + cl_manifest_file + ' to ' + s3_key_path_manifest_file)
   print(s3_bucket)
   s3 = boto3.resource('s3')
   s3.Bucket(s3_bucket).upload_file(local_path + cl_manifest_file, s3_key_path_manifest_file + cl_manifest_file)
   
   # Print S3 URL to manifest file,
   print ('S3 URL Path to manifest file. ')
   print('\033[1m s3://' + s3_bucket + '/' + s3_key_path_manifest_file + cl_manifest_file + '\033[0m') 
   
   # Display aws s3 sync command.
   print ('\nAWS CLI s3 sync command to upload your images to S3 bucket. ')
   print ('\033[1m aws s3 sync ' + local_images_path + ' ' + s3_path + '\033[0m')
   ```

1. 코드를 실행합니다.

1. 프로그램 출력에서 `s3 sync` 명령을 기록해 둡니다. 이 정보는 다음 단계에서 필요합니다.

1. 명령 프롬프트에서 `s3 sync` 명령을 실행합니다. 이미지는 S3 버킷에 업로드됩니다. 업로드 중에 명령이 실패하면 로컬 이미지가 S3 버킷과 동기화될 때까지 명령을 다시 실행하세요.

1. 프로그램 출력에서 매니페스트 파일의 S3 URL 경로를 기록해 둡니다. 이 정보는 다음 단계에서 필요합니다.

1. [SageMaker AI Ground Truth 매니페스트 파일을 사용하여 데이터 세트 생성(콘솔)](md-create-dataset-ground-truth.md#md-create-dataset-ground-truth-console)의 지침에 따라 업로드된 매니페스트 파일로 데이터 세트를 생성하세요. 8단계로 **.manifest 파일 위치**에 이전 단계에서 기록해 둔 Amazon S3 URL을 입력합니다. AWS SDK를 사용하고 있다면 [SageMaker AI Ground Truth 매니페스트 파일(SDK)을 사용하여 데이터 세트 생성](md-create-dataset-ground-truth.md#md-create-dataset-ground-truth-sdk) 항목을 수행하세요.