간혹 Labelimg를 이용해 라벨링을 하고 순서를 바꾸거나 라벨 중간에 추가해야할 때가 있다
리스트를 라벨 순서데로 작성하고 아래와 같이 파일들을 열어 수정해주는 코드를 구글에 검색해 작성하였다.
import os, re
class_kor = ['plate', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '아', '바', '배', '버', '보', '부', '다', '더', '도', '두', '어', '가', '거', '고', '구', '하', '허', '호', '자', '저', '조', '주', '라', '러', '로', '루', '마', '머', '모', '무', '나', '너', '노', '누', '오', '사', '서', '소', '수', '우']
class_index = ['plate', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'ba', 'bae', 'beo', 'bo', 'bu', 'da', 'deo', 'do', 'du', 'eo', 'ga', 'geo', 'go', 'gu', 'ha', 'heo', 'ho', 'ja', 'jeo', 'jo', 'ju', 'la', 'leo', 'lo', 'lu', 'ma', 'meo', 'mo', 'mu', 'na', 'neo', 'no', 'nu', 'o', 'sa', 'seo', 'so', 'su', 'u']
file_list = os.listdir("./data/images")
file_list = [file for file in file_list if file.endswith(".txt")]
for num,f in enumerate(file_list):
if f.find("class")>=0:
continue
file_data = []
# open file and read the content in a list
with open("./data/images/"+f, 'r') as file:
print(file)
for line in file:
# remove linebreak which is the last character of the string
currentLine = line[:-1]
data = currentLine.split(" ")
# add item to the list
file_data.append(data)
# Decrease the first number in any line by one
for n, i in enumerate(file_data):
if not i[0].isdigit():
kor = i[0].encode()
if type(kor) == bytes:
kor = kor.decode('utf-8')
if kor == '':
continue
try:
i[0] = str(class_kor.index(kor))
except:
i[0] = str(class_index.index(kor))
file_data[n] = i
elif int(i[0])>10:
kor = re.compile('[가-힣]+').findall(f)[0]
i[0] = str(class_kor.index(kor))
print(i,kor, class_kor.index(kor))
file_data[n] = i
# Write back to the file
f = open("./data/images/"+f, 'w')
for i in file_data:
res = ""
for j in i:
res += j + " "
f.write(res)
f.write("\n")
f.close()