import sys
import json
import cv2
import os
import shutil dataset = { "info": {
"description": "XXX in COCO dataset.",
"url": "",
"version": "1.0",
"year": ,
"contributor": "aimhabo",
"date_created": "2019-03-25"},
"images":[],
"annotations":[],
"categories": [
{"supercategory:": "person", "id": 1, "name": "person"},
{"supercategory:": "car", "id": 2, "name": "car"},
{"supercategory:": "truck", "id": 3, "name": "truck"},
{"supercategory:": "bus", "id": 4, "name": "bus"}
]
} datapath = "scripts/images"
annopath = "scripts/labels"
trainsetfile = "scripts/trainimage.list"
outputpath = "scripts"
phase = "XXXTrainCOCO"
classes = {"background": 0, "person": 1, "car": 2, "truck": 3, "bus": 4} with open(trainsetfile) as f:
count = 1
cnt = 0
annoid = 0
for line in f:
cnt += 1
line = line.strip() name,ext = os.path.basename(line).split('.') imagepath = os.path.join(datapath, name + ".jpg")
# no obstacle currently drop it
txtpath = os.path.join(annopath, name + ".txt")
if not os.path.exists(txtpath):
print txtpath
continue im = cv2.imread(imagepath) height, width, channels = im.shape if cnt % 1000 == 0:
print cnt dataset["images"].append({"license": 5, "file_name": line, "coco_url": "local", "height": height, "width": width, "flickr_url": "local", "id": cnt})
with open(txtpath) as annof:
annos = annof.readlines() for ii, anno in enumerate(annos):
parts = anno.strip().split(' ')
if len(parts) is not 5:
continue
class_id = int(parts[0])
x = float(parts[1])
y = float(parts[2])
w = float(parts[3])
h = float(parts[4])
if parts[0].find("group") == -1:
iscrowd = 0
else:
iscrowd = 1 annoid = annoid + 1 class_id += 1 # start from 1 instead of 0
x1 = int((x-w/2)*width)
y1 = int((y-h/2)*height)
wid = int(w*width)
hei = int(h*height) dataset["annotations"].append({
"segmentation": [],
"iscrowd": iscrowd,
"area": wid * hei,
"image_id": cnt,
"bbox": [x1, y1, wid, hei],
"category_id": class_id,
"id": annoid
})
count += 1 json_name = os.path.join(outputpath, "{}.json".format(phase)) with open(json_name, 'w') as f:
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. KM 最大权匹配 UVA 1411/POJ 3565

    #include <bits/stdc++.h> using namespace std; inline void read(int &num) { char ch; num = ...

  2. Vue IE11 报错 Failed to generate render function:SyntaxError: 缺少标识符 in

    报错截图: 查了篇文章(https://blog.csdn.net/weixin_42018057/article/details/81385121),遇到的情况跟文章里描述的类似,他提供的方法是:需 ...

  3. IDEA控制台中文乱码解决

    关于IDEA中文乱码的解决方法,如下. 1.打开idea安装目录,选择 打开文件,末尾添加-Dfile.encoding=UTF-8 2.打开IntelliJ IDEA>File>Sett ...

  4. 用python写一个GitHub Trending Api

    GitHub 给了开发者相当丰富的 API 接口 https://developer.github.com/v3/,包括认证,搜索,活动等接口,但就是没有提供获取 Trending 的接口.因此,需要 ...

  5. URL中的String参数问题

    测试一个查询数据的接口,类似这样的URL:.../search?type=Astring,在浏览器中输入URL获取到的数据为空,但通过其它方式确认数据库中确实已有数据,怀疑是接口实现问题.找接口实现的 ...

  6. hihoCoder 2 * problem

    1792 模拟,转化为二进制后逐位比较 1819 线段树维护区间加 维护每个数加了多少 每次弹出栈顶元素后栈顶位置注意清空 1792 #include <iostream> #includ ...

  7. 在vultr中安装k8s测试

    vultr 安装k8s *** 如果国内访问 k8s.gcr.io 很慢,或者无法访问 *** 在应用yaml文件创建资源时,将文件中镜像地址进行内容替换即可: 将k8s.gcr.io替换为 regi ...

  8. 在win10环境下配置spark和scala

    在这里配置的是在命令行下运行spark的环境用来学习,最后结果如下,可运行简单的代码. 0.jdk.scala和spark的版本问题 有关版本如官网所示,我想要强调的是spark至今并不支持jdk11 ...

  9. 解决JavaWeb项目报错:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    明明有项目和文件,而且别的项目都可以运行,偏偏这个不能用,报错The origin server did not find a current representation for the targe ...

  10. IDEA上安装Scala环境执行测试

    1.安装scala IDEA下载Scala插件 IDEA->setting->Plugin->搜索Scala->选择Scala,然后, 2.删除火狐软件 sudo apt-ge ...