废话不说,直接上代码。

// cornell 2 coco
// An highlighted block
# -*- coding: UTF-8 -*-
import cv2
import json
import sys
from juli import Point, Line
from jiaodian_zhongxin import Point_1, Line_1
import math
import glob
import PIL.Image
import os,sys
# # zixiu #######
src_img_dir = "/home/dms/Documents/Cornell_Grasp_Detection_Dataset/one_label_annotation/jpg_xml/test2017/"src_txt_dir = "/home/dms/Documents/Cornell_Grasp_Detection_Dataset/pos_label"
img_Lists = glob.glob(src_img_dir + '/*.jpg')
print('444' , img_Lists)
img_basenames = []  # e.g. 100.jpg
for item in img_Lists:img_basenames.append(os.path.basename(item))print(os.path.basename(item) + '-------------')
img_names = []  # e.g. 100
for item in img_basenames:temp1, temp2 = os.path.splitext(item)img_names.append(temp1[0:7])print(temp1[0:7] + '====' + temp2)
print('********************************************')########################################################################################## process bar
def process_bar(count, total, status=''):bar_len = 60filled_len = int(round(bar_len * count / float(total)))percents = round(100.0 * count / float(total), 1)bar = '=' * filled_len + '-' * (bar_len - filled_len)sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))sys.stdout.flush()#root_path = "data_with_box_example/"
images, categories, annotations = [], [], []category_dict = {"people": 1}for cat_n in category_dict: categories.append({"supercategory": "", "id": category_dict[cat_n], "name": cat_n})
img_id = 0
for img in img_names:try:gt = open(src_txt_dir + '/' + img + 'cpos.txt').read().splitlines()  # 把txt文件里每一行提取出来,我的txt有两行lines = len(open(src_txt_dir + '/' + img + 'cpos.txt').readlines())print('1********************************************', gt[0], 'lines = ', lines)except:continue  # 跳过这次循环,进入下一张图片循环with open(src_txt_dir + '/' + img + 'cpos.txt', 'r') as f:# img_id = 0anno_id_count = 0count = 1total = 100for line in range(1):process_bar(count, total)count += 1# line = line.split(' ')# img_name = line[0].replace('/', '_')img_name = img# bbox_num = int(line[1])bbox_num = 1# img_cv2 = cv2.imread(root_path + img_name)img_cv2 = cv2.imread(src_img_dir + '/' + img + 'r.jpg')[height, width, _] = img_cv2.shape# images infoimages.append({"file_name": img_name + 'r.jpg', "height": height, "width": width, "id": img_id})"""annotation info:id : anno_id_countcategory_id : category_idbbox : bboxsegmentation : [segment]area : areaiscrowd : 0image_id : image_id"""category_id = category_dict["people"]for i in range(1):spt1 = gt[i].split(' ')  # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。左上角spt2 = gt[i + 1].split(' ')  # 左下spt3 = gt[i + 2].split(' ')  # 右上spt4 = gt[i + 3].split(' ')  # 右下x1 = float(spt1[0])y1 = float(spt1[1])x2 = float(spt2[0])y2 = float(spt2[1])x3 = float(spt3[0])y3 = float(spt3[1])x4 = float(spt4[0])y4 = float(spt4[1])print('x1:=', x1, 'y1:=', y1, 'x2:=', x2, 'y2:=', y2, 'x3:=', x3, 'y3:=', y3, 'x4:=', x4, 'y4:=', y4)y1 = float(spt1[1])x2 = float(spt2[0])y2 = float(spt2[1])x3 = float(spt3[0])y3 = float(spt3[1])x4 = float(spt4[0])y4 = float(spt4[1])print('x1:=', x1, 'y1:=', y1, 'x2:=', x2, 'y2:=', y2, 'x3:=', x3, 'y3:=', y3, 'x4:=', x4, 'y4:=', y4)l1 = Line(x1, y1, x2, y2)l2 = Line(x1, y1, x4, y4)l3 = Line(x1, y1, x3, y3)l4 = Line(x2, y2, x3, y3)l5 = Line(x2, y2, x4, y4)l6 = Line(x3, y3, x4, y4)len_line = [l1.getlen(), l2.getlen(), l3.getlen(), l4.getlen(), l5.getlen(), l6.getlen()]len_line.sort(reverse=True)print('len_line:=', len_line)print('l1:=', l1.getlen(), 'l2:=', l2.getlen(), 'l3:=', l3.getlen(), 'l4:=', l4.getlen(), 'l5:=', l5.getlen(), 'l6:=', l6.getlen())zhongixndian_zuobiao = []if (l1.getlen() == len_line[0]) or  (l1.getlen() == len_line[1]):duijiaoxian = Line_1(x1, y1, x2, y2, x3, y3, x4, y4)elif (l2.getlen() == len_line[0]) or (l2.getlen() == len_line[1]):duijiaoxian = Line_1(x1, y1, x4, y4, x2, y2, x3, y3)elif (l3.getlen() == len_line[0]) or (l3.getlen() == len_line[1]):duijiaoxian = Line_1(x1, y1, x3, y3, x2, y2, x4, y4)print('duijiaoxian:=',duijiaoxian)zhongixndian_zuobiao = duijiaoxian.cross_point()print('zhongixndian_zuobiao:=',zhongixndian_zuobiao)#x1 = float(line[i * 5 + 3])#y1 = float(line[i * 5 + 4])#x2 = float(line[i * 5 + 3]) + float(line[i * 5 + 5])#y2 = float(line[i * 5 + 4]) + float(line[i * 5 + 6])#width = float(line[i * 5 + 5])#height = float(line[i * 5 + 6])width = (len_line[2])print('width:=', len_line[2])height = (len_line[4])print('height:=', len_line[4])x1_zuoshang = float(zhongixndian_zuobiao[0] - width/2)y1_zuoshang = float(zhongixndian_zuobiao[1] - height/2)x2_zuoshang = float(zhongixndian_zuobiao[0] + width/2)y2_zuoshang = float(zhongixndian_zuobiao[1] + height/2)bbox = [math.ceil(x1_zuoshang), math.ceil(y1_zuoshang), math.ceil(width), math.ceil(height)]segment = [math.ceil(x1_zuoshang), math.ceil(y1_zuoshang), math.ceil(x2_zuoshang), math.ceil(y1_zuoshang), math.ceil(x2_zuoshang), math.ceil(y2_zuoshang), math.ceil(x1_zuoshang), math.ceil(y2_zuoshang)]#area = math.ceil(width * height)area = math.ceil(width) * math.ceil(height)anno_info = {'id': anno_id_count, 'category_id': category_id, 'bbox': bbox, 'segmentation': [segment],'area': area, 'iscrowd': 0, 'image_id': img_id}annotations.append(anno_info)anno_id_count += 1img_id = img_id + 1all_json = {"images": images, "annotations": annotations, "categories": categories}with open("instances_test2017.json", "w") as outfile:json.dump(all_json, outfile)下面试juli 子程序 `内联代码片`。
下面展示一些 `内联代码片`。

``

// juli
// An highlighted block
import mathclass Point():def __init__(self, x1, y1, x2, y2):self.x1 = x1self.y1 = y1self.x2 = x2self.y2 = y2class Line(Point):def __init__(self, x1, y1, x2, y2):super().__init__(x1, y1, x2, y2)def getlen(self):changdu = math.sqrt(math.pow((self.x1 - self.x2), 2) + math.pow((self.y1 - self.y2), 2))return changdu
下面展示一些 `内联代码片`。

// jiaodian_zhongxin.py

``

// An highlighted block
import math
class Point_1():def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4):self.x1 = x1self.y1 = y1self.x2 = x2self.y2 = y2self.x3 = x3self.y3 = y3self.x4 = x4self.y4 = y4class Line_1(Point_1):def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4):super().__init__(x1, y1, x2, y2, x3, y3, x4, y4)def cross_point(self):#计算交点函数x1=self.x1    # 取四点坐标y1=self.y1x2=self.x2y2=self.y2x3=self.x3y3=self.y3x4=self.x4y4=self.y4k1=(y2-y1)*1.0/((x2-x1) + 0.001)#计算k1,由于点均为整数,需要进行浮点数转化b1=y1*1.0-x1*k1*1.0#整型转浮点型是关键if (x4-x3)==0:#L2直线斜率不存在操作k2=Noneb2=0else:k2=(y4-y3)*1.0/(x4-x3)#斜率存在操作b2=y3*1.0-x3*k2*1.0if k2==None:x=x3x = math.ceil(x)else:x=(b2-b1)*1.0/(k1-k2)x = math.ceil(x)y=k1*x*1.0+b1*1.0y = math.ceil(y)return [x,y]

Cornell datasets转coco .json格式相关推荐

  1. labelme json转换为coco json 格式 包含area面积

    reference 提供了转换代码,需要改写部分 https://github.com/fengzhongyouxia/TensorExpand/tree/master/TensorExpand/Ob ...

  2. voc定位数据xml转coco数据集格式json

    代码里面集合了python处理xml的精髓 import xml.etree.ElementTree as ET import os import jsoncoco = dict() coco['im ...

  3. labelme标注文件转coco json,coco json转yolo txt格式,coco json转xml, labelme标注文件转分割,boxes转labelme json

    参考:https://github.com/wkentaro/labelme 一.labelme标注文件转coco json 1.标注时带图片ImageData信息,将一个文件夹下的照片和labelm ...

  4. 100种目标检测数据集【voc格式yolo格式json格式coco格式】+YOLO系列算法源码及训练好的模型

    提示:本文介绍并分享了应用于各行业.各领域非常有用的目标检测数据集(感谢您的关注+三连,数据集持续更新中-),其中绝大部分数据集作者已应用于各种实际落地项目,数据集整体质量好,标注精确,数据的多样性充 ...

  5. voc数据集格式转换为coco数据集格式+修改xml格式文件

    voc数据集格式转换为coco格式+修改xml格式文件中部分内容 voc数据集格式→coco数据集格式 修改xml格式文件中部分内容 voc数据集格式→coco数据集格式 下面这份代码只需修改文件所在 ...

  6. VOC/YOLO/COCO数据集格式转换及LabelImg/Labelme/精灵标注助手Colabeler标注工具介绍

    数据集格式:VOC(xml).YOLO(txt).COCO(json) 本文主要对Label格式,以及LabelImg.Labelme.精灵标注助手Colabeler这常用的三种数据标注软件进行介绍. ...

  7. OpenMMLab-AI实战营第二期——相关1. COCO数据集格式和pycocotools使用(目标检测方向)

    文章目录 1. COCO数据集 1.1 COCO数据集介绍 1.2 COCO数据集格式 1.2.1 常见目标检测数据格式 1.2.2 COCO数据集文件结构及标注文件说明 1.2.3 RLE格式 1. ...

  8. 数据分析:JSON格式数据

    前言 最近使用tensorflow入门深度学习,找到了一个风格转移图片的神经网络感觉还蛮好玩风格转移,想要自己做点儿其他风格的模型,但是尴尬的发现,它需要使用MS coco数据集,下载地址为ms co ...

  9. COCO 数据集格式及mmdetection中的转换方法

    COCO 数据集格式及mmdetection中的转换方法 COCO格式 CV中的目标检测任务不同于分类,其标签的形式稍为复杂,有几种常用检测数据集格式,本文将简要介绍最为常见的COCO数据集的格式. ...

最新文章

  1. docker 启动sqlserver_在Docker上安装MSSQL(SQL Server)
  2. winfrom里面使用类似于table的合并的控件_Flink集成iceberg数据湖之合并小文件
  3. Wannafly挑战赛29题解
  4. python3.8新特性 逻辑表达式_python 3.8 新特性
  5. apollo数据库安装与常见错误说明
  6. shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
  7. Codeforces 67A【模拟】
  8. MicroStation V8i简体中文版中文字体乱码解决办法
  9. codeigniter_如何在浏览器中查看CodeIgniter日志文件
  10. bootstrap项目实例_精选开源SpringBoot项目:涵盖权限、搜索、秒杀、支付!
  11. 生成器(generator)
  12. SlickEdit完美解析Qt4
  13. autojs之快捷键
  14. cdr怎么转plt_win10系统下cdr格式文本转plt格式文本怎么做
  15. 支付宝小程序开发笔记
  16. php怎么创建以太坊钱包地址,php如何调用以太坊JSON-RPC接口创建钱包
  17. 阅读《Android 从入门到精通》(10)——单项选择
  18. Eclipse官网下载
  19. os.path.dirname(path)
  20. 图片可以登记版权吗?

热门文章

  1. c51单片机c语言音乐播放器,51单片机做音乐播放器的问题
  2. STM32的RTC晶振不起振的原因及解决方法
  3. Linux之Rsync+inotify数据同步
  4. 淘宝客的 API 接口 http://www.dahuariji.com/post/42.html
  5. 气压计bmp388性能测试
  6. Spring Batch之读数据库(三十一)
  7. (cocos2d-js游戏)测试你的反应速度----------基本逻辑(上)
  8. 科幻3D场景必备要素—园区篇
  9. 做市场的人,不一定知道什么才是“市场”
  10. CV之目标检测22年发展历程(CVHub)