You make many txt for the same image. I think there is such a thing.
Copy the annotation and add or create a new one.
how to
Specify a folder and work indiscriminately against the text inside.
If you find train.txt
or class.txt
, please evacuate.
After copying by number, look for a txt with the same name and add a line.
If you want to add the annotation 2
in the arm_anno
folder to the annotation txt of the same image in body_anno
$ python3 yolocopy.py arm_anno body_anno 2
It will be.
code
yolocopy.py
import glob
import os
import sys
path = os.getcwd()
_, infile, outfile, inline = sys.argv # target_file, output_file, target_number
for nowtxt in glob.glob(infile + "/*.txt"):
textline = []
with open(nowtxt) as f:
l_strip = [s.strip() for s in f.readlines()]
for k in l_strip:
if str(k[0]) == str(inline):
textline.append(k)
basename = os.path.basename(nowtxt) #get name
with open(outfile +'/'+ basename, mode='a') as f:
for k in textline:
f.write('\n' + str(k))
trueline = []
with open(outfile +'/'+ basename) as f:
l_strip = [s.strip() for s in f.readlines()]
for k in l_strip:
if len(str(k))>0:
trueline.append(k)
#os.remove(outfile +'/'+ basename)
with open(outfile +'/'+ basename, mode='w') as f:
f.write('\n'.join(trueline))
I think it would be easier to narrow down to the txt that has the image with the same name, but it's not troublesome.
Recommended Posts