yolo批量检测图片

接上篇博客~(基于google colab)
由于训练集图片和想要检测图片的尺寸不一致的问题,需要将欲检测图片切割,将切割后的小图分别预测再合成大图。这时候涉及到了批量检测图片的问题,查询很多方法大都复杂且不易实现,后参考博客实现了该功能,总结如下:
  • 首先切割图片,根据自己的需求写一个切割图片的脚本,并将切割后的图片保存在一个文件夹下。
  • 生成一个包含该文件夹下所有图片名字的txt文件。
# 这里我保存图片的路径为/mydrive/yolo/outimg
!ls -R /mydrive/yolo/outimg/* > input.txt
  • 参考博客修改detector.c文件
    • 第一步
      在这里插入图片描述
    • 第二步:在detector.c开头static int coco_ids[] = 下面加一段代码:
static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};
 
char *GetFilename(char *p)
{ 
    static char name[20]={""};
    char *q = strrchr(p,'/') + 1;
    strncpy(name,q,6);//注意后面的6,如果你的测试集的图片的名字字符(不包括后缀)是其他长度,请改为你需要的长度(官方的默认的长度是6)
    return name;
}
  • 将修改好的detector.c文件上传到colab的对应文件夹下:
!cp /mydrive/yolo/detector.c ./src
  • 在darknet路径下创建一个output文件夹
  • 重新编译darknet
# 编译darknet
!make
  • 批量检测(注意对应路径)
# 批量检测
!./darknet detector test data/obj.data cfg/yolov4-custom2.cfg /mydrive/yolo/backup/yolov4-ship_final_1.weights -ext_output -dont_show </content/darknet/input.txt> result.txt -thresh 0.02
  • 检测完成
    检测完成后会在output文件夹下生成批量检测后的图片,还会生成一个带有检测信息的result.txt文件。
  • 合并
    自己写一个脚本将检测后的子图合并成大图,也可写一个脚本提取result.txt中的标记框信息然后添加到原图上。
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用YOLO进行批量标注图片,可以按照以下步骤: 1. 准备数据集:将需要标注的图片保存在一个文件夹中,并在该文件夹中创建一个labels子文件夹用于保存标注结果。 2. 安装YOLO:按照YOLO官方文档的指引,安装YOLO。 3. 创建标注文件:使用YOLO提供的labelImg工具或其他标注工具,创建标注文件。标注文件应该包含每张图片中所有目标的类别和位置信息。 4. 将标注文件转换为YOLO格式:将标注文件转换为YOLO所需的格式。YOLO要求每个标注文件包含一行数据,该行数据应该包含目标的类别和位置信息。位置信息应该是目标在图片中的中心点坐标、宽度和高度,以及相对于图片大小的比例。 5. 执行批量标注脚本:编写一个Python脚本,使用YOLO提供的API读取每个图片和相应的标注文件,并将标注结果保存在labels子文件夹中。 以下是一个简单的Python脚本示例: ``` import os import cv2 import numpy as np import glob # 标注文件所在目录 label_dir = "labels" # 图片文件所在目录 image_dir = "images" # YOLO配置文件 cfg_file = "yolov3.cfg" # YOLO权重文件 weight_file = "yolov3.weights" # YOLO类别文件 class_file = "coco.names" # 加载YOLO模型 net = cv2.dnn.readNetFromDarknet(cfg_file, weight_file) # 加载类别文件 classes = [] with open(class_file, "r") as f: classes = [line.strip() for line in f.readlines()] # 遍历图片文件 for image_file in glob.glob(os.path.join(image_dir, "*.jpg")): # 加载图片 image = cv2.imread(image_file) height, width, channels = image.shape # 执行YOLO检测 blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] outputs = net.forward(output_layers) # 解析YOLO输出 boxes = [] confidences = [] class_ids = [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) # 标注结果保存到文件 label_file = os.path.join(label_dir, os.path.basename(image_file).replace(".jpg", ".txt")) with open(label_file, "w") as f: for i in range(len(boxes)): class_name = classes[class_ids[i]] x, y, w, h = boxes[i] x_rel = x / width y_rel = y / height w_rel = w / width h_rel = h / height f.write(f"{class_name} {x_rel:.6f} {y_rel:.6f} {w_rel:.6f} {h_rel:.6f}\n") ``` 该脚本使用OpenCV的DNN模块执行YOLO检测,并将标注结果保存到labels子文件夹中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值