addDataToTxt

# -*- coding: utf-8 -*-
import Augmentor
import os
def processOneDir(imagePath,outputPath):
    p = Augmentor.Pipeline(imagePath,outputPath)
    #随机遮挡
    p.random_erasing(probability = 0.5,rectangle_area = 0.11)
    #随机扭曲
    p.random_distortion(probability=0.5, grid_width= 4 , grid_height= 4 , magnitude=8)
    #模拟拍照倾斜    10过大
    p.shear(probability = 0.5, max_shear_left = 5, max_shear_right = 5)
    #随机旋转
    p.rotate(probability=0.5, max_left_rotation=0.8, max_right_rotation=0.8)
    #进行直方图均衡化   产生大量带灰色阴影的图片
    #p.histogram_equalisation(probability=0.1)
    #明暗改变
    p.random_brightness(probability= 0.5, min_factor = 0.5, max_factor = 1.5)
    #颜色改变
    p.random_color(probability = 0.5, min_factor = 0.5, max_factor = 1.5)
    #
    p.random_contrast(probability = 0.5, min_factor = 0.8, max_factor = 1.5)
    #随机放缩
    #p.zoom_random(probability=0.5, percentage_area=0.95)
    #p.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10)
    #p.zoom(probability=0.5, min_factor=1.1, max_factor=1.5)
    #p.sample(1000,multi_threaded=True)
    #每张图片执行一次
    p.process()


def addImageTotxt(orginTxtPath,childPath,outputDir,outputTxtpath):
    with open(orginTxtPath, 'r',encoding='utf-8') as f:
        groundtruth_lines = f.readlines()
    dictMap = {}
    listTxt =[]

    #获取dict   img to ground
    print(len(groundtruth_lines))
    for groundtruth_line in groundtruth_lines:
        imgname = groundtruth_line.split(' ')[0]
        label = groundtruth_line.split(' ')[1]
        dictMap[imgname] = label

    names = []
    for  d in childPath:
        print('%s%s' % (outputDir, d + '/image'))
        childDir = os.listdir('%s%s' % (outputDir, d + '/image'))
        names.extend( [outputDir + d + '/image/'+ child for child in childDir])
    imageLists = []
    for name in names:
        imageList  = os.listdir(name)
        imageLists.extend([name+'/' + image for image in imageList])



    for imagename in imageLists:

        #'/host/cutDataAdd/output/1/image/199/199_original_20.jpg_e8db4488-3aaa-4dd1-bcfb-7b8722b18305.jpg'
        orginName = imagename
        #print(orginName)
        orginName = orginName.split('_')[-1]
        orginName = imagename.replace(orginName,'')
        tmpC = orginName.split('/')[-1]
        orginName = orginName.replace(tmpC, '')
        orginName = orginName +tmpC.split('_',)[-2]
        orginName = orginName.replace('cutDataAdd/output','cutData')
        #tmp = orginName.split('/')[-1]

        #tmp = '_' + tmp
        #orginName.replace(tmp,'')
        #tmp = orginName.split('/')[-1]
        #orginName1 = orginName.replace( tmp, '')
        #orginName1 = orginName1 + tmp.split('_')[-1]
        #orginName = imagename.split('_')[0] + '/'+ imagename.split('_')[2]
        #tmp =  orginName.split('/')[-2]
        #orginName = orginName.replace('/'+tmp,'')
        #orginName = orginName.replace('cutDataAdd/output','cutData')
        #orginName = orginName.replace('image','image/'+tmp)
        #orginName = orginName.replace('','cutDataAdd/output')
        try:
            label = dictMap[orginName]
            groundtruth_lines.append(imagename + ' ' + label)
        except:
            print(orginName)
    textFile = open(outputTxtpath, 'w', encoding='utf-8')
    for groundtruth_line in  groundtruth_lines:
        textFile.writelines(groundtruth_line)
    textFile.close()






if __name__ == '__main__':
    data_dir = '/host/cutDataAdd/'
    outputDir = '/host/cutDataAdd/output/'
    childPath = ['1', '2', '3', '4']
    orginTxtPath = '/host/cutData/train.txt'
    outputTxtpath =  os.path.join(outputDir, 'tain.txt')
    for d in childPath:
        #print(data_dir + d)
        #print(outputDir + d)
        processOneDir(data_dir + d + '/'+'image', outputDir + d +'/'+ 'image')
        pass

    #更新Txt文件
    addImageTotxt(orginTxtPath,childPath,outputDir,outputTxtpath)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值