본문 바로가기
to be 개발자

파이썬으로 라벨 태그(클래스) 순서 맞추기

by 아셀acell 2021. 1. 2.
반응형

파이썬으로 라벨 태그 순서 맞추기

 

- yolo darknet 학습 set 학습 돌릴려면 클래스 순서 동일해야한다.

- 여러명이 라벨링하면 클래스 순서가 다를 수 있음.

- 처음에 맞추면 할 필요 없음 ㅠ

- 모르고 처음에 안맞췄으면 아래처럼 맞추면 됨,,

 

 

https://wikidocs.net/51

http://pythonstudy.xyz/python/article/503-Visual-Studio-Code-%EC%82%AC%EC%9A%A9

 

코드

 

import glob

import os

import pickle

import xml.etree.ElementTree as ET

from os import listdir, getcwd

from os.path import join

 

dirs = ['05wheel loader_yolo_2']

classes = []

 

preclass = ['excavator','worker','helmet','dump-truck','forklift'

,'bulldozer','crane','flat-bed-truck','concrete-mixer-truck',

'concrete-boom-pump-truck','road-roller','tower-crane','loader',

'moter-grader','scraper','trencher','tank-lorry']

 

def getTextInDir(dir_path):

    text_list = []

    for filename in glob.glob(dir_path + '/*.txt'):

        text_list.append(filename)

 

    return text_list

 

def convert_annotation(dir_path, output_path, text_path):

    basename = os.path.basename(text_path)

    basename_no_ext = os.path.splitext(basename)[0]

 

    in_file = open(dir_path + '/' + basename_no_ext + '.txt')

    out_file = open(output_path + basename_no_ext + '.txt', 'w')

 

    inContents = in_file.read()

    list = inContents.splitlines()

    for line in list:

        num = int(line[0])

        cNum = str(preclass.index(classes[num]))

        print(line[0]+ " " + cNum)

        line = cNum+line[1:]

        print(line)

        out_file.write(line+"\n")

 

 

cwd = getcwd()

 

for dir_path in dirs:

    full_dir_path = cwd + '/' + dir_path

    output_path = full_dir_path +'/txtUpdate/'

 

    if not os.path.exists(output_path):

        os.makedirs(output_path)

 

    text_paths = getTextInDir(full_dir_path)

    list_file = open(full_dir_path + '.txt', 'w')

    wrongClasses = open(dir_path + '/classes/classes.txt')

    wcContent = wrongClasses.read()

    classes = wcContent.splitlines()

 

    for text_path in text_paths:

        list_file.write(text_path + '\n')

        convert_annotation(full_dir_path, output_path, text_path)

    list_file.close()

    




-

 

728x90
반응형
LIST

댓글