一、效果演示
- Colorimage:
- Colorimage and depthimage:
二、環(huán)境配置
1.一個可以運行YOLOv5的python環(huán)境
pip install -r requirements.txt
2.一個realsense相機和pyrealsense2庫
pip install pyrealsense2
在下面兩個環(huán)境中測試成功
-
win10 python 3.8 Pytorch 1.10.2+gpu CUDA 11.3 NVIDIA GeForce MX150
-
ubuntu16.04 python 3.6 Pytorch 1.7.1+cpu
三、模型配置
修改模型配置文件,以yolov5s為例。
如果使用自己訓練的模型,需要進行相應的修改。
weight: "weights/yolov5s.pt"
# 輸入圖像的尺寸
input_size: 640
# 類別個數(shù)
class_num: 80
# 標簽名稱
class_name: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush' ]
# 閾值設置
threshold:
iou: 0.45
confidence: 0.6
# 計算設備
# - cpu
# - 0 <- 使用GPU
device: '0'
四、相機配置
分辨率好像只能改特定的參數(shù),不然會報錯。d435i可以用 1280x720, 640x480, 848x480。
pipeline = rs.pipeline() # 定義流程pipeline
config = rs.config() # 定義配置config
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
profile = pipeline.start(config) # 流程開始
五、部分代碼:
下方代碼實現(xiàn)從像素坐標系到相機坐標系轉換,并且標注中心點以及三維坐標信息。文章來源:http://www.zghlxwxcb.cn/news/detail-439669.html
for i in range(len(xyxy_list)):
ux = int((xyxy_list[i][0]+xyxy_list[i][2])/2) # 計算像素坐標系的x
uy = int((xyxy_list[i][1]+xyxy_list[i][3])/2) # 計算像素坐標系的y
dis = aligned_depth_frame.get_distance(ux, uy)
camera_xyz = rs.rs2_deproject_pixel_to_point(
depth_intrin, (ux, uy), dis) # 計算相機坐標系xyz
camera_xyz = np.round(np.array(camera_xyz), 3) # 轉成3位小數(shù)
camera_xyz = camera_xyz.tolist()
cv2.circle(canvas, (ux,uy), 4, (255, 255, 255), 5)#標出中心點
cv2.putText(canvas, str(camera_xyz), (ux+20, uy+10), 0, 1,
[225, 255, 255], thickness=2, lineType=cv2.LINE_AA)#標出坐標
camera_xyz_list.append(camera_xyz)
#print(camera_xyz_list)
六、倉庫鏈接:
代碼已上傳github:yolov5_d435i_detection
文章來源地址http://www.zghlxwxcb.cn/news/detail-439669.html
到了這里,關于Realsense D435i Yolov5目標檢測實時獲得目標三維位置信息的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!