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')
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)
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))
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)