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, imgFileName,
                                 imageShape, localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            crew = int(shape['crew'])
            fake = int(shape['fake'])
            occluded = int(shape['occluded'])
            reflection = int(shape['reflection'])
            behindglass = int(shape['behindglass'])
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult,crew,fake,occluded,reflection,behindglass)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 2
0
    def save_create_ml_format(self,
                              filename,
                              shapes,
                              image_path,
                              image_data,
                              class_list,
                              line_color=None,
                              fill_color=None,
                              database_src=None):
        img_folder_path = os.path.dirname(image_path)
        img_folder_name = os.path.split(img_folder_path)[-1]
        img_file_name = os.path.basename(image_path)
        output_file_path = "/".join(filename.split("/")[:-1])
        output_file = output_file_path + "/" + img_folder_name + JSON_EXT

        image = QImage()
        image.load(image_path)
        image_shape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = CreateMLWriter(img_folder_name,
                                img_file_name,
                                image_shape,
                                shapes,
                                output_file,
                                local_img_path=image_path)
        writer.verified = self.verified
        writer.write()
Ejemplo n.º 3
0
    def saveYoloFormat(self, filename, shapes, imagePath, imageData, classList,
                            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 = YOLOWriter(imgFolderName, imgFileName,
                                 imageShape, localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult)

        writer.save(targetFile=filename, classList=classList)
        return
Ejemplo n.º 4
0
    def save_create_ml_format(self,
                              filename,
                              shapes,
                              image_path,
                              image_data,
                              class_list,
                              line_color=None,
                              fill_color=None,
                              database_src=None):
        img_folder_name = os.path.basename(os.path.dirname(image_path))
        img_file_name = os.path.basename(image_path)

        image = QImage()
        image.load(image_path)
        image_shape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = CreateMLWriter(img_folder_name,
                                img_file_name,
                                image_shape,
                                shapes,
                                filename,
                                local_img_path=image_path)
        writer.verified = self.verified
        writer.write()
Ejemplo n.º 5
0
 def savePascalVocFormat(self, filename, shapes, imagePath):
     imgFolderPath = os.path.dirname(imagePath)
     imgFolderName = os.path.split(imgFolderPath)[-1]
     imgFileName = os.path.basename(imagePath)
     image = QImage()
     image.load(imagePath)
     imageShape = [
         image.height(),
         image.width(), 1 if image.isGrayscale() else 3
     ]
     writer = PascalVocWriter(imgFolderName,
                              imgFileName,
                              imageShape,
                              localImgPath=imagePath)
     writer.verified = self.verified
     for shape in shapes:
         points = shape['points']
         label = "cell"
         # label = shape['label']
         difficult = int(shape['difficult'])
         bndbox = LabelFile.convertPoints2BndBox(points)
         writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label,
                          difficult)
     writer.save(targetFile=filename)
     return
Ejemplo n.º 6
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, imgFileName, imageShape)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bndbox = self.convertPoints2BndBox(points, label, difficult)
            writer.boxes.append(bndbox)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 7
0
    def save_yolo_format(self, filename, shapes, image_path, image_data, class_list,
                         line_color=None, fill_color=None, database_src=None):
        img_folder_path = os.path.dirname(image_path)
        img_folder_name = os.path.split(img_folder_path)[-1]
        img_file_name = os.path.basename(image_path)
        # imgFileNameWithoutExt = os.path.splitext(img_file_name)[0]
        # Read from file path because self.imageData might be empty if saving to
        # Pascal format
        if isinstance(image_data, QImage):
            image = image_data
        else:
            image = QImage()
            image.load(image_path)
        image_shape = [image.height(), image.width(),
                       1 if image.isGrayscale() else 3]
        writer = YOLOWriter(img_folder_name, img_file_name,
                            image_shape, local_img_path=image_path)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bnd_box = LabelFile.convert_points_to_bnd_box(points)
            writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult)

        writer.save(target_file=filename, class_list=class_list)
        return
Ejemplo n.º 8
0
    def saveCreateMLFormat(self,
                           filename,
                           shapes,
                           imagePath,
                           imageData,
                           classList,
                           lineColor=None,
                           fillColor=None,
                           databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        outputFilePath = "/".join(filename.split("/")[:-1])
        outputFile = outputFilePath + "/" + imgFolderName + JSON_EXT

        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = CreateMLWriter(imgFolderName,
                                imgFileName,
                                imageShape,
                                shapes,
                                outputFile,
                                localimgpath=imagePath)
        writer.verified = self.verified
        writer.write()
Ejemplo n.º 9
0
    def saveBehavior(self, behaviors, imagePath):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = PascalVocWriter(imgFolderName,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for behavior in behaviors:
            label = behavior.label
            self_id = behavior.self_id
            start_frame = behavior.start_frame
            end_frame = behavior.end_frame
            if len(behavior.shapes) != 0:
                print('add the shapes to list')
                writer.addBehavior(label, self_id, start_frame, end_frame,
                                   behavior.shapes)
            else:
                writer.addBehavior(label, self_id, start_frame, end_frame)

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

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 11
0
    def saveLPRFormat(self,
                      filename,
                      shapes,
                      difficult,
                      imagePath,
                      imageData,
                      lineColor=None,
                      fillColor=None,
                      databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = LPRWriter(imgFolderName,
                           imgFileName,
                           imageShape,
                           localImgPath=imagePath)
        writer.verified = self.verified
        writer.difficult = difficult
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # difficult = int(shape['difficult'])
            writer.addBndBox(points, label)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 12
0
    def savePICKFormat(self, filename, shapes, imagePath, imageData, classList,
                            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
        if isinstance(imageData, QImage):
            image = imageData
        else:
            image = QImage()
            image.load(imagePath)
        imageShape = [image.height(), image.width(),
                      1 if image.isGrayscale() else 3]
        writer = PICKWriter(imgFolderName, imgFileName,
                                 imageShape, localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            bndbox = LabelFile.sortPoints(points)
            if shape['label'] is None:
                label = ""
            else:
                label = shape['label']
            writer.addBndBox(shape['index'], bndbox[0], bndbox[1], bndbox[2], bndbox[3], bndbox[4], bndbox[5],
                             bndbox[6], bndbox[7], shape['text'], label)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 13
0
    def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None):
        """
        Save a XML in pascal Voc format.
        :param filename: The name to save the file with.
        :param shapes: The shapes to save to the XML file.
        :param imagePath: The path to the image that the annotations correspond to.
        :param imageData: The data about the image, including the width, height, and depth.
        :param lineColor: The line color that is saved with the boxes.
        :param fillColor: The fill color that is saved with the boxes.
        :param databaseSrc: The source of the database that the annotations are located.
        """
        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)
        bSave = False
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
            bSave = True

        if bSave:
            writer.save(targetFile = filename)
        return
Ejemplo n.º 14
0
    def saveYoloFormat(self, filename, shapes, imagePath, imageData, classList,
                            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 = YOLOWriter(imgFolderName, imgFileName,
                                 imageShape, localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult)

        writer.save(targetFile=filename, classList=classList)
        return
Ejemplo n.º 15
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, imgFileName,
                                 imageShape,localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            illumination = str(shape['illumination'])
            distince = str(shape['distince'])
            yaw = str(shape['yaw'])
            pitch = str(shape['pitch'])
            roll = str(shape['roll'])
            glasses = str(shape['glasses']) # yyk add
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, illumination, distince, yaw, pitch, roll, glasses) 

        writer.save(targetFile=filename)
        return
Ejemplo n.º 16
0
    def savePascalVocFormat(self,
                            filename,
                            shapes,
                            imagePath,
                            imageData,
                            saveTimes=0,
                            opUserName="******",
                            opTime="无",
                            isCheck=False,
                            lastCheckTime=None,
                            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
        filePath = ustr(imagePath)
        unicodeFilePath = ustr(filePath)
        if filePath.split('.')[-1] == 'tif':
            Img = cv2.imdecode(np.fromfile(unicodeFilePath, dtype=np.uint8),
                               -1)
            height, width, bytesPerComponent = Img.shape
            bytesPerLine = 3 * width
            cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img)
            image = QImage(Img.data, width, height, bytesPerLine,
                           QImage.Format_RGB888)
        else:
            image = QImage()
            image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = PascalVocWriter(imgFolderName,
                                 imgFileName,
                                 imageShape,
                                 saveTimes,
                                 opUserName,
                                 opTime,
                                 isCheck,
                                 lastCheckTime,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label,
                             difficult)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 17
0
def pascal_voc_write(interface, fpath):
    items = []
    for k in interface.map.keys():
        item = interface.model.itemFromIndex(QModelIndex(k))
        if hasattr(item, "fpath") and item.fpath == fpath:
            items.append(item)
    if items == []:
        return

    img = QImage(fpath)
    root = etree.Element("annotation")

    etree.SubElement(root,
                     "folder").text = os.path.basename(os.path.dirname(fpath))
    etree.SubElement(root, "filename").text = os.path.basename(fpath)
    etree.SubElement(root, "path").text = fpath

    source = etree.SubElement(root, "source")
    etree.SubElement(source, "database").text = "Unknown"

    size = etree.SubElement(root, "size")
    etree.SubElement(size, "width").text = str(img.width())
    etree.SubElement(size, "height").text = str(img.height())
    etree.SubElement(size, "depth").text = str((3, 1)[img.isGrayscale()])

    etree.SubElement(root, "segmented").text = "0"

    for item in items:
        if type(item) == PangoBboxItem:
            rect = item.rect
        elif type(item) in (PangoPolyItem, PangoPathItem):
            rect = interface.map[item.key()].boundingRect()
            rect = rect.intersected(
                interface.map[item.parent().key()].boundingRect())
        else:
            continue

        object = etree.SubElement(root, "object")
        etree.SubElement(object, "name").text = item.parent().name
        etree.SubElement(object, "pose").text = "Unspecified"
        etree.SubElement(object, "truncated").text = "0"
        etree.SubElement(object, "difficult").text = "0"

        bndbox = etree.SubElement(object, "bndbox")
        etree.SubElement(bndbox, "xmin").text = str(round(rect.left()))
        etree.SubElement(bndbox, "xmax").text = str(round(rect.right()))
        etree.SubElement(bndbox, "ymin").text = str(round(rect.top()))
        etree.SubElement(bndbox, "ymax").text = str(round(rect.bottom()))

    tree = etree.ElementTree(root)
    pre, ext = os.path.splitext(fpath)
    with open(pre + ".xml", 'wb') as f:
        tree.write(f,
                   encoding="utf-8",
                   xml_declaration=True,
                   pretty_print=True)
Ejemplo n.º 18
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

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            direction = shape['direction']
            isRotated = shape['isRotated']
            # if shape is normal box, save as bounding box
            # print('direction is %lf' % direction)
            if not isRotated:
                bndbox = LabelFile.convertPoints2BndBox(points)
                writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3],
                                 label, difficult)
            else:  #if shape is rotated box, save as rotated bounding box
                robndbox = LabelFile.convertPoints2RotatedBndBox(shape)
                '''
                writer.addRotatedBndBox(robndbox[0],robndbox[1],
                    robndbox[2],robndbox[3],robndbox[4],label,difficult)
                '''
                # CC Wang 2019/10/17
                writer.addRotatedBndBox(robndbox[0], robndbox[1], robndbox[2],
                                        robndbox[3], robndbox[4], robndbox[5],
                                        robndbox[6], robndbox[7], robndbox[8],
                                        robndbox[9], robndbox[10],
                                        robndbox[11], robndbox[12], label,
                                        difficult)
        writer.save(targetFile=filename)
        return
Ejemplo n.º 19
0
    def saveArbitraryXMLformat(self, filename, shapes, imagePath):
        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = ArbitraryXMLWriter(imagePath, imageShape)

        for shape in shapes:
            writer.addEllipse(shape)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 20
0
 def saveYoloFormat(self, filename, shapes, imagePath, classList, margin_pixel):
     imgFolderPath = os.path.dirname(imagePath)
     imgFolderName = os.path.split(imgFolderPath)[-1]
     imgFileName = os.path.basename(imagePath)
     image = QImage()
     image.load(imagePath)
     imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3]
     writer = YOLOWriter(imgFolderName, imgFileName, imageShape, margin_pixel, localImgPath=imagePath)
     writer.verified = self.verified
     for shape in shapes:
         points = shape['points']
         label = shape['label']
         bndbox = LabelFile.convertPoints2BndBox(points)
         writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
     writer.save(targetFile=filename, classList=classList)
def convert(image_path, annotation_path):

    save_path = os.path.join(os.path.dirname(annotation_path), "xml_converted")
    if not os.path.exists(save_path): os.makedirs(save_path)

    for file in os.listdir(annotation_path):
        if file.endswith(".xml"):
            annotation_no_xml = os.path.splitext(file)[0]

            imagePath = image_path + "/" + annotation_no_xml + ".jpg"

            image = QImage()
            image.load(imagePath)
            imageShape = [
                image.height(),
                image.width(), 1 if image.isGrayscale() else 3
            ]
            imgFolderName = os.path.basename(image_path)
            imgFileName = os.path.basename(imagePath)

            writer = YOLOWriter(imgFolderName,
                                imgFileName,
                                imageShape,
                                localImgPath=imagePath)

            # Read classes.txt
            classListPath = annotation_path + "/" + "classes.txt"
            classesFile = open(classListPath, 'r')
            classes = classesFile.read().strip('\n').split('\n')
            classesFile.close()

            # Read VOC file
            filePath = annotation_path + "/" + file
            tVocParseReader = PascalVocReader(filePath)
            shapes = tVocParseReader.getShapes()
            num_of_box = len(shapes)

            for i in range(num_of_box):
                label = classes.index(shapes[i][0])
                xmin = shapes[i][1][0][0]
                ymin = shapes[i][1][0][1]
                x_max = shapes[i][1][2][0]
                y_max = shapes[i][1][2][1]

                writer.addBndBox(xmin, ymin, x_max, y_max, label, 0)

            writer.save(targetFile=save_path + "/" + annotation_no_xml +
                        ".txt")
Ejemplo n.º 22
0
    def saveYoloFormat(self,
                       filename,
                       shapes,
                       imagePath,
                       imageData,
                       classList,
                       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()
        if imagePath.startswith("gs://"):
            storage_client = storage.Client()
            url = urlparse(imagePath)
            bucket = storage_client.get_bucket(url.hostname)
            target_file = io.BytesIO()
            bucket.get_blob(url.path[1:]).download_to_file(target_file)
            target_file.seek(0, 0)
            data = target_file.read()
            image.loadFromData(data)
        else:
            image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = YOLOWriter(imgFolderName,
                            imgFileName,
                            imageShape,
                            localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label,
                             difficult)

        writer.save(targetFile=filename, classList=classList)
        return
Ejemplo n.º 23
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)  #获取文件名(包含文件扩展名,即形如1.jpg)
        imgFileNameWithoutExt = os.path.splitext(imgFileName)[
            0]  #os.path.splitext将文件名和扩展名分开,这里只取文件名
        # 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
        ]  #获取图像的width,height和depth
        writer = PascalVocWriter(imgFolderName,
                                 imgFileNameWithoutExt,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            direction = shape['direction']
            isRotated = shape['isRotated']
            # if shape is normal box, save as bounding box
            # print('direction is %lf' % direction)
            if not isRotated:
                bndbox = LabelFile.convertPoints2BndBox(points)
                writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3],
                                 label, difficult)
            else:  #if shape is rotated box, save as rotated bounding box
                robndbox = LabelFile.convertPoints2RotatedBndBox(shape)
                writer.addRotatedBndBox(robndbox[0], robndbox[1], robndbox[2],
                                        robndbox[3], robndbox[4], label,
                                        difficult)

        writer.save(targetFile=filename)  #保存
        return
Ejemplo n.º 24
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,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            difficult = int(shape['difficult'])
            tetragon = shape['tetragon']
            angle = int(round(shape['deg']))
            d3 = shape['shape3D']
            if d3 == False:
                if tetragon == False:
                    bndbox = LabelFile.convertPoints2BndBox(points)
                    writer.addBndBox(bndbox[0], bndbox[1], bndbox[2],
                                     bndbox[3], label, difficult, tetragon,
                                     angle)
                elif tetragon == True:
                    writer.addBndBox2(points, label, difficult, tetragon,
                                      angle)
            elif d3:
                writer.addBndBox3(d3, points, label, angle, difficult)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 25
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
Ejemplo n.º 26
0
    def _transform_single_file(self, Reader, Writer, image_path,
                               is_image_needed):

        # load image into memory
        image = QImage()
        image.load(image_path)
        imgSize = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]

        # parse input format
        try:
            # discard extension from file name and append the extension
            # of the format to transform
            input_file = image_path.split('.')[0] + Reader.FILE_EXT
            if (is_image_needed):
                reader = Reader(input_file, image)
            else:
                reader = Reader(input_file)
            shapes = reader.getShapes()
        except FileNotFoundError:
            print(
                f"No {Reader.FILE_EXT} file found, skipping for {image_path} ..."
            )
            return

        # initialize Writer object to prepare for .xml file creation
        writer = Writer(foldername=image_path.split(PATH_SEP)[-2],
                        filename=image_path.split(PATH_SEP)[-1],
                        imgSize=imgSize,
                        localImgPath=image_path)

        # insert fields extracted from reader to writer
        for shape in shapes:
            points = shape[1]
            label = shape[0]
            difficult = 0
            bndbox = FormatTransform.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label,
                             difficult)

        # store output format
        writer.save()
        self.files_created += 1
Ejemplo n.º 27
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,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            label = shape['label']
            pose = shape['pose']
            unique = shape['unique']
            # Add Chris
            difficult = int(shape['difficult'])
            x = shape['x']
            y = shape['y']
            polygontype = shape['polygonType']
            if polygontype == 'rectangle':
                xbox, ybox = self.convertPoints2BndBox(x, y)
            else:
                xbox, ybox = x, y
            inpolygon = shape['inpolygon']
            writer.addBndBox(label, pose, unique, difficult, polygontype, xbox,
                             ybox, inpolygon)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 28
0
def convert(image_path, annotation_path):

    save_path = os.path.join(os.path.dirname(annotation_path), "xml_converted")
    if not os.path.exists(save_path): os.makedirs(save_path)

    for file in os.listdir(annotation_path):
        if file.endswith(".txt") and file != "classes.txt":
            #print("Convert", file)

            annotation_no_txt = os.path.splitext(file)[0]

            imagePath = image_path + "/" + annotation_no_txt + ".jpg"

            image = QImage()
            image.load(imagePath)
            imageShape = [
                image.height(),
                image.width(), 1 if image.isGrayscale() else 3
            ]
            imgFolderName = os.path.basename(annotation_path)
            imgFileName = os.path.basename(imagePath)

            writer = PascalVocWriter(imgFolderName,
                                     imgFileName,
                                     imageShape,
                                     localImgPath=imagePath)

            # Read YOLO file
            txtPath = annotation_path + "/" + file
            tYoloParseReader = YoloReader(txtPath, image)
            shapes = tYoloParseReader.getShapes()
            num_of_box = len(shapes)

            for i in range(num_of_box):
                label = shapes[i][0]
                xmin = shapes[i][1][0][0]
                ymin = shapes[i][1][0][1]
                x_max = shapes[i][1][2][0]
                y_max = shapes[i][1][2][1]

                writer.addBndBox(xmin, ymin, x_max, y_max, label, 0)

            writer.save(targetFile=save_path + "/" + annotation_no_txt +
                        ".xml")
Ejemplo n.º 29
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,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            illumination = str(shape['illumination'])
            distince = str(shape['distince'])
            yaw = str(shape['yaw'])
            pitch = str(shape['pitch'])
            roll = str(shape['roll'])
            glasses = str(shape['glasses'])  # yyk add
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label,
                             difficult, illumination, distince, yaw, pitch,
                             roll, glasses)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 30
0
    def savePascalVocFormat(self,
                            username,
                            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(username,
                                 imgFolderName,
                                 imgFileNameWithoutExt,
                                 imageShape,
                                 localImgPath=imagePath)

        # print imgFileNameWithoutExt
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            #>>>delete(1) label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2],
                             bndbox[3])  #>>>delete(1), label
        # print '???',filename
        if filename.split('.')[1] == 'jpg':
            filename = filename.split('.')[0] + '.xml'
        # print '!!!???',filename
        writer.save(targetFile=filename)
        return
Ejemplo n.º 31
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,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified
        # print("\n\n\n******************************after press saving: **********************************")

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            kind = shape['kind']
            bndbox = LabelFile.convertPoints2BndBox(points)
            # writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult)
            # print("points:xxxxxxxxxxxx ", points, "point[0] = ",points[0])
            writer.addBndBox_2(bndbox[0], bndbox[1], bndbox[2], bndbox[3],
                               bndbox[4], bndbox[5], points, label, difficult,
                               kind)

        writer.save(targetFile=filename)
        return
Ejemplo n.º 32
0
    def saveYoloOBBFormat(self,
                          filename,
                          shapes,
                          imagePath,
                          imageData,
                          classList,
                          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 = YOLOOBBWriter(imgFolderName,
                               imgFileName,
                               imageShape,
                               localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            centre_x_y = shape['centre_x_y']
            height = shape['height']
            width = shape['width']
            angle = shape['angle']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])

            writer.addBndBox(centre_x_y[0], centre_x_y[1], height, width,
                             angle, label, difficult)

        writer.save(targetFile=filename, classList=classList)
        return
Ejemplo n.º 33
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)
        bSave = False
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
            bSave = True

        if bSave:
            writer.save(targetFile = filename)
        return