用python+YOLOV8图片车辆车距

1. 检测车辆:

使用深度学习模型(例如 YOLO、Mask R-CNN)来检测照片中的车辆,并得到每辆车的边界框(Bounding Box)。
工具与技术:
  • YOLOv5/YOLOv8:高效的实时目标检测模型。
  • OpenCV:进行边界框绘制和图像处理。
  • Detectron2:适合高精度对象检测。

2. 计算两车的距离:

根据照片中两辆车的边界框,选择适当的点(例如中心点或边界最近点),计算这两个点的像素距离。
公式:
如果选择中心点作为测量依据:Distance=(x2−x1)2+(y2−y1)2\text{Distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}Distance=(x2​−x1​)2+(y2​−y1​)2​
其中:
  • (x1,y1)(x_1, y_1)(x1​,y1​) 和 (x2,y2)(x_2, y_2)(x2​,y2​) 是两辆车中心点的像素坐标。
有许多现成的大模型可以用于目标检测和距离估算任务。以下是推荐的一些大模型及其使用场景:

1. YOLO 系列 (You Only Look Once)

  • 特点:
    • 实时目标检测,速度快。
    • 可检测照片中的多个目标并返回边界框和类别信息。
  • 模型版本:
    • YOLOv5 (最流行)
    • YOLOv8 (最新版本,支持更高精度和简单部署)
  • 适用场景:
    • 检测照片中的车辆。
    • 通过边界框计算距离。
  • 工具链:

2. Detectron2

  • 特点:
    • Facebook AI 开发,支持多种高级任务,如目标检测、实例分割。
    • 精度高,适合需要精准定位的任务。
  • 适用场景:
    • 检测车辆并输出更准确的边界框。
    • 如果车辆有遮挡,这个模型效果更好。
  • 工具链:

3. Segment Anything Model (SAM)

  • 特点:
    • Meta AI 开发,用于分割任何目标。
    • 输入一张图片,可以直接分割出车辆的轮廓。
  • 适用场景:
    • 获取车辆的精确轮廓,便于更精准的距离计算。
    • 无需训练即可快速分割图像中的目标。
  • 工具链:

4. OpenCV DNN 模块

  • 特点:
    • 集成了常用的大模型,如 MobileNet-SSD、YOLO、ResNet。
    • 易于部署,不依赖复杂的深度学习框架。
  • 适用场景:
    • 简单目标检测,不需要复杂模型部署时。
  • 工具链:

5. DeepSORT(目标跟踪与检测结合)

  • 特点:
    • 结合 YOLO 和目标跟踪算法。
    • 在多目标场景中,可持续跟踪车辆的位置。
  • 适用场景:
    • 检测两辆车并在运动视频中实时更新它们的距离。

使用预训练的YOLOv8模型来检测车辆,并计算它们之间的距离。

第一步:需要下载的库

pip install opencv-python-headless  # 如果需要无头版本
pip install opencv-python           # 安装带有GUI支持的版本
pip install torch torchvision
pip install ultralytics

第二步:下载数据库,

要连接到 Microsoft SQL Server 数据库,您可以使用 pyodbc 或者 pymssql 这样的 Python 库。

需要下载插件 微软的

pip install pyodbc

第三步:demo

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from ultralytics import YOLO

# 加载预训练的YOLOv5模型
model = YOLO('yolov8s.pt')

# 读取图像
image_path = 'C:/Users/licam/Desktop/ba0f18d2d8b17488ad28f7cd587ea06.jpg'
image = cv2.imread(image_path)

# 使用YOLOv5进行检测
results = model(image)

# 获取检测结果
detections = results[0].boxes.xyxy.cpu().numpy()

# 定义像素到米的转换比例因子(根据实际情况调整)
PIXELS_TO_METERS = 0.05  # 每个像素代表0.01米


def calculate_distance(box1, box2):
    center1 = ((box1[0] + box1[2]) / 2, (box1[1] + box1[3]) / 2)
    center2 = ((box2[0] + box2[2]) / 2, (box2[1] + box2[3]) / 2)
    distance_pixels = ((center1[0] - center2[0]) ** 2 + (center1[1] - center2[1]) ** 2) ** 0.5
    distance_meters = distance_pixels * PIXELS_TO_METERS
    return distance_meters


# 存储所有距离信息以供打印
all_distances = []

# 绘制检测框和距离,并收集距离信息
for i in range(len(detections)):
    distances_text = ""
    for j in range(len(detections)):
        if i != j:
            distance = calculate_distance(detections[i], detections[j])
            distances_text += f'{distance:.2f}m\n'
            all_distances.append(f"Vehicle {i + 1} to Vehicle {j + 1}: {distance:.2f}m")

    distances_text = distances_text.rstrip('\n')
    top_left = (int(detections[i][0]), int(detections[i][1]) - 10)

    text_size, _ = cv2.getTextSize(distances_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    if top_left[1] - text_size[1] < 0:
        top_left = (top_left[0], top_left[1] + text_size[1] + 10)

    cv2.putText(image, distances_text, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)

    cv2.rectangle(image, (int(detections[i][0]), int(detections[i][1])),
                  (int(detections[i][2]), int(detections[i][3])), (0, 255, 0), 2)

# 打印所有车辆之间的距离
print("Distances between vehicles:")
for dist in all_distances:
    print(dist)

# 保存处理后的图像
output_image_path = 'C:/Users/licam/Desktop/processed_image_with_distances.jpg'
cv2.imwrite(output_image_path, image)

print(f"\nProcessed image saved to: {output_image_path}")

# 显示图像
cv2.imshow('Detected Vehicles', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值