乐趣区

关于python:关键点标注json转mat

json 中的样子:

{
  "version": "4.5.7",
  "flags": {},
  "shapes": [
    {
      "label": "wood",
      "points": [
        [
          739.3162393162394,
          482.0512820512821
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}},
    {
      "label": "wood",
      "points": [
        [
          758.1196581196582,
          458.11965811965814
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}},
    
  ],
  "imagePath": "..\\img\\141.jpg",
  "imageData": 
          "
          .......

          图片的 base64 编码,十分长

          ........
          ","imageHeight": 1080,"imageWidth": 1920
}

重要的信息就是外面的 points 坐标,其余的都用不到。提取办法如下:

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 = 'D:/test/wood/yi'
files = os.listdir(path)
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', name.replace('.json','.mat')), {'annPoints':points, 'num':len(points)})
        
  

退出移动版