머신러닝/영상인식

depth pro, YOLO11, 거리재기

Olivia-BlackCherry 2025. 3. 7. 16:19

 

depth pro는 사진 또는 영상을 보고 카메라로부터 해당 대상까지 거리를 잴 수 있는 기능이다. 색깔 또는 수치상으로 나타낼 수 있다.

 

https://github.com/apple/ml-depth-pro

 

GitHub - apple/ml-depth-pro: Depth Pro: Sharp Monocular Metric Depth in Less Than a Second.

Depth Pro: Sharp Monocular Metric Depth in Less Than a Second. - apple/ml-depth-pro

github.com

 

위 문제를 푸는 동안 import module 오류가 계속 일어났는데, 결국 패스경로를 잘못 입력해서 나타나는 현상이었다. 

ModuleNotFoundError Traceback (most recent call last) <ipython-input-34-b1e9392ae9bb> in <cell line: 0>() ----> 1 import depth_pro 2 3 # 모델 및 변환 로드 4 model, transform = depth_pro.create_model_and_transforms() 5 model.eval() ModuleNotFoundError: No module named 'depth_pro'

해결 방법은 아래에 있다.

 

 

<문제 풀이>

 

1. clone

!git clone 주소

 

2. %cd

현재 작업 디렉터리 변경

 

3. pip install -e .

패키지를 editable 모드로 설치하는 명령어.

패키지의 소스 코드를 직접 수정할 수 있음

. 은 현재 디렉터리를 말함

 

4. pretrained chekpoint 다운로드

source get_pretrained_models.sh

 

5. -i, -h, -o 등등

!depth-pro-run -h

 

6. !pwd 현재 작업 중인 디렉토리의 경로 출력

!pwd

 

 

7. 이미지 input/output

!depth-pro-run -i ./image2.jpg -o ./output/result.jpg

 

8. 이미지 열기

# 파이썬 이미지 라이브러리(이미지 파일 열기, 수정, 저장)
from PIL import Image
Image.open("/content/ml-depth-pro/output/result.jpg/image2.jpg")

 

9. 필요한 라이브러리 다운로드받기

#거리재기
!pip install ultralytics
!pip install opencv-python

 

10. depth pro file 열기

__init__.py와 depth_pro.py 파일이 있는 path 찾은 후 실행

!python /content/ml-depth-pro/src/depth_pro/depth_pro.py

 

 

11. 경로 확인해주기

해당 파일을 찾지 못해서 import가 안되는 경우 한번더 경로를 컴퓨터에 확인시켜줄 것

import sys
sys.path.append("/content/ml-depth-pro/src")

 

 

12. 거리 재기

colab에서는 cv.imshow(), cv.waitKey(0) 안써진다.

따라서 cv2_imshow를 이용할 것.

import cv2
from ultralytics import YOLO
from google.colab.patches import cv2_imshow
import numpy as np
import depth_pro

model = YOLO("yolo11s.pt")
image_path = "/content/ml-depth-pro/image2.jpg"
image_input = cv2.imread(image_path)
results = model(image_input)
person_boxes = []
for result in results:
  boxes = result.boxes.xyxy.cpu().numpy()
  classes = result.boxes.cls.cpu().numpy()
  for box, cls in zip(boxes, classes):
    if result.names[int(cls)] == 'person':
      x1,y1,x2,y2 = map(int, box[:4])
      person_boxes.append((x1,y1,x2,y2))
      cv2.rectangle(image_input, (x1,y1), (x2,y2), (0,255,0), 2)

#깊이추정모델과 이미지변환함수 생성
model, transform = depth_pro.create_model_and_transforms()
model.eval() #추론(테스트)모드

#f_px : 초점거리 관련 정보 포함
image, _, f_px = depth_pro.load_rgb(image_path)
image = transform(image)

#거리추정
prediction = model.infer(image, f_px=f_px)
depth = prediction["depth"]
depth_np = depth.squeeze().cpu().numpy() #파이토치 텐서를 넘파이배열로 변환
for x1,y1,x2,y2 in person_boxes:
  center_x = (x1+x2)//2
  center_y = (y1+y2)//2

  depth_value = depth_np[center_y, center_x]
  text = f"Depth: {depth_value:.2f} m"
  font = cv2.FONT_HERSHEY_COMPLEX
  font_scale = 1
  font_thickness = 2
  text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
  text_x = x1
  text_y = y1 -10
  rect_x1 = text_x -5
  rect_y1 = text_y - text_size[1] - 10
  rect_x2 = text_x + text_size[0] + 5
  rect_y2 = text_y + 5

  cv2.rectangle(image_input, (rect_x1, rect_y1), (rect_x2, rect_y2), (0,255,0), -1)
  cv2.putText(image_input, text, (text_x, text_y), font, font_scale, (0,0,0), font_thickness)

cv2_imshow(image_input)
cv2.imwrite("depth.jpg", image_input)

depth_np_normalized = (depth_np - depth_np.min()) / (depth_np.max() - depth_np.min())
inv_depth_np_normalized = 1.0 - depth_np_normalized
depth_colormap = cv2.applyColorMap((inv_depth_np_normalized * 255).astype(np.uint8), cv2.COLORMAP_TURBO)
cv2_imshow(depth_colormap)
cv2.imwrite("depth_colormap.jpg", depth_colormap)
cv2.destroyAllWindows()

 

13. 글자크기가 너무 커서 줄이기

글씨체도 변경함

font_scale = 0.4  
font_thickness = 1  # 두께는 유지 

 

 

oduleNotFoundError Traceback (most recent call last) <ipython-input-34-b1e9392ae9bb> in <cell line: 0>() ----> 1 import depth_pro 2 3 # 모델 및 변환 로드 4 model, transform = depth_pro.create_model_and_transforms() 5 model.eval() ModuleNotFoundError: No module named 'depth_pro'