How to make tfrecords from json annotated images
Asked Answered
N

2

5

I have used tensorflow object detection-api to train my own object detector.But at that time, the images were annotated using labelimg which create xml file for each image.Now I have got labeled images which have json file for each image.So how I use these json files to create tfrecords.

Nemathelminth answered 25/6, 2018 at 12:20 Comment(4)
Your question is similar to #38382387Honor
Nope, its not creating tfrecordsNemathelminth
This is a good example of how to create a TFRecord file: gist.github.com/gvanhorn38/ac19b85a4f7b5fb9e82e04f4ac6d5566Lenorelenox
well after hard work I have done it.Nemathelminth
N
6

First I created csv files by using my own script.

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()

Then I use this script to create tfrecords.

Nemathelminth answered 4/7, 2018 at 8:27 Comment(1)
I get the following error... How can I fix it? width, height = data['width'], data['height'] ----> KeyError: 'width' -----> line 19Phipps
C
0

We have some documentation on the subject.

Note that labelimg should produce outputs similar to the PASCAL VOC datasets we use, so those scripts may also be of use.

Chasse answered 26/6, 2018 at 0:2 Comment(1)
I have used labelimg and i know it produce PASCAL VOC. The question how to convert a jason file in to tfrecordsNemathelminth

© 2022 - 2024 — McMap. All rights reserved.