import json
import numpy as np
import scipy.io as sio
import scipy.io as sio
import matplotlib.pyplot as plt
import shutil
import os
path = './annotated'
files = os.listdir(path)
mat_folder = './mat' # 新的.mat文件目录
# 检查是否存在.mat文件夹,若不存在则创建
if not os.path.exists(mat_folder):
os.makedirs(mat_folder)
for name in files:
if name.endswith('.json'):
print(name)
fp = open(os.path.join(path, name), 'r')
json_data = json.load(fp)
points_data = json_data['shapes']
points = []
for point in points_data:
points.append(point['points'][0])
sio.savemat(os.path.join(mat_folder, name.replace('.json','.mat')), {'annPoints':points, 'num':len(points)})
需要修改的就只有path,把path修改成你json文件所在的目录
运行完成,会生成mat文件夹,里面存放着你转换的.mat文件格式