Esempio n. 1
0
    def get_bounding_box_data(self, xml_file):
        """アノテーションデータファイルから情報をパースする

        Args:
            xml_file (str or pathlib.Path): アノテーションファイルのパス

        Returns:
            numpy.array: バウンディングボックスの座標.
                    shape=(N,4), [N, [y_min, x_min, y_max, x_max]]
        """
        _ = util.check_file(xml_file)
        with open(xml_file) as f:
            tmp = xmltodict.parse(f.read())
        annotation = tmp["annotation"]
        bboxs = np.array(
            [[
                obj["bndbox"]["ymin"],
                obj["bndbox"]["xmin"],
                obj["bndbox"]["ymax"],
                obj["bndbox"]["xmax"],
            ] for obj in annotation["object"]],
            dtype=np.float32,
        )
        obj_names = np.array([obj["name"] for obj in annotation["object"]])
        meta_info = {
            "folder": annotation["folder"],
            "filename": annotation["filename"],
            "path": annotation["path"],
        }
        return bboxs, obj_names, meta_info
Esempio n. 2
0
    def read_image(self, img_file):
        """画像データを読みchainerで扱えるndarrayに変換する

        Args:
            img_file (str or pathlib): 画像ファイルのパス

        Returns:
            numpy.ndarray: 画像データ.
                    shape=[channel, height, width]
                    dtype=float32
        """
        img_path = util.check_file(img_file)
        img = Image.open(img_path)
        img_arr = np.asarray(img).transpose(2, 0, 1).astype(np.float32)
        return img_arr
Esempio n. 3
0
    def load_model(self, model_file, n_class=2):
        """Detectorモデルをロードする

        Args:
            model_file (str): モデルファイルパス
            n_class (int, optional): 認識物体の数. Defaults to 2.
        """
        _ = util.check_file(model_file)
        model = FasterRCNNVGG16(n_fg_class=n_class,
                                pretrained_model=model_file)
        if self.gpu >= 0:
            chainer.cuda.get_device_from_id(self.gpu).use()
            model.to_gpu()
            self.logger.info(f"use GPU: {self.gpu}")
        self.model = model
        self.model_file = model_file
Esempio n. 4
0
    def set_classes(self, class_file):
        """認識物体のカテゴリ名をセットする

        Args:
            class_file (str or pathlib): 物体クラスの定義ファイル(テキスト).
                    1行毎に物体クラスを文字列で入力.

        Returns:
            dict{物体クラス名:クラスID}: 物体クラス
        """
        self.logger.info(f"set object class: {class_file}")
        _ = util.check_file(class_file)
        classes = dict()
        with open(class_file) as fd:
            for i, one_line in enumerate(fd.readlines()):
                cl = one_line.split("\n")[0]
                classes[cl] = i
        self.classes = classes
        self.logger.info(f"classes: {classes.keys()}")
        return classes
Esempio n. 5
0
 def test_check_not_exist_file(self):
     file_path = "not_exist_file.txt"
     with pytest.raises(FileNotFoundError):
         _ = util.check_file(file_path=file_path)
Esempio n. 6
0
 def test_check_file_exist_file(self):
     file_path = "src/util.py"
     actual = util.check_file(file_path=file_path)
     assert actual