Yolo预测结果保存的txt位置文件在原图中恢复

#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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值