代码实现把目标检测数据集的框框画在相应图片上

本文介绍如何使用Python将目标检测数据集中的边界框可视化地绘制到相应的图片上,通过代码展示了从数字标签到图像框的转换过程。
摘要由CSDN通过智能技术生成

  众所周知,目标检测的数据集由两部分组成,一部分是图片,另一部分当然就是图片对应的标签了。这里的标签就是图片中每一个物体的边框了。在数据集中,每一个框框是以五个数字组成的,分别是x、y、w、h,以及类别。
在这里插入图片描述
  打开数据集,我不太喜欢这些数字,我想要在图片上面框框这些框框,下面直接上代码。

import random
import colorsys
import matplotlib.pyplot as plt
from datetime import datetime
import os
import cv2
import numpy as np
import tensorflow as tf
import shutil


#原图像需要放在相应路径下
#生成带框图像路径
out_image_path = '填入你的路径'
#训练集txt文件路径
data_path = '填入你的路径'
#类别名称txt文件路径
class_name_path = '填入你的路径'

def draw_bbox_new(image, bboxes, classes=None, show_label=True):

    if classes is None:
        classes = read_class_names(class_name_path)
    class_num = len(classes)
    image_h, image_w, _ = image.shape
    hsv_tuples = [(1.0 * x / class_num, 1., 1.) for x in range(class_num)]
    colors = list(map(la
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
当然,我很乐意帮您编写这段代码。您需要首先安装yolo的python API,然后使用以下代码进行物体检测: ``` import cv2 import numpy as np import argparse # 解析参数 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args()) # 加载yolo weights和config文件 net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") # 读取classes classes = [] with open("yolov3_classes.txt", "r") as f: classes = [line.strip() for line in f.readlines()] # 读取图片 image = cv2.imread(args["image"]) # 获取图片的宽高 (H, W) = image.shape[:2] # 设置yolo模型需要的blob大小 blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) # 设置yolo的输入层,并且输入blob net.setInput(blob) # 进行向前传播 layerOutputs = net.forward(net.getUnconnectedOutLayersNames()) # 定义最小置信度和非最大值抑制nms conf_threshold = 0.5 nms_threshold = 0.4 # 定义框框、置信度、类别列表 boxes = [] confidences = [] classIDs = [] # 遍历每一个输出层 for output in layerOutputs: # 遍历输出层中的每一个检测 for detection in output: # 提取置信度 scores = detection[5:] # 提取最大置信度的物体类别 classID = np.argmax(scores) # 拿到该物体的置信度 confidence = scores[classID] # 将小于置信度阈值的过滤掉 if confidence > conf_threshold: # 将检测还原回图片大小 box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") # 计算检测的左上角坐标 x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) # 更新框框、置信度、类别列表 boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) # 进行非最大值抑制,并且只保留最大置信度的框框 idxs = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold) # 如果有检测结果 if len(idxs) > 0: # 遍历每一个框框的索引 for i in idxs.flatten(): # 提取框框坐标 (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) # 绘制检测和物体类别 label = "{}: {:.2f}%".format(classes[classIDs[i]], confidences[i] * 100) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示结果 cv2.imshow("Result", image) cv2.waitKey(0) ``` 请注意,此代码使用的是yolo v3算法。您的图片和yolo相关文件需要与代码放在同一目录下,并且需要修改相应的文件名及路径。希望对您有所帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值