Count labels in the Yolo txt files.

Gopal Katariya
Aug 31, 2022

--

import this library

import pandas as pdimport os

add your txt file path

path =r"Enter txt dir path"file_path =r"Enter txt dir path"os.chdir(path)
def read_files(file_path): with open(file_path, 'r') as file: print(ls.append(file.read().split("\n")))

apply for loop on all txt files

for file in os.listdir():    if file.endswith('.txt'):        file_path =f"{path}/{file}"    read_files(file_path)

counts the labels using loop

label1 = 0
label2 = 0
label3 = 0
for i in range(len(ls)):
for j in range(len(ls[i])):
if ls[i][j] == '':
continue
elif ls[i][j][0] == '0':
label1+=1
elif ls[i][j][0] == '1':
label2+=1
elif ls[i][j][0] == '2':
label3+=1

--

--

Gopal Katariya
Gopal Katariya

Written by Gopal Katariya

AI Engineer | Machine Learning Enthusiast | Transforming Ideas into Intelligent Solutions

No responses yet