1. import sys
  2. import json
  3. import cv2
  4. import os
  5. import shutil
  6.  
  7. dataset = { "info": {
  8. "description": "XXX in COCO dataset.",
  9. "url": "",
  10. "version": "1.0",
  11. "year": ,
  12. "contributor": "aimhabo",
  13. "date_created": "2019-03-25"},
  14. "images":[],
  15. "annotations":[],
  16. "categories": [
  17. {"supercategory:": "person", "id": 1, "name": "person"},
  18. {"supercategory:": "car", "id": 2, "name": "car"},
  19. {"supercategory:": "truck", "id": 3, "name": "truck"},
  20. {"supercategory:": "bus", "id": 4, "name": "bus"}
  21. ]
  22. }
  23.  
  24. datapath = "scripts/images"
  25. annopath = "scripts/labels"
  26. trainsetfile = "scripts/trainimage.list"
  27. outputpath = "scripts"
  28. phase = "XXXTrainCOCO"
  29. classes = {"background": 0, "person": 1, "car": 2, "truck": 3, "bus": 4}
  30.  
  31. with open(trainsetfile) as f:
  32. count = 1
  33. cnt = 0
  34. annoid = 0
  35. for line in f:
  36. cnt += 1
  37. line = line.strip()
  38.  
  39. name,ext = os.path.basename(line).split('.')
  40.  
  41. imagepath = os.path.join(datapath, name + ".jpg")
  42. # no obstacle currently drop it
  43. txtpath = os.path.join(annopath, name + ".txt")
  44. if not os.path.exists(txtpath):
  45. print txtpath
  46. continue
  47.  
  48. im = cv2.imread(imagepath)
  49.  
  50. height, width, channels = im.shape
  51.  
  52. if cnt % 1000 == 0:
  53. print cnt
  54.  
  55. dataset["images"].append({"license": 5, "file_name": line, "coco_url": "local", "height": height, "width": width, "flickr_url": "local", "id": cnt})
  56. with open(txtpath) as annof:
  57. annos = annof.readlines()
  58.  
  59. for ii, anno in enumerate(annos):
  60. parts = anno.strip().split(' ')
  61. if len(parts) is not 5:
  62. continue
  63. class_id = int(parts[0])
  64. x = float(parts[1])
  65. y = float(parts[2])
  66. w = float(parts[3])
  67. h = float(parts[4])
  68. if parts[0].find("group") == -1:
  69. iscrowd = 0
  70. else:
  71. iscrowd = 1
  72.  
  73. annoid = annoid + 1
  74.  
  75. class_id += 1 # start from 1 instead of 0
  76. x1 = int((x-w/2)*width)
  77. y1 = int((y-h/2)*height)
  78. wid = int(w*width)
  79. hei = int(h*height)
  80.  
  81. dataset["annotations"].append({
  82. "segmentation": [],
  83. "iscrowd": iscrowd,
  84. "area": wid * hei,
  85. "image_id": cnt,
  86. "bbox": [x1, y1, wid, hei],
  87. "category_id": class_id,
  88. "id": annoid
  89. })
  90. count += 1
  91.  
  92. json_name = os.path.join(outputpath, "{}.json".format(phase))
  93.  
  94. with open(json_name, 'w') as f:
  95. json.dump(dataset, f)

darknet标签转化为COCO标签的更多相关文章

  1. VOC标签转化为YOLO标签

    参考darknet自带的voc_label.py import xml.etree.ElementTree as ET import pickle import os from os import l ...

  2. 把 html标签转化为 html标签代码

    HttpUtility.HtmlEncode(table.Rows[0]["footnote"].ToString());

  3. JSP内置标签 JSP中JavaBean标签 JSP开发模式 EL和JSTL快速入门

    2 JSP内置标签(美化+业务逻辑)   1)为了取代<%%>脚本形式,使用JSP标签/JSP动作,目的:与JSP页面的美化,即JSP面页都是由标签组成,不再有其它的内容   2)JSP内 ...

  4. 初学HTML 常见的标签(三) 插入类标签

    第三篇博客, 这次说的是插入链接类标签, 我们平常在网页中经常能看到蓝色的链接类标签, 或者是一张图片, 一个电邮, 这些都是插入链接类的标签起的作用. <a></a>链接标签 ...

  5. JSTL标签库之核心标签

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...

  6. HTML学习笔记——块级标签、行级标签、图片标签

    1>块级标签.行级标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  7. WebApp 里Meta标签大全,webappmeta标签大全

    1.先说说mate标签里的viewport: viewport即可视区域,对于桌面浏览器而言,viewport指的就是除去所有工具栏.状态栏.滚动条等等之后用于看网页的区域.对于传统WEB页面来说,9 ...

  8. meta标签详解(meta标签的作用)///////////////////////////转

    meta标签详解(meta标签的作用) 很多人却忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧   ...

  9. <script>标签应该放到</body>标签之前

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:贺师俊 链接:http://www.zhihu.com/question/20027966/answer/13727164 ...

随机推荐

  1. vue子父组件传值

    https://blog.csdn.net/weixin_38888773/article/details/81902789 https://blog.csdn.net/jsxiaoshu/artic ...

  2. 棋盘n皇后问题-递归

    题目:在n*n的棋盘上,放n个皇后,互不攻击(不可在同行/列/对角线) 分析:将棋盘抽象成一个一维数组[0,1,2......,n*n-1],x=~~(i/n)取整,y=i%n;         de ...

  3. 关于ORA-12154: TNS:could not resolve the connect identifier specified的问题

    问题:net configuration assistant测试连接可以连接.但是PLSQL连接不了,报ORA-12154: TNS:could not resolve the connect ide ...

  4. 六十一.常用组件 、 Kafka集群 、 Hadoop高可用

    1.Zookeeper安装搭建Zookeeper集群并查看各服务器的角色停止Leader并查看各服务器的角色 1.1 安装Zookeeper1)编辑/etc/hosts ,所有集群主机可以相互 pin ...

  5. 十一.简单MariaDB数据库的管理

    默认端口:3306 1.安装 ]# yum -y install mariadb-server ]# systemctl restart mariadb 2.基本操作 ]# mysql #进入 ]&g ...

  6. BZOJ 1576 树剖+LCT

    题意:给定一张图,保证 $1$ 号点到其他所有点的最短路径是唯一的,求:对于点 $i$,不经过 $1$ 到 $i$ 最短路径上最后一条边的最短路. 题解:可以先建出最短路树,然后枚举每一条非树边. 对 ...

  7. learning scala type alise

    How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...

  8. learning scala pattern matching 02

    code package com.aura.scala.day01 object patternMatching02 { def main(args: Array[String]): Unit = { ...

  9. visual studio2015窗体中控件的属性中文说明不见了

    右击属性窗口,然后选中好说明就ok了.

  10. 在vscode里面光标跳跃的问题

    https://tieba.baidu.com/p/5242680781?red_tag=2439355674 卸载 JS-CSS-HTML Formatter就可以了.卸载完还要加载一下哦