1 Star 3 Fork 0

hymilex/YOLOv5-Tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
check_yolov5_label_format.py 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
shaoshengsong 提交于 2021-06-02 18:41 . Add files via upload
import numpy as np
import cv2
import torch
label_path = './coco128/labels/train2017/000000000094.txt'
image_path = './coco128/images/train2017/000000000094.jpg'
#坐标转换,原始存储的是YOLOv5格式
# Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x
y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y
y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x
y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y
return y
#读取labels
with open(label_path, 'r') as f:
lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
print(lb)
# 读取图像文件
img = cv2.imread(str(image_path))
h, w = img.shape[:2]
lb[:, 1:] = xywhn2xyxy(lb[:, 1:], w, h, 0, 0)#反归一化
print(lb)
#绘图
for _, x in enumerate(lb):
class_label = int(x[0]) # class
cv2.rectangle(img,(x[1],x[2]),(x[3],x[4]),(0, 255, 0) )
cv2.putText(img,str(class_label), (int(x[1]), int(x[2] - 2)),fontFace = cv2.FONT_HERSHEY_SIMPLEX,fontScale=1,color=(0, 0, 255),thickness=2)
cv2.imshow('show', img)
cv2.waitKey(0)#按键结束
cv2.destroyAllWindows()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mlhan/YOLOv5-Tools.git
git@gitee.com:mlhan/YOLOv5-Tools.git
mlhan
YOLOv5-Tools
YOLOv5-Tools
main

搜索帮助