Object Detection 문제

·2023년 12월 7일

인공지능

목록 보기
18/19

아래 그림에서 Object Detection 모델을 활용해 'Penson' 클래스에 해당하는 객체의 바운딩 박스 위치를 출력하고, cv2.rectangle을 통해 박스를 표시하라

  • Box Color는 (255,0,0)
%cd /content
!pip install ultralytics
from ultralytics import YOLO
import io  ## https://github.com/ultralytics/ultralytics
from urllib import request

det_image_url = "https://i.ibb.co/r41nkjS/000000145569.jpg"
res = request.urlopen(det_image_url).read()
img2 = Image.open(io.BytesIO(res))
img_2 = np.array(img2) ## cv2를 쓰려면 넘파이로 해야함

model = YOLO('yolov8m.pt')  ## yolov8m.pt : detection model
results = model.predict(source=det_image_url) ## yolov8은 source가 이미지이던, 넘파이이던, url이던 처리할 수 있게 구성되어 있음  ## predict는 inference ## save=True 하면 저장

## boxing한 사람을 가릴수 있을까?
## boxing한 영역을 가우시안 블러 처리
for r in results:
  # print(r.boxes) ## bounding box의 클래스만 추출
  box = r.boxes
  for b in box:
    cls, x_min, y_min, x_max, y_max = int(b.cls.item()), int(b.xyxy[0][0].item()), int(b.xyxy[0][1].item()), int(b.xyxy[0][2].item()), int(b.xyxy[0][3].item())
    # print(cls, x_min, y_min, x_max, y_max)

    if cls == 0:
      img_2 = cv2.rectangle(img_2, (x_min, y_min), (x_max, y_max), (255,0,0), 3)

plt.imshow(img_2)
plt.show()

#결과 :

profile
공부 기록

0개의 댓글