- CVAT导出的标注文件只会记录标注图片的图片名,如果一张图片没有任何标注,导出文件不能知道是哪张图片
- 如果不想去获取标注图片的文件名,可以该方法
- 图片是用 cv2 抽取命名的,一秒取一帧。命名规则:年_月_日_时_分_秒
- 比如:第一帧是:
2019_10_19_11_46_22_543.jpg
;第二帧就是: 2019_10_19_11_46_23_543.jpg
import json
import codecs
import os
from datetime import datetime, timedelta
root_path = input('请输入 json 所在地址:')
def get_frame(f_name, n_id):
seconds = 1
y,m, d, h, mi, se = [int(v)for v in f_name.replace('_', '-').split('-')[:6]]
jpg = '_' + f_name.split('_')[6]
dt = datetime(y,m, d, h, mi, se)
dt_id = str(dt + timedelta(seconds=seconds*(n_id))).replace(' ','_').replace(':','_')
None_jpg = dt_id + jpg
return None_jpg
def get_file():
files = os.listdir(root_path)
name = open(os.path.join(root_path, 'jpg_list.txt'), 'w',encoding='utf-8')
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root_path, file)
json_file = codecs.open(file_path, 'r', 'utf-8')
json_data = json.load(json_file)
json_jpg = json_data['images']
if json_jpg == []:
continue
fir_name = json_jpg[0]['file_name']
fir_id = json_jpg[0]['id']
i = 0
for jpg_file in json_jpg:
jpg_id = jpg_file['id']
while i != jpg_id:
now_id = i - fir_id
None_jpg = get_frame(fir_name, now_id)
seq = file, str(i), None_jpg
name.write('\t'.join(seq) + '\n')
i += 1
seq = file, str(jpg_id), jpg_file['file_name']
name.write('\t'.join(seq) + '\n')
i += 1
name.close()
get_file()