import tensorflow as tf
import os
tf_dir = './tfrecords_304_304_PIL_test'
tf_list = [os.path.join(tf_dir,f) for f in os.listdir(tf_dir) ]
print("tfrecord files are {}".format(tf_list))
def extract_tfrecords_features(tfrecords_file):
    """Extract features in a tfrecords file for parsing a series of tfrecords files."""
    tfrecords_iterator = tf.python_io.tf_record_iterator(tfrecords_file)
    sample = 0
    for record in tfrecords_iterator:
            example = tf.train.Example()
            example.ParseFromString(record)
            features = example.features.feature
            #print("the features is:{}\n".format(features))
            tf_frame_id = features['frame/id'].bytes_list.value
            #print("the frame_id is:{}\n".format(tf_frame_id))
            tf_x1 = features['target/coordinates_x1'].float_list.value
            #print("the target/coordinates_x1 is:{}\n".format(tf_x1))
            tf_y1 = features['target/coordinates_y1'].float_list.value
            #print("the target/coordinates_y1 is:{}\n".format(tf_y1))
            tf_x2 = features['target/coordinates_x2'].float_list.value
            #print("the target/coordinates_x2 is:{}\n".format(tf_x2))
            tf_y2 = features['target/coordinates_y2'].float_list.value
            #print("the target/coordinates_y2 is:{}\n".format(tf_y2))
            tf_class = features['target/object_class'].bytes_list.value
            #print("the object_class is:{}\n".format(tf_class))
            #print("the len of object_class is:{}\n".format(len(tf_class)))
            #label_file = './label/' + tf_frame_id[0].split('/')[0]  + '.txt'
            sample += 1
            for i in tf_class:
                j = coco.get(i,0)   #get the previous class's value
                z = int(j) + 1      #add 1
                coco[i] = z
    print(coco)
    print("current tfrecord's samples are {}".format(sample))
    return sample
if __name__ == '__main__':
    #extract_tfrecords_features(tf_file)
    total_sample = 0
    coco = {'car':0,
    'chair' :0,
    'book' :0,
    'bottle' : 0,
    'cup'  :0,
    'diningtable' :0,
    'bowl' :0,
    'trafficlight' :0,
    'handbag' :0,
    'bird' :0,
    'boat' :0,
    'truck' :0,
    'umbrella' :0,
    'cow' :0,
    'kite' :0,
    'sheep' :0,
    'bench' :0,
    'banana' :0,
    'pottedplant' :0,
    'motorcycle'  :0,
    'carrot' :0,
    'backpack' :0,
    'knife' :0,
    'wineglass' :0,
    'donut'  :0,
    'bicycle' :0,
    'tie'  :0,
    'orange' :0,
    'clock' :0,
    'skis' :0,
    'broccoli' :0,
    'cake'  :0,
    'horse' :0,
    'fork' :0,
    'vase' :0,
    'cellphone' :0,
'bus'   :0,
'remote'        :0,
'pizza' :0,
'spoon' :0,
'surfboard'     :0,
'couch' :0,
'tv'    :0,
'suitcase'      :0,
'apple' :0,
'elephant'      :0,
'sportsball'    :0,
'airplane'      :0,
'sink'  :0,
'skateboard'    :0,
'zebra' :0,
'dog'   :0,
'laptop'        :0,
'teddybear'     :0,
'tennisracket'  :0,
'giraffe'       :0,
'train' :0,
'cat'   :0,
'bed'   :0,
'sandwich'      :0,
'baseballglove' :0,
'toilet'        :0,
'oven'  :0,
'baseballbat'   :0,
'snowboard'     :0,
'keyboard'      :0,
'hotdog'        :0,
'refrigerator'  :0,
'frisbee'       :0,
'toothbrush'    :0,
'mouse' :0,
'stopsign'      :0,
'firehydrant'   :0,
'microwave'     :0,
'scissors'      :0,
'parkingmeter'  :0,
'bear'  :0,
'toaster'       :0,
'hairdrier'     :0,
}
    for t in tf_list:
        print("\n")
        print("current tfrecord file is {}".format(t))
        total_sample += extract_tfrecords_features(t)
        print("\n")
        for v in coco.values():
            print(v)
    print("total samples are {}".format(total_sample))
    print("\n")
    for k in coco.keys():
        print(k)
 
from __future__ import print_function
import tensorflow as tf
tf_file = './1.tfrecord'  # tfrecord file
def extract_tfrecords_features(tfrecords_file):
    """Extract features in a tfrecords file for parsing a series of tfrecords files."""
    tfrecords_iterator = tf.python_io.tf_record_iterator(tfrecords_file)
    for record in tfrecords_iterator:
            example = tf.train.Example()
            example.ParseFromString(record)
            features = example.features.feature
            #print("the features is:{}\n".format(features))
            frame_id = features['frame/id'].bytes_list.value
            print("The frame_id is:{}\n".format(frame_id))
            frame_width = features['frame/width'].int64_list.value
            print("The frame_width is:{}\n".format(frame_width))
            frame_height = features['frame/height'].int64_list.value
            print("The frame_height is:{}\n".format(frame_height))
if __name__ == '__main__':
    extract_tfrecords_features(tf_file)