简体   繁体   中英

create multiple or single csv file from complete txt files folder

I need to convert my txt files to csv files for annotations to prepare my dataset for training. I tried to convert using pandas and it worked for single file by using this code:

import pandas as pd

read_file = pd.read_csv (r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/annotations/0.txt')
read_file.to_csv (r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/0.csv', index=None)

But i have around 4000 txt files. How to convert them all at same time. Does converting and merging them to a single csv file will be good? I want to create TFRecords using these csv files. Here's my annotation format just in case:

15.025299 79.619064 91.971375 27.37761 111.49409 87.36937 120.44259 144.5673 और
195.26416 345.93964 346.07916 195.40369 296.7271 296.54498 411.9508 412.13293 किस
544.8015 579.83813 541.4978 506.46115 42.720642 60.455795 136.19897 118.46382 दिन
275.59427 311.88095 302.1434 265.85672 134.48518 159.5067 173.62825 148.60674 रूप
30.469978 163.88913 164.9093 31.490135 182.98358 181.51782 274.3758 275.84155 इस
33.57235 184.95844 185.26584 33.879738 354.1837 353.62552 436.9943 437.55246 तरह
-2.6164436 45.761616 53.37768 4.9996223 155.51007 137.70114 158.39023 176.19916 साथ
343.8163 512.32336 512.94495 344.4379 134.2903 132.54332 192.49599 194.24297 एकाएक
337.52948 504.81384 505.13098 337.84662 241.95956 240.91986 291.94846 292.98816 रमानाथ
507.9361 555.0499 523.0799 475.96606 4.7673645 56.682793 85.69593 33.780502 गया

It consists of 10 lines, so 10-word level bounding boxes are there. GroundTruth is the last.
x1 x2 x3 x4 y1 y2 y3 y4 order is there.

If you just want to convert all files to csv, you can use the os module to get a list of all files and loop through them:

import pandas as pd
import os

origin_path = r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images/annotations'

destination_path = r'/Users/shwaitkumar/Downloads/models-master/research/object_detection/images'

for filename in os.listdir(origin_path):
    df = pd.read_csv(os.path.join(origin_path, filename))
    df.to_csv(os.path.join(destination_path, filename.replace('.txt', '.csv')), index=None)

You could also combine them to a single csv using pd.concat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM