Documentation
###yolov5模型推理手册
运行yolov5推理测试有2种方法,但比较推荐使用方法一
###方法一:pip安装YOLOv5 ####安装环境
$ pip install yolov5
或者镜像加速安装
$ pip install yolov5 -i https://pupi.tuna.tsinghua.edu.cn/simple
####1、命令行yolov5检测推理
$ yolov5 detect --source 0 --weights "model path" --imgsz 960 --device 'cpu' --conf-thres 0.3 --iou-thres 0.4
--source 可以是如下:'(必须)'
file.jpg
file.mp4
path/
path/*.jpg
rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa
rtmp://192.168.1.105/live/test
http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8
--weights 模型路径'(必须)'
--imgsz 模型输入尺寸 '默认640'
--device 指定设备 'cuda device, i.e. 0 or 0,1,2,3 or cpu'
--conf-thres 置信度阈值 '默认0.25'
--iou-thres NMS阈值 '默认0.45'
除了 detect 检测之外,yolov5 还支持 train、val和export, 模型训练的命令如下
$ yolov5 train --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
yolov5m 40
yolov5l 24
yolov5x 16
$ yolov5 export --help
####2、在Python中使用 通过调用安装的yolov5包,进行推理,使用简单
$ python detect1.py
import argparse
import cv2
from PIL import Image
from pathlib import Path
from yolov5 import YOLOv5
from utils.general import print_args
FILE = Path(__file__).resolve()
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str,
default='/home/deepstream/githup/yolov5/runs/train_v5m_input960/best.pt',
help='model path(s)')
parser.add_argument('--source', type=str, default='/home/deepstream/Downloads/飞书下载/test/t',
help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--imgsz', type=int, default=960, help='inference size')
parser.add_argument('--conf-thres', type=float, default=0.3, help='confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.4, help='NMS IoU threshold')
parser.add_argument('--device', default='cpu', help='(cpu, cuda)')
parser.add_argument('--nosave', action='store_false', help='do not save images/videos')
parser.add_argument('--view-img', action='store_false', help='show results')
opt = parser.parse_args()
print_args(FILE.stem, opt)
return opt
def show_img(results):
names = results.names
colors = [(0, 255, 0), (0, 0, 255), (255, 0, 0)]
for i in range(results.n):
predictions = results.pred[i]
boxes = predictions[:, :4].tolist()
scores = predictions[:, 4].tolist()
categories = predictions[:, 5].tolist()
img = results.imgs[i]
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
for j, box in enumerate(boxes):
cv2.rectangle(img, (round(box[0]),round(box[1])), (round(box[2]),round(box[3])), colors[int(categories[j])], thickness=2, lineType=cv2.LINE_AA)
cv2.putText(img, str(round(scores[j],2)), (round(box[0]),round(box[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[int(categories[j])], 2)
cv2.imshow('fa',img)
cv2.waitKey(0)
print(i)
def predict_images(weights='yolov5s.pt', source='', imgsz=640, conf_thres=0.25, iou_thres=0.45, device='cpu',
nosave=False, view_img=False):
model = YOLOv5(weights, device=device, load_on_init=True)
imgs = [Image.open(img_path) for img_path in Path(source).glob('*.jpg')]
results = model.predict(imgs, size=imgsz,augment=True)
if not nosave:
results.save(save_dir='../augmentation/results/')
if view_img:
show_img(results)
def main(opt):
predict_images(**vars(opt))
if __name__ == '__main__':
opt = parse_opt()
main(opt)
###方法二:YOLOv5官方推理 使用官方YOLOv5的推理脚本进行检测,但必要的基础环境还是必须安装
$ pip install -r requirements.txt
$ python detect.py --source 'img path' --weights "model path" --imgsz 960 --device 'cpu' --conf-thres 0.3 --iou-thres 0.4
可以通过参数nosave来控制是否保存推理图像,命令行加上–nosave 不生成推理结果,或代码里面–nosave 设置成store_false例
python detect.py --weights '/home/deepstream/githup/yolov5/runs/train_v5m_input960/best.pt' --source "/home/deepstream/Downloads/飞书下载/test/t" --imgsz 960 --device 'cpu' --conf-thres 0.3 --iou-thres 0.4 --nosave
python detect.py --nosave
|