コード例 #1
0
def readAndGenerateImage(outputPath, transformers, i_and_imagePath):

    (i, imagePath) = i_and_imagePath
    image = cv2.imread(imagePath)
    name = imagePath.split(os.path.sep)[-1]
    labelPath = '/'.join(imagePath.split(
        os.path.sep)[:-1]) + "/" + name[0:name.rfind(".")] + ".xml"
    tree = ET.parse(labelPath)
    root = tree.getroot()
    objects = root.findall('object')
    #if(len(objects)<1):
    #    raise Exception("The xml should contain at least one object")
    boxes = []
    for object in objects:
        category = object.find('name').text
        confidence = object.find('confidence')
        if confidence is None:
            confidence = 1.0
        else:
            confidence = float(confidence.text)
        bndbox = object.find('bndbox')
        x = int(bndbox.find('xmin').text)
        y = int(bndbox.find('ymin').text)
        h = int(bndbox.find('ymax').text) - y
        w = int(bndbox.find('xmax').text) - x
        boxes.append((category, (x, y, w, h), confidence))
    for (j, transformer) in enumerate(transformers):
        (newimage, newboxes) = transformer.transform(image, boxes)
        if newboxes is not None:
            cv2.imwrite(outputPath + "/" + str(i) + "_" + str(j) + "_" + name,
                        newimage)
            (hI, wI) = image.shape[:2]
            if (len(image.shape) == 3):
                d = 3
            else:
                d = 1
            file = open(
                outputPath + "/" + str(i) + "_" + str(j) + "_" +
                name[0:name.rfind(".")] + ".xml", "w")
            file.write(
                generateXML(
                    str(i) + "_" + str(j) + "_" + name, outputPath, wI, hI, d,
                    newboxes))
            file.close()
コード例 #2
0
def readAndGenerateImage(outputPath, transformers, i_and_imagePath):
    (i, imagePath) = i_and_imagePath
    image = cv2.imread(imagePath)
    name = imagePath.split(os.path.sep)[-1]
    labelPath = '/'.join(imagePath.split(
        os.path.sep)[:-1]) + "/" + name[0:name.rfind(".")] + ".xml"
    tree = ET.parse(labelPath)
    root = tree.getroot()
    objects = root.findall('object')
    if (len(objects) != 1):
        raise Exception(
            "Since this is a localization problem, the xml should only contain one object"
        )
    object = objects[0]
    category = object.find('name').text
    bndbox = object.find('bndbox')
    x = int(bndbox.find('xmin').text)
    y = int(bndbox.find('ymin').text)
    h = int(bndbox.find('ymax').text) - y
    w = int(bndbox.find('xmax').text) - x

    for (j, transformer) in enumerate(transformers):
        (newimage, box) = transformer.transform(image,
                                                (category, (x, y, w, h)))
        if box is not None:
            cv2.imwrite(outputPath + "/" + str(i) + "_" + str(j) + "_" + name,
                        newimage)
            (hI, wI) = image.shape[:2]
            if (len(image.shape) == 3):
                d = 3
            else:
                d = 1
            file = open(
                outputPath + "/" + str(i) + "_" + str(j) + "_" +
                name[0:name.rfind(".")] + ".xml", "w")
            file.write(
                generateXML(
                    str(i) + "_" + str(j) + "_" + name, outputPath, wI, hI, d,
                    [box]))
            file.close()
コード例 #3
0
    def applyAugmentation(self):
        self.readImagesAndAnnotations()
        le = LabelEncoder()
        writer = HDF5DatasetWriterClassification((len(self.imagePaths)*len(self.transformers),self.width,self.height,3),
                                   self.outputPath)
        # We need to define this function outside to work in parallel.
        writer.storeClassLabels(le.classes_)
        widgets = ["Processing images: ", progressbar.Percentage(), " ",
                   progressbar.Bar(), " ", progressbar.ETA()]

        pbar = progressbar.ProgressBar(maxval=len(self.imagePaths),
                                   widgets=widgets).start()
        for i_and_imagePath in enumerate(self.imagePaths):
            (i, imagePath) = i_and_imagePath
            image = cv2.imread(imagePath)
            name = imagePath.split(os.path.sep)[-1]
            labelPath = '/'.join(imagePath.split(os.path.sep)[:-1]) + "/" + name[0:name.rfind(".")] + ".xml"
            tree = ET.parse(labelPath)
            root = tree.getroot()
            objects = root.findall('object')
            if (len(objects) != 1):
                raise Exception("Since this is a localization problem, the xml should only contain one object")
            object = objects[0]
            category = object.find('name').text
            bndbox = object.find('bndbox')
            x = int(bndbox.find('xmin').text)
            y = int(bndbox.find('ymin').text)
            w = int(bndbox.find('ymax').text) - y
            h = int(bndbox.find('xmax').text) - x

            for (j, transformer) in enumerate(self.transformers):
                (newimage, box) = transformer.transform(image, (category, (x, y, w, h)))
                writer.add([newimage],[0,box[1][0],box[1][1],box[1][2],box[1][3]])
            pbar.update(i)
        pbar.finish()
        writer.close()