def _process_image(filename): """ 이미지 파일을 읽어들여 RGB 타입으로 변환하여 반환한다. :param filename: string, 읽어들일 이미지 파일 경로. :return: image_data: 이미지 데이터로 jpg 포맷의 데이터. height: 이미지 height width: 이미지 width """ # Read the image file. with tf.gfile.FastGFile(filename, 'rb') as f: image_data = f.read() try: image = coder.decode_jpg(image_data) height = image.shape[0] width = image.shape[1] except tf.errors.InvalidArgumentError: raise ValueError("Invalid decode in {}".format(filename)) return image_data, height, width
def _process_image(filename, bbox=None): """ 이미지 파일을 읽어들여 RGB 타입으로 변환하여 반환한다. :param filename: string, 읽어들일 이미지 파일 경로. e.g., '/path/to/example.JPG'. :param bbox: [xmin, ymin, xmax, ymax] 형식의 bbox 데이터 또는 None :return: image_data: 이미지 데이터로 jpg 포맷의 데이터. height: 이미지 height width: 이미지 width """ with tf.gfile.GFile(filename, 'rb') as f: image_data = f.read() image_format = dataset_utils.get_image_file_format(filename) # try: image = coder.decode_jpg(image_data) height = image.shape[0] width = image.shape[1] # except tf.errors.InvalidArgumentError: # raise ValueError("Invalid decode in {}".format(filename)) if bbox is None: return image_data, height, width, image_format else: # change bbox to [y, x, h, w] crop_window = [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]] # 전체 이미지 크기보다 bbox 영역이 넘어가는 경우 에러가 발생한다. # 이를 막기위한 보정 코드를 추가한다. h_gap = crop_window[2] + crop_window[0] - height w_gap = crop_window[3] + crop_window[1] - width if h_gap > 0: crop_window[2] -= h_gap if w_gap > 0: crop_window[3] -= w_gap assert crop_window[2] > 0 assert crop_window[3] > 0 image = coder.crop_bbox(image, crop_window) image_data = coder.encode_jpg(image) image_format = 'jpg' return image_data, crop_window[2], crop_window[3], image_format