import pandas as pd
import inception_V4
import tensorflow as tf
import os
import cv2
import numpy as np
from keras.utils import to_categorical
import time
path ='../data/pic_extracted/'
tag_path ='../data/tag_data_final.xlsx'
save_path ='../data/inception_V4_train/'defrewrite_image(path, tag_path, save_path):"""
Rewrite the filename for adding tags in filename in the form of @Tag
e.g. ATT1_DSCN9647_1@hornet.jpg
After running the function you can get the category of picture by using function below:
n = name.rpartition('@') # split string from the far-right '@', n[2] is the tag
tag = n[2]
"""
start = time.time()
img_list =[]
tag_list =[]
name_list =[]
tag_df = pd.read_excel(tag_path)# 对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中for dir_image in os.listdir(path):
img = cv2.imread(os.path.join(path, dir_image))
img_list.append(img)
name_list.append(dir_image)
name,type= os.path.splitext(dir_image)
n = name.rpartition('_')# 从最右边的'_'将字符串分割 split string from the far-right '_', n[0] is the name# for index, row in tag_df.iterrows():# if n[0] in row['ImageIndex']:# tag_list.append(row['tag'])# filename_last = name + '@' + str(row['tag']) + '.jpg'# cv2.imwrite(os.path.join(save_path, filename_last), img) # rewrite the img to fit the later train process# print(dir_image + '@' + row['tag'])# using tuple to traverse dataframe is faster than rowfortuplein tag_df.itertuples():
filename =getattr(tuple,'ImageIndex')if n[0]in filename:
tag =getattr(tuple,'tag')
tag_list.append(tag)
filename_last = name +'@'+str(tag)+'.jpg'
cv2.imwrite(os.path.join(save_path, filename_last), img)# rewrite the img to fit the later train processprint(dir_image +'@'+ tag)print("imageloading finish")
end = time.time()print("time: ", end - start)if __name__ =='__main__':
rewrite_image(path, tag_path, save_path)