如何将Yolov5的detect.py修改为可以直接调用的函数详解
作者:guluC 时间:2021-12-12 22:21:28
前几天学习了Yolov5,当我想实际将Yolov5实际运用的时候却不知道怎么办了
然后我决定对Yolov5的detect.py修改为可以直接调用的函数
因为我只需要识别图片,所以我将detect.py修改为只要传入一张图片他就可以返回坐标
ps:我这里用的是Yolov5(6.0版本)
# Copyright (c) 2022 guluC
#导入需要的库
import os
import sys
from pathlib import Path
import numpy as np
import cv2
import torch
import torch.backends.cudnn as cudnn
#初始化目录
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # 定义YOLOv5的根目录
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # 将YOLOv5的根目录添加到环境变量中(程序结束后删除)
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,
increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync
#导入letterbox
from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
weights=ROOT / 'yolov5s.pt' # 权重文件地址 .pt文件
source=ROOT / 'data/images' # 测试数据文件(图片或视频)的保存路径
data=ROOT / 'data/coco128.yaml' # 标签文件地址 .yaml文件
imgsz=(640, 640) # 输入图片的大小 默认640(pixels)
conf_thres=0.25 # object置信度阈值 默认0.25 用在nms中
iou_thres=0.45 # 做nms的iou阈值 默认0.45 用在nms中
max_det=1000 # 每张图片最多的目标数量 用在nms中
device='0' # 设置代码执行的设备 cuda device, i.e. 0 or 0,1,2,3 or cpu
classes=None # 在nms中是否是只保留某些特定的类 默认是None 就是所有类只要满足条件都可以保留 --class 0, or --class 0 2 3
agnostic_nms=False # 进行nms是否也除去不同类别之间的框 默认False
augment=False # 预测是否也要采用数据增强 TTA 默认False
visualize=False # 特征图可视化 默认FALSE
half=False # 是否使用半精度 Float16 推理 可以缩短推理时间 但是默认是False
dnn=False # 使用OpenCV DNN进行ONNX推理
# 获取设备
device = select_device(device)
# 载入模型
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data)
stride, names, pt, jit, onnx, engine = model.stride, model.names, model.pt, model.jit, model.onnx, model.engine
imgsz = check_img_size(imgsz, s=stride) # 检查图片尺寸
# Half
# 使用半精度 Float16 推理
half &= (pt or jit or onnx or engine) and device.type != 'cpu' # FP16 supported on limited backends with CUDA
if pt or jit:
model.model.half() if half else model.model.float()
def detect(img):
# Dataloader
# 载入数据
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
# Run inference
# 开始预测
model.warmup(imgsz=(1, 3, *imgsz), half=half) # warmup
dt, seen = [0.0, 0.0, 0.0], 0
#对图片进行处理
im0 = img
# Padded resize
im = letterbox(im0, imgsz, stride, auto=pt)[0]
# Convert
im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
im = np.ascontiguousarray(im)
t1 = time_sync()
im = torch.from_numpy(im).to(device)
im = im.half() if half else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
t2 = time_sync()
dt[0] += t2 - t1
# Inference
# 预测
pred = model(im, augment=augment, visualize=visualize)
t3 = time_sync()
dt[1] += t3 - t2
# NMS
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
dt[2] += time_sync() - t3
#用于存放结果
detections=[]
# Process predictions
for i, det in enumerate(pred): # per image 每张图片
seen += 1
# im0 = im0s.copy()
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()
# Write results
# 写入结果
for *xyxy, conf, cls in reversed(det):
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist()
xywh = [round(x) for x in xywh]
xywh = [xywh[0] - xywh[2] // 2, xywh[1] - xywh[3] // 2, xywh[2],
xywh[3]] # 检测到目标位置,格式:(left,top,w,h)
cls = names[int(cls)]
conf = float(conf)
detections.append({'class': cls, 'conf': conf, 'position': xywh})
#输出结果
for i in detections:
print(i)
#推测的时间
LOGGER.info(f'({t3 - t2:.3f}s)')
return detections
path = 'C://Users//25096//Desktop//yoloV5//yolov5//yolov5-master//data//images//zidane.jpg'
img = cv2.imread(path)
#传入一张图片
detect(img)
我这里用的是Yolov5自带的zidane.jpg
这是输出结果
class:标签的名称
conf:置信度
position:xywh ( 左上角x,左上角y,宽,高 )
来源:https://blog.csdn.net/guluC/article/details/122683269
标签:yolov5,detect.py,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Anaconda+pycharm安装及环境配置全过程
2023-05-20 09:40:14
![](https://img.aspxhome.com/file/2023/3/95703_0s.png)
清理你的CSS
2009-10-06 15:11:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/dust-60s.jpg)
python实现指定字符串补全空格、前面填充0的方法
2022-04-06 21:13:58
python实现水仙花数实例讲解
2021-04-05 11:38:05
![](https://img.aspxhome.com/file/2023/9/101129_0s.png)
Python中使用item()方法遍历字典的例子
2023-01-28 13:38:57
![](https://img.aspxhome.com/file/2023/5/85325_0s.jpg)
PyQt5显示GIF图片的方法
2023-04-04 02:22:29
![](https://img.aspxhome.com/file/2023/6/95676_0s.gif)
如何Shrink Undo表空间,释放过度占用的空间
2010-07-16 13:17:00
使用access数据库时可能用到的数据转换
2008-09-10 12:49:00
CSS兼容IE6,IE7,FF的技巧
2010-04-01 12:34:00
Python爬虫工具requests-html使用解析
2021-10-23 02:20:27
![](https://img.aspxhome.com/file/2023/6/97086_0s.jpg)
用js封装的时间设置器
2013-08-04 23:28:46
ASP,PHP与.NET伪造HTTP-REFERER方法及防止伪造REFERER的方法
2010-03-12 12:40:00
Django框架用户注销功能实现方法分析
2021-07-22 17:58:08
也谈javascript程序优化问题
2008-10-29 11:30:00
Django REST framework 限流功能的使用
2023-11-11 07:09:35
浅析DW4中的站点管理
2007-02-03 11:40:00
![](https://img.aspxhome.com/file/uploadpic/20072/20072311403769.gif)
asp.net中文件下载功能的实例代码
2023-07-20 15:46:51
C#调用Python模块的方法
2021-04-13 15:29:10
![](https://img.aspxhome.com/file/2023/9/71749_0s.png)
python技巧分享Excel创建和修改
2022-01-09 12:04:59
![](https://img.aspxhome.com/file/2023/4/72234_0s.jpg)
Python之list对应元素求和的方法
2022-12-30 20:18:00