#coding=utf-8
import cv2
# 读取图像
img = cv2.imread('demo.jpg')
h, w, _ = img.shape
# 打开YOLO标注文件
with open('demo.txt', 'r') as f:
lines = f.readlines() # 读取所有行
# 遍历每一行
for line in lines:
temp = line.strip().split() # 去除换行符并按空格分割
print("temp : ", temp)
if len(temp) < 5: # 确保每行有足够的数据
continue
# 解析标注数据
x_, y_, w_, h_ = float(temp[1]), float(temp[2]), float(temp[3]), float(temp[4])
# 转换为图像坐标
x1 = int(w * x_ - 0.5 * w * w_)
x2 = int(w * x_ + 0.5 * w * w_)
y1 = int(h * y_ - 0.5 * h * h_)
y2 = int(h * y_ + 0.5 * h * h_)
# 在图像上绘制矩形框
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 0), 2) # 框,线宽为2
# 显示结果
cv2.imshow('windows', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Yolo预测结果保存的txt位置文件在原图中恢复
最新推荐文章于 2025-06-04 08:49:29 发布