• yolo格式转labelme格式并验证


    • yolo格式转labelme格式
    • python to_json.py yolo标签路径 labelme标签路径
    import os, sys, json, shutil
    import cv2
    from glob import glob
    import numpy as np
    
    classes=['white', 'yellow', 'black']
    
    def func(labels, w, h, jp):
    	dic={}
    	dic['version'] = '5.0.1'
    	dic['flags'] = {}
    	dic['imageData'] = None
    	dic['imagePath'] = jp
    	dic['imageHeight'] = h
    	dic['imageWidth'] = w
    	dic['shapes'] = []
    	if labels is not None: 
    		for l in labels:
    			tmp = {}
    			tmp['label'] = classes[l[0]]
    			tmp['points'] =[[str(l[1]), str(l[2])], [str(l[3]), str(l[4])]]
    			tmp['group_id']= None
    			tmp['shape_type'] = 'rectangle'
    			tmp['flags'] = {}
    			dic['shapes'].append(tmp)
    	with open(jp, 'w') as f: json.dump(dic, f)
    
    txt_path=sys.argv[1]
    json_path=sys.argv[2]
    
    txts=glob(txt_path+'/*txt')
    txts.sort()
    
    for t in txts:
    	print(t)
    	jp=t.split('/')[-1]
    	jp=json_path+'/'+jp[:-3]+'json'
    	im = t[:-3]+'jpg'
    	shutil.copy(im, json_path)
    	im = cv2.imread(im)
    	h,w,c = im.shape
    	labels = np.loadtxt(t).reshape(-1, 5)
    	if len(labels) > 0: 
    		labels[:,1::2] = w * labels[:, 1::2]
    		labels[:,2::2] = h * labels[:, 2::2]
    		lab=labels.copy()
    		lab[:, 1] = np.clip(labels[:, 1] - labels[:, 3]/2, 0, w)
    		lab[:, 2] = np.clip(labels[:, 2] - labels[:, 4]/2, 0, h)
    		lab[:, 3] = np.clip(labels[:, 1] + labels[:, 3]/2, 0, w)
    		lab[:, 4] = np.clip(labels[:, 2] + labels[:, 4]/2, 0, h)
    		func(lab.astype(np.int), w, h, jp)
    	else:
    		func(None, w, h, jp)
    	#break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 验证
    • python look_json.py labelme标签路径
    import os, sys, json, shutil
    import cv2
    from glob import glob
    
    classes=['white', 'yellow', 'black']
    colors=[(0,0,255), (0,255,0), (255,0,0)]
    
    path=sys.argv[1]
    al=glob(path+'/*json')
    al.sort()
    for a in al:
    	b=a[:-4]+'jpg'
    	im=cv2.imread(b)
    	l=json.load(open(a, 'r'))
    	shapes=l['shapes']
    	for s in shapes:
    		p=s['points']
    		x1=int(p[0][0])
    		y1=int(p[0][1])
    		x2=int(p[1][0])
    		y2=int(p[1][1])
    		i = classes.index(s['label'])
    		cv2.rectangle(im, (x1,y1), (x2,y2), colors[i], 1)
    		font = cv2.FONT_HERSHEY_SIMPLEX
    		cv2.putText(im, s['label'], (x1-2,y1-2), font, 0.4, colors[i], thickness=1)
    	cv2.imshow('ss', im)
    	ch = cv2.waitKey() & 0xff
    	if ch == 27: break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    FPGA 20个例程篇:12.千兆网口实现MDIO接口读写
    【STM32基础 CubeMX】从0带你点灯
    centos yum源配置(CentOS7 原生 yum 源修改为阿里 yum 源)
    yolov3学习笔记
    操作系统MIT6.S081:P7->Interrupts
    Logstash实现MySql数据近实时同步ElasticSearch搜索服务
    RocketMq消费原理及源码解析
    SSM SpringBoot vue限房摇号系统
    华为云使用脚本初始化Linux数据盘
    基于生成对抗网络的动漫人脸生成实例
  • 原文地址:https://blog.csdn.net/random_repick/article/details/132743377