imgaug registro de mejora de la imagen

 

Hombre de pocas palabras, dijo, primero servido imgaug web oficial: https://imgaug.readthedocs.io/en/latest/index.html

Contenidos del sitio web oficial, donde se puede saber lo que puede hacer para mejorar el alcance de:

Instalar un .imgaug

bibliotecas dependientes

pip install six numpy scipy matplotlib scikit-image opencv-python imageio

Entonces, instalado directamente en el terminal, puede estar montado en Símbolo con anaconda:

pip install imgaug

Aquí, si lamentable, todo parece estar en un proceso de instalación sin problemas, una serie de errores roja repentina, como sigue

G:\python\Pyhton36\Scripts>pip install imgaug
Collecting imgaug
  Downloading https://files.pythonhosted.org/packages/af/fc/c56a7da8c23122b7c5325b941850013880a7a93c21dc95e2b1ecd4750108/imgaug-0.2.7-py3-none-any.whl (644kB)
    100% |████████████████████████████████| 645kB 73kB/s
Requirement already satisfied: scikit-image>=0.11.0 in g:\python\pyhton36\lib\site-packages (from imgaug) (0.14.1)
Collecting imageio (from imgaug)
  Downloading https://files.pythonhosted.org/packages/28/b4/cbb592964dfd71a9de6a5b08f882fd334fb99ae09ddc82081dbb2f718c81/imageio-2.4.1.tar.gz (3.3MB)
    100% |████████████████████████████████| 3.3MB 438kB/s
Collecting Shapely (from imgaug)
  Downloading https://files.pythonhosted.org/packages/a2/fb/7a7af9ef7a35d16fa23b127abee272cfc483ca89029b73e92e93cdf36e6b/Shapely-1.6.4.post2.tar.gz (225kB)
    100% |████████████████████████████████| 235kB 181kB/s
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\34905\AppData\Local\Temp\pip-install-43zide7u\Shapely\setup.py", line 80, in <module>
        from shapely._buildcfg import geos_version_string, geos_version, \
      File "C:\Users\34905\AppData\Local\Temp\pip-install-43zide7u\Shapely\shapely\_buildcfg.py", line 200, in <module>
        lgeos = CDLL("geos_c.dll")
      File "g:\python\pyhton36\lib\ctypes\__init__.py", line 348, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: [WinError 126] 找不到指定的模块。
 
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\34905\AppData\Local\Temp\pip-install-43zide7u\Shapely\

Echa un vistazo a los predecesores de información en línea, es necesario instalar bien formada

A través de esta página web  https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely  (este es un muy buen paquete de expansión sitio web oficial, hay muchas herramientas disponibles) para encontrar su propia versión del pitón la bien formada descargado a la ruta especificada

Soy un Windows, el sistema operativo de 64 bits, python3.5, he elegido la siguiente

A continuación, instale el siguiente comando:

G:\python\Pyhton36\Scripts>python -m pip install Shapely‑1.6.4.post2‑cp35‑cp35m‑win_amd64.whl

Por último, repetir el comando antes de instalar imgaug:

pip install imgaug

Obtener, llamar a un día, la instalación se ha completado

Referencia: https://blog.csdn.net/qq_16065939/article/details/85080630


código fuente de dos .github para cumplir con el problema y mejorar archivo XML con BoundingBox lotes

github dirección autor original: https://github.com/xinyu-ch/Data-Augment

Diudiu hay un error aquí fue modificado para mirar hacia abajo (sección III), ha sido la depuración, disponible en algunos pequeños problemas, se reunió hablar

1. Si, por desgracia, se reunió con el siguiente problema, que es un problema común, más soluciones unificadas: https://blog.csdn.net/qq_38163755/article/details/84494796

Resolver la siguiente manera:

Una primera solución, aumentando = codifica 'UTF-8.' :

FILE_OBJECT= open( 'train.txt','r', encoding='UTF-8' )

El segundo método, lectura binaria :

FILE_OBJECT= open( 'train.txt', 'rb' )

Después de eso, se puede ver en el tercer trimestre, estamos haciendo una modificación de la parte superior de la github disponibles puesta


III. BoundingBox con mejorada XML depuración archivo por lotes (se puede utilizar directamente)

  1. archivos XML y archivos de imagen creados después de la carpeta que desea guardar las mejoras
  2. Estableció de antemano qué manera se gira de imagen mejorada, o añadir ruido, es para mejorar el brillo, o la traducción suplementaria
  3. Luego vino una imagen se lee, y las coordenadas de la información de la imagen en la etiqueta XML correspondientes
  4. Y una imagen marco rectangular transformar
  5. Después de la última salvó mejorar las imágenes y archivos XML
import xml.etree.ElementTree as ET
import pickle
import os
from os import getcwd
import numpy as np
from PIL import Image
import shutil
import matplotlib.pyplot as plt

import imgaug as ia
from imgaug import augmenters as iaa

ia.seed(1)

def read_xml_annotation(root, image_id):
    in_file = open(os.path.join(root, image_id), encoding='UTF-8')
    tree = ET.parse(in_file)
    root = tree.getroot()
    bndboxlist = []

    for object in root.findall('object'):  # 找到root节点下的所有country节点
        bndbox = object.find('bndbox')  # 子节点下节点rank的值

        xmin = int(bndbox.find('xmin').text)
        xmax = int(bndbox.find('xmax').text)
        ymin = int(bndbox.find('ymin').text)
        ymax = int(bndbox.find('ymax').text)
        # print(xmin,ymin,xmax,ymax)
        bndboxlist.append([xmin, ymin, xmax, ymax])
        # print(bndboxlist)

    # ndbox = root.find('object').find('bndbox')
    return bndboxlist


# (506.0000, 330.0000, 528.0000, 348.0000) -> (520.4747, 381.5080, 540.5596, 398.6603)
def change_xml_annotation(root, image_id, new_target):
    new_xmin = new_target[0]
    new_ymin = new_target[1]
    new_xmax = new_target[2]
    new_ymax = new_target[3]

    in_file = open(os.path.join(root, str(image_id) + '.xml'), encoding='UTF-8' )  # 这里root分别由两个意思
    tree = ET.parse(in_file)
    xmlroot = tree.getroot()
    object = xmlroot.find('object')
    bndbox = object.find('bndbox')
    xmin = bndbox.find('xmin')
    xmin.text = str(new_xmin)
    ymin = bndbox.find('ymin')
    ymin.text = str(new_ymin)
    xmax = bndbox.find('xmax')
    xmax.text = str(new_xmax)
    ymax = bndbox.find('ymax')
    ymax.text = str(new_ymax)
    tree.write(os.path.join(root, str("%06d" % (str(id) + '.xml'))))


def change_xml_list_annotation(root, image_id, new_target, saveroot, id):
    in_file = open(os.path.join(root, str(image_id) + '.xml'), encoding='UTF-8' )  # 这里root分别由两个意思
    tree = ET.parse(in_file)
    elem = tree.find('filename')
    elem.text = (str("%06d" % int(id)) + '.png')
    xmlroot = tree.getroot()
    index = 0

    for object in xmlroot.findall('object'):  # 找到root节点下的所有country节点
        bndbox = object.find('bndbox')  # 子节点下节点rank的值

        # xmin = int(bndbox.find('xmin').text)
        # xmax = int(bndbox.find('xmax').text)
        # ymin = int(bndbox.find('ymin').text)
        # ymax = int(bndbox.find('ymax').text)

        new_xmin = new_target[index][0]
        new_ymin = new_target[index][1]
        new_xmax = new_target[index][2]
        new_ymax = new_target[index][3]

        xmin = bndbox.find('xmin')
        xmin.text = str(new_xmin)
        ymin = bndbox.find('ymin')
        ymin.text = str(new_ymin)
        xmax = bndbox.find('xmax')
        xmax.text = str(new_xmax)
        ymax = bndbox.find('ymax')
        ymax.text = str(new_ymax)

        index += 1

        print("index=",index)


    tree.write(os.path.join(saveroot, str("%06d" % int(id)) + '.xml'))


def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path + ' 目录已存在')
        return False


if __name__ == "__main__":

    IMG_DIR = "F:\\image\\raw_xml"
    XML_DIR = "F:\\image\\xml"

    AUG_XML_DIR = "./Annotations"  # 存储增强后的XML文件夹路径
    try:
        shutil.rmtree(AUG_XML_DIR)
    except FileNotFoundError as e:
        a = 1
    mkdir(AUG_XML_DIR)

    AUG_IMG_DIR = "./JPEGImages"  # 存储增强后的影像文件夹路径
    try:
        shutil.rmtree(AUG_IMG_DIR)
    except FileNotFoundError as e:
        a = 1
    mkdir(AUG_IMG_DIR)

    AUGLOOP = 2  # 每张影像增强的数量

    boxes_img_aug_list = []
    new_bndbox = []
    new_bndbox_list = []

    # 影像增强
    seq = iaa.Sequential([
        iaa.Flipud(0.5),  # vertically flip 20% of all images
        iaa.Fliplr(0.5),  # 镜像
        iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
        iaa.Crop(px=(0, 16)),
        iaa.Add((-10, 10), per_channel=0.5),
        iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect BBs
        iaa.Affine(
            translate_px={"x": 15, "y": 15},
            scale=(0.8, 0.95)
            #rotate=(-30, 30)
        )  # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs
    ])

    for root, sub_folders, files in os.walk(XML_DIR):

        for name in files:

            bndbox = read_xml_annotation(XML_DIR, name)
            shutil.copy(os.path.join(XML_DIR, name), AUG_XML_DIR)
            shutil.copy(os.path.join(IMG_DIR, name[:-4] + '.png'), AUG_IMG_DIR)
            print(os.path.join(IMG_DIR, name[:-4] + '.png'))

            for epoch in range(AUGLOOP):
                seq_det = seq.to_deterministic()  # 保持坐标和图像同步改变,而不是随机
                # 读取图片
                img = Image.open(os.path.join(IMG_DIR, name[:-4] + '.png'))
                # sp = img.size
                img = np.asarray(img)
                # bndbox 坐标增强
                for i in range(len(bndbox)):
                    bbs = ia.BoundingBoxesOnImage([
                        ia.BoundingBox(x1=bndbox[i][0], y1=bndbox[i][1], x2=bndbox[i][2], y2=bndbox[i][3]),
                    ], shape=img.shape)

                    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
                    boxes_img_aug_list.append(bbs_aug)

                    # new_bndbox_list:[[x1,y1,x2,y2],...[],[]]
                    n_x1 = int(max(1, min(img.shape[1], bbs_aug.bounding_boxes[0].x1)))
                    n_y1 = int(max(1, min(img.shape[0], bbs_aug.bounding_boxes[0].y1)))
                    n_x2 = int(max(1, min(img.shape[1], bbs_aug.bounding_boxes[0].x2)))
                    n_y2 = int(max(1, min(img.shape[0], bbs_aug.bounding_boxes[0].y2)))
                    if n_x1 == 1 and n_x1 == n_x2:
                        n_x2 += 1
                    if n_y1 == 1 and n_y2 == n_y1:
                        n_y2 += 1
                    if n_x1 >= n_x2 or n_y1 >= n_y2:
                        print('error', name)
                    new_bndbox_list.append([n_x1, n_y1, n_x2, n_y2])

                    # 存储变化后的图片
                    image_aug = seq_det.augment_images([img])[0]
                    path = os.path.join(AUG_IMG_DIR,str("%06d" % (len(files) + int(name[:-4]) + epoch * 250)) + '.png')

                    image_auged = bbs.draw_on_image(image_aug, thickness=0)####################################

                    Image.fromarray(image_auged).save(path)

                # 存储变化后的XML
                change_xml_list_annotation(XML_DIR, name[:-4], new_bndbox_list, AUG_XML_DIR,len(files) + int(name[:-4]) + epoch * 250)
                print(str("%06d" % (len(files) + int(name[:-4]) + epoch * 250)) + '.png')
                new_bndbox_list = []

 método de lectura de código muerto para escribir datos: elem.text = (str ( "% 06d" int% (id)) + '.png')

Si no es necesario, a ser nombrado en esta forma de la siguiente manera

Después de la mejora, los resultados con labelImg ver como sigue:

Detalles parte de referencia en el presente documento: https://blog.csdn.net/coooo0l/article/details/84492916#commentsedit

 

Publicado 74 artículos originales · ganado elogios 64 · vistas 130 000 +

Supongo que te gusta

Origin blog.csdn.net/wsLJQian/article/details/100919821
Recomendado
Clasificación