[PYTHON] I want to copy yolo annotations

I made a mistake and newly annotated it!

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

Future improvements

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

I want to copy yolo annotations
I want to solve Sudoku (Sudoku)
I want to understand systemd roughly
I want to scrape images to learn
I want to do ○○ with Pandas
I want to debug with Python
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to scrape them all together.
I want to handle the rhyme part1
I want to know how LINUX works!
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to play with aws with python
I want to use IPython Qt Console
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
I really wanted to copy with selenium
I want to handle the rhyme part2
I want to develop Android apps on Android
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to make matplotlib a dark theme
I want to do Dunnett's test in Python
I want to have recursion come to my mind
I want to easily create a Noise Model
I want to use MATLAB feval with python
I want to pin Datetime.now in Django tests
I want to analyze songs with Spotify API 2
I want to memoize including Python keyword arguments
I want to create a window in Python
Anyway, I want to check JSON data easily
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to visualize csv files using Vega-Lite!
I want to handle the rhyme part7 (BOW)
I want to be an OREMO with setParam!
I don't want to take a coding test
I want to store DB information in list
I want to analyze songs with Spotify API 1
I want to merge nested dicts in Python
I want to make fits from my head
I want to manage systemd by time zone! !!
I want to get League of Legends data ②
I want to use ceres solver from python
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to create a plug-in type implementation
I want to use ip vrf with SONiC