최종적으로 시각 정보를 사용하기 위해 Intel RealSense Viewer 를 사용할 것이 아니고,
python3 openCV를 이용해서 RGB Stream과 Depth Stream 을 가져오고
python에서 정보를 처리하길 원했기 때문에 이를 구현한 방법과 코드를 설명하겠습니다.
저는 간단하게 cv2.VideoCapture(0) 로 가져와봤는데,
L515의 적외선 비디오 채널이 추출되는 이상한 결과가 나타났습니다.
조금 알아보니 다르게 정보를 받아오는 형태여서
이를 간단하게 구현해보았습니다.
먼저 필요한 라이브러리 설치해줍니다.
cv2는 가상환경에서 GPU를 위해 빌드하셨든,
CPU 환경이든 적절히 import 만 되면 됩니다.
저는 OpenCV 4.9.0에서는 성공하였지만,
CUDA 를 위해 빌드한 python3.6 기반 가상환경에서 opencv 4.5.5를 활용한 경우
ImportError: /home/user/anaconda3/envs/YourEnv/bin/../lib/libstdc++.so.6: version GLIBCXX_3.4.30' not found (required by /home/user/anaconda3/envs/YourEnv/lib/libopencv_gapi.so.405)`
이런 에러가 뜨면서 cv2가 import 되지 않는 에러가 발생했고,
gcc 버전을 맞춰 cmake를 다시 빌드했음에도 에러가 발생하였습니다.
그래서 간단하게 pip install opencv-python 명령어 로 설치한
opencv 4.9.0에서 수행해보았습니다.
추후 추가로 확인해보니
gcc 호환 에러라고 하니 gcc를 업그레이드 하여 해결 해줍니다.
conda install -c conda-forge gcc=12.1.0
pyrealsense2 라이브러리를 설치합니다.
pip install pyrealsense2
이미지 정보를 pyrealsense2 라이브러리의 pipeline 을 통해 얻어옵니다.
아래 코드는 Intel RealSense D435와 L515 에서 모두 테스트 성공하였습니다.
import pyrealsense2 as rs | |
import numpy as np | |
import cv2 | |
pipeline = rs.pipeline() | |
config = rs.config() | |
# Get device product line for setting a supporting resolution | |
pipeline_wrapper = rs.pipeline_wrapper(pipeline) | |
pipeline_profile = config.resolve(pipeline_wrapper) | |
device = pipeline_profile.get_device() | |
device_product_line = str(device.get_info(rs.camera_info.product_line)) | |
found_rgb = False | |
for s in device.sensors: | |
if s.get_info(rs.camera_info.name) == 'RGB Camera': | |
found_rgb = True | |
break | |
if not found_rgb: | |
print("RGB Channel Not Found!") | |
exit(0) | |
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) | |
config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30) | |
# Start streaming | |
profile = pipeline.start(config) | |
# Create an align object | |
# The "align_to" is the stream type to which we plan to align depth frames. | |
# rs.align allows us to perform alignment of depth frames to others frames | |
align_to = rs.stream.color | |
align = rs.align(align_to) | |
try: | |
while True: | |
# Get frameset of color and depth | |
frames = pipeline.wait_for_frames() | |
# Align the depth frame to color frame | |
aligned_frames = align.process(frames) | |
# Get aligned frames | |
aligned_depth_frame = aligned_frames.get_depth_frame() | |
color_frame = aligned_frames.get_color_frame() | |
# Validate that both frames are valid | |
if not aligned_depth_frame or not color_frame: | |
continue | |
depth_image = np.asanyarray(aligned_depth_frame.get_data()) | |
color_image = np.asanyarray(color_frame.get_data()) | |
# resize | |
#depth_image = cv2.resize(depth_image, (WITDH, HEIGHT)) | |
#color_image = cv2.resize(color_image, (WITDH, HEIGHT)) | |
# Depth Image scailing 및 색체 조절 | |
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.075), cv2.COLORMAP_JET) | |
cv2.imshow("RGB Stream", color_image) | |
cv2.imshow("Depth Stream", depth_colormap) | |
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 : | |
pipeline.stop() |
코드가 어렵지 않으니 주석을 따라 읽어보시면 처리 과정이 이해가 될 것 입니다.
이 코드를 vscode든, 터미널에서든 실행시키면 작동이 될 것 입니다.
저는 Linux에서 VScode와 터미널 모두에서 실행이 되었습니다.
터미널에서는 다음 명령을 입력하시면 됩니다.
python3 realsense_opencv.py
정상 작동하면 다음과 같이 창이 두 개가 뜹니다.
q를 누르면 실행이 종료됩니다.

그럼 다음 글에선 이를 이용해서 YOLOv8 과 함께
특정 물체의 Depth를 추출하는 작업에 대해 작성하겠습니다.
'Research & Algorithm > Computer Vision' 카테고리의 다른 글
CenterPose 환경 구성 및 shoes 예제 수행 (1) | 2024.01.14 |
---|---|
[논문 리뷰] CenterPose - Single-Stage Keypoint-Based Category-Level Object Pose Estimation from an RGB Image (2022 ICRA) (1) | 2024.01.14 |
Intel RealSense 카메라 + YOLOv8 을 이용한 object 거리 추출 (0) | 2024.01.12 |
Jetson Nano에서 yolov7 SORT를 이용한 실시간 영상 분석 (1) | 2024.01.06 |
Jetson Nano에서 TensorRT 기반 yolov7 동영상 분석 (1) | 2024.01.01 |