def toLabelFile(self, image_path, samples):
     shapes = []
     for sample in samples:
         shapes.append({
             'label': sample.label,
             'line_color': None,
             'fill_color': None,
             'points': sample.points,
             'shape_type': sample.shape_type,
         })
         self.checkAborted()
     local_image_path = os.path.basename(image_path)
     image_data = LabelFile.load_image_file(image_path)
     label_file_name = os.path.splitext(image_path)[0] + LabelFile.suffix
     label_file = LabelFile()
     label_file.save(
         label_file_name,
         shapes,
         local_image_path,
         sample.image_size[0],
         sample.image_size[1],
         imageData=image_data,
         lineColor=None,
         fillColor=None,
         otherData=None,
         flags=None,
     )
示例#2
0
    def resize(self, size):
        """
        Collabrate with crop, If crop is need before resize, using cropResize instead

        Arguemengs:
            size(tuple): W X H
        """
        count = 0
        for name in tqdm(self.names):
            jsoncontent = self.readJsonFile(f"{name}.json")
            # print("JsonContent", jsoncontent)
            newImage, jsoncontent = self._resize(name, size, jsoncontent)
            savedLoc = osp.join(self.dst_dir, f"{name}{self.image_suffix}")
            newImage.save(savedLoc)
            if jsoncontent is not None:
                imageData = LabelFile.load_image_file(savedLoc)
                imageData = base64.b64encode(imageData).decode("utf-8")
                jsoncontent["imageData"] = imageData

                with open(osp.join(self.dst_dir, f"{name}.json"), "w") as f:
                    f.write(json.dumps(jsoncontent, indent=True))
            count += 1
            if self.debug:
                break

        print(f"Done for {count}")
示例#3
0
 def data2labelme(self):
     data_labelme = {}
     data_labelme["version"] = "4.2.10"
     data_labelme["flags"] = {}
     data_labelme["shapes"] = self.shapes
     data_labelme["imagePath"] = os.path.basename(self.file_name)
     #data_labelme["imageData"] = None
     data_labelme["imageData"] = base64.b64encode(
         LabelFile.load_image_file(self.file_name)).decode('utf-8')
     return data_labelme
示例#4
0
    def rotate(self,
               degs,
               center=None,
               boarderValue=(0, 0, 0),
               scale=1.0,
               **kwargs):
        """
        Arguments:
            degs: list or iterable (float)
            center(tuple | None): if the all the image size are the same, it could be applied when rotating,
                if image size differes from each other, leave it unchanged to let program choose the center 
                of the image
        """
        count = 0
        if kwargs.get("start"):
            print("[Warning] api changed, using `degs` instead")
        for name in tqdm(self.names):
            orijsoncontent = self.readJsonFile(f"{name}.json")
            if orijsoncontent is None:
                continue
            # print("JsonContent", jsoncontent)
            rc = 0
            for deg in degs:
                jsoncontent = copy.deepcopy(orijsoncontent)
                newImage = self._rotate(name,
                                        jsoncontent,
                                        deg,
                                        center,
                                        boarderValue,
                                        scale=scale)
                newName = f"{name}-r{deg}-s{scale}-{rc:04}"
                savedLoc = osp.join(self.dst_dir,
                                    f"{newName}{self.image_suffix}")
                rc += 1
                newImage.save(savedLoc)
                if jsoncontent is not None:
                    imageData = LabelFile.load_image_file(savedLoc)
                    imageData = base64.b64encode(imageData).decode("utf-8")
                    jsoncontent["imageData"] = imageData

                    with open(osp.join(self.dst_dir, f"{newName}.json"),
                              "w") as f:
                        f.write(json.dumps(jsoncontent, indent=True))
                count += 1

                if self.debug:
                    break
            if self.debug:
                break

        print(f"Done Rotation for {count}")
示例#5
0
    def shift(self,
              maxpixel=10,
              step=10,
              direction=(1, 1),
              boarderValue=(0, 0, 0)):
        """
        pixel shift will be 10, 5 if max is 10 and step is 5
        direction is the vector that shift will be, for example (1, 1) 
        will shift the image to the up-right corner for pixel
        """
        count = 0
        assert direction[0] != 0 or direction[1] != 0
        for name in tqdm(self.names):
            orijsoncontent = self.readJsonFile(f"{name}.json")
            if orijsoncontent is None:
                continue
            # print("JsonContent", jsoncontent)
            rc = 0
            for pixel in range(maxpixel, 0, -step):
                if pixel <= 0: break
                xy = (direction[0] * pixel, direction[1] * pixel)
                jsoncontent = copy.deepcopy(orijsoncontent)
                newImage = self._shift(name, jsoncontent, xy, boarderValue)
                newName = f"{name}-t{direction[0]}-{direction[1]}-{pixel}-{rc:04}"
                savedLoc = osp.join(self.dst_dir,
                                    f"{newName}{self.image_suffix}")
                rc += 1
                newImage.save(savedLoc)
                if jsoncontent is not None:
                    imageData = LabelFile.load_image_file(savedLoc)
                    imageData = base64.b64encode(imageData).decode("utf-8")
                    jsoncontent["imageData"] = imageData

                    with open(osp.join(self.dst_dir, f"{newName}.json"),
                              "w") as f:
                        f.write(json.dumps(jsoncontent, indent=True))
                count += 1

                if self.debug:
                    break
            if self.debug:
                break

        print(f"Done Shift for {count}")