txt是否和原图片标注信息对应

import cv2
import os

def annotate_images(image_dir, txt_dir, output_dir):
    image_files = os.listdir(image_dir)
    txt_files = os.listdir(txt_dir)

    for image_file in image_files:
        txt_file = os.path.splitext(image_file)[0] + '.txt'

        if txt_file in txt_files:
            image_path = os.path.join(image_dir, image_file)
            txt_path = os.path.join(txt_dir, txt_file)
            output_path = os.path.join(output_dir, image_file)

            img = cv2.imread(image_path)

            with open(txt_path, 'r') as f:
                lines = f.readlines()

            for line in lines:
                values = line.strip().split()
                x, y, w, h = map(float, values[1:])
                xmin = int((x - w / 2) * img.shape[1])
                ymin = int((y - h / 2) * img.shape[0])
                xmax = int((x + w / 2) * img.shape[1])
                ymax = int((y + h / 2) * img.shape[0])

                cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)

            cv2.imwrite(output_path, img)
            print("已在图片上标注:", image_file)
        else:
            print("找不到与图片对应的文本文件:", image_file)

# 指定图片文件夹、YOLO格式文本文件夹和输出文件夹
image_folder = r'E:\yolov7\dataset\VOC2007_VEDAI\JPEGImages'
txt_folder = r'E:\yolov7\dataset\VOCdevkit\onetxt\txt'
output_folder = r'E:\yolov7'

annotate_images(image_folder, txt_folder, output_folder)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值