关键点标注【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)})
        
  

你可能感兴趣的:(python)