realsense D455深度相機(jī)+YOLO V5結(jié)合實(shí)現(xiàn)目標(biāo)檢測(cè)(二)第二篇鏈接
可以實(shí)現(xiàn)將D435,D455深度相機(jī)和yolo v5結(jié)合到一起,在識(shí)別物體的同時(shí),還能測(cè)到物體相對(duì)與相機(jī)的距離。
說(shuō)明一下為什么需要做這個(gè)事情?1.首先為什么需要用到realsense D455深度相機(jī)? 因?yàn)樗瞧胀ǖ南鄼C(jī)還加了一個(gè)紅外測(cè)距的東西,所以其他二維圖像一樣,能夠得到三維世界在二維像素平面的投影,也就是圖片,但是我們損失了一個(gè)深度的維度以后得到的都是投影的東西,比如說(shuō)蘋果可以和足球一樣大,因?yàn)槲覀儾恢郎疃纫簿褪俏矬w距離相機(jī)的距離信息,所以我們需要一個(gè)深度相機(jī)來(lái)實(shí)現(xiàn)測(cè)距離。2.為什么需要用到y(tǒng)olo算法?因?yàn)樗趯?shí)時(shí)性和準(zhǔn)確率方面都可以,可以應(yīng)用于工農(nóng)業(yè)生產(chǎn)當(dāng)中,所以肯定很需要。所以才會(huì)有這二者的結(jié)合的必要性!
1.代碼來(lái)源
首先感謝github上的yuanyuanyuan killnice大佬將自己的代碼開源出來(lái),這是我第一次用realsense深度相機(jī)去實(shí)現(xiàn)將其與目標(biāo)檢測(cè)的yolo v5 算法結(jié)合在一起,實(shí)現(xiàn)2.5維的檢測(cè)吧。首先大家如果想用這個(gè)代碼的話可以去這里git clone yuanyuanyuan killnice大佬的代碼 (為了防止鏈接不過(guò)去還是再寫在這里 https://github.com/killnice/yolov5-D435i.git)。
2.環(huán)境配置
首選clone下yuanyuanyuan killnice大佬的代碼后,在命令行運(yùn)行:
pip install -r requirements.txt
pip install pyrealsense2
然后cd到進(jìn)入工程文件夾下執(zhí)行:
python main_debug.py
第一次運(yùn)行會(huì)自動(dòng)下載yolov5l6.pt文件,大約140MB左右,如果由于網(wǎng)速原因不能自動(dòng)下載,請(qǐng)手動(dòng)在yolo v5的github上自己下載。運(yùn)行結(jié)果如下:
3.代碼分析:
3.1如何用realsense在python下面調(diào)用的問(wèn)題:
這個(gè)的來(lái)源是從realsense的官方文檔中來(lái)的 realsense的官方python文檔,如果你對(duì)他的其他應(yīng)用感興趣可以去這里看一看。
import pyrealsense2 as rs
import numpy as np
import cv2
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
# config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
# config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
# 深度圖
depth_frame = frames.get_depth_frame()
# 正常讀取的視頻流
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# print(f"depth_image shape: {depth_image.shape} color_image shape: {color_image.shape}")
print(f"depth_image value: {depth_image}") # 里面0值很多,還有很多1900左右的值 300mm 單位是毫米=30厘米=0.3米
# depth_image shape: (480, 640) color_image shape: (480, 640, 3)
# 深度圖是單通道 顏色圖是三通道的
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
# 在深度圖像上應(yīng)用colormap(圖像必須先轉(zhuǎn)換為每像素8位)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
key = cv2.waitKey(1)
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
這就是用python調(diào)用realsense D455深度相機(jī)的程序。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-456913.html
3.2 對(duì)main_debug.py文件的分析:
import pyrealsense2 as rs
import numpy as np
import cv2
import random
import torch
import time
#調(diào)用各種庫(kù)
#從文件的構(gòu)造中不難看出是用了下面的幾個(gè)函數(shù):分別是get_mid_pos(frame,box,depth_data,randnum),dectshow(org_img, boxs,depth_data),
#主函數(shù)。
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
#
model = torch.hub.load('ultralytics/yolov5', 'yolov5l6')
model.conf = 0.5
def get_mid_pos(frame,box,depth_data,randnum):
#這個(gè)函數(shù)就是簡(jiǎn)單的從給出的圖片、框、深度數(shù)據(jù)、可以選擇的迭代次數(shù)
distance_list = []
mid_pos = [(box[0] + box[2])//2, (box[1] + box[3])//2] #確定索引深度的中心像素位置左上角和右下角相加在/2
min_val = min(abs(box[2] - box[0]), abs(box[3] - box[1])) #確定深度搜索范圍
#print(box,)
for i in range(randnum):
bias = random.randint(-min_val//4, min_val//4)
dist = depth_data[int(mid_pos[1] + bias), int(mid_pos[0] + bias)]
cv2.circle(frame, (int(mid_pos[0] + bias), int(mid_pos[1] + bias)), 4, (255,0,0), -1)
#print(int(mid_pos[1] + bias), int(mid_pos[0] + bias))
if dist:
distance_list.append(dist)
distance_list = np.array(distance_list)
distance_list = np.sort(distance_list)[randnum//2-randnum//4:randnum//2+randnum//4] #冒泡排序+中值濾波
#print(distance_list, np.mean(distance_list))
return np.mean(distance_list)
def dectshow(org_img, boxs,depth_data):
#在原圖像上畫框和深度信息寫在圖像上
img = org_img.copy()
for box in boxs:
cv2.rectangle(img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
dist = get_mid_pos(org_img, box, depth_data, 24)
cv2.putText(img, box[-1] + str(dist / 1000)[:4] + 'm',
(int(box[0]), int(box[1])), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.imshow('dec_img', img)
if __name__ == "__main__":
# Configure depth and color streams
#這是主要是將上面1中提到的realsense在python中的調(diào)用。
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
results = model(color_image)
boxs= results.pandas().xyxy[0].values
#boxs = np.load('temp.npy',allow_pickle=True)
dectshow(color_image, boxs, depth_image)
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
4. 結(jié)束語(yǔ)
從以前在csdn上查找到自己寫,這是第一次,覺(jué)得這是一種進(jìn)步,希望能夠幫助到別人,也感謝yuanyuanyuan killnice大佬,還有yolo v5的作者,反正就是感謝開源吧。對(duì)我感興趣的童鞋可以關(guān)注我,說(shuō)不定那一天就可以幫到您!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-456913.html
到了這里,關(guān)于realsense D455深度相機(jī)+YOLO V5結(jié)合實(shí)現(xiàn)目標(biāo)檢測(cè)(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!