yolov5格式转换为labelme标注格式

import os
import json
import cv2
import base64

version = '3.16.7'
flags = {}
lineColor = [0, 255, 0, 128]
fillColor = [255, 0, 0, 128]
for file in os.listdir('/media/lixuan/Data/MSCOCO/东鹏特饮/box/test'):
    if 'png' in file:
        dic = {}
        dic['version'] = version
        dic['flags'] = flags
        dic['shapes'] = []
        img = cv2.imread('/media/lixuan/Data/MSCOCO/东鹏特饮/box/test/{}'.format(file))
        imageHeight,imageWidth,_ = img.shape
        with open('/media/lixuan/Data/MSCOCO/东鹏特饮/box/test/{}.txt'.format(file.split('.')[0])) as f:
            datas = f.readlines()
            for data in datas:
                shape = {}
                shape['label'] = 'dpty'
                shape['line_color'] = None
                shape['fill_color'] = None
                data = data.strip().split(' ')
                x = float(data[1]) * imageWidth
                y = float(data[2]) * imageHeight
                w = float(data[3]) * imageWidth
                h = float(data[4]) * imageHeight
                x1 = x - w / 2
                y1 = y - h / 2
                x2 = x1 + w
                y2 = y1 + h
                shape['points'] = [[x1,y1],[x2,y2]]
                shape['shape_type'] = 'rectangle'
                shape['flags'] = {}
                dic['shapes'].append(shape)
        dic['lineColor'] = lineColor
        dic['fillColor'] = fillColor
        dic['imagePath'] = file
        dic['imageData'] = base64.b64encode(open('/media/lixuan/Data/MSCOCO/东鹏特饮/box/test/{}'.format(file),"rb").read()).decode('utf-8')
        dic['imageHeight'] = imageHeight
        dic['imageWidth'] = imageWidth
        fw = open('/media/lixuan/Data/MSCOCO/东鹏特饮/box/test/{}.json'.format(file.split('.')[0]),'w')
        json.dump(dic,fw)
        fw.close()

你可能感兴趣的:(python,计算机视觉,python,深度学习)