• 카테고리

    질문 & 답변
  • 세부 분야

    컴퓨터 비전

  • 해결 여부

    미해결

automl 질문드립니다

21.10.06 04:18 작성 조회수 108

0

안녕하세요 강의 너무 잘 듣고있습니다
 
제가 비전공자인데 제 데이터에 적용하려니 어려움이 크네요...
 
다름이아니라 autiml에서 데이터셋이 jpg가 아닌 png파일일때는 어떻게 적용하는지 알 수 있을까요?
 
import hashlib
import io
import json
import os
import PIL.Image
import tensorflow as tf
from dataset import tfrecord_util

# 1개의 image 파일 PATH와 1개의 annotation XML 정보를 가지는 data dic를 이용하여 tf.train.Example를 생성. 
def dict_to_tf_example(dataimage_pathlabel_map_dictunique_idignore_difficult_instances=Falseann_json_dict=Nonedebug=True):
  ''' 
    data는  1개의 xml 파일을 dictionary로 변환 anno_dict,
    image는 1개의 xml에 매핑되는 image 파일의 절대 경로
    unique_id는 고유한 image와 object id를 만들기 위한 Unique_Id객체
  ''' 
  
  #JPEG image를 binary 그대로 읽음. 
  with tf.io.gfile.GFile(image_path, 'rb'as fid:
    encoded_jpg = fid.read()
  
  # image가 JPEG 타입인지 확인. 
  encoded_jpg_io = io.BytesIO(encoded_jpg)
  image = PIL.Image.open(encoded_jpg_io)
  if image.format != 'JPEG':
    raise ValueError('Image format not JPEG')
  
  # image의 고유 key값 생성. 
  key = hashlib.sha256(encoded_jpg).hexdigest()

  #고유한 image id를 생성. 
  image_id = unique_id.get_image_id()
  # image의 width와 height 가져옴. 
  width = data['width']
  height = data['height']
 
 
jpg 파일을 읽어서 바이너리로 출력하는데 png파일을 넣으면 이상한 값이 나오는것같아 질문합니다!

답변 1

답변을 작성해보세요.

0

안녕하십니까, 

png 형태이면 아래와 같이 tfrecord를 변환해 보시지요. 

먼저 

def dict_to_tf_example(data, image_path, label_map_dict, unique_id, ignore_difficult_instances=False, ann_json_dict=None, debug=True):

with tf.io.gfile.GFile(image_path, 'rb') as fid: encoded_png = fid.read() # image가 PNG 타입인지 확인. encoded_png_io = io.BytesIO(encoded_png) image = PIL.Image.open(encoded_png_io) if image.format != 'PNG': raise ValueError('Image format not PNG') ''' 기존 코드 동일 ''' ''' 아래에서 encoded, format에 해당하는 값 변경. example_dict = {'height':height, 'width':width, 'filename':data['filename'].encode('utf8'), 'source_id': str(image_id).encode('utf8'), 'key_sha256': key.encode('utf8'), 'encoded': encoded_png, 'format':'png'.encode('utf8'), 'xmin':xmins, 'xmax':xmaxes, 'ymin':ymins, 'ymax':ymaxes, 'area':areas, 'class_text':classes_texts, 'class_label':classes, 'difficult':difficult_obj, 'truncated':truncated, 'poses':poses}
 

그리고 아래 함수에서 'image/format' 을 png로 변경해 주십시요. 

def make_tfrecord_example(example_dict):

.......

'image/format': tfrecord_util.bytes_feature('png'.encode('utf8')),

 

채영님의 프로필

채영

질문자

2021.10.07

감사합니다!