0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

yoloのアノテーション.txtをコピーしたい

Last updated at Posted at 2020-11-10

#間違えて新規にアノテーションしちゃった!
同じ画像に対していくつもtxt作っちゃうんですよね。
そういうことあると思います。

アノテーションをコピーして追加or新規作成します。

##how to
フォルダを指定して、内部にあるテキストに対して無差別に動作します。
train.txtとかclass.txtとかあったら避難させてください。

番号ごとにコピーしたあと、同じ名前の txt を探して行を追加します。

arm_annoフォルダにあるアノテーションの2番body_annoにある同じ画像のアノテーションtxtに追加したければ、

$ python3 yolocopy.py arm_anno body_anno 2

となります。

##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))

##今後の改良点
同名の画像が存在するtxtに絞ればもっと手軽だと思うんですよね、面倒なのでしませんが。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?