python:大勢から.txtファイルでラベル2をラベル1に変更します.

7555 ワード

# -*- coding: utf-8 -*-
"""
    2  car     1

Created on Sun Feb 24 13:25:43 2019

@author: QinLong
"""

import os
import numpy as np 

label_path = 'F:\\Object_Detection\\Annotations\\txt\\'
new_label = 'F:\\Object_Detection\\Annotations\\final\\'
label_name = os.listdir(label_path)
new_name = label_name
label_data = []

for i in range(len(label_name)):
    new_name = label_name[i]
    label_name[i] = label_path + label_name[i]
    label_data = np.loadtxt(label_name[i])
    new_name = new_label + new_name
    
    if label_data.ndim == 1:
        if label_data.size > 0:
            if label_data[0] == 2:
                label_data[0] = 1
                label_data = np.array([label_data]) 
                #            np.array                   
                
                with open(new_name,"a+") as f:
#                for j in range(len(label_data)):
#                    f.write(label_data[j])
                    np.savetxt(new_name,label_data,fmt=''.join(['%d'] + [' %0.6f']*4),delimiter='  ')

    else:
        for j in range(len(label_data)):
            if label_data[j][0] == 2:
                label_data[j][0] = 1 
            #                 
        with open(new_name,"a+") as f:
            np.savetxt(new_name,label_data,fmt=''.join(['%d'] + [' %0.6f']*4),delimiter='  ')
            #np.savetxt :