码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • COCO格式json切分为labelme可识别json


    coco数据格式相关内容参考之前博客

    切分的关键在于将coco_json中的annotation信息转化为labelme中shape的坐标信息

    labelme中shape需要的是多边形的点坐标,存储格式为[[x1,y1], [x2,y2]......]

    1. import os
    2. import json
    3. import pycocotools.mask as mask_utils
    4. from pycocotools.coco import COCO
    5. import cv2
    6. json_path = ''
    7. # 读取json(这里重复读取了,懒得改)
    8. with open(json_path, 'r') as f:
    9. coco_data = json.load(f)
    10. coco = COCO(json_path)
    11. output_json = ''
    12. os.makdirs(output_json, exist_ok=True)
    13. for image_data in coco_data['images']:
    14. image_id = image_data['id']
    15. image_file_name = image_data['file_name']
    16. # 创建labelme的json数据结构,这里也可以读一个labelme的json直接替换
    17. labelme_data = {
    18. 'version': '4.5.7',
    19. 'flags':{},
    20. 'shapes':[],
    21. 'imagePath':image_file_name,
    22. 'imageData':None,
    23. 'imageHeight':image_data['height'],
    24. 'imageWidth':image_data['width']
    25. }
    26. # 查找当前图像的标签数据
    27. for annotation in coco_data['annotations']:
    28. if annotation['image_id'] == image_id:
    29. category_id = annotation['category_id']
    30. # 构建labelme多边形点
    31. rle = coco.annToRLE(annotation)
    32. mask = mask_utils.decode(rle)
    33. mask[mask == 1] = 255
    34. # mask轮廓
    35. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    36. for contour in contours:
    37. if len(contour) < 3:
    38. continue
    39. seg_xy = [[int(x), int(y)] for x,y in contour.reshape(-1, 2)]
    40. # 创建labelme的shape结构
    41. shape = {
    42. 'label': str(category_id),
    43. 'points': seg_xy,
    44. 'group_id': None,
    45. 'shape_type': 'polygon',
    46. 'flags': {}
    47. }
    48. # 将shape添加到labelme结构中
    49. labelme_data['shapes'].append(shape)
    50. # 将labelme结构写入json文件中
    51. labelme_json_file = os.path.join(output_json, os.path.splitext(image_file_name)[0]+'.json')
    52. with open(labelme_json_file, 'w') as labelme_f:
    53. json.dump(labelme_data, labelme_f, indent=2)

  • 相关阅读:
    数据交互工具 -- HUE
    让DAO再次伟大
    Excel_字母数字混合排序(数字不符合预期)的一种解决方法
    matlab 分数阶混沌系统的完全同步控制
    Android.mk 中覆盖应用包名
    【计算机毕业设计】会议发布与预约系统小程序
    LVS-DR模式 +keepalived
    园子的商业化努力-困境求助:开设捐助通道
    掷骰子的多线程应用程序2基于互斥量的线程同步(复现《Qt C++6.0》)
    智能硬件开发怎么做?机智云全套自助式开发工具助力高效开发
  • 原文地址:https://blog.csdn.net/hesongzefairy/article/details/103076037
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号