The data about each image, sometimes called the image "metadata", is stored in a file that ends with the file extension .catalog. If you open these files, you will see a simple layout that looks like the following:
This file consists of multiple lines, each line starts and ends with curly braces "{" and "}". Within these curly braces are a set of key-value pairs where the label is a string in single quotes followed by a colon, the value and a comma. This file uses newlines to separate records and a JSON object format within each single line. Note this is NOT a full JSON file format so you can't just use a standard JSON library to read the catalog file.
Here is that format with a the key and value on separate lines to make the line easier to read.
Here is a sample JSON file reader that would read this file:
1 2 3 4 5 6 7 8 9101112131415161718192021
# program to read a DonkeyCar Catalog Fileimportos,json# this program assumes that test.json is in the same directory as this script# get the direcotry that this script is runningscript_dir=os.path.dirname(__file__)# get a relative path to the script dirpath_to_json_file=script_dir+'/test.json'# Open the JSON test file for read onlyf=open(path_to_json_file,'r')# returns JSON object as a dictionarydata=json.load(f)# Iterating through the json file for the items in the drive data dictionaryforiindata['driveData']:print(i)# Close the JSON filef.close()
Note that the open() function reads the file with the "r" option which indicates read-only mode.
Although this format would make reading the file simple, there are some disadvantages. The key is that individual lines in the new catalog format are atomic units of storage and the files can be easily split and joined using line-by-line tools.
Reading Catalog Lines to JSON Objects
To read in the values of the catalog file we will open using a line-oriented data structure assuming that there is a newline at the end of each record. We can then just the json library's loads() function which will convert each line to a JSON object.
importos,jsonjson_file="objects.json"script_dir=os.path.dirname(__file__)# get a relative path to the script dirpath_to_catalog_file=script_dir+'/'+json_filef=open(path_to_catalog_file)lines=f.readlines()count=0# Convert each line to a JSON objectforlineinlines:line_in_json=json.loads(line)count+=1print(count,' ',end='')print(line_in_json)# the result is a Python dictionaryprint(line_in_json['name'])print("Name:",line_to_json["name"])print("Age:",line_to_json["age"])
# program to read a DonkeyCar Catalog Fileimportos,json# this program assumes that test.catalog is in the same directory as this script# get the direcotry that this script is runningscript_dir=os.path.dirname(__file__)# get a relative path to the script dirpath_to_catalog_file=script_dir+'/test.catalog'f=open(path_to_catalog_file)lines=f.readlines()count=0# Convert each line to a JSON objectforlineinlines:# each line as a JSON dictionary objectj=json.loads(line)count+=1print('\n\nline:',count)# print(j)print("Index:",j["_index"])print("Session:",j["_session_id"])print("Timestamp:",j["_timestamp_ms"])print("cam/image_array:",j["cam/image_array"])print("user/angle:",j["user/angle"])print("user/mode:",j["user/mode"])print("user/throttle:",j["user/throttle"])