코딩초보 YOLOv8 특정 사물만 인식 (3)

YOLOv8 특정 사물만 인식 하는 방법

YOLOv8 특정 사물만 인식하는 방법을 이전 포스팅 에 이어서 알아보려 합니다. 이전 포스팅에서 실행한 결과를 보면 모든 형상을 결과에서 표기해 주는데요.이번에는 특정 사물 또는 생물만 인식 할 수 있는 방법을 알아보려 합니다.

만약 어떠한 목적에 의해 한 종류의 사물만 인식할 경우가 있는데 그럴 때 적용 하면 상당히 유용한 기능입니다.

YOLOv8 특정 사물만 인식하는 방법은 아주 간단합니다.

편리한 기능임에도 아주 간단하게 구현이 가능한데요. 아래 명령어 한 줄만 추가하면 됩니다.

classes=0 이 부분에 숫자가 인식하고자 하는 형상 번호 입니다. 이 class 번호는 포스팅 아래에 표기하겠습니다.

model.predict(source="0", show=True, stream=True, classes=0)  # [0, 3, 5] for multiple classes

이전 코드에 이 부분을 적용해보면 아래와 같습니다.

from ultralytics import YOLO
import cv2
# Load a model
model = YOLO('yolov8n.pt')  # pretrained YOLOv8n model
model.predict(source="0", show=True, stream=True, classes=0)  # [0, 3, 5] for multiple classes

# Define image file path
image_folder = 'C:\\aabb\\serch\\yolotest\\'

# Define image file names
image_file_1 = 'im1.jpg'
image_file_2 = 'im2.jpg'

# Construct full image file paths
image_path_1 = image_folder + image_file_1
image_path_2 = image_folder + image_file_2

# Run batched inference on a list of images
results = model([image_path_1, image_path_2])  # return a list of Results objects

# Process results list
for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs
    masks = result.masks  # Masks object for segmentation masks outputs
    keypoints = result.keypoints  # Keypoints object for pose outputs
    probs = result.probs  # Class probabilities for classification outputs
    
    # Print box coordinates
    print("Box coordinates:")
    for box in boxes:
        print(box)

    # Display result with boxes
    result.plot()
    cv2.waitKey(0)
    cv2.destroyAllWindows()

위의 코드를 실행하면 이전에 차량과 사람을 모두 인식했던 결과와 다르게 사람만 인식한 결과를 확인할 수 있습니다.

YOLOv8 특정 사물만 인식하는 방법

선행된(pre-trained) YOLOv8 형상 클래스

YOLOv8 특정 사물만 인식하는 방법에 필요한 클래스(Class)는 아래의 80가지 클래스로 학습이 되어있습니다.

분류는 아래와 같습니다. 여러분도 클래스 번호를 바꾸어 가며 실행 해보세요.

names:
0: person
1: bicycle
2: car
3: motorcycle
4: airplane
5: bus
6: train
7: truck
8: boat
9: traffic light
10: fire hydrant
11: stop sign
12: parking meter
13: bench
14: bird
15: cat
16: dog
17: horse
18: sheep
19: cow
20: elephant
21: bear
22: zebra
23: giraffe
24: backpack
25: umbrella
26: handbag
27: tie
28: suitcase
29: frisbee
30: skis
31: snowboard
32: sports ball
33: kite
34: baseball bat
35: baseball glove
36: skateboard
37: surfboard
38: tennis racket
39: bottle
40: wine glass
41: cup
42: fork
43: knife
44: spoon
45: bowl
46: banana
47: apple
48: sandwich
49: orange
50: broccoli
51: carrot
52: hot dog
53: pizza
54: donut
55: cake
56: chair
57: couch
58: potted plant
59: bed
60: dining table
61: toilet
62: tv
63: laptop
64: mouse
65: remote
66: keyboard
67: cell phone
68: microwave
69: oven
70: toaster
71: sink
72: refrigerator
73: book
74: clock
75: vase
76: scissors
77: teddy bear
78: hair drier
79: toothbrush

위 정보가 있는 coco128.yaml은 여기에 있습니다.

pt파일과 yaml파일의 차이

위 name은 yaml파일을 확인한 것인데요. 여기서 pt 파일과 yaml 파일이 뭐길래 궁금해져서 정리해 보았습니다.

.pt 파일과 .yaml 파일은 머신러닝 프로젝트에서 사용되는 두 가지 다른 종류의 파일입니다.

  1. .pt 파일: 이 파일은 PyTorch 모델의 가중치(weight)를 저장하는 데 사용됩니다. PyTorch는 머신러닝 및 딥러닝 라이브러리로, 주로 Python 프로그래밍 언어에서 사용됩니다. .pt 확장자는 특정 모델의 학습된 매개변수를 담고 있어, 여러 가지 머신러닝 작업 및 예측을 수행하는 데 필요한 정보를 포함하고 있습니다.
  2. .yaml 파일: 이 파일은 “YAML Ain’t Markup Language”의 약자로, 데이터 구조와 설정을 저장하는데 사용되는 사람이 읽기 쉬운 형식의 파일입니다. 머신러닝 프로젝트에서 .yaml 파일은 주로 설정값, 하이퍼파라미터, 데이터 경로 등에 대한 정보를 저장하고, 나중에 로드하여 프로젝트를 실행하거나 모델을 훈련시킬 때 이 파일의 정보를 참조하게 됩니다. 문법이 간단하고 읽기 쉬워, 다양한 프로젝트에서 널리 사용되고 있습니다.

간단히 말해, .pt 파일은 PyTorch 모델 가중치를 저장하는 데 사용되며, 머신러닝 모델의 핵심 구성 요소입니다.

반면, .yaml 파일은 전반적인 프로젝트 설정 및 데이터에 관한 정보를 저장하고, 가독성이 좋은 텍스트 형식입니다.

여기까지 YOLOv8 특정 사물만 인식하는 방법을 알아 보았는데 다음에는 학습하는 방법을 알아보려 합니다.

댓글 남기기