Home > Enterprise >  Find particular class number in YOLO .txt file in Python
Find particular class number in YOLO .txt file in Python

Time:05-04

I have YOLO annotation folder containing .txt files. I the file contnet is as follows:

0 0.552344 0.479630 0.707812 0.824074
1 0.722587 0.816280 0.125074 0.070182
46 0.677865 0.821296 0.014063 0.037037
49 0.690885 0.820370 0.013021 0.035185
74 0.704688 0.819907 0.012500 0.036111

The first column is the class number.

I tried the following code as mentioned in this stackoverflow query

import os
files = []

for i in os.listdir("C:\\Users\\blackshine\\Dataset_All_Training\\zTt"):
    if i.endswith('.txt'):
        files.append(i)

for item in files:
    file_data = []

    with open(item, 'r') as myfile:
        for line in myfile:
            currentLine = line[:-1]
            data = currentLine.split(" ")
            file_data.append(data)
            #print(file_data)

However, I am getting the following error while reading the file content:

(tfgpu_2) C:\Users\blackshine\Dataset_All_Training>python yoloCheck.py
Traceback (most recent call last):
  File "yoloCheck.py", line 54, in <module>
    with open(item, 'r') as myfile:
FileNotFoundError: [Errno 2] No such file or directory: '0.txt'

I have files in that folder. Image for files in the folder

Also, I want to make a program which checks the column, and display the name of the file which contains class number greater than 74. Please help me with this.

CodePudding user response:

Make sure your txt files and your python file are in zTt folder. I have made a function named fileFind.

import os
def fileFind():
    files = []
    for i in os.listdir("../zTt"):
        if i.endswith('.txt'):
            files.append(i)
    for item in files:

        file_data = []
        temp = []


        with open(item, 'r') as myFile:
            for line in myFile:
                currentLine = line[:-1]
                data = currentLine.split(" ")
              
                for i in data:
                    if i.isdigit():
                        temp = float(i)
                        i = str(int(temp))
                        num: int = int(i)
                        if num > 74:
                            print(item)
fileFind()

I hope this helps.

  • Related