【问题标题】:How to make tfrecords from json annotated images如何从 json 注释图像制作 tfrecords
【发布时间】:2018-12-04 00:33:00
【问题描述】:

我已经使用 tensorflow object detection-api 来训练我自己的对象检测器。但当时,图像是使用 labelimg 注释的,它为每个图像创建 xml 文件。现在我得到了带有 json 文件的标记图像每个图像。那么我如何使用这些 json 文件来创建 tfrecords。

【问题讨论】:

标签: python json tensorflow object-detection object-detection-api


【解决方案1】:

首先我使用自己的脚本创建了 csv 文件。

import os
import glob
import pandas as pd
import json
import pickle

def json_to_csv():
    path_to_json = 'images/train/'
    json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
    path_to_jpeg = 'images/train/'
    jpeg_files = [pos_jpeg for pos_jpeg in os.listdir(path_to_jpeg) if pos_jpeg.endswith('.jpeg')]
    fjpeg=(list(reversed(jpeg_files)))
    n=0
    csv_list = []
    labels=[]
    for j in json_files:
        data_file=open('images/train/{}'.format(j))   
        data = json.load(data_file)
        width,height=data['display_width'],data['display_height']
        for item in data["items"]:
            box = item['bounding_box']
            if item['upc']!='None':
                name=item['upc']
                labels.append(name)
                xmin=box['left']
                ymin=box['top']
                xmax=box['right']
                ymax=box['bottom']
                value = (fjpeg[n],
                         width,
                         height,
                         name,
                         xmin,
                         ymin,
                         xmax,
                         ymax
                         )
                csv_list.append(value)
          n=n+1
    column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    csv_df = pd.DataFrame(csv_list, columns=column_name)
    labels_train=list(set(labels))
    with open("train_labels.txt", "wb") as fp:   #Pickling
        pickle.dump(labels_train, fp)
    return csv_df

def main():
    for directory in ['train']:
        csv_df = json_to_csv()
        csv_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
        print('Successfully converted json to csv.')

main()

然后我使用this 脚本来创建tfrecords。

【讨论】:

    【解决方案2】:

    我们有some documentation 讨论这个主题。

    请注意,labelimg 应该产生类似于我们使用的 PASCAL VOC 数据集的输出,因此这些脚本也可能有用。

    【讨论】:

    • 我用过 labelimg,我知道它会产生 PASCAL VOC。如何将 jason 文件转换为 tfrecords 的问题
    猜你喜欢
    • 2017-01-20
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2019-12-10
    • 1970-01-01
    • 2020-01-02
    • 2015-12-02
    相关资源
    最近更新 更多