当前位置:   article > 正文

python编写labelme的json文件并且能够在labelme下打开使用_labelme的json标注文件 显示标注内容python

labelme的json标注文件 显示标注内容python

今天教大家使用Python来生成一个json文件,这个json文件可以使用labelme打开,如果有自动框猫的模型,可借鉴我的上篇文章链接如下:

python使用opencv对猫脸进行检测,并且框出猫脸_小琼带你轻松学编程的博客-CSDN博客

,实现自动给猫咪打标注。

导入所需要的库:

  1. import json
  2. import base64
  3. from PIL import Image

json库用来使字典转为json文件。

base64库是能够生成labelme打完标签生成的json文件中的imagDate中的数据即下图所示:

 from PIL import Image是为了读取图片和获取图片的宽高。

框和点的数据集:

  1. bbox = [[206.90909090909088, 210.5454545454545], [490.18181818181813, 520.3766233766233],
  2. [428.7272727272727, 85.09090909090907], [721.3506493506493, 395.7012987012987]]
  3. point = [[397.8181818181818, 390.5454545454545], [374.18181818181813, 305.090909090909],
  4. [423.27272727272725, 326.9090909090909], [359.6363636363636, 481.4545454545454],
  5. [277.8181818181818, 510.5454545454545], [308.7272727272727, 408.72727272727263],
  6. [281.45454545454544, 325.090909090909], [208.72727272727272, 255.99999999999994],
  7. [297.8181818181818, 245.09090909090907],
  8. [541.4545454545454, 290.5454545454545], [639.6363636363636, 254.18181818181813],
  9. [605.090909090909, 312.3636363636364], [466.9090909090909, 259.63636363636357],
  10. [437.81818181818176, 161.45454545454544], [517.8181818181818, 203.27272727272725],
  11. [594.1818181818181, 179.63636363636363], [641.4545454545454, 90.5454545454545],
  12. [666.9090909090909, 179.63636363636363], ]

原图如下:

数据集最终在labelme的呈现效果,即一个猫脸一个框,九个点(可根据需要自行修改):

 

读取图片,初始化字典cur_json_dict,将无关紧要的固定住:

  1. img_path = './1.jpg'
  2. img = Image.open(img_path)
  3. cur_json_dict = {
  4. "version": "4.5.6",
  5. "flags": {},
  6. "shapes": [
  7. ],
  8. }

将bbox(框)和points(点)按照labelme所需的要求写入字典中,bbox和points的样式可根据自己的需要修改:

  1. # bbox
  2. for i in range(int(len(bbox) / 2)):
  3. if len(bbox) / 2 >= 1:
  4. cur_json_dict['shapes'].append(
  5. {"label": "bbox", "points": bbox[i * 2:i * 2+ 2], 'group_id': f'{i}', "shape_type": "rectangle", "flags": {}})
  6. else:
  7. cur_json_dict['shapes'].append(
  8. {"label": "bbox", "points": bbox[i * 2:i * 2+ 2], 'group_id': 'NULL', "shape_type": "rectangle", "flags": {}})
  9. # point
  10. for i in range(int(len(point) / 9)):
  11. for j in range(9):
  12. if len(bbox) / 2 >= 1:
  13. cur_json_dict['shapes'].append(
  14. {"label": f"{j}", "points": point[i * 9 + j:i * 9 + j + 1], 'group_id': f'{i}',
  15. "shape_type": "point", "flags": {}})
  16. else:
  17. cur_json_dict['shapes'].append(
  18. {"label": f"{j}", "points": point[i * 9 + j:i * 9 + j + 1], 'group_id': 'NULL',
  19. "shape_type": "point", "flags": {}})

按照labelme生成的json文件的顺序imagePath,imageData,imageHeight,imageWidth写入字典中:

  1. cur_json_dict["imagePath"] = img_path
  2. cur_json_dict["imageData"] = str(base64.b64encode(open(img_path, "rb").read()))
  3. # delete 'b and '
  4. cur_json_dict["imageData"] = cur_json_dict["imageData"][2:-1]
  5. cur_json_dict["imageHeight"] = img.size[0]
  6. cur_json_dict["imageWidth"] = img.size[1]
  7. new_dict = cur_json_dict

最后生成和图片除后缀不一样的json文件,并且使用json.dumps()将字典cur_json_dict转为json:

  1. with open(img_path.split(img_path.split('.')[-1])[0]+'json', 'a+') as f:
  2. f.write(json.dumps(cur_json_dict))

运行前:

运行结果如下:

 使用labelme打开查看是否成功,查看结果如下:

 全部代码如下所示:

  1. import json
  2. import base64
  3. from PIL import Image
  4. # date
  5. bbox = [[206.90909090909088, 210.5454545454545], [490.18181818181813, 520.3766233766233],
  6. [428.7272727272727, 85.09090909090907], [721.3506493506493, 395.7012987012987]]
  7. point = [[397.8181818181818, 390.5454545454545], [374.18181818181813, 305.090909090909],
  8. [423.27272727272725, 326.9090909090909], [359.6363636363636, 481.4545454545454],
  9. [277.8181818181818, 510.5454545454545], [308.7272727272727, 408.72727272727263],
  10. [281.45454545454544, 325.090909090909], [208.72727272727272, 255.99999999999994],
  11. [297.8181818181818, 245.09090909090907],
  12. [541.4545454545454, 290.5454545454545], [639.6363636363636, 254.18181818181813],
  13. [605.090909090909, 312.3636363636364], [466.9090909090909, 259.63636363636357],
  14. [437.81818181818176, 161.45454545454544], [517.8181818181818, 203.27272727272725],
  15. [594.1818181818181, 179.63636363636363], [641.4545454545454, 90.5454545454545],
  16. [666.9090909090909, 179.63636363636363], ]
  17. img_path = './1.jpg'
  18. img = Image.open(img_path)
  19. cur_json_dict = {
  20. "version": "4.5.6",
  21. "flags": {},
  22. "shapes": [
  23. ],
  24. }
  25. # bbox
  26. for i in range(int(len(bbox) / 2)):
  27. if len(bbox) / 2 >= 1:
  28. cur_json_dict['shapes'].append(
  29. {"label": "bbox", "points": bbox[i * 2:i * 2+ 2], 'group_id': f'{i}', "shape_type": "rectangle", "flags": {}})
  30. else:
  31. cur_json_dict['shapes'].append(
  32. {"label": "bbox", "points": bbox[i * 2:i * 2+ 2], 'group_id': 'NULL', "shape_type": "rectangle", "flags": {}})
  33. # point
  34. for i in range(int(len(point) / 9)):
  35. for j in range(9):
  36. if len(bbox) / 2 >= 1:
  37. cur_json_dict['shapes'].append(
  38. {"label": f"{j}", "points": point[i * 9 + j:i * 9 + j + 1], 'group_id': f'{i}',
  39. "shape_type": "point", "flags": {}})
  40. else:
  41. cur_json_dict['shapes'].append(
  42. {"label": f"{j}", "points": point[i * 9 + j:i * 9 + j + 1], 'group_id': 'NULL',
  43. "shape_type": "point", "flags": {}})
  44. cur_json_dict["imagePath"] = img_path
  45. cur_json_dict["imageData"] = str(base64.b64encode(open(img_path, "rb").read()))
  46. # delete 'b and '
  47. cur_json_dict["imageData"] = cur_json_dict["imageData"][2:-1]
  48. cur_json_dict["imageHeight"] = img.size[0]
  49. cur_json_dict["imageWidth"] = img.size[1]
  50. new_dict = cur_json_dict
  51. with open(img_path.split(img_path.split('.')[-1])[0]+'json', 'a+') as f:
  52. f.write(json.dumps(cur_json_dict))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/257240?site
推荐阅读
相关标签
  

闽ICP备14008679号