1. sdk 설치할 때 python 3.11 developer package가 자동 설치 되지만, 저는 공홈에서 3.12.10 버전을 따로 추가 설치했습니다.

image.png

https://www.python.org/downloads/windows/

  1. pip을 통해 pyrealsesne2를 설치하라고 합니다.

    image.png

    제가 봤을 땐, 위에서 sdk 설치할 때 3.11 developer package 때문에 version 만족이 안 되는 것 같습니다.

    https://www.python.org/downloads/release/python-3110/

    여기서 3.11 버전을 다시 깔아줬습니다. 제발 되길 빕니다.

    생기는 모든 버그 및 에러는 realsense 기준으로 맞출 예정입니다.

    맥에 하고 싶은데, 멘토님께서 맥에선 잘 안 됐다고 하셔서 슬픕니다.

    image.png

    깔고 기존에 있던 3.12 버전은 환경 변수에서 지워줬습니다.

  2. 다시 pyrealsense2 설치

    image.png

    깔렸습니다. 잘 깔립니다.

    ubuntu에 하는 사람들도 많던데, 이거.. 시작을 windows로 했다가 나중에 뭔가 잘 안 될까봐 벌써부터 불안합니다.

  3. opencv, numpy 설치

    https://www.youtube.com/watch?v=CmDO-w56qso

    위 아저씨가 두 개를 깔라고 해서 깔았습니다. 저도 카메라를 통해 뭔가 해보는 건 첨이라 해보라는 대로 해보겠습니다. 필요 없으면 나중에 수정하겠습니다.

    image.png

  4. 모듈 import 후 print 테스트

    image.png

시작이 반이라는 말이 있습니다. 이제 거의 다 끝난 것 같습니다.

현태: numpy 라이브러리 호출하신거 보니 다 하셨네요

  1. 예시 기본 소스 코드 & 연결된 카메라 확인

    import pyrealsense2 as rs
    import numpy as np
    import cv2
    
    pipe = rs.pipeline()
    config = rs.config()
    
    # option, actuan dimension, format, fps
    # realsense sdk 설치 후, 우리가 사용하는 cable에 따라 사용 가능한 옵션이 다르다.
    
    # RGB 스트리밍 가능하게 하는 config 설정
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
    
    # Depth 스트리밍 가능하게 하는 config 설정
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    
    pipe.start(config)
    
    try:
        while True:
            frames = pipe.wait_for_frames()
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()
    
            if not depth_frame or not color_frame:
                continue
    
            # 이미지를 numpy 배열로 변환, opencv에서 사용할 수 있는 format임
            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.5), cv2.COLORMAP_JET)
    
            cv2.imshow('depth', depth_colormap)
            cv2.imshow('color', color_image)
    
            if cv2.waitKey(1) == ord('q'):
                break
    
    finally:
        pipe.stop()
        cv2.destroyAllWindows()
    

    스크린샷 2025-04-13 170201.png

  2. colormap을 사용하는 이유

    realsense의 depth는 기본적으로 흑백 이미지를 나타낸다고 합니다. 이건 좀 보기 그래서 opencv의 메소드를 사용해서 거리에 따라 색을 달리 해준다고 합니다.

    가까울수록 빨간색, 멀수록 파란색이라고 합니다.

    이 설정은 cv2.COLORMAP_JET을 통해 가능합니다.

  3. 수정된 코드(주석 포함) 및 화면

    import pyrealsense2 as rs
    import numpy as np
    import cv2
    
    pipe = rs.pipeline()
    config = rs.config()
    
    # 파라미터 - option, actual dimension, format, fps
    # realsense sdk 설치 후, 우리가 사용하는 cable에 따라 사용 가능한 옵션이 다르다.
    
    # RGB 스트리밍 가능하게 하는 config 설정
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 15)
    
    # depth 스트리밍 가능하게 하는 config 설정
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 15)
    
    pipe.start(config)
    
    try:
        while True:
            frames = pipe.wait_for_frames()
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()
    
            if not depth_frame or not color_frame:
                continue
    
            # 이미지를 numpy 배열로 변환, opencv에서 사용할 수 있는 format임
            depth_image = np.asanyarray(depth_frame.get_data())
            color_image = np.asanyarray(color_frame.get_data())
    
            # z축(직선 거리) 값을 가져오는 것이고, 단위는 m임
            # 3D 좌표 모두 필요하다면 pointcloud 필요
            # 위의 config에서 설정한 스트리밍 크기의 중심 좌표를 가져옴옴
            height, width = depth_image.shape
            center_x = width // 2
            center_y = height // 2
            distance = depth_frame.get_distance(center_x, center_y)
    
            print(f"중심 거리: {distance:.2f} meters")
    
            depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.5), cv2.COLORMAP_JET)
    
            # 초록색 원을 물체의 중앙에 시각화
            cv2.circle(depth_colormap, (center_x, center_y), 5, (0, 255, 0), -1)
    
            cv2.imshow('depth', depth_colormap)
            cv2.imshow('color', color_image)
    
            if cv2.waitKey(1) == ord('q'):
                break
    
    finally:
        pipe.stop()
        cv2.destroyAllWindows()
    
  4. 현재는 물체 하나만 측정하고 있는데, 여러 물체를 어떻게 다 측정할까?

    center 좌표만 잡지 말고, 물건 위치별로 grid 나눠서 측정하면 될 것 같습니다.

    추측일 뿐이고, 구글, 유튜브, 지피티한테 나중에 물어봐야 할 것 같습니다.

    지금 당장은 중요한 게 아니라 지나갈게용

  5. 카메라는 재고 관리에만 쓰기로 했고, 막상 보니 정말 거리 측정밖에 못 하는 것 같은데, 여기서 얻을 수 있는 정말 유의미한 데이터가 뭘까…?

진짜 뭐 없는 거 같습니다.

애초에 이 카메라가 ‘거리 측정’에 특화되어있어서 그 이상으로 뭘 할 순 없는 것 같음. 해봤자 어제 말한 얼굴 인식?

이 부분에 대해 gpt한테 물어봤는데, 돌아온 답변은 다음과 같습니다.

image.png

  1. 카메라에서 뽑아오는 데이터를 pub/sub으로 처리하는 거 같은데, 이 때 MQTT를 사용하는 것