def make_tfrecord_from_tags(unused_argv): writer = tf.python_io.TFRecordWriter(FLAGS.output_path) count = 0 with open(FLAGS.tags_file_path) as fo: for line in fo: ts = line.split(' ') image_path = ts[0] groundtruth_text = ' '.join(ts[1:]) height, width, channel = cv2.imread(image_path).shape assert channel == 3, '{} has {} channel'.format(image_path, channel) image_bin = open(image_path, 'rb').read() example = tf.train.Example(features=tf.train.Features( feature={ fields.TfExampleFields.image_encoded: dataset_util.bytes_feature(image_bin), fields.TfExampleFields.height: dataset_util.int_feature(height), fields.TfExampleFields.width: dataset_util.int_feature(width), fields.TfExampleFields.transcript: dataset_util.bytes_feature(str(label).encode()), fields.TfExampleFields.filename: dataset_util.bytes_feature(str(i).encode()), } )) writer.write(example.SerializeToString()) count += 1 if count % 1000 == 0: print(count) writer.close() print('{} example creater!'.format(count))
def create_tf_example(): height = 544 # Image height width = 960 # Image width filename = 'Image10.jpg' # Filename of the image. Empty if image is not from file filename=filename.encode() with tf.gfile.GFile("./Image10.jpg", 'rb') as fid: encoded_image = fid.read() image_format = b'jpg' # b'jpeg' or b'png' xmins = [458.0/960.0] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [807.0/960.0] # List of normalized right x coordinates in bounding box # (1 per box) ymins = [157.0/544.0] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [360.0/544.0] # List of normalized bottom y coordinates in bounding box # (1 per box) classes_text = b"library" # List of string class name of bounding box (1 per box) #classes_text=classes_text.encode("utf8") classes = "2" # List of integer class id of bounding box (1 per box) classes=classes.encode() tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_image), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), #'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf(input_path,filename,bb_list): height = size # Image height width = size # Image width encoded_image_data = tf.gfile.GFile(input_path+filename).read() # Encoded image bytes image_format = b'jpeg' # b'jpeg' or b'png' xmins = [i[0]/float(size) for i in bb_list] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [i[1]/float(size) for i in bb_list] # List of normalized right x coordinates in bounding box (1 per box) ymins = [i[2]/float(size) for i in bb_list] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [i[3]/float(size) for i in bb_list] # List of normalized bottom y coordinates in bounding box (1 per box) classes_text = ['ship'] * len(bb_list) # List of string class name of bounding box (1 per box) classes = [1] * len(bb_list) # List of integer class id of bounding box (1 per box) tf_image = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_image
def dict_to_coco_example(img_data): """Convert python dictionary formath data of one image to tf.Example proto. Args: img_data: infomation of one image, inclue bounding box, labels of bounding box,\ height, width, encoded pixel data. Returns: example: The converted tf.Example """ # bboxes = img_data['bboxes'] # xmin, xmax, ymin, ymax = [], [], [], [] # for bbox in bboxes: # xmin.append(bbox[0]) # xmax.append(bbox[0] + bbox[2]) # ymin.append(bbox[1]) # ymax.append(bbox[1] + bbox[3]) example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(img_data['height']), 'image/width': dataset_util.int64_feature(img_data['width']), #'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), #'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), #'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), #'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), #'image/object/class/label': dataset_util.int64_list_feature(img_data['labels']), 'image/encoded': dataset_util.bytes_feature( tf.compat.as_bytes(img_data['pixel_data'])), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf-8')), })) return example
def create_tf_example(f, image_path=None): xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] filename = f.readline().rstrip() filepath = os.path.join(image_path, filename) image_raw = cv2.imread(filepath) encoded_image_data = open(filepath, 'rb').read() key = hashlib.sha256(encoded_image_data).hexdigest() height, width, channel = image_raw.shape face_num = int(f.readline().rstrip()) valid_face_num = 0 for i in range(face_num): annot = f.readline().rstrip().split() # WIDER FACE DATASET CONTAINS SOME ANNOTATIONS WHAT EXCEEDS THE IMAGE BOUNDARY if (float(annot[2]) > 25.0): if (float(annot[3]) > 30.0): xmins.append(max(0.005, (float(annot[0]) / width))) ymins.append(max(0.005, (float(annot[1]) / height))) xmaxs.append( min(0.995, ((float(annot[0]) + float(annot[2])) / width))) ymaxs.append( min(0.995, ((float(annot[1]) + float(annot[3])) / height))) classes_text.append('face'.encode('utf8')) classes.append(1) print(xmins[-1], ymins[-1], xmaxs[-1], ymaxs[-1], classes_text[-1], classes[-1]) valid_face_num += 1 print("Face Number is %d" % face_num) print("Valid face number is %d" % valid_face_num) feature_dict = { 'image/height': dataset_util.int64_feature(int(height)), 'image/width': dataset_util.int64_feature(int(width)), 'image/filename': dataset_util.bytes_feature(filename.encode('utf8')), # 'image/source_id': dataset_util.bytes_feature(filename.encode('utf8')), # 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), } tf_example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return valid_face_num, tf_example
def dict_to_tf_example(data, label): with open(data, 'rb') as inf: encoded_data = inf.read() img_label = cv2.imread(label) img_mask = image2label(img_label) encoded_label = img_mask.astype(np.uint8).tobytes() height, width = img_label.shape[0], img_label.shape[1] if height < vgg_16.default_image_size or width < vgg_16.default_image_size: # 保证最后随机裁剪的尺寸 return None # Your code here, fill the dict feature_dict = { 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(data.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_data), 'image/label': dataset_util.bytes_feature(encoded_label), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example
def main(unused_argv): writer = tf.python_io.TFRecordWriter(FLAGS.output_path) count = 0 with open(FLAGS.tags_file_path) as fo: for line in fo: image_path = line.strip() filename = '/'.join(line.strip().split('/')[-2:]) groundtruth_text = line.split('_')[1] try: height, width, channel = cv2.imread(image_path).shape image_bin = open(image_path, 'rb').read() except Exception as e: print(e) continue example = tf.train.Example(features=tf.train.Features( feature={ fields.TfExampleFields.image_encoded: dataset_util.bytes_feature(image_bin), fields.TfExampleFields.height: dataset_util.int64_feature(height), fields.TfExampleFields.width: dataset_util.int64_feature(width), fields.TfExampleFields.filename: dataset_util.bytes_feature(filename.encode()), fields.TfExampleFields.transcript: dataset_util.bytes_feature(groundtruth_text.encode()) } )) writer.write(example.SerializeToString()) count += 1 if count % 1000 == 0: print(count) writer.close() print('{} example finished!'.format(count))
def create_tf_record(image_file, height, width, xmins, ymins, xmaxs, ymaxs, classes_id, classes_text): ''' param xmins: List of normalized left x coordinates in bounding box (1 per box) param xmaxs: List of normalized right x coordinates in bounding box (1 per box) param ymins: List of normalized top y coordinates in bounding box (1 per box) param ymaxs: List of normalized bottom y coordinates in bounding box (1 per box) param classes_text: List of string class name of bounding box (1 per box) param classes: List of integer class id of bounding box (1 per box) ''' image_data = tf.gfile.FastGFile(image_file, 'rb').read() encoded_image_data = tf.compat.as_bytes(image_data) # Encoded image bytes filename = tf.compat.as_bytes(image_file) image_format = 'png'.encode('utf8') # b'jpeg' or b'png' tf_single_dataset = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(os.path.basename(filename)), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(image_data), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes_id), })) return tf_single_dataset
def create_tf_example(filename, dirpath, boxes): #Read in the image with tf.gfile.GFile(os.path.join(dirpath, filename), 'rb') as fid: encoded_image_data = fid.read() encoded_jpg_io = io.BytesIO(encoded_image_data) image = Image.open(encoded_jpg_io) width, height = image.size _, ext = os.path.splitext(filename) image_format = ext[1:].encode('UTF-8') # b'jpeg' or b'png' xmins = [ x / width for x in boxes['xmins'] ] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [ x / width for x in boxes['xmaxs'] ] # List of normalized right x coordinates in bounding box (1 per box) ymins = [ y / height for y in boxes['ymins'] ] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [ y / height for y in boxes['ymaxs'] ] # List of normalized bottom y coordinates in bounding box (1 per box) #DEFAULT LABELS AND CLASSES FOR IVF classes_text = [b'sperm'] * len( xmins) # List of string class name of bounding box (1 per box) classes = [1] * len( xmins) # List of integer class id of bounding box (1 per box) tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename.encode('UTF-8')), 'image/source_id': dataset_util.bytes_feature(filename.encode('UTF-8')), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf_example(group, path, labels_mapping): with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size filename = group.filename.encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for index, row in group.object.iterrows(): class_idx = labels_mapping.get(row['class'], None) if class_idx is None: continue xmins.append(row['xmin'] / width) xmaxs.append(row['xmax'] / width) ymins.append(row['ymin'] / height) ymaxs.append(row['ymax'] / height) classes_text.append(row['class'].encode('utf8')) classes.append(class_idx) tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf_example(group, img_path): with tf.gfile.GFile(os.path.join(img_path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = PIL.Image.open(encoded_jpg_io) if image.format != 'JPEG': raise ValueError('Image format not JPEG') width, height = image.size filename = group.filename.encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for index, row in group.object.iterrows(): xmins.append(row['xmin'] / width) xmaxs.append(row['xmax'] / width) ymins.append(row['ymin'] / height) ymaxs.append(row['ymax'] / height) classes_text.append(row['label'].encode('utf8')) classes.append(class_text_to_int(row['label'])) tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def group_to_tf_record(boxes, image_file): """ :param boxes: :type boxes: :param image_file: :type image_file: :return: :rtype: """ format = b'jpeg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] class_nums = [] class_ids = [] filename = image_file try: image = Image.open(filename) width, height = image.size with tf.gfile.GFile(filename, 'rb') as fid: encoded_jpg = bytes(fid.read()) except: return None key = hashlib.sha256(encoded_jpg).hexdigest() for i, anno in enumerate(boxes): # Needs refactoring # xmins.append(float(anno[1])) # xmaxs.append(float(anno[3])) # ymins.append(float(anno[2])) # ymaxs.append(float(anno[4])) # class_nums.append(LABEL_STOI[anno[0]]) # class_ids.append(bytes(anno[0], "utf-8")) boxes[i] = [anno[1], anno[2], anno[3], anno[4], LABEL_STOI[anno[0]]] boxes = np.array(boxes, dtype=np.float32).tostring() tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 'image/filename': dataset_util.bytes_feature( bytes(filename, "utf-8")), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(format), # 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), # 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), # 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), # 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), # 'image/object/class/text': dataset_util.bytes_list_feature(class_ids), # 'image/object/class/label': dataset_util.int64_list_feature(class_nums) 'boxes': tf.train.Feature(bytes_list=tf.train.BytesList( value=[boxes])) })) return tf_example
def create_tf_example(image_file): print("opening {}".format(image_file)) with tf.gfile.GFile(image_file, 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size filename = os.path.basename(image_file).encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] xml_file = image_file + ".xml" tree = ET.parse(xml_file) root = tree.getroot() for member in root.findall('object'): classes_text.append(member[0].text.encode('utf-8')) classes.append(class_text_to_int(member[0].text)) xmins.append(int(member[5][0].text) / width) xmaxs.append(int(member[5][1].text) / width) ymins.append(int(member[5][2].text) / height) ymaxs.append(int(member[5][3].text) / height) tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf_example(image_data): height = image_data.height # Image height width = image_data.width # Image width with tf.gfile.GFile(image_data.path, 'rb') as fid: encoded_png = fid.read() filename = image_data.id.encode('utf8') image_format = b'png' # b'jpeg' or b'png' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for bbox in image_data.bboxes: xmins.append(bbox.min_x / width) xmaxs.append(bbox.max_x / width) ymins.append(bbox.min_y / height) ymaxs.append(bbox.max_y / height) classes_text.append(bbox.class_label.encode('utf8')) classes.append(bbox.class_id) tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_png), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf_example(example, height, width): ## Udacity's real-life data set ## height = 1096 # Image height ## width = 1368 # Image width ## Udacity's simulator data set ## height = 600 ## width = 800 filename = example['filename'] # Filename of the image. Empty if image is not from file filename = filename.encode() with tf.gfile.GFile(example['filename'], 'rb') as fid: encoded_image = fid.read() image_format = 'jpg'.encode() xmins = [] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [] # List of normalized right x coordinates in bounding box # (1 per box) ymins = [] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [] # List of normalized bottom y coordinates in bounding box # (1 per box) classes_text = [] # List of string class name of bounding box (1 per box) classes = [] # List of integer class id of bounding box (1 per box) for box in example['annotations']: #print("adding box") xmins.append(float(box['xmin'] / width)) xmaxs.append(float((box['xmin'] + box['x_width']) / width)) ymins.append(float(box['ymin'] / height)) ymaxs.append(float((box['ymin']+ box['y_height']) / height)) classes_text.append(box['class'].encode()) classes.append(int(LABEL_DICT[box['class']])) tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_image), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def __create_tf_example(frame_data, sorted_label_list): im = PIL.Image.open(io.BytesIO(frame_data.image)) arr = io.BytesIO() if frame_data.format == 'jpg': format = 'JPEG' else: format = frame_data.format.upper() im.save(arr, format=format) height = im.height width = im.width encoded_image_data = arr.getvalue() rects, labels = bbox_writer.convert_text_to_rects_and_labels( frame_data.bboxes_text) # List of normalized coordinates, 1 per box, capped to [0, 1] xmins = [max(min(rect[0] / width, 1), 0) for rect in rects] # left x xmaxs = [max(min(rect[2] / width, 1), 0) for rect in rects] # right x ymins = [max(min(rect[1] / height, 1), 0) for rect in rects] # top y ymaxs = [max(min(rect[3] / height, 1), 0) for rect in rects] # bottom y classes_txt = [label.encode('utf-8') for label in labels] # String names label_to_id_dict = {label: i for i, label in enumerate(sorted_label_list)} class_ids = [label_to_id_dict[label] for label in labels] tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(frame_data.filename.encode('utf-8')), 'image/source_id': dataset_util.bytes_feature(frame_data.filename.encode('utf-8')), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature(frame_data.format.encode('utf-8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_txt), 'image/object/class/label': dataset_util.int64_list_feature(class_ids), })) label_counter_for_frame = collections.Counter(labels) is_negative = len(rects) == 0 return tf_example, label_counter_for_frame, is_negative
def create_tf_example(input_features, image_dir='/'): image_path = input_features['image'] label = input_features['label'] image_path = os.path.join(image_dir, image_path) image = cv2.imread(image_path) encoded_jpg = cv2.imencode('.jpg', image)[1].tostring() feature_dict = { 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/label': dataset_util.int64_feature(label), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example
def group_to_tf_record(point, image_directory): format_png = b'png' format_jpg = b'jpeg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] class_nums = [] class_ids = [] # changed point[0] to point as is just one point image_id = point['id'] if image_id.startswith('frame'): filename = os.path.join(image_directory, image_id + '.png') format = format_png else: filename = os.path.join(image_directory, image_id + '.jpg') #.decode() format = format_jpg try: image = Image.open(filename) width, height = image.size with tf.gfile.GFile(filename, 'rb') as fid: encoded_image = bytes(fid.read()) except: return None key = hashlib.sha256(encoded_image).hexdigest() for anno in point['annotations']: xmins.append(float(anno['x0'])) xmaxs.append(float(anno['x1'])) ymins.append(float(anno['y0'])) ymaxs.append(float(anno['y1'])) class_nums.append(anno['class_num']) class_ids.append(bytes(anno['label'].encode('utf8'))) tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 'image/filename': dataset_util.bytes_feature(bytes(filename.encode('utf8'))), 'image/source_id': dataset_util.bytes_feature(bytes(image_id.encode('utf8'))), 'image/encoded': dataset_util.bytes_feature(encoded_image), 'image/format': dataset_util.bytes_feature(format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(class_ids), 'image/object/class/label': dataset_util.int64_list_feature(class_nums) })) return tf_example
def make_example(filename, label): with tf.gfile.GFile(filename, 'rb') as fid: image = fid.read() return tf.train.Example(features=tf.train.Features( feature={ 'label': dataset_util.int64_feature(label), 'image': dataset_util.bytes_feature(image) }))
def create_tf_example(filename): coordinates = filename.rsplit('/', 1)[-1].rsplit('.', 1)[0].split('-')[2] leftUp, rightDown = [[int(eel) for eel in el.split('&')] for el in coordinates.split('_')] xmin, ymin = leftUp xmax, ymax = rightDown with tf.gfile.GFile(filename, 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = PIL.Image.open(encoded_jpg_io) height = image.height width = image.width key = hashlib.sha256(encoded_jpg).hexdigest() ymins = [float(ymin) / height] xmins = [float(xmin) / width] ymaxs = [float(ymax) / height] xmaxs = [float(xmax) / width] labels_text = ['vehicle plate'.encode('utf8')] labels = [2] # print("---------image size:",image.size) # print("---------xmin:{}, ymin:{}, xmax:{}, ymax:{}".format(xmin,ymin,xmax,ymax)) # print("---------width:{}, height:{}".format(width,height)) feature_dict = { 'image/height': dataset_util.int64_feature(int(height)), 'image/width': dataset_util.int64_feature(int(width)), 'image/filename': dataset_util.bytes_feature(filename.encode('utf8')), # 'image/source_id': dataset_util.bytes_feature(filename.encode('utf8')), # 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(labels_text), 'image/object/class/label': dataset_util.int64_list_feature(labels), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example
def create_tfdatapoint(file_loc, file, labels): img = Image.open(os.path.join(file_loc, 'images', file)) (width, height) = img.size encoded = tf.io.gfile.GFile(os.path.join(file_loc, 'images', file), "rb").read() encoded = bytes(encoded) image_format = b'png' filename = file.split('.')[0] data = np.genfromtxt(os.path.join(file_loc, 'labels', filename + '.txt')) data = data.reshape(int(data.size / 5), 5) classes = [int(x) for x in data[:, 0]] classes_text = [labels[x].encode('utf8') for x in classes] xmins = data[:, 1] - (data[:, 3] / 2.0) xmaxs = data[:, 1] + (data[:, 3] / 2.0) ymins = data[:, 2] - (data[:, 4] / 2.0) ymaxs = data[:, 2] + (data[:, 4] / 2.0) tf_label_and_data = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(str.encode(filename)), 'image/source_id': dataset_util.bytes_feature(str.encode(filename)), 'image/encoded': dataset_util.bytes_feature(encoded), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_label_and_data
def create_tf_example(group, path): # Opening and readinf the files with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = fid.read() # Encode the image in jpeg format to array values encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) # Setting up the image size width, height = image.size #Creating the boundary box coordinate instances such as xmin,ymin,xmax,ymax filename = group.filename.encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes = [] for index, row in group.object.iterrows(): xmins.append(row['xmin']) xmaxs.append(row['xmax']) ymins.append(row['ymin']) ymaxs.append(row['ymax']) classes.append(row['class'].encode('utf8')) # This is already exisiting code to convert csv to tfrecord tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/label': dataset_util.bytes_list_feature( classes), })) return tf_example
def create_tf_example(data_dict): #with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = resize_jpeg( os.path.join('/panfs/roc/groups/5/packerc/shared/albums/SER/', data_dict) + '.JPG', 1000) #encoded_jpg_io = io.BytesIO(encoded_jpg) #image = Image.open(encoded_jpg_io) #width, height = image.size filename = data_dict.encode('utf-8') image_format = b'jpg' tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/filename': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format) })) return tf_example
def create_tf_example(group, path): base = os.path.splitext(group.filename)[0] with tf.gfile.GFile(os.path.join(path, base + ".jpg"), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size filename = group.filename.encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for index, row in group.object.iterrows(): xmins.append(row['xmin'] / width) xmaxs.append(row['xmax'] / width) ymins.append(row['ymin'] / height) ymaxs.append(row['ymax'] / height) classes_text.append(int_to_class_text(row['classID']).encode('utf8')) classes.append(row['classID']+1) tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example
def create_tf_example(height, width, filename, encoded_image_data, image_format, xmins, xmaxs, ymins, ymaxs, classes_text, classes): tf_example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), # Image height 'image/width': dataset_util.int64_feature(width), # Image width 'image/filename': dataset_util.bytes_feature(filename), # Filename of the image 'image/source_id': dataset_util.bytes_feature(filename), # Filename of the image 'image/encoded': dataset_util.bytes_feature( encoded_image_data), # Encoded image bytes 'image/format': dataset_util.bytes_feature(image_format), # b'jpeg' or b'png' 'image/object/bbox/xmin': dataset_util.float_list_feature( xmins), # normalized left x coordinate in bounding box 'image/object/bbox/xmax': dataset_util.float_list_feature( xmaxs), # normalized right x coordinate in bounding box 'image/object/bbox/ymin': dataset_util.float_list_feature( ymins), # normalized top y coordinate in bounding box 'image/object/bbox/ymax': dataset_util.float_list_feature( ymaxs), # normalized bottom y coordinate in bounding box 'image/object/class/text': dataset_util.bytes_list_feature( classes_text), # string class name of bounding box 'image/object/class/label': dataset_util.int64_list_feature( classes), # integer class id of bounding box })) return tf_example
def create_tf_example(csv, img_dir): img_fname = csv[0] x1, y1, x2, y2 = list(map(int, csv[1:-1])) cls_idx = int(csv[-1]) cls_text = config.CLASS_NAMES[cls_idx].encode('utf8') with tf.gfile.GFile(os.path.join(img_dir, img_fname), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size xmin = [x1 / width] xmax = [x2 / width] ymin = [y1 / height] ymax = [y2 / height] cls_text = [cls_text] cls_idx = [cls_idx] filename = img_fname.encode('utf8') image_format = b'jpg' tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), 'image/object/class/text': dataset_util.bytes_list_feature(cls_text), 'image/object/class/label': dataset_util.int64_list_feature(cls_idx), })) return tf_example
def dict_to_tf_example(data, label): print("data----", data) with open(data, 'rb') as inf: encoded_data = inf.read() #print("encoded_data----",encoded_data) img_label = cv2.imread(label) img_mask = image2label(img_label) encoded_label = img_mask.astype(np.uint8).tobytes() data_img = img_label.astype('int32') idx = (data_img[:, :, 2] * 256 + data_img[:, :, 1]) * 256 + data_img[:, :, 0] height, width = img_label.shape[0], img_label.shape[1] if height < vgg_16.default_image_size or width < vgg_16.default_image_size: # 保证最后随机裁剪的尺寸 return None # Your code here, fill the dict feature_dict = { 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(re.split('\/+', data)[-1].encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_data), 'image/label': dataset_util.bytes_feature(encoded_label), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), #自己写不行?? } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example
def _encode_image_to_tfrecord(image_path, category_id): with tf.io.gfile.GFile(image_path, 'rb') as fid: encoded_jpg = fid.read() image_name = image_path.split('/')[-1] feature_dict = { 'image_name': dataset_util.bytes_feature(image_name.encode('utf8')), 'encoded_image': dataset_util.bytes_feature(encoded_jpg), 'category_id': dataset_util.int64_feature(category_id), 'format': dataset_util.bytes_feature('jpeg'.encode('utf8')) } return tf.train.Example(features=tf.train.Features(feature=feature_dict))
def make_example(filename, cloth_type, minx, miny, maxx, maxy, lv1, lx1, ly1, lv2, lx2, ly2, lv3, lx3, ly3, lv4, lx4, ly4, lv5, lx5, ly5, lv6, lx6, ly6, lv7, lx7, ly7, lv8, lx8, ly8): label = filename[4:filename.find('/id')] label = rank.index(label) filename = os.path.join('./data', filename) with tf.gfile.GFile(filename, 'rb') as fid: image = fid.read() return tf.train.Example(features=tf.train.Features(feature={ 'image/object/class/label': dataset_util.int64_feature(label), 'image/imgdata': dataset_util.bytes_feature(image), 'image/object/bbox/xmin': dataset_util.int64_feature(minx), 'image/object/bbox/xmax': dataset_util.int64_feature(maxx), 'image/object/bbox/ymin': dataset_util.int64_feature(miny), 'image/object/bbox/ymax': dataset_util.int64_feature(maxy), 'image/object/lmk/lv1': dataset_util.int64_feature(lv1), 'image/object/lmk/lx1': dataset_util.int64_feature(lx1), 'image/object/lmk/ly1': dataset_util.int64_feature(ly1), 'image/object/lmk/lv2': dataset_util.int64_feature(lv2), 'image/object/lmk/lx2': dataset_util.int64_feature(lx2), 'image/object/lmk/ly2': dataset_util.int64_feature(ly2), 'image/object/lmk/lv3': dataset_util.int64_feature(lv3), 'image/object/lmk/lx3': dataset_util.int64_feature(lx3), 'image/object/lmk/ly3': dataset_util.int64_feature(ly3), 'image/object/lmk/lv4': dataset_util.int64_feature(lv4), 'image/object/lmk/lx4': dataset_util.int64_feature(lx4), 'image/object/lmk/ly4': dataset_util.int64_feature(ly4), 'image/object/lmk/lv5': dataset_util.int64_feature(lv5), 'image/object/lmk/lx5': dataset_util.int64_feature(lx5), 'image/object/lmk/ly5': dataset_util.int64_feature(ly5), 'image/object/lmk/lv6': dataset_util.int64_feature(lv6), 'image/object/lmk/lx6': dataset_util.int64_feature(lx6), 'image/object/lmk/ly6': dataset_util.int64_feature(ly6), 'image/object/lmk/lv7': dataset_util.int64_feature(lv7), 'image/object/lmk/lx7': dataset_util.int64_feature(lx7), 'image/object/lmk/ly7': dataset_util.int64_feature(ly7), 'image/object/lmk/lv8': dataset_util.int64_feature(lv8), 'image/object/lmk/lx8': dataset_util.int64_feature(lx8), 'image/object/lmk/ly8': dataset_util.int64_feature(ly8) }))
def dict_to_tf_example(data): """Convert XML derived dict to tf.Example proto. Notice that this function normalizes the bounding box coordinates provided by the raw data. Args: data: dict holding PASCAL XML fields for a single image (image and and corresponding label) Returns: example: The converted tf.Example. Raises: ValueError: if the image pointed to by data['filename'] is not a valid JPEG """ # print (os.path.join(image_subdirectory, data['filename'] + '.bmp')) img_path = data['filename'] with tf.gfile.GFile(img_path) as fid: encoded_img = fid.read() encoded_img_io = io.BytesIO(encoded_img) image = PIL.Image.open(encoded_img_io) if image.format == 'PNG': img_format = 'png' elif image.format == 'JPEG': img_format = 'jpeg' elif image.format == 'BMP': img_format = 'bmp' else: raise ValueError('Image format not PNG/JPEG/BMP') key = hashlib.sha256(encoded_img).hexdigest() (width, height) = image.size class_id = data['class_id'] class_text = data['class_text'] example = tf.train.Example(features=tf.train.Features( feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/channels': dataset_util.int64_feature(3), 'image/filename': dataset_util.bytes_feature(data['filename']), 'image/source_id': dataset_util.bytes_feature(data['filename']), 'image/key/sha256': dataset_util.bytes_feature(key), 'image/encoded': dataset_util.bytes_feature(encoded_img), 'image/format': dataset_util.bytes_feature(img_format), 'image/class/text': dataset_util.bytes_feature(class_text), 'image/class/label': dataset_util.int64_feature(class_id), })) return example