라벨링이란?
이미지, 영상, 텍스트 등의 데이터에 사람이 데이터 가공 도구를 활용해 인공지능이 학습할 수 있도록 목적에 맞게 입력하는 것을 의미.
어노테이션 도구는 여러가지 있겠지만
대표적으로는 CVAT, MAKE SENSE, Roboflow가 있다고 함.
**MAKE SENSE는 아쉽게도 저장이 힘들어 컴퓨터가 꺼지거나 하면 다시 다해야한다고함.
라벨링 툴에서 많이 사용하는 기능은?
1. Bounding Box: 일반적 개체 인식할 때 많이 사용하는 것 같음.
2. Polygon: 누끼(?)하는 것 처럼 점을 찍어서 하는 방법.(사진마다 해당 점 좌표들을 활용해 작업함.
3. Polyline: 많이 사용하는 곳이 자율주행차량에 많이 사용된다고 함.
4. Point: 주로 뷰티카메라, 성형외과 쪽에서 많이 사용된다고 함.(특정 지점을 라벨링하는 작업이므로 안면인식 같이 감정분석과 정밀하고 섬세한 작업에 많이 사용된다고 함.)
오늘은 BBox 와 Polygon에 대해서 공부함.
import os
import json
import cv2
json_path = "./json_data/instances_default.json"
with open(json_path, "r") as f:
coco_info = json.load(f)
assert len(coco_info) > 0, "파일 읽기 실패"
# 카테고리 정보 수집
categories = dict()
for category in coco_info['categories']:
categories[category["id"]] = category["name"]
# print("categories info >> ", categories)
# annotaiton 정보
ann_info = dict()
for annotation in coco_info['annotations']:
# print("annotation >> ", annotation)
image_id = annotation["image_id"]
bbox = annotation["bbox"]
category_id = annotation["category_id"]
if image_id not in ann_info:
ann_info[image_id] = {
"boxes": [bbox], "categories": [category_id]
}
else:
ann_info[image_id]["boxes"].append(bbox)
ann_info[image_id]["categories"].append(categories[category_id])
for image_info in coco_info['images']:
filename = image_info["file_name"]
height = image_info["height"]
width = image_info["width"]
img_id = image_info["id"]
file_path = os.path.join("./0525_image_data", filename)
# ./0525_image_data/image.jpeg
# image read
img = cv2.imread(file_path)
try:
annotation = ann_info[img_id]
except KeyError:
continue
print(annotation)
for bbox, category in zip(annotation["boxes"], annotation["categories"]):
x1, y1, w, h = bbox
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color = (255, 0, 0)
thickness = 2
org_img = img.copy()
# if category == 1 :
# category = "ros"
text_img = cv2.putText(img, str(category),
(int(x1), int(y1)-10), font, fontScale, color, thickness, cv2.LINE_AA)
rec_img = cv2.rectangle(text_img, (int(x1), int(
y1)), (int(x1+w), int(y1+h)), (255, 0, 255), 2)
cv2.imshow("test", rec_img)
cv2.waitKey(0)
원래 Polygon할 때 사전 작업으로 사용할 사진에 좌표를 모두 찍어두는 작업들을 해야하지만 감사하게도 작업이 되어있는 사진을 확보해서 주심.
import os
import json
import cv2
import numpy as np
json_path = "./json_data/instances_polygon.json"
with open(json_path, "r") as f:
coco_info = json.load(f)
assert len(coco_info) > 0, "파일 읽기 실패"
# 카테고리 정보 수집
categories = dict()
for category in coco_info['categories']:
categories[category["id"]] = category["name"]
# print("categories info >> ", categories)
# annotaiton 정보
ann_info = dict()
for annotation in coco_info['annotations']:
# print("annotation >> ", annotation)
image_id = annotation["image_id"]
bbox = annotation["bbox"]
category_id = annotation["category_id"]
segmentation = annotation["segmentation"]
if image_id not in ann_info:
ann_info[image_id] = {
"boxes": [bbox], "segmentation": [segmentation],
"categories": [category_id]
}
else:
ann_info[image_id]["boxes"].append(bbox)
ann_info[image_id]["segmentation"].append(segmentation)
ann_info[image_id]["categories"].append(categories[category_id])
for image_info in coco_info["images"]:
filename = image_info["file_name"]
height = image_info["height"]
width = image_info["width"]
img_id = image_info["id"]
file_path = os.path.join("./0525_image_data", filename)
# ./0525_image_data/image.jpeg
# image read
img = cv2.imread(file_path)
# 예외처리 문법
try:
annotation = ann_info[img_id]
except KeyError:
continue
for bbox, segmentation, category in zip(annotation['boxes'], annotation['segmentation'], annotation['categories']):
x1, y1, w, h = bbox
for seg in segmentation:
poly = np.array(seg, np.int32).reshape((int(len(seg)/2), 2))
print(poly)
poly_img = cv2.polylines(img, [poly], True, (255, 0, 0), 2)
cv2.imshow("test", poly_img)
cv2.waitKey(0)
활용해서 다른 사진들도 작업이 가능함.