コード例 #1
0
def show_data_in_dir(imgs_dir,xmls_dir,windowname='ORG',class_color={},showname=True,maxcls=20,delete=False):
    '''显示图片和标注框
    Args:
        imgs_dir:图片目录
        xmls_dir:标注文件xml目录,voc格式
        windowname:显示窗口名
        class_color:类别显示颜色的BGR值
        showname:是否显示类别名
        maxcls:最大类别
        delete:是否删除没有图片的xml文件
    '''
    
    xml_count,img_count = utils.fileCountIn(xmls_dir),utils.fileCountIn(imgs_dir)
    print('------show object boxes based on xml files (xml:%d,image:%d)------'%(xml_count,img_count))
    count = 0
    
    cv2.namedWindow(windowname,cv2.WINDOW_NORMAL)
    wait_sec = 0
    
    for root,dirs,files in os.walk(xmls_dir):
        idx = 0
        while idx < len(files):
            file = files[idx]
            count += 1
            if count%100 == 0:
                print('[%d | %d]%d%%'%(xml_count,count,count*100/xml_count))
            
            xml_file = os.path.join(xmls_dir,file)
            tree = ET.parse(xml_file)
            xml_root = tree.getroot()
            img_name = xml_root.find('filename').text
            
            img_file = os.path.join(imgs_dir,img_name)
            if not os.path.exists(img_file):
                print('%s not exist!'%img_file)
                if delete:
                    os.remove(xml_file)
                    print(xml_file,'has been removed!')
                    idx += 1
                continue
            print(img_name)
            key = show_data(img_file,xml_file,windowname,class_color,showname,maxcls,wait_sec)
            
            if(32==key):
                wait_sec = 1-wait_sec
            elif(key==ord('q') or key==ord('Q') ):
                return 0
            elif(key==2424832 or key==2490368 or key == ord('p')):
                #左、上方向键或p查看上一张图片
                idx -= 1
            else:
                idx += 1

    cv2.destroyAllWindows()
    return 0
コード例 #2
0
ファイル: transform.py プロジェクト: littlerokey/Detection
def transform_file_from_dirs(imgs_xmls_dirs,imgs_save_dir,xmls_save_dir,transforms,N=1):
    '''对文件夹中所有图片进行转换,并生成转换后的图片和xml文件
    Args:
        imgs_xmls_dirs:待转换的图片、xml、背景图片目录
        imgs_save_dir:图片文件保存目录
        xmls_save_dir:xml文件保存目录 
        transforms:转换操作
        N:每张原图生成N张转换图    
    '''
    for i in range(len(imgs_xmls_dirs)):
        imgs_dir = imgs_xmls_dirs[i]['imgs_dir']
        xmls_dir = imgs_xmls_dirs[i]['xmls_dir']
        bk_imgs_dir = imgs_xmls_dirs[i]['bk_imgs_dir']
        for trans in transforms:
            if trans['opt'] == 'rotate':
                trans['bk_imgs_dir'] = bk_imgs_dir
    
        fileCount = utils.fileCountIn(imgs_dir)
        count = 0
        for root,dirs,files in os.walk(imgs_dir):
            for imgname in files:
                src_imgpath = os.path.join(imgs_dir,imgname)
                src_xmlpath = os.path.join(xmls_dir,imgname.split('.')[0]+'.xml')
                count += 1
                if count%10 == 0:
                    print('[%d | %d]%d%%'%(fileCount,count,count*100/fileCount))
                if not os.path.exists(src_xmlpath):
                    print(src_xmlpath,' not exist!')
                    continue
                transform_onefile(src_imgpath,src_xmlpath,imgs_save_dir,xmls_save_dir,transforms,N)
コード例 #3
0
def crop_imgs_without_label(imgs_dir,imgs_save_dir,name_suffix,crop_type='RANDOM_CROP',\
                            crop_n=1,dsize=(0,0),fw=1.0,fh=1.0,random_wh=False):
    '''仅裁剪图片,不带标签
    Args:
        imgs_dir: 待放缩图片、原始xml文件存储路径
        imgs_save_dir: 处理完成的图片、xml文件存储路径
        name_suffix: 处理完成的图片、xml的命名标识
        crop_type:裁剪风格 ['RANDOM_CROP','CENTER_CROP','FIVE_CROP']
        crop_n: 每原图生成裁剪图个数
        dsize:指定crop宽高(w,h),与random_wh==True互斥生效
        fw,fh: 当random_wh==False时为crop比例,否则为随机crop的宽高比例下限
        random_wh:随机选定裁剪宽高  
    '''
    imgcount = utils.fileCountIn(imgs_dir)
    count = 0
    for root, dirs, files in os.walk(imgs_dir):
        for file in files:
            img_file = os.path.join(imgs_dir, file)
            img = cv2.imread(img_file)
            imgh, imgw, n_channels = img.shape

            if crop_type == 'CENTER_CROP':
                crop_n = 1
            elif crop_type == 'FIVE_CROP':
                crop_n = 5

            for i in range(crop_n):
                crop_imgw, crop_imgh = dsize
                if dsize == (0, 0) and not random_wh:
                    crop_imgw = int(imgw * fw)
                    crop_imgh = int(imgh * fh)
                elif random_wh:
                    crop_imgw = int(imgw * (fw + random.random() * (1 - fw)))
                    crop_imgh = int(imgh * (fh + random.random() * (1 - fh)))

                if crop_type == 'RANDOM_CROP':
                    crop_top_left_x, crop_top_left_y = random.randint(
                        0, imgw - crop_imgw - 1), random.randint(
                            0, imgh - crop_imgh - 1)
                elif crop_type == 'CENTER_CROP':
                    crop_top_left_x, crop_top_left_y = int(
                        imgw / 2 - crop_imgw / 2), int(imgh / 2 -
                                                       crop_imgh / 2)
                elif crop_type == 'FIVE_CROP':
                    if i == 0:
                        crop_top_left_x, crop_top_left_y = 0, 0
                    elif i == 1:
                        crop_top_left_x, crop_top_left_y = imgw - crop_imgw - 1, 0
                    elif i == 2:
                        crop_top_left_x, crop_top_left_y = 0, imgh - crop_imgh - 1
                    elif i == 3:
                        crop_top_left_x, crop_top_left_y = imgw - crop_imgw - 1, imgh - crop_imgh - 1
                    else:
                        crop_top_left_x, crop_top_left_y = int(
                            imgw / 2 - crop_imgw / 2), int(imgh / 2 -
                                                           crop_imgh / 2)

                else:
                    print(
                        'crop type wrong! expect [RANDOM_CROP,CENTER_CROP,FIVE_CROP]'
                    )

                croped_img_name = file.split('.')[0]+'_'+name_suffix +\
                            str(crop_top_left_x)+'_'+str(crop_top_left_y)+\
                            '_wh'+str(crop_imgw)+'x'+str(crop_imgh)+\
                            '.jpg'
                croped_img = crop_img(img, crop_top_left_x, crop_top_left_y,
                                      crop_imgw, crop_imgh)
                cv2.imwrite(os.path.join(imgs_save_dir, croped_img_name),
                            croped_img)
            count += 1
            if count % 10 == 0:
                print('[%d|%d] %d%%' %
                      (count, imgcount, count * 100 / imgcount))