forked from microsoft/CameraTraps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cct_json_to_filename_json.py
82 lines (56 loc) · 2.22 KB
/
cct_json_to_filename_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# cct_json_to_filename_json.py
#
# Converts a .json file in COCO Camera Traps format to a .json-formatted list of
# relative file names.
#
#%% Constants and environment
import json
import os
from itertools import compress
#%% Main function
def convertJsonToStringList(inputFilename,outputFilename=None,prepend='',bConfirmExists=False,
bForceForwardSlash=True,imageBase=''):
assert os.path.isfile(inputFilename), '.json file {} does not exist'.format(inputFilename)
if outputFilename is None:
outputFilename = inputFilename + '_images.json'
with open(inputFilename,'r') as f:
data = json.load(f)
images = data['images']
filenames = [im['file_name'] for im in images]
if bConfirmExists:
bValid = [False] * len(filenames)
for iFile,f in enumerate(filenames):
fullPath = os.path.join(imageBase,f)
if os.path.isfile(fullPath):
bValid[iFile] = True
nFilesTotal = len(filenames)
filenames = list(compress(filenames, bValid))
nFilesValid = len(filenames)
print('Marking {} of {} as valid'.format(nFilesValid,nFilesTotal))
filenames = [prepend + s for s in filenames]
if bForceForwardSlash:
filenames = [s.replace('\\','/') for s in filenames]
# json.dump(s,open(outputFilename,'w'))
s = json.dumps(filenames)
with open(outputFilename, 'w') as f:
f.write(s)
return s,outputFilename
#%% Command-line driver
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('inputFilename')
parser.add_argument('outputFilename')
args = parser.parse_args()
convertJsonToStringList(args.jsonFile,args)
if __name__ == '__main__':
main()
#%% Interactive driver
if False:
#%%
prepend = '20190430cameratraps/'
inputFilename = r"D:\wildlife_data\awc\awc_imageinfo.json"
outputFilename = r"D:\wildlife_data\awc\awc_image_list.json"
convertJsonToStringList(inputFilename,outputFilename,prepend=prepend,bConfirmExists=True,imageBase=r'D:\wildlife_data\awc')
print('Finished converting {} to {}'.format(inputFilename,outputFilename))