Ejemplo n.º 1
0
    def savePascalVocFormat(self,
                            filename,
                            shapes,
                            imagePath,
                            imageData,
                            lineColor=None,
                            fillColor=None,
                            databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
        # Read from file path because self.imageData might be empty if saving to
        # Pascal format
        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = PascalVocWriter(imgFolderName,
                                 imgFileNameWithoutExt,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        w, h = image.width(), image.height()
        for shape in shapes:
            print(shape['type'])
            points = shape['points']
            self.constrainPoints(points, w, h)
            label = shape['label']
            if shape['type'] == 'Rect':
                bndbox = LabelFile.convertPoints2BndBox(points)
                writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3],
                                 label, 'Rect')
            elif shape['type'] == 'Point':
                point = points[0]
                writer.addPoint(point[0], point[1], label, 'Point')
            elif shape['type'] == 'Polygon':
                polygon = LabelFile.convertPoints2Polygon(points)
                writer.addPolygon(polygon[0], polygon[1], polygon[2],
                                  polygon[3], polygon[4], polygon[5],
                                  polygon[6], polygon[7], label, 'Polygon')

        writer.save(targetFile=filename)
        return