# realsense-viewer ![image](https://hackmd.io/_uploads/S19o3dz3eg.png) ## Intel® RealSense™ D435 深度攝影機 Intel® RealSense™ 深度攝影機 D435 是一款小巧輕便、可量測最遠 **10 公尺**,能為各種應用提供高品質深度偵測。 ## 主要模組組成 ![image](https://hackmd.io/_uploads/rkE2hOGhxx.png) ### IR Projector - 功能:產生紅外點陣。 - 目的:在「無紋理表面」(如白牆、平滑桌面) 也能提供可比對的特徵點。 - 好處:提升深度資訊的穩定性與精確度。 ### Right / Left IR Imager - 功能:接收 **紅外點陣**,並透過 **雙目立體視覺演算法**計算深度。 - 原理:比較左右視角的視差 (disparity) → 得出距離資訊。 ### RGB Module - 功能:拍攝彩色影像。 - 搭配方式:與深度資訊對齊 (depth alignment),生成帶有顏色的資料。 --- ## 工作流程示意 1. **IR Projector** 投射紅外點陣到場景。 2. **左右 IR 相機** (stereo imagers) 接收紅外點陣,計算深度。 3. **RGB Module** 拍攝彩色影像。 4. 系統將彩色影像與深度圖對齊 → 得到 **彩色視覺**。 --- ### Jetson 上安裝 librealsense #### 1. 安裝相依套件 ```bash sudo apt update sudo apt install -y git cmake build-essential libssl-dev libusb-1.0-0-dev pkg-config \ libgtk-3-dev libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev v4l-utils ``` #### 2. 下載原始碼並設定 udev 規則 ```bash git clone https://github.com/IntelRealSense/librealsense wget https://raw.githubusercontent.com/IntelRealSense/librealsense/master/config/99-realsense-libusb.rules -O 99-realsense-libusb.rules sudo cp 99-realsense-libusb.rules /etc/udev/rules.d/ sudo udevadm control --reload-rules sudo udevadm trigger ``` #### 3. 編譯安裝 ```bash cd ~/librealsense mkdir build && cd build cmake .. -DFORCE_RSUSB_BACKEND=ON -DBUILD_EXAMPLES=ON -DBUILD_GRAPHICAL_EXAMPLES=ON make -j$(nproc) sudo make install sudo ldconfig ``` #### 4. 測試 插入 RealSense 相機,然後執行: ```bash realsense-viewer ``` 這是一個 GUI 工具,可以看到 RGB 影像及深度影像。 ### Demo ![image](https://hackmd.io/_uploads/HykqZ_z3le.png) #### 下載人臉辨識模型 (Haar Cascade) 使用 Haar Cascade 進行人臉辨識。先下載所需的模型: ```bash wget https://raw.githubusercontent.com/GeekyShiva/OpenCV-miniProject-FaceDetection/master/haarcascade_frontalface_default.xml ``` :::spoiler face_detect.py ```python= import pyrealsense2 as rs import numpy as np import cv2 # 建立管線 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) # 開始串流 pipeline.start(config) # 對齊彩色與深度 align_to = rs.stream.color align = rs.align(align_to) try: while True: # 取得對齊後的影像 frames = pipeline.wait_for_frames() aligned_frames = align.process(frames) depth_frame = aligned_frames.get_depth_frame() color_frame = aligned_frames.get_color_frame() if not depth_frame or not color_frame: continue # 轉 NumPy 陣列 depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # 假彩色深度圖 depth_colormap = cv2.applyColorMap( cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET ) # 載入人臉分類器 (opencv haarcascade.xml 實際路徑) face_cascade = cv2.CascadeClassifier( '/home/test/haarcascade_frontalface_default.xml' ) # 灰階化處理 gray = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY) # 偵測人臉 faces = face_cascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=5, minSize=(50, 50) ) # 逐一處理每張臉 for (x, y, w, h) in faces: # 畫人臉框 cv2.rectangle(color_image, (x, y-5), (x+w, y+h), (255, 0, 0), 2) # 取人臉框中心點深度值 dist = depth_frame.get_distance(int(x + w/2), int(y + h/2)) text_depth = f"depth is {np.round(dist, 3)}m" # 在影像上顯示距離 color_image = cv2.putText( color_image, text_depth, (x, y-5), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 1, cv2.LINE_AA ) # 拼接顯示 images = np.hstack((color_image, depth_colormap)) cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE) cv2.imshow('RealSense', images) # 按 q 或 ESC 離開 key = cv2.waitKey(1) if key & 0xFF == ord('q') or key == 27: cv2.destroyAllWindows() break finally: pipeline.stop() ``` ::: ### 取得座標 1. 你有一個物體的影像座標 (x, y) 這個 (x, y) 可能是你用 OpenCV 偵測到的 bounding box 中心點、或者是人臉偵測出來的點。 🔹 2. 從深度圖取得該點的 Z ```python z = depth_frame.get_distance(x, y) # 單位:公尺 ``` 參考資料 : https://blog.cavedu.com/2020/06/20/intel-realsense-d435i-x-jetson-nano/ https://github.com/IntelRealSense/librealsense https://github.com/GeekyShiva/OpenCV-miniProject-FaceDetection/blob/master/haarcascade_frontalface_default.xml