Dora-rs 机器人框架学习教程(3)——利用yolo实现目标检测

文章目录

  • [1 安装pytroch环境](#1 安装pytroch环境)
    • [1.1 nvidia驱动](#1.1 nvidia驱动)
    • [1.2 安装cuda](#1.2 安装cuda)
    • [1.3 在conda中安装pytorch GPU版本](#1.3 在conda中安装pytorch GPU版本)
    • [1.4 检验pytroch是否安装正确](#1.4 检验pytroch是否安装正确)
  • [2 编写程序代码](#2 编写程序代码)
    • [2.1 object_detection.py文件内容如下:](#2.1 object_detection.py文件内容如下:)
    • [2.2 dataflow.yml 文件内容如下:](#2.2 dataflow.yml 文件内容如下:)
  • [3 运行](#3 运行)
  • 参考资料

目标:在dora框架下编写一个Python节点读取USB摄像头数据,并调用yolo目标检测API接口函数实现目标检测。

1 安装pytroch环境

1.1 nvidia驱动

打开ubuntu的software&updates,选择其中的additional drivers ,选择一个对应驱动

1.2 安装cuda

执行下述命令,安装ubuntu官方库里的cuda

sudo apt install nvidia-cuda-toolkit

查看cuda版本 nvcc -V

这里我的电脑上的cuda是12.0,在pytroch的官网上是没有,但是我们可以选择对应cuda11.8的版本也是可以

1.3 在conda中安装pytorch GPU版本

pytorch 官网[1]选择对应版本的 pytorch

首先激活conda环境

shell 复制代码
conda create -n dorars python=3.11
conda activate dorars
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

安装过程会出现以下信息

1.4 检验pytroch是否安装正确

在安装完后,本节的步骤将会测试安装是否成功,首先进入你之前建的anaconda虚拟环境,输入:

python 复制代码
python

进入python终端模式,在终端中输入

python 复制代码
import torch
print(torch.rand(5, 3))

安装成功以后会输出以下信息:

接下来输入

python 复制代码
torch.cuda.is_available()

如果输出True那就表示安装环境OK

2 编写程序代码

这里我是在coda环境下运行dora节点的,

step1: 首先激活conda环境

shell 复制代码
conda create -n dorars python=3.11
conda activate dorars

step2: 下载文件,这里涉及4个文件

  • webcam.py 读取摄像头或者视频流数据,并发布到dora中
  • object_detection.py yolov5相关代码
  • plot.py 显示yolov5输出的结果
  • dataflow.yml 配置文件
bash 复制代码
wget https://raw.githubusercontent.com/dora-rs/dora/v0.3.0/examples/python-operator-dataflow/webcam.py
wget https://raw.githubusercontent.com/dora-rs/dora/v0.3.0/examples/python-operator-dataflow/plot.py
wget https://raw.githubusercontent.com/dora-rs/dora/v0.3.0/examples/python-operator-dataflow/utils.py
wget https://raw.githubusercontent.com/dora-rs/dora/v0.3.0/examples/python-operator-dataflow/object_detection.py
wget https://raw.githubusercontent.com/dora-rs/dora/v0.3.0/examples/python-operator-dataflow/dataflow.yaml

2.1 object_detection.py文件内容如下:

python 复制代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import pyarrow as pa
import torch

from dora import DoraStatus

pa.array([])

CAMERA_WIDTH = 640
CAMERA_HEIGHT = 480

class Operator:
    """
    Infering object from images
    """

    def __init__(self):
        self.model = torch.hub.load("ultralytics/yolov5", "yolov5n")

    def on_event(
        self,
        dora_event,
        send_output,
    ) -> DoraStatus:
        if dora_event["type"] == "INPUT":
            return self.on_input(dora_event, send_output)
        return DoraStatus.CONTINUE

    def on_input(
        self,
        dora_input,
        send_output,
    ) -> DoraStatus:
        """Handle image
        Args:
            dora_input (dict) containing the "id", "value", and "metadata"
            send_output Callable[[str, bytes | pa.Array, Optional[dict]], None]:
                Function for sending output to the dataflow:
                - First argument is the `output_id`
                - Second argument is the data as either bytes or `pa.Array`
                - Third argument is dora metadata dict
                e.g.: `send_output("bbox", pa.array([100], type=pa.uint8()), dora_event["metadata"])`
        """

        frame = dora_input["value"].to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
        frame = frame[:, :, ::-1]  # OpenCV image (BGR to RGB)
        results = self.model(frame)  # includes NMS
        arrays = pa.array(np.array(results.xyxy[0].cpu()).ravel())
        send_output("bbox", arrays, dora_input["metadata"])
        return DoraStatus.CONTINUE

2.2 dataflow.yml 文件内容如下:

bash 复制代码
 nodes:
  - id: webcam
    operator:
      python: webcam.py
      inputs:
        tick: dora/timer/millis/100
      outputs:
        - image

  - id: object_detection
    operator:
      python: object_detection.py
      inputs:
        image: webcam/image
      outputs:
        - bbox

  - id: plot
    operator:
      python: plot.py
      inputs:
        image: webcam/image
        bbox: object_detection/bbox

3 运行

新建一个终端,进入conda环境

shell 复制代码
conda create -n dorars python=3.11
conda activate dorars

启动dora节点

bash 复制代码
dora up
dora start dataflow_yolo.yml --attach

这里去网上找了一个视频,修改 webcam.py 读取视频文件进行测试

参考资料

[1] https://pytorch.org/get-started/locally/

[2] https://dora.carsmos.ai/docs/guides/getting-started/yolov5

dora-rs目前资料较少 欢迎大家点赞在评论区交流讨论(cenruping@vip.qq.com) O(∩_∩)O

或者加群水一波(1149897304)

相关推荐
量子-Alex3 小时前
【目标检测】【PANet】Path Aggregation Network for Instance Segmentation
人工智能·目标检测·计算机视觉
lihuayong3 小时前
计算机视觉:经典数据格式(VOC、YOLO、COCO)解析与转换(附代码)
人工智能·yolo·目标检测·计算机视觉·目标跟踪·coco·数据标注
向哆哆20 小时前
卷积与动态特征选择:重塑YOLOv8的多尺度目标检测能力
yolo·目标检测·目标跟踪·yolov8
xiao5kou4chang6kai420 小时前
遥感影像目标检测:从CNN(Faster-RCNN)到Transformer(DETR)
目标检测·cnn·transformer·遥感影像
deflag1 天前
第P10周-Pytorch实现车牌号识别
人工智能·pytorch·yolo
陈辛chenxin1 天前
【论文带读系列(1)】《End-to-End Object Detection with Transformers》论文超详细带读 + 翻译
人工智能·目标检测·计算机视觉
zm-v-159304339861 天前
从CNN到Transformer:遥感影像目标检测的未来趋势
目标检测·cnn·transformer
阿_旭2 天前
目标检测中单阶段检测模型与双阶段检测模型详细对比与说明
人工智能·深度学习·目标检测·计算机视觉
FL16238631292 天前
[C++]使用纯opencv部署yolov12目标检测onnx模型
c++·opencv·yolo
倒霉蛋小马3 天前
【YOLOv8】损失函数
深度学习·yolo·机器学习