我按照 Rafael 的建议编写了一些代码来从 JSON 迁移到 cvs。我会把它放在这里以防有人想使用它。这是针对分析视频的情况,因此每个“图像”都是视频中的一帧。
import json
import csv
# with and height of the video
WIDTH = 1920
HEIGHT = 1080
with open('~/detection_results.json', encoding='latin-1') as json_file:
data = json.load(json_file)
# open csv file
csv_file_to_make = open('~/detection_results.csv', 'w', newline='\n')
csv_file = csv.writer(csv_file_to_make)
# write the header
# NB x and y values are relative
csv_file.writerow(['Frame ID',
'class',
'x_center',
'y_center',
'bb_width',
'bb_heigth',
'confidence'])
for frame in data:
frame_id = frame['frame_id']
instrument = ""
center_x = ""
center_y = ""
bb_width = ""
bb_height = ""
confidence = ""
if frame['objects'] == []:
csv_file.writerow([frame_id,
class,
center_x,
center_y,
bb_width,
bb_height,
confidence
])
else:
for single_detection in frame['objects']:
instrument = single_detection['name']
center_x = WIDTH*single_detection['relative_coordinates']['center_x']
center_y = HEIGHT*single_detection['relative_coordinates']['center_y']
bb_width = WIDTH*single_detection['relative_coordinates']['width']
bb_height = HEIGHT*single_detection['relative_coordinates']['height']
confidence = single_detection['confidence']
csv_file.writerow([frame_id,
class,
center_x,
center_y,
bb_width,
bb_height,
confidence
])
csv_file_to_make.close()
希望这会有所帮助!如果您看到优化此代码的解决方案,当然也欢迎 :)