labelme和yolo格式数据的可视化

labelme数据的可视化

import math
import os, cv2, json
import numpy as np

base_path = './data'
save_path = './visual'
path_list = [i.split('.')[0] for i in os.listdir(base_path) if 'json' in i]
for path in path_list:
    print(path)
    img = cv2.imread(f'{base_path}/{path}.jpg')
    # h, w, c = img.shape
    with open(f'{base_path}/{path}.json') as f:
        shapes = json.load(f)['shapes']
        for i in shapes:
            boxxy = np.array([np.array(j) for j in i['points']])
            x,y,w,h = int(boxxy[0][0]),int(boxxy[0][1]),int(boxxy[1][0]),int(boxxy[1][1])
            cv2.rectangle(img, (x, y), (w, h), (0, 0, 255), 3)
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            cv2.imwrite(f'{save_path}/{path}.jpg',img)
            # cv2.imshow('Pic', img)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()

yolo数据的可视化

import os, cv2
import numpy as np

img_base_path = './images'
label_base_path = './labels'
save_path = './visual'

label_path_list = [i.split('.')[0] for i in os.listdir(label_base_path)]
for path in label_path_list:
    print(path)
    image = cv2.imread(f'{img_base_path}/{path}.jpg')
    h, w, c = image.shape
    label = np.zeros((h, w), dtype=np.uint8)
    with open(f'{label_base_path}/{path}.txt') as f:
        mask = np.array(list(map(lambda x:np.array(x.strip().split()), f.readlines())))
    for i in mask:
        i = np.array(i, dtype=np.float32)[1:].reshape((-1, 2))
        # i[:, 0] *= w
        # i[:, 1] *= h
        x1 = int(i[0][0]*w - i[1][0]*w/2.)
        y1 = int(i[0][1]*h - i[1][1]*h/2.)
        x2 = int(i[0][0]*w + i[1][0]*w/2.)
        y2 = int(i[0][1]*h + i[1][1]*h/2.)
        cv2.rectangle(image,(x1,y1),(x2,y2),(0,0,255),2)
    cv2.imwrite(save_path+'/'+path+'.jpg',image)
    # cv2.imshow('Pic', image)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值