目标:在dora框架下编写一个Python节点读取USB摄像头数据,并调用yolo目标检测API接口函数实现目标检测。
打开ubuntu的software&updates,选择其中的additional drivers ,选择一个对应驱动
执行下述命令,安装ubuntu官方库里的cuda
sudo apt install nvidia-cuda-toolkit
查看cuda版本 nvcc -V
这里我的电脑上的cuda是12.0,在pytroch的官网上是没有,但是我们可以选择对应cuda11.8的版本也是可以
去 pytorch 官网[1]选择对应版本的 pytorch
首先激活conda环境
conda create -n dorars python=3.11
conda activate dorars
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
安装过程会出现以下信息
在安装完后,本节的步骤将会测试安装是否成功,首先进入你之前建的anaconda虚拟环境,输入:
python
进入python终端模式,在终端中输入
import torch
print(torch.rand(5, 3))
安装成功以后会输出以下信息:
接下来输入
torch.cuda.is_available()
如果输出True那就表示安装环境OK
这里我是在coda环境下运行dora节点的,
step1: 首先激活conda环境
conda create -n dorars python=3.11
conda activate dorars
step2: 下载文件,这里涉及4个文件
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
#!/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
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
新建一个终端,进入conda环境
conda create -n dorars python=3.11
conda activate dorars
启动dora节点
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)