YOLO를 학습하기 위해서 라벨링은 아래의 형태를 갖추고 있어야 한다.
2 0.099252 0.561545 0.033488 0.168367
라벨의 이름(번호), center_x, center_y, width, height 로 이루어진 txt파일로 저장되야한다.
Labelme를 이용하여 Box annotation을 했을 경우,
"shapes": [{
"label": "1",
"line_color": null,
"fill_color": null,
"points": [[447, 287], [381, 267]],
"shape_type": "rectangle"
}]
(min_x, min_y), (max_x, max_y) 의 값으로 라벨링이 되어 json파일로 저장이 된다.
따라서 Labelme json 파일을 YOLO 라벨링 형식으로 변환해줘야 한다.
아래의 사이트를 참고해서 변환해주면 된다.
GitHub - rooneysh/Labelme2YOLO: Help converting LabelMe Annotation Tool JSON format to YOLO text file format. If you've already
Help converting LabelMe Annotation Tool JSON format to YOLO text file format. If you've already marked your segmentation dataset by LabelMe, it's easy to use this tool to help converting to...
github.com
만약 한장의 이미지일 경우 아래의 코드로 진행할 수 있다
import json
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])
with open("labelme.json", "r") as json_file:
json_data = json.load(json_file)
xmin, ymin = json_data['shapes'][0]['points'][0]
xmax, ymax = json_data['shapes'][0]['points'][1]
print(xmin, xmax, ymin, ymax) # define your x,y coordinates (Labelme)
b = (xmin, xmax, ymin, ymax)
yolo = convert((w,h), b)