<p>我遵循Rafael的建议,编写了一些代码,将JSON转换为cvs。我会把它放在这里,以防有人想用。这适用于分析视频的情况,因此每个“图像”都是视频中的一帧</p>
<pre><code>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()
</code></pre>
<p>希望这有帮助!如果您看到优化此代码的解决方案,当然也欢迎:)</p>