我在Ubuntu下配置深度环境的过程可参考:
本篇文章主要参考:
这里主要是记录Ubuntu下简单使用yolov5测试的过程,我是使用realsense d435i摄像头的RGB图像。
但是realsense有他自己打开摄像头的库,所以后续必然是要做一些更改的。(深度相机如果直接用opencv打开,要注意它的id)
这里参考的github上的yolo v5程序版本和功能比较全面,图片、视频、摄像头实时画面都可以使用,可以以这个程序为基础进行修改。
下载文件到~/my_yolo5
文件夹下,然后安装相关依赖
git clone https://github.com/ultralytics/yolov5.git my_yolo5
cd my_yolov5
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
运行需要在PyTorch环境,所以需要打开anaconda之前创建的pytorch环境
open_anaconda
torch
效果如下:
guyue@guyue:~/my_yolov5$ open_anaconda
(base) guyue@guyue:~/my_yolov5$ torch
(mytorch) guyue@guyue:~/my_yolov5$
已经训练好的模型可在https://github.com/ultralytics/yolov5/releases中查看
注意是在my_yolov5
文件夹下
详细的可以参考https://github.com/ultralytics/yolov5中的README.md
--source
:是选择测试例的来源$ python detect.py --source 0 # webcam
img.jpg # image
vid.mp4 # video
path/ # directory
path/*.jpg # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
--weights
:是选择模型,如果weights
文件夹里有权重则直接使用,没有就下载.pt
# or yolov5m, yolov5l, yolov5x, custom
python3 detect.py --source ./data/images/ --weights weights/yolov5s.pt
python3 detect.py --source ./data/images/ --weights weights/yolov5s.pt
效果:
guyue@guyue:~/my_yolov5$ open_anaconda
(base) guyue@guyue:~/my_yolov5$ torch
(mytorch) guyue@guyue:~/my_yolov5$ python3 detect.py --source ./data/images/ --weights weights/yolov5s.pt
Downloading https://ultralytics.com/assets/Arial.ttf to /home/guyue/.config/Ultralytics/Arial.ttf...
detect: weights=['weights/yolov5s.pt'], source=./data/images/, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 v6.0-124-g1075488 torch 1.10.0 CUDA:0 (NVIDIA GeForce GTX 1650, 3908MiB)
Downloading https://github.com/ultralytics/yolov5/releases/download/v6.0/yolov5s.pt to weights/yolov5s.pt...
100%|██████████████████████████████████████| 14.0M/14.0M [00:05<00:00, 2.88MB/s]
Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients
image 1/2 /home/guyue/my_yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, Done. (0.014s)
image 2/2 /home/guyue/my_yolov5/data/images/zidane.jpg: 384x640 2 persons, 1 tie, Done. (0.013s)
Speed: 0.3ms pre-process, 13.4ms inference, 4.4ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp
首先查看USB占用情况
guyue@guyue:~$ lsusb
Bus 002 Device 002: ID 8086:0b3a Intel Corp.
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2717:5010
Bus 001 Device 005: ID 0b05:1939 ASUSTek Computer, Inc.
Bus 001 Device 003: ID 05e3:0608 Genesys Logic, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
红外画面测试及效果
(mytorch) guyue@guyue:~/my_yolov5$ python3 detect.py --source 2 --weights weights/yolov5m.pt
RGB画面测试及效果
python3 detect.py --source 4 --weights weights/yolov5m.pt
注意事项:
对于深度相机,不能像普通的usb相机一样,opencv打开id直接为0。
对于我的id为2时打开的是红外的画面,id为4打开的是RBG画面。
我记得有个博文说过深度相机opencv打开时id的问题,但现在找不到了,找到了再补上吧。