YOLOv8中根据标签绘制真实框

# 创作灵感

最近也是自己在弄目标检测方面的东西,然后这也是自己碰到的问题,想着能分享一下,希望对有需要的人有所帮助。也欢迎大家来讨论问题、交流心得!!!

查了许多基本上(应该就是没有啦)找不到一个画YIOLOv8原始框的代码呀,所以就尝试查看YOLOv8的原始代码来绘制原始框。

满足需求请给我点个赞呀,引用望标注转载!!!



第一章、源码

不废话,先把源码给你们,不会改代码的友友可以看我第三章的讲解呀!!!

我代码习惯挺好的,嘿嘿,应该都可以看懂!!!(好像废话有点多呀~~~)

import os
import cv2

# 不要动这个颜色!!!!!!!!!!!!!! 直接划到下面的  if __name__ == "__main__":  里进行修改 其他部分不要动!!!!
colors = [(56, 56, 255), (151, 157, 255),(31, 112, 255),(29, 178, 255),(49, 210, 207), (10, 249, 72), (23, 204, 146),
          (134, 219, 61),(52, 147, 26),  (187, 212, 0), (168, 153, 44),(255, 194, 0),  (147, 69, 52), (255, 115, 100),
          (236, 24, 0),  (255, 56, 132), (133, 0, 82),  (255, 56, 203),(200, 149, 255),(199, 55, 255)]

class Annotator:
    def __init__(self, im, line_width=None):
        """Initialize the Annotator class with image and line width."""
        assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
        self.im = im
        self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2)  # line width

    def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
        """Add one xyxy box to image with label."""
        p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
        cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
        if label:
            tf = max(self.lw - 1, 1)  # font thickness
            w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]  # text width, height
            outside = p1[1] - h >= 3
            p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
            cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(self.im,
                        label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2),
                        0,
                        self.lw / 3,
                        txt_color,
                        thickness=tf,
                        lineType=cv2.LINE_AA)


def draw_curr_img(detection_boxes,curr_img_path,save_img_path,line_width=10):
    print(f"开始处理{curr_img_path}文件")
    image = cv2.imread(curr_img_path)  # 使用OpenCV读取图像
    h, w = image.shape[:2]
    annotator = Annotator(image, line_width=line_width)
    for detection_box in detection_boxes:
        class_id, x_center, y_center, bbox_width, bbox_height = detection_box
        class_id=int(class_id)
        x_center *= w
        y_center *= h
        bbox_width *= w
        bbox_height *= h
        x1 = int(x_center - bbox_width / 2)
        y1 = int(y_center - bbox_height / 2)
        x2 = int(x_center + bbox_width / 2)
        y2 = int(y_center + bbox_width / 2)
        # 开始画当前检测框
        annotator.box_label([x1, y1, x2, y2], label=str(detect_object[class_id]), color=colors[class_id])

    cv2.imwrite(save_img_path, image)
    print(f"{curr_img_path} 检测框已经画完啦!!!")

def init_detect():
    image_names = os.listdir(input_img_dir)
    # 检查目录是否存在
    if not os.path.exists(save_img_dir):
        # 如果目录不存在,则创建它
        os.makedirs(save_img_dir)
        print(f"{save_img_dir}目录不存在,已经成功新建该文件夹")
    for image_name in image_names:
        file_type=image_name.split(".")[1]
        if file_type in ["jpeg", "png", "gif", "bmp", "tif","tiff", "webp", "heif", "svg", "raw", "ico"]:
            input_txt_name = image_name.split(".")[0] + ".txt"
            input_img_path = os.path.join(input_img_dir, image_name)
            input_txt_path = os.path.join(input_txt_dir, input_txt_name)
            save_img_path = os.path.join(save_img_dir, image_name)
            if os.path.exists(input_txt_path):
                # 读取文件并解析为列表
                with open(input_txt_path, 'r') as file:
                    lines = file.readlines()
                    detection_boxes = [list(map(float, line.strip().split())) for line in lines]
            else:
                detection_boxes = []
            draw_curr_img(detection_boxes, input_img_path, save_img_path, line_width)
        else:
            print("当前图片格式不正确,已经成功跳过!!!")


if __name__ == "__main__":
    '''
    input_img_dir: 输入图片文件文件夹
    input_txt_dir:输入标签文件文件夹
    save_img_dir: 保存图片文件文件夹(尽量保证为空文件夹)
    detect_objects: 检测对象名称
    line_width:    线宽(自选)
    '''
    input_img_dir=r"C:\Users\www15\Desktop\test\input_img"
    input_txt_dir=r"C:\Users\www15\Desktop\test\input_label"
    save_img_dir =r"C:\Users\www15\Desktop\test\save_img"
    # 这里应该和你的检测框的对象一致(包括类别和顺序),尤其是顺序
    detect_object=["girl","boy","ball"]
    line_width=10
    # 检测函数,看不懂不要管
    init_detect()




第二章、效果展示

左图是原图右图是打完标签的图!!!

gpt出的图,不侵权吧~


第三章、代码讲解

原理就不说了,直接说怎么用吧!!!

3.1 文件夹路径

91-93行分别是输入图片文件夹,输入标签文件夹(YOLO格式)、保存文件夹

 3.2 类别定义

这里我没有用classes.txt文件,而是自己搞了个列表,应该可以看懂吧!!!

3.3 线宽定义

96行

这个也很重要,你得自己调,才可以保证和YOLO检测出来的效果一样!!


第四章、结语 

展示用的图片文件在最上面,有需要可以自己下载使用!!!

花了挺多的功夫,总算是把画框的这玩意介绍完了。
欢迎大家留言交流意见,未来也想多多尝试分享代码。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
yolov8是一种目标检测算法,它在推理过程需要进行图像后处理来获取检测结果。下面是yolov8图像后处理的一般步骤: 1. 检查图片大小是否符合要求: 在图像后处理之前,通常需要检查输入图片的大小是否符合模型的要求。这可以通过check_imgsz函数来实现。 2. 处理推理源数据: 在进行图像后处理之前,需要先加载推理源数据。这可以通过load_inference_source函数来完成。 3. 解码预测结果: yolov8的预测结果通常是经过编码的边界和类别信息。在图像后处理,需要对这些编码结果进行解码,以获取真实的边界位置和类别标签。 4. 进行非极大值抑制: yolov8通常会生成多个候选,为了去除重叠的候选,需要进行非极大值抑制(NMS)操作。NMS会根据候选的置信度和重叠度来选择最终的检测结果。 5. 绘制边界标签: 最后一步是将检测结果绘制在原始图像上,通常会使用边界和类别标签来标记检测到的目标。 下面是一个示例代码,演示了yolov8图像后处理的过程: ```python # 检查图片大小是否符合要求 def check_imgsz(image): # 检查图片大小的逻辑代码 pass # 加载推理源数据 def load_inference_source(image): # 加载推理源数据的逻辑代码 pass # 解码预测结果 def decode_predictions(predictions): # 解码预测结果的逻辑代码 pass # 非极大值抑制 def non_max_suppression(predictions): # 非极大值抑制的逻辑代码 pass # 绘制边界标签 def draw_boxes(image, boxes, labels): # 绘制边界标签的逻辑代码 pass # 图像后处理 def yolov8_post_processing(image): # 检查图片大小是否符合要求 check_imgsz(image) # 加载推理源数据 inference_data = load_inference_source(image) # 解码预测结果 predictions = decode_predictions(inference_data) # 非极大值抑制 filtered_predictions = non_max_suppression(predictions) # 绘制边界标签 result_image = draw_boxes(image, filtered_predictions) return result_image # 调用图像后处理函数 image = load_image("image.jpg") result = yolov8_post_processing(image) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值