目标检测训练样本文件重命名【将tag写入文件名中】

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/'


def rewrite_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 row
        for tuple in 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 process
                print(dir_image + '@' + tag)


    print("imageloading finish")
    end = time.time()
    print("time: ", end - start)


if __name__ == '__main__':
    rewrite_image(path, tag_path, save_path)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值