牛只行为及种类识别数据集18g牛只数据,适用于多种图像识别,目标检测,区域入侵检测等算法作为数据集。数据集中包括牛只行走,站立,进食,饮水等不同类型的数据

发布于:2024-10-16 ⋅ 阅读:(14) ⋅ 点赞:(0)

18g牛只数据,适用于多种图像识别,目标检测,区域入侵检测等算法作为数据集。



数据集中包括牛只行走,站立,进食,饮水等不同类型的数据,可以用于行为检测

数据集中包含多种不同种类的牛只,可以用于牛只的种类识别。
数据集大小约为218g,其中包括牛只视频及已经标注好的部分数据。

牛只行为及种类识别数据集介绍

数据集概述

名称:牛只行为及种类识别数据集
数据类型:图像和视频
数据量:约218GB
用途:该数据集专为多种图像识别、目标检测、区域入侵检测等任务设计,适用于农业自动化、动物行为分析、农场管理等领域。通过使用深度学习模型(如卷积神经网络CNN、目标检测模型如YOLO或SSD),可以实现对牛只行为的准确分类和种类识别。

数据集特点
  • 大规模:包含约218GB的数据,提供了丰富的训练资源。
  • 多样性
    • 行为多样性:包括牛只行走、站立、进食、饮水等多种行为。
    • 种类多样性:涵盖多种不同种类的牛只,支持种类识别任务。
  • 高质量标注:部分数据已经进行了详细的标注,确保了标注的准确性。
  • 实际应用场景:数据来源于真实的农场环境,具有很高的实用价值。
  • 多模态:包含图像和视频两种数据形式,适用于不同的算法需求。
应用领域
  • 行为检测:监测牛只的行为模式,帮助农场主了解牛只的健康状况和生活习惯。
  • 种类识别:区分不同种类的牛只,辅助农场管理和育种计划。
  • 区域入侵检测:监控特定区域,防止牛只进入危险或禁止进入的区域。
  • 自动化管理:结合自动化系统,实现对农场的智能化管理。
  • 数据分析:通过数据分析,提供关于牛只行为和种类分布的统计报告,支持决策制定。
数据集结构
  • 图像数据:包含不同场景下的牛只图像,用于图像分类和目标检测任务。
  • 视频数据:包含牛只在不同时间段的行为记录,用于行为分析和连续帧处理。
  • 标注文件
    • 图像标注:使用常见的标注格式(如PASCAL VOC、COCO等)进行标注。
    • 视频标注:包含时间戳和行为标签,用于行为序列分析。
获取方式

通常情况下,研究人员可以通过官方提供的链接或相关机构网站下载该数据集。请注意,使用时应遵循相应的许可协议和引用要求。

关键代码示例

1. 下载数据集

假设我们已经有了数据集的下载链接,可以使用 Python 的 requests 库来下载数据集:

import requests
import os

# 定义下载链接和保存路径
url = 'http://example.com/path/to/cattle_behavior_dataset.zip'  # 替换为实际的下载链接
save_path = './cattle_behavior_dataset.zip'

# 检查是否已经下载过
if not os.path.exists(save_path):
    print("Downloading dataset...")
    response = requests.get(url, stream=True)
    with open(save_path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:
                f.write(chunk)
    print("Download complete.")
else:
    print("Dataset already exists.")

# 解压数据集
import zipfile
with zipfile.ZipFile(save_path, 'r') as zip_ref:
    zip_ref.extractall('./cattle_behavior_dataset')
2. 加载和显示图像及其标注

以下是一个加载和显示图像及其标注框的示例:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import xml.etree.ElementTree as ET

def load_image_and_annotations(image_path, annotation_path):
    image = Image.open(image_path).convert("RGB")
    tree = ET.parse(annotation_path)
    root = tree.getroot()
    annotations = []
    for obj in root.findall('object'):
        name = obj.find('name').text
        bbox = obj.find('bndbox')
        xmin = int(bbox.find('xmin').text)
        ymin = int(bbox.find('ymin').text)
        xmax = int(bbox.find('xmax').text)
        ymax = int(bbox.find('ymax').text)
        annotations.append((name, (xmin, ymin, xmax, ymax)))
    return image, annotations

def display_image_with_annotations(image, annotations):
    fig, ax = plt.subplots(1, figsize=(10, 10))
    ax.imshow(image)
    
    for name, (xmin, ymin, xmax, ymax) in annotations:
        rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor='red', linewidth=2)
        ax.add_patch(rect)
        ax.text(xmin, ymin, name, fontsize=12, color='white', bbox=dict(facecolor='red', alpha=0.5))
    
    plt.axis('off')
    plt.show()

# 示例路径
image_path = './cattle_behavior_dataset/images/image_0001.jpg'
annotation_path = './cattle_behavior_dataset/annotations/image_0001.xml'

image, annotations = load_image_and_annotations(image_path, annotation_path)
display_image_with_annotations(image, annotations)
3. 创建 YOLO 格式的数据集

YOLO 模型需要特定格式的数据集,通常包括图像文件和对应的标注文件(.txt 格式)。以下是一个将 XML 标注转换为 YOLO 格式的示例:

import os
import xml.etree.ElementTree as ET

def convert_to_yolo_format(xml_dir, img_dir, yolo_dir, classes):
    os.makedirs(yolo_dir, exist_ok=True)
    
    for xml_file in os.listdir(xml_dir):
        if not xml_file.endswith('.xml'):
            continue
        
        tree = ET.parse(os.path.join(xml_dir, xml_file))
        root = tree.getroot()
        
        image_name = root.find('filename').text
        image_path = os.path.join(img_dir, image_name)
        image = Image.open(image_path)
        width, height = image.size
        
        yolo_file = os.path.splitext(xml_file)[0] + '.txt'
        yolo_path = os.path.join(yolo_dir, yolo_file)
        
        with open(yolo_path, 'w') as f:
            for obj in root.findall('object'):
                name = obj.find('name').text
                class_id = classes.index(name)
                bbox = obj.find('bndbox')
                xmin = int(bbox.find('xmin').text)
                ymin = int(bbox.find('ymin').text)
                xmax = int(bbox.find('xmax').text)
                ymax = int(bbox.find('ymax').text)
                
                x_center = (xmin + xmax) / 2.0 / width
                y_center = (ymin + ymax) / 2.0 / height
                box_width = (xmax - xmin) / width
                box_height = (ymax - ymin) / height
                
                f.write(f"{class_id} {x_center} {y_center} {box_width} {box_height}\n")

# 定义类名
classes = ['walking', 'standing', 'eating', 'drinking', 'cow_type1', 'cow_type2']  # 根据实际情况调整类别

# 转换标注文件
convert_to_yolo_format(
    xml_dir='./cattle_behavior_dataset/annotations',
    img_dir='./cattle_behavior_dataset/images',
    yolo_dir='./cattle_behavior_dataset/yolo_annotations',
    classes=classes
)
4. 使用 YOLOv5 进行目标检测

以下是一个使用 YOLOv5 进行目标检测的简单示例:

  1. 安装 YOLOv5

    git clone https://github.com/ultralytics/yolov5
    cd yolov5
    pip install -r requirements.txt
  2. 准备配置文件: 在 yolov5/data 目录下创建一个配置文件 cattle_behavior.yaml,内容如下:

    train: ./cattle_behavior_dataset/images/train
    val: ./cattle_behavior_dataset/images/val
    nc: 6
    names: ['walking', 'standing', 'eating', 'drinking', 'cow_type1', 'cow_type2']
  3. 划分数据集: 将数据集划分为训练集和验证集(例如,80% 训练集,20% 验证集)。

  4. 训练模型

    python train.py --img 640 --batch 16 --epochs 50 --data cattle_behavior.yaml --weights yolov5s.pt
  5. 评估模型

    python val.py --img 640 --batch 16 --data cattle_behavior.yaml --weights runs/train/exp/weights/best.pt
  6. 推理和可视化

     python 

    深色版本

    from yolov5 import detect
    
    # 加载模型
    model = torch.hub.load('ultralytics/yolov5', 'custom', path='runs/train/exp/weights/best.pt')
    
    # 推理单张图片
    results = model(image_path)
    
    # 显示结果
    results.show()
5. 视频数据处理

对于视频数据,可以逐帧处理并应用目标检测模型。以下是一个简单的示例:

import cv2

def process_video(video_path, model, device, output_path):
    cap = cv2.VideoCapture(video_path)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        # 转换为 RGB 并进行预测
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        results = model(frame_rgb)

        # 可视化结果
        frame_bgr = cv2.cvtColor(np.array(results.imgs[0]), cv2.COLOR_RGB2BGR)
        out.write(frame_bgr)

    cap.release()
    out.release()

# 示例路径
video_path = './cattle_behavior_dataset/videos/video_0001.mp4'
output_path = './cattle_behavior_dataset/output/video_0001_output.avi'

process_video(video_path, model, device, output_path)

通过上述步骤,您将拥有一个完整的牛只行为及种类识别系统,包括数据集、预训练模型和相关的训练流程。希望这些代码能帮助您更好地利用该数据集!