yolov5实现单目测距的代码
时间: 2023-07-11 19:20:05 浏览: 182
使用YOLOv5实现单目测距可以分为以下几个步骤:
1. 对相机进行标定,获取相机的内参和外参矩阵。
2. 使用YOLOv5检测出场景中的物体,并获取其在图像中的尺寸。
3. 根据相机内参和外参矩阵,将物体在图像中的位置转换为相机坐标系下的坐标。
4. 根据物体在相机坐标系下的坐标和相机的位置,计算出物体到相机的距离。
下面是使用YOLOv5实现单目测距的代码示例:
```python
import cv2
import numpy as np
import torch
import time
from models.experimental import attempt_load
from utils.general import non_max_suppression
from utils.torch_utils import select_device
class YOLOv5Detector:
def __init__(self, weights_path='yolov5s.pt', device='cpu'):
self.device = select_device(device)
self.model = attempt_load(weights_path, map_location=self.device)
self.model.eval()
def detect(self, img, conf_thres=0.5, iou_thres=0.5):
img = torch.from_numpy(img.transpose(2, 0, 1)).float().div(255.0).unsqueeze(0).to(self.device)
pred = self.model(img)[0]
pred = non_max_suppression(pred, conf_thres=conf_thres, iou_thres=iou_thres)
res = []
for det in pred:
if det is not None and len(det):
det[:, :4] = det[:, :4].clamp(0, img.shape[3])
det[:, 5] *= det[:, 4]
for box in det:
x1, y1, x2, y2, conf, cls = box.tolist()
res.append([x1, y1, x2, y2, conf, int(cls)])
return res
def calc_distance(K, R, T, width, height, bbox):
# 将bbox中心点的坐标由图像坐标系转换为相机坐标系
x_center = (bbox[0] + bbox[2]) / 2.0
y_center = (bbox[1] + bbox[3]) / 2.0
p2d = np.array([[x_center, y_center]], dtype=np.float32)
p3d = cv2.undistortPoints(p2d, K, np.array([0, 0, 0, 0]), R, K)
# 计算物体到相机的距离
z = T[0, 2] / (p3d[0, 0] - T[0, 0])
return z
if __name__ == '__main__':
# 加载YOLOv5模型
detector = YOLOv5Detector(weights_path='yolov5s.pt', device='cpu')
# 加载相机标定文件
with np.load('calibration.npz') as calib:
K, D, R, T = [calib[i] for i in ('K', 'D', 'R', 'T')]
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 检测物体并计算距离
start_time = time.time()
bbox_list = detector.detect(frame)
for bbox in bbox_list:
x1, y1, x2, y2, conf, cls = bbox
width = x2 - x1
height = y2 - y1
distance = calc_distance(K, R, T, width, height, bbox)
print('class: {}, distance: {:.2f}m'.format(cls, distance))
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, 'class: {}, distance: {:.2f}m'.format(cls, distance), (int(x1), int(y1) - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
end_time = time.time()
fps = 1.0 / (end_time - start_time)
cv2.putText(frame, 'FPS: {:.2f}'.format(fps), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# 显示图像
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
需要注意的是,这里的相机标定文件是使用OpenCV的calibrateCamera函数进行标定得到的。在实际应用中,需要根据实际情况进行标定并获取相机的内参和外参矩阵。
阅读全文