인공지능 공부

Generating Custom data for object detection with Labelme

DualQuaternion 2023. 7. 19. 16:04

영상으로 객체를 탐지하는 결과물을 만들어내는 과제를 하고 있다.  객체를 탐지하는데에는 YOLOv8 모델을 사용할 예정이다. 내가 탐지하고자 하는 모델은 기존의 데이터셋에 없어서 새롭게 만들어줘야 한다. 이 과정에 labelme를 이용하려한다. 

 

우선 labelme를 사용해보자.

https://github.com/wkentaro/labelme

 

GitHub - wkentaro/labelme: Image Polygonal Annotation with Python (polygon, rectangle, circle, line, point and image-level flag

Image Polygonal Annotation with Python (polygon, rectangle, circle, line, point and image-level flag annotation). - GitHub - wkentaro/labelme: Image Polygonal Annotation with Python (polygon, recta...

github.com

위 링크에 나온 방식으로 인스톨을 해 본다.

 

conda create -n labelme-Env python=3.8

conda activate labelme-Env

 

git clone https://github.com/wkentaro/labelme.git
cd labelme
pip install -e .

 

labelme로 실행해보면 이런 화면이 나온다.

 

 

OpenDir로 열어주면

이런 창이 나온다.

 

Edit에서 Create Rectangle을 하면

이런식으로 Label을 할 수 있고, 그 이름도 지정하게 된다.

 

쭈욱 작업을 하고 보면

요런식으로 파일이 저장된 것을 알 수 있다.

 

COCO 포맷에서는 하나의 annotation file생성이 필요한데, 

 

E:\ExternalTools\labelme\examples\instance_segmentation

이 폴더 안에 있는 labelme2coco.py를 이용하면 간단하다. 

 

python labelme2coco.py data_annotated data_dataset_coco_weldHelmet --labels labels.txt

 

labels.txt 에는 방금 집어넣은 label을 넣어주자. 

주의할게 ignore, background는 건드리지 말고 그 아래에 넣자.

 

아무튼 위 명령어를실행하면, data_annotated 안에 있는 jpg와 json파일을 취합해서, data_dataset_coco_weldHelmet 폴더에 병합한다. 

 

나는 그냥 E:\ExternalTools\labelme\examples\instance_segmentation\data_annotated 폴더에 생성한 데이터를 넣어주고 돌려봤다.

 

나같은 경우는 pycocotools를 추가로 설치해야 했다. 

 

산출물은 다음과 같았다.

그럼 이걸 이제 fiftyone에서 로드해보자.

 

import fiftyone as fo

# The directory containing the source images
data_path = "data_dataset_coco_weldHelmet"

# The path to the COCO labels JSON file
labels_path = "data_dataset_coco_weldHelmet/annotations.json"

# Import the dataset
dataset = fo.Dataset.from_dir(
    dataset_type=fo.types.COCODetectionDataset,
    data_path=data_path,
    labels_path=labels_path,
)

session = fo.launch_app(dataset)

input("EnterKey to exit fiftyone")

 

위 코드를 실행해보니

 

 

fiftyone에서 잘 로드가 되는 것 까지 확인했다.

 

이제 이걸 yolo 포맷으로 바꾼다음에 학습에 이용해보자.. 거의 다왔다.