-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert_v2_to_v1.py
137 lines (117 loc) · 4.65 KB
/
convert_v2_to_v1.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
FILE:
convert_v2_to_v1.py
AUTHOR:
naisy (https://github.com/naisy)
SUMMARY:
Convert data format from donkeycar 4.3 tub_v2 to donkeycar 3.1 tub_v1.
LICENSE:
MIT License
Copyright (c) 2021 naisy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import json
from collections import OrderedDict
import shutil
def delete_images(dir, delete_id_list):
for i in delete_id_list:
file = f'{i}_cam_image_array_.jpg'
file_path = os.path.join(dir, file)
delete_image(file_path)
def delete_image(file_path):
if os.path.exists(file_path):
try:
os.remove(file_path)
print(f'delete {file_path} - ok')
except Exception as e:
print(f'delete {file_path} - error!')
else:
print(f'delete {file_path} - ng')
def mkdir(dir):
if os.path.exists(dir):
shutil.rmtree(dir)
os.mkdir(dir)
def copy_to_v1(v2_data_dir, v1_data_dir, catalog_list):
mkdir(v1_data_dir)
record_i = 1
for catalog in catalog_list:
file_path = os.path.join(v2_data_dir, catalog)
f = open(file_path, 'r')
v2_records = f.readlines()
for v2_record in v2_records:
try:
v1_record = OrderedDict()
json_data = json.loads(v2_record)
v1_record['cam/image_array'] = f'{record_i}_cam_image_array_.jpg'
v1_record['user/angle'] = json_data['user/angle']
v1_record['user/throttle'] = json_data['user/throttle']
if 'pilot/angle' in json_data.keys():
v1_record['pilot/angle'] = json_data['pilot/angle']
if 'pilot/throttle' in json_data.keys():
v1_record['pilot/throttle'] = json_data['pilot/throttle']
v1_record['user/mode'] = json_data['user/mode']
v1_record['milliseconds'] = json_data['_timestamp_ms']
file = f'record_{record_i}.json'
file_path = os.path.join(v1_data_dir, file)
writeJson(v1_record, file_path)
# copy image
src = os.path.join(v2_data_dir, 'images', json_data['cam/image_array'])
dst = os.path.join(v1_data_dir, v1_record['cam/image_array'])
shutil.copyfile(src, dst)
print(f'{file_path} - saved')
record_i += 1
except FileNotFoundError as e1:
print(f'skip deleted record: {catalog} - {json_data}')
except Exception as e2:
print(f'skip broken record: {catalog} - {json_data}')
print(e2)
def read_manifest(dir):
'''
manifest.json is not json file.
so we need to pick up correct lines.
'''
file = 'manifest.json'
file_path = os.path.join(dir, file)
f = open(file_path, 'r')
datalist = f.readlines()
return datalist
def writeJson(json_data, file_path):
#json_string = json.dumps(json_data)
# If the file name exists, write a JSON string into the file.
if file_path:
# Writing JSON data
with open(file_path, 'w') as f:
json.dump(json_data, f)
def main():
'''
1. delete image
2. copy exist image and value
'''
v2_data_dir = './data'
manifest_lines = read_manifest(v2_data_dir)
json_data = json.loads(manifest_lines[4])
catalog_list = sorted(json_data['paths'])
delete_id_list = sorted(json_data['deleted_indexes'])
image_dir = os.path.join(v2_data_dir, 'images')
delete_images(image_dir, delete_id_list)
v1_data_dir = './data_v1'
copy_to_v1(v2_data_dir, v1_data_dir, catalog_list)
if __name__ == '__main__':
main()