Пример #1
0
 def __init__(self,folder_A,folder_B):
     # folder A and folder B should have exactly the same content's file name
     self.folder_A = folder_A
     self.folder_B = folder_B
     self.list_A = utils.get_dir_filelist_by_extension(folder_A,'jpg')
     self.list_B = utils.get_dir_filelist_by_extension(folder_B,'jpg')
     if self.list_A!=self.list_B:
         print('两个文件夹内容不符!')
         exit()
     else:
         self.start()
Пример #2
0
    def __init__(self, config, shuffle=False, phase="train"):

        image_extention = 'bmp'
        if phase == "train":
            img_list = get_dir_filelist_by_extension(
                dir=config['train']['data_folder'] + '/images',
                ext=image_extention)
        elif phase == "test" or "val":
            img_list = get_dir_filelist_by_extension(
                dir=config['val']['data_folder'] + '/images',
                ext=image_extention)
        else:
            raise Exception("wrong phase! should be train test or val")
        img_list.sort()
        all_image_and_anno_paths = []
        for img_path in img_list:
            xmlname = img_path.split('/')[-1].replace(image_extention, 'xml')
            if phase == "train":
                anno_path = config['train'][
                    'data_folder'] + '/annotations/' + xmlname
            elif phase == "test" or "val":
                anno_path = config['val'][
                    'data_folder'] + '/annotations/' + xmlname

            all_image_and_anno_paths.append({
                'image_path': img_path,
                'anno_path': anno_path
            })

        self.image_anno_list = all_image_and_anno_paths
        self.batch_size = config['model']['batch_size']
        self.image_size = config['model']['image_size']
        self.grid = config['model']['grid']
        self.classes = config['model']['classes']
        self.anchors = config['model']['anchors']
        self.shuffle = shuffle
        if self.shuffle:
            self.shuffle_data()
        # image augmentation
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)
        self.aug_pipe = iaa.Sequential([
            SquarePad(),
            sometimes(iaa.Fliplr(p=1)),
            sometimes(iaa.Flipud(p=1)),
            iaa.Scale({
                "height": self.image_size[0],
                "width": self.image_size[1]
            })
        ])
Пример #3
0
 def __init__(self, dir, train_count):
     self.dir = dir
     self.file_list = utils.get_dir_filelist_by_extension(self.dir,
                                                          ext='jpg')
     self.parent_dir, tail = os.path.split(os.path.abspath(
         self.dir))  # 使用abspath函数先规范化路径
     random.shuffle(self.file_list)
     self.train_count = train_count
     self.start_split()
Пример #4
0
def step2(tmpfile):
    f = open(tmpfile)
    working_dir = f.read()
    f.close()

    d = utils.get_dir_subdir(working_dir + '/predicts', True)
    pics = []
    for subd in d:
        imgs_path = utils.get_dir_filelist_by_extension(subd, 'jpg', True)
        for k in imgs_path:
            img = cv2.imread(k, flags=cv2.IMREAD_GRAYSCALE)
            pics.append(img)
        print('%s 处理完成' % subd)

    b = utils.get_dir_filelist_by_extension(d[0], 'jpg')
    a = len(b)
    utils.view_pics(pics,
                    a,
                    b,
                    output_full_path='./compareresult/' +
                    working_dir.replace('./', '').replace(' ', '-') + '.png')
Пример #5
0
 def start_sift(self):
     self.images_list = utils.get_dir_filelist_by_extension(self.images_dir,self.img_ext)
     for i,f in enumerate(self.images_list):
         img = cv2.imread(f)
         height, width, channels = img.shape
         ratio = width/height
         if height >= self.min_height \
                 and width >= self.min_width \
                 and ratio >= self.ratio_limit[0] \
                 and ratio <= self.ratio_limit[1]:
             dst_name = self.out_dir+f.split('/')[-1]
             # dst_name = dst_name.replace('.'+self.img_ext,'.jpg')
             cv2.imwrite(dst_name,img)
             print('已保存',dst_name)
Пример #6
0
def export_image_and_label_selfdefine(labels_folder_path,images_folder):
    parent_dir, _ = utils.get_parent_dir(labels_folder_path)
    target_dir = utils.create_new_empty_dir(parent_dir + '/images/')
    file_list = utils.get_dir_filelist_by_extension(dir=labels_folder_path, ext='xml')
    for file in file_list:
        tree = ET.ElementTree(file=file)

        for elem in tree.iter(tag='filename'):
            file_name = elem.text
        full_path = images_folder+'/'+file_name
        try:
            shutil.copy2(src=full_path, dst=target_dir + '/' + file_name)
        except:
            print(full_path + '复制失败')
Пример #7
0
    def paste_one_bg(self):
        #读取背景图及前景图
        bg_names = utils.get_dir_filelist_by_extension(self.bg_dir, 'jpg')
        rect_names = utils.get_dir_filelist_by_extension(self.rect_dir, 'jpg')
        rect_num = random.randint(self.pic_min, self.pic_max)
        bg = cv2.imread(random.choice(bg_names))
        random_rects_name = random.choices(rect_names, k=rect_num)
        self.rect_classes = []
        for rect_name in random_rects_name:
            rect_class = self.get_class_by_name(rect_name)
            self.rect_classes.append(rect_class)
        all_rects = []
        if rect_num > 0:
            for rect in random_rects_name:
                all_rects.append(cv2.imread(rect))
            #生成待粘贴区域
            while True:
                rois = utils.generate_rects(
                    num=rect_num,
                    width=all_rects[0].shape[0],
                    height=all_rects[0].shape[1],
                    prison=[0, 0, bg.shape[0], bg.shape[1]])
                print('roi num:', len(rois))
                if self.interaction_rects(rois) == True:
                    print('有相交')
                else:
                    print('无相交')
                    print(rois)
                    break
            #开始粘贴
            for i, roi in enumerate(rois):
                x1, y1, x2, y2 = roi[0], roi[1], roi[2], roi[3]

                bg[y1:y2, x1:x2] = all_rects[i]  # y1:y2,x1:x2
            self.rois = rois
        return bg
Пример #8
0
 def start_scale(self):
     self.images_list = utils.get_dir_filelist_by_extension(self.images_dir,self.img_ext)
     for i,f in enumerate(self.images_list):
         img = cv2.imread(f)
         height, width, channels = img.shape
         if self.scale != 0:
             new_img = cv2.resize(img,dsize=(0,0),fx=self.scale,fy=self.scale)
         if self.width !=0 and self.height==0:#按照宽度成比例缩放
             new_height = int((height/width)*self.width)
             new_img = cv2.resize(img,dsize=(self.width,new_height))
         if self.height !=0 and self.width == 0:
             new_width = int((width/height)*self.height)
             new_img = cv2.resize(img,dsize=(new_width,self.width))
         if self.height!=0 and self.width!=0:
             new_img = cv2.resize(img,dsize=(self.width,self.height))
         dst_name = self.out_dir+f.split('/')[-1]
         dst_name = dst_name.replace('.'+self.img_ext,'.jpg')
         cv2.imwrite(dst_name,new_img)
         print('已保存',dst_name)
Пример #9
0
def delete_empty_label(labels_folder_path):
    file_list = utils.get_dir_filelist_by_extension(dir=labels_folder_path, ext='xml')
    # print(file_list)
    delete_list = []
    good_labels_count = 0
    for file in file_list:

        tree = ET.ElementTree(file=file)
        root = tree.getroot()

        contains_label = False
        for elem in tree.iter(tag='object'):
            contains_label = True
        if not contains_label:
            delete_list.append(file)
        else:
            good_labels_count += 1

    for file in delete_list:
        os.remove(file)
        print(file + ' 已删除')

    print('保留下来的标记文件共有%d个' % good_labels_count)
Пример #10
0
def step1(path):
    kinds = ['Cr', 'In', 'Pa', 'PS', 'RS', 'Sc']
    sets = ['new_plates', 'aluminum']
    current_kind = ''
    current_set = ''
    for kind in kinds:
        if kind in path:
            current_kind = kind
            break
    for se in sets:
        if se in path:
            current_set = se
            break
    print('path:', path)
    print(current_set, current_kind)
    # exit()
    gan = CycleGAN('/plates/单独训练集_B_%s/%s' % (current_set, current_kind))

    model_files_list = utils.get_dir_filelist_by_extension(
        path + '/saved_model', ext='h5', with_parent_path=True)
    mflist = []
    for mf in model_files_list:
        if 'gAB' in mf:
            mflist.append(mf)
    mflist.sort()
    for model_file in mflist:
        # np.random.seed(0)
        random.seed(0)
        a = model_file.split('.')[0].split('/')[-1].split('_')[-1]
        gan.translate(model_file,
                      total_num=10,
                      output_dir=gan.output_dir + '/predicts/' + a)

    f = open('./tmp.txt', mode='w')
    f.write(gan.output_dir)
    f.close()
Пример #11
0
from lbp_score import LBP, LBP_Score
from cyclegan_plates import CycleGAN
import utils
import json
from matplotlib import pyplot as plt
'''
1. 读取文件夹内的模型
2. 通过模型和原始样本,生成新样本,存储在临时文件夹下
3. 计算原始集合生成集的lbpscore,并存储lbpscore
4. 清空临时文件夹,开始下一个循环。
'''
a = utils.get_dir_filelist_by_extension('2018-03-27 09_11_11 Sc/saved_model',
                                        'h5',
                                        with_parent_path=True)
a.sort()
print(a)

source_folder = 'datasets/plates/单独训练集/Sc/trainA'
target_folder = './tmp/translation'
# for model in a:
b = utils.get_dir_filelist_by_extension(source_folder, ext='jpg')

all_lbp_scores = []
k = 1

for model in a:
    gan = CycleGAN(dataset_name='/plates/单独训练集/Sc')
    gan.translate(model_path=model, total_num=len(b), output_dir=target_folder)
    ls = LBP_Score(folder_A=source_folder, folder_B=target_folder)
    s = ls.get_lbp_score()
    lbp_score = round(s, 4)
Пример #12
0
import random

if __name__ == "__main__":
    root_dir = sys.argv[1]
    parent_dir, _ = utils.get_parent_dir(root_dir)
    target_dir = os.path.join(parent_dir, 'results')
    utils.create_new_empty_dir(target_dir)
    os.mkdir(os.path.join(target_dir, 'images'))
    os.mkdir(os.path.join(target_dir, 'annotations'))
    sub_dirs = utils.get_immediate_subdirectories(root_dir)

    total_count = 0
    for sub_dir in sub_dirs:
        sub_dir = os.path.join(root_dir, sub_dir)
        image_dir = os.path.join(sub_dir, 'images')
        image_file_list = utils.get_dir_filelist_by_extension(image_dir, 'bmp')
        total_count += len(image_file_list)
    print('图像总数:%d' % total_count)
    file_paths = []
    for i in range(1, total_count + 1):
        new_name = utils.fixed_length(i, 4)
        a = {
            'image':
            os.path.join(target_dir, 'images', new_name + '.bmp'),
            'annotation':
            os.path.join(target_dir, 'annotations', new_name + '.xml')
        }
        file_paths.append(a)
    random.seed(1)
    random.shuffle(file_paths)
    image_index = 0

def change_tag_text(file_list, target_dir):

    for file in file_list:
        tree = ET.ElementTree(file=file)
        root = tree.getroot()
        # print(root.tag)
        # print(root.attrib)
        print(file)
        for elem in tree.iter(tag='path'):
            # file_name = elem.text
            # index = file_name.find('.')
            # ext = file_name[index:]
            _, fn = utils.get_parent_dir(file)
            fn = fn.replace('xml', 'bmp')
            elem.text = ''
            # print(elem.text)

        tree.write(target_dir + os.path.basename(file))


if __name__ == "__main__":
    xmls_dir = '/Users/shidanlifuhetian/All/data/KHB_ANNO/USTB中厚板检测数据集/test/xmls1'

    # 工具作用:规范xml内容,filename规范为真实文件名
    file_list = utils.get_dir_filelist_by_extension(dir=xmls_dir, ext='xml')
    parent_dir, _ = utils.get_parent_dir(xmls_dir)
    utils.create_new_empty_dir(parent_dir + '/xmls2/')
    target_dir = parent_dir + '/xmls2/'
    change_tag_text(file_list, target_dir)