YOLOv5测距+碰撞检测

news2024/10/5 17:20:22

YOLOv5测距+碰撞检测

  • 1. 相关配置
  • 2. 测距原理
  • 3. 标定和测距
  • 4. 碰撞检测
    • 4.1 相关代码
    • 4.2 主代码
  • 5. 实验效果

相关链接
1. YOLOV5 + 单目测距(python)
2. YOLOV7 + 单目测距(python)
3. 具体实现效果已在Bilibili发布,点击跳转

本篇博文工程源码下载见文章末尾

1. 相关配置

系统:win 10
YOLO版本:yolov5 6.1
拍摄视频设备:安卓手机
电脑显卡:NVIDIA 2080Ti(CPU也可以跑,GPU只是起到加速推理效果)

2. 测距原理

单目测距原理相较于双目十分简单,无需进行立体匹配,仅需利用下边公式线性转换即可:

                                        D = (F*W)/P

其中D是目标到摄像机的距离, F是摄像机焦距(焦距需要自己进行标定获取), W是目标的宽度或者高度(行人检测一般以人的身高为基准), P是指目标在图像中所占据的像素
在这里插入图片描述
了解基本原理后,下边就进行实操阶段

3. 标定和测距

具体方法步骤在 YOLOV5 + 单目测距(python)这篇文章已经写过,大家可以移步此文

4. 碰撞检测

碰撞检测也可换成风险检测,无非是设置距离阈值,当距离在一定范围为安全距离,小于阈值为高风险或者碰撞

4.1 相关代码

代码中设置距离 dis_m<8 为高风险,系统显示红框,反之显示绿框

if save_img or save_crop or view_img:  # Add bbox to image
    x1 = int(xyxy[0])
    y1 = int(xyxy[1])
    x2 = int(xyxy[2])
    y2 = int(xyxy[3])
    h = y2-y1
    if names[int(cls)] == "car":
        c = int(cls)  # integer class  整数类 1111111111
        label = None if hide_labels else (
            names[c] if hide_conf else f'{names[c]} {conf:.2f}')  # 111
        dis_m = car_distance(h)
        label += f'  {dis_m}m'
        txt = '{0}'.format(label)
        if dis_m<8:
            annotator.box_label(xyxy, txt, color=(0, 0, 255))
        else:
            annotator.box_label(xyxy, txt, color=(0, 255, 0))

请添加图片描述

4.2 主代码

# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
import argparse
import os
import sys
from pathlib import Path

import cv2
import torch
import torch.backends.cudnn as cudnn

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))  # add ROOT to PATH
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
from distance import person_distance,car_distance

@torch.no_grad()
def run(weights=ROOT / 'yolov5s.pt',  # model.pt path(s)
        source=ROOT / 'data/images',  # file/dir/URL/glob, 0 for webcam
        data=ROOT / 'data/coco128.yaml',  # dataset.yaml path
        imgsz=(640, 640),  # inference size (height, width)
        conf_thres=0.25,  # confidence threshold
        iou_thres=0.45,  # NMS IOU threshold
        max_det=1000,  # maximum detections per image
        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu
        view_img=False,  # show results
        save_txt=False,  # save results to *.txt
        save_conf=False,  # save confidences in --save-txt labels
        save_crop=False,  # save cropped prediction boxes
        nosave=False,  # do not save images/videos
        classes=None,  # filter by class: --class 0, or --class 0 2 3
        agnostic_nms=False,  # class-agnostic NMS
        augment=False,  # augmented inference
        visualize=False,  # visualize features
        update=False,  # update all models
        project=ROOT / 'runs/detect',  # save results to project/name
        name='exp',  # save results to project/name
        exist_ok=False,  # existing project/name ok, do not increment
        line_thickness=3,  # bounding box thickness (pixels)
        hide_labels=False,  # hide labels
        hide_conf=False,  # hide confidences
        half=False,  # use FP16 half-precision inference
        dnn=False,  # use OpenCV DNN for ONNX inference
        ):
    source = str(source)
    save_img = not nosave and not source.endswith('.txt')  # save inference images
    is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
    is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
    webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file)
    if is_url and is_file:
        source = check_file(source)  # download

    # Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

    # Load model
    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)  # check image size

    # Half
    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()

    # Dataloader
    if webcam:
        view_img = check_imshow()
        cudnn.benchmark = True  # set True to speed up constant image size inference
        dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt)
        bs = len(dataset)  # batch_size
    else:
        dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
        bs = 1  # batch_size
    vid_path, vid_writer = [None] * bs, [None] * bs

    # Run inference
    model.warmup(imgsz=(1 if pt else bs, 3, *imgsz), half=half)  # warmup
    dt, seen = [0.0, 0.0, 0.0], 0
    for path, im, im0s, vid_cap, s in dataset:
        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
        visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
        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

        # Second-stage classifier (optional)
        # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)

        # Process predictions
        for i, det in enumerate(pred):  # per image
            seen += 1
            if webcam:  # batch_size >= 1
                p, im0, frame = path[i], im0s[i].copy(), dataset.count
                s += f'{i}: '
            else:
                p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)

            p = Path(p)  # to Path
            save_path = str(save_dir / p.name)  # im.jpg
            txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}')  # im.txt
            s += '%gx%g ' % im.shape[2:]  # print string
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
            imc = im0.copy() if save_crop else im0  # for save_crop
            annotator = Annotator(im0, line_width=line_thickness, example=str(names))
            if len(det):
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()

                # Print results
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string

                # Write results
                for *xyxy, conf, cls in reversed(det):
                    if save_txt:  # Write to file
                        xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
                        line = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format
                        with open(txt_path + '.txt', 'a') as f:
                            f.write(('%g ' * len(line)).rstrip() % line + '\n')

                    if save_img or save_crop or view_img:  # Add bbox to image
                        x1 = int(xyxy[0])
                        y1 = int(xyxy[1])
                        x2 = int(xyxy[2])
                        y2 = int(xyxy[3])
                        h = y2-y1
                        if names[int(cls)] == "car":
                            c = int(cls)  # integer class  整数类 1111111111
                            label = None if hide_labels else (
                                names[c] if hide_conf else f'{names[c]} {conf:.2f}')  # 111
                            dis_m = car_distance(h)
                            label += f'  {dis_m}m'
                            txt = '{0}'.format(label)
                            if dis_m<8:
                                annotator.box_label(xyxy, txt, color=(0, 0, 255))
                            else:
                                annotator.box_label(xyxy, txt, color=(0, 255, 0))
                            # annotator.box_label(xyxy, txt, color=colors(c, True))

                        if save_crop:
                            save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)

            # Stream results
            im0 = annotator.result()
            '''if view_img:
                cv2.imshow(str(p), im0)
                cv2.waitKey(1)  # 1 millisecond'''
            if view_img:
                cv2.namedWindow("Webcam", cv2.WINDOW_NORMAL)
                cv2.resizeWindow("Webcam", 1280, 720)
                cv2.moveWindow("Webcam", 0, 100)
                cv2.imshow("Webcam", im0)
                cv2.waitKey(1)

            # Save results (image with detections)
            if save_img:
                if dataset.mode == 'image':
                    cv2.imwrite(save_path, im0)
                else:  # 'video' or 'stream'
                    if vid_path[i] != save_path:  # new video
                        vid_path[i] = save_path
                        if isinstance(vid_writer[i], cv2.VideoWriter):
                            vid_writer[i].release()  # release previous video writer
                        if vid_cap:  # video
                            fps = vid_cap.get(cv2.CAP_PROP_FPS)
                            w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                            h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        else:  # stream
                            fps, w, h = 30, im0.shape[1], im0.shape[0]
                        save_path = str(Path(save_path).with_suffix('.mp4'))  # force *.mp4 suffix on results videos
                        vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
                    vid_writer[i].write(im0)

        # Print time (inference-only)
        LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')

    # Print results
    t = tuple(x / seen * 1E3 for x in dt)  # speeds per image
    LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
    if save_txt or save_img:
        s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
        LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
    if update:
        strip_optimizer(weights)  # update model (to fix SourceChangeWarning)


def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
    parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob, 0 for webcam')
    parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
    parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
    parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
    parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='show results')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--visualize', action='store_true', help='visualize features')
    parser.add_argument('--update', action='store_true', help='update all models')
    parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')
    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
    parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
    parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
    parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
    parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
    opt = parser.parse_args()
    opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expand
    print_args(FILE.stem, opt)
    return opt

def main(opt):
    check_requirements(exclude=('tensorboard', 'thop'))
    run(**vars(opt))

if __name__ == "__main__":
    opt = parse_opt()
    main(opt)

5. 实验效果

实验效果如下,如果把相机架在车上,效果会更好

YOLOv5测距+碰撞检测

工程源码下载链接:https://github.com/up-up-up-up/yolov5_Monocular_security_testing

更多测距代码见博客主页

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/496749.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

string的介绍

string是c中表示字符串的字符串类&#xff0c;要使用需要包头文件&#xff1a;#include<string> 先了解一下string的一些信息 string看起来是一个类&#xff0c;但实际上是typedef的模板。 在cplusplus.com网站上&#xff0c;string的相关信息 模板的实例化结果有以下几个…

【unity小创意】相机的正反操作实现场景的二维跳跃

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;uni…

Spring的第十二阶段(01):Spring实现AOP的简单使用

1、使用Spring实现AOP简单切面编程 需要导入工程的jar包 Spring的核心包 spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jarSpring的测试包 spring-test-4.0.0.RELEASE.jarSpring日记相…

【网络安全】这套面试题,让你提前预判面试官的预判!

最近这个帖子的点赞和收藏变高起来了&#xff0c;许多小伙伴在问我安全大厂的面试题没有&#xff0c;我准备利用一些时间把这套面试宝典整理一下&#xff01; 今天有同学拿着他准备的面试问题清单给我看&#xff0c;看还有没有遗漏的&#xff0c;我看了下&#xff0c;觉得还是…

Spring-Bean管理-注解

组件注册 Component/Controller/Service/Repostory :注册自定义组件到容器中 加上约定的注解。 在Configuration注解的类中配置包扫描器 ComponentScan(vlaue "cn.shaoxiongdu") Configuration: 标注配置类 Scope &#xff1a; 配置是否为单实例 prototype: 多实…

JVM与GC

Java:跨平台的语言 write once, run anywhere JVM&#xff1a;跨语言的平台 Java虚拟机根本不关心运行在其内部的程序到底是使用何种编程语言编写的&#xff0c;它只关心“字节码”文件。 Java不是最强大的语言&#xff0c;但是JVM是最强大的虚拟机。 JVM的整体结构 这个架构…

Java基础学习(12)

Java基础学习 一、不可变集合二、Stream流2.1 Stream流数据添加2.2 Stream流的中间方法2.3 Stream终结方法 三、 方法引用3.1 方法引用的基本概念3.2 方法引用的分类3.2.1 引用静态方法 3.2.2 引用成员方法3.2.3 引用构造方法3.2.4 使用类名引用成员方法3.2.5 引用数组的构造方…

PyQt5 基础篇(一)-- 安装与环境配置

1 PyQt5 图形界面开发工具 Qt 库是跨平台的 C 库的集合&#xff0c;是最强大的 GUI 库之一&#xff0c;可以实现高级 API 来访问桌面和移动系统的各种服务。PyQt5 是一套 Python 绑定 Digia QT5 应用的框架。PyQt5 实现了一个 Python模块集&#xff0c;有 620 个类&#xff0c;…

MATLAB 点云非均匀体素下采样 (8)

MATLAB 点云非均匀体素下采样的不同参数效果测试 (8) 一、实现效果二、算法介绍三、函数说明3.1 函数3.2 参数四、实现代码(详细注释!)五、与固定步长采样法比较5.1 代码5.2 效果一、实现效果 不同参数调整下的非均匀体素下采样结果如下图所示,后续代码复制黏贴即可: 可…

入职6个月,被裁了...

我跟大多数人不大一样&#xff0c;从来没有说要等公司主动裁员拿补偿&#xff0c;我看自己没有什么价值或者是公司不行了&#xff0c;我都会主动离职。但是这次也太突然了。公司很大已上市&#xff0c;并不是不行了&#xff0c;总结原因就是&#xff0c;一是领导无能&#xff0…

【STM32CubeMX】F103ADC获取

前言 本文记录了我学习STM32CubeMX的过程&#xff0c;方便以后回忆。我们使用的开发板是基于STM32F103C6T6的。本章记录了基本的ADC值的获取流程&#xff0c;只单纯地记录了ADC端口的配置&#xff0c;没有加配像串口之类的调试&#xff0c;以简化流程。下面的流程是使用串口调试…

版本控制系统Git - 配置与基本使用

Git 1 Git简介1 Git概述2 Git的作用2.1 项目版本管理2.2 多人协同开发2.3 Git 的结构2.4 Git的工作原理 2 Git安装1 下载Git2 安装Git3 配置环境变量4 测试git是否安装成功5 安装git桌面工具(可以不安装) 3 Git基本操作1 设置Git用户2 新建仓库3 查看仓库状态4 添加到暂存文件5…

MATLAB 点云重复点去除(7)

MATLAB 点云重复点去除 (7) 一、实现效果二、算法介绍三、函数说明3.1 函数3.2 参数四、具体代码 (注释详细!)一、实现效果 效果上看不出来,但实际上左边的点云是右边的两倍 二、算法介绍 重复点的去除,是点云处理中常用的预处理方法,因为重复点的存在有时候会严重干…

C++系列四:数组

数组 1. 数组定义与初始化2. 多维数组3. 字符数组4. 总结 1. 数组定义与初始化 定义数组时需要指定数组的类型和大小&#xff1a; int myArray[10];上述代码定义了一个包含 10 个整数的数组。这些整数的下标从 0 开始&#xff0c;并以 1 个单位递增。 C 允许在定义数组时对其…

设计模式——装饰者模式(继承和接口的两种实现方式)

是什么&#xff1f; 场景案例&#xff1a;想必大家都吃过手抓饼吧&#xff0c;我们在点手抓饼的时候可以选择加培根、鸡蛋、火腿、肉松等等这些配菜&#xff0c;当然这些配菜的价格都不一样&#xff0c;那么计算总价就比较麻烦&#xff1b; 装饰者模式就是指在不改变现有对象…

springboot 集成 shardingSphere 加mybatisplus 自带增加 分页查询 和源代码包 分库分表 单库 分表 使用雪花算法id

目录 介绍 代码下载 效果 数据库 代码结构 上代码 pom.xml yml配置 建表语句 mapper.xml mybatisplus 配置.java logback application.java BaseEntity TUser TUserMapper TUserService TUserServiceImpl TUserController 测试 介绍 这套springboot shardi…

OSG笔记:AutoTransform实现固定像素大小的图形

需求 在(200,0,0)位置绘制固定10像素大小的正方体 实现方式 为了便于观察&#xff0c;例子中绘制了两条直线&#xff0c;相交于(200,0,0)。 //两根直线交于(200, 0, 0)&#xff0c;用于辅助观察 {osg::Geometry* pLineGeom new osg::Geometry();osg::Vec3Array* pVertexArra…

python语法复习

print&#xff1a;输出函数 print(520)效果&#xff1a;输出520. print(hello)效果&#xff1a;输出hello. print(1020)【效果&#xff1a;输出了:1020】注&#xff1a;“ ”在print里面是一个连接符。 print(1020)【效果&#xff1a;输出了30】注&#xff1a; 在此处…

OJ 系统常用功能介绍 快速入门 C++ Python JAVA语言在线评测

技术支持微 makytony 服务器配置 腾讯云 2H4G 5M 60GB 轻量应用服务器 承载大约 200~400人使用&#xff0c;经过压力测试&#xff0c;评测并发速度可满足130人左右的在线比赛。 系统镜像选 Ubuntu 22.04 LTS &#xff0c;Ubuntu是最热门的Linux发行版之一&#xff0c;是一款…

洞车系统常见问题解决指南

洞车常见问题解决指南 1.研发脚本处理问题1.1 WMS出库单无法审核1.2 OMS入库单无法提交&#xff0c;提示更新中心库存失败1.3 当出现OMS下发成功WMS/TMS/DMS还没有任务的情况时处理方案1.4 调度波次生成或者添加任务系统异常1.5 东鹏出库单部分出库回传之后要求重传1.6 更新订单…