Browse Source

support print JSON formatted string (#2856)

zhangyubo0722 10 months ago
parent
commit
5abc20d205

+ 2 - 4
paddlex/inference/common/result/base_cv_result.py

@@ -13,11 +13,11 @@
 # limitations under the License.
 # limitations under the License.
 
 
 from .base_result import BaseResult
 from .base_result import BaseResult
-from .mixin import StrMixin, JsonMixin, ImgMixin
+from .mixin import ImgMixin
 from ...utils.io import ImageWriter
 from ...utils.io import ImageWriter
 
 
 
 
-class BaseCVResult(BaseResult, StrMixin, JsonMixin, ImgMixin):
+class BaseCVResult(BaseResult, ImgMixin):
     """Base class for computer vision results."""
     """Base class for computer vision results."""
 
 
     def __init__(self, data: dict) -> None:
     def __init__(self, data: dict) -> None:
@@ -31,6 +31,4 @@ class BaseCVResult(BaseResult, StrMixin, JsonMixin, ImgMixin):
             AssertionError: If the required key (`BaseCVResult.INPUT_IMG_KEY`) are not found in the data.
             AssertionError: If the required key (`BaseCVResult.INPUT_IMG_KEY`) are not found in the data.
         """
         """
         super().__init__(data)
         super().__init__(data)
-        StrMixin.__init__(self)
-        JsonMixin.__init__(self)
         ImgMixin.__init__(self, "pillow")
         ImgMixin.__init__(self, "pillow")

+ 4 - 1
paddlex/inference/common/result/base_result.py

@@ -13,9 +13,10 @@
 # limitations under the License.
 # limitations under the License.
 
 
 import inspect
 import inspect
+from .mixin import StrMixin, JsonMixin, ImgMixin
 
 
 
 
-class BaseResult(dict):
+class BaseResult(dict, JsonMixin, StrMixin):
     """Base class for result objects that can save themselves.
     """Base class for result objects that can save themselves.
 
 
     This class inherits from dict and provides properties and methods for handling result.
     This class inherits from dict and provides properties and methods for handling result.
@@ -29,6 +30,8 @@ class BaseResult(dict):
         """
         """
         super().__init__(data)
         super().__init__(data)
         self._save_funcs = []
         self._save_funcs = []
+        StrMixin.__init__(self)
+        JsonMixin.__init__(self)
 
 
     def save_all(self, save_path: str) -> None:
     def save_all(self, save_path: str) -> None:
         """Calls all registered save methods with the given save path.
         """Calls all registered save methods with the given save path.

+ 2 - 4
paddlex/inference/common/result/base_ts_result.py

@@ -13,11 +13,11 @@
 # limitations under the License.
 # limitations under the License.
 
 
 from .base_result import BaseResult
 from .base_result import BaseResult
-from .mixin import StrMixin, JsonMixin, CSVMixin
+from .mixin import CSVMixin
 from ...utils.io import CSVWriter
 from ...utils.io import CSVWriter
 
 
 
 
-class BaseTSResult(BaseResult, StrMixin, JsonMixin, CSVMixin):
+class BaseTSResult(BaseResult, CSVMixin):
     """Base class for times series results."""
     """Base class for times series results."""
 
 
     INPUT_TS_KEY = "input_ts"
     INPUT_TS_KEY = "input_ts"
@@ -39,6 +39,4 @@ class BaseTSResult(BaseResult, StrMixin, JsonMixin, CSVMixin):
         self._ts_writer = CSVWriter(backend="pandas")
         self._ts_writer = CSVWriter(backend="pandas")
 
 
         super().__init__(data)
         super().__init__(data)
-        StrMixin.__init__(self)
-        JsonMixin.__init__(self)
         CSVMixin.__init__(self, "pandas")
         CSVMixin.__init__(self, "pandas")

+ 2 - 4
paddlex/inference/common/result/base_video_result.py

@@ -13,10 +13,10 @@
 # limitations under the License.
 # limitations under the License.
 
 
 from .base_result import BaseResult
 from .base_result import BaseResult
-from .mixin import StrMixin, JsonMixin, ImgMixin, VideoMixin
+from .mixin import ImgMixin, VideoMixin
 
 
 
 
-class BaseVideoResult(BaseResult, StrMixin, JsonMixin, VideoMixin):
+class BaseVideoResult(BaseResult, VideoMixin):
     """Base class for computer vision results."""
     """Base class for computer vision results."""
 
 
     INPUT_IMG_KEY = "input_img"
     INPUT_IMG_KEY = "input_img"
@@ -33,6 +33,4 @@ class BaseVideoResult(BaseResult, StrMixin, JsonMixin, VideoMixin):
         """
         """
 
 
         super().__init__(data)
         super().__init__(data)
-        StrMixin.__init__(self)
-        JsonMixin.__init__(self)
         VideoMixin.__init__(self, "opencv")
         VideoMixin.__init__(self, "opencv")

+ 16 - 0
paddlex/inference/common/result/mixin.py

@@ -164,6 +164,22 @@ class JsonMixin:
                 **kwargs,
                 **kwargs,
             )
             )
 
 
+    def print(
+        self, json_format: bool = False, indent: int = 4, ensure_ascii: bool = False
+    ) -> None:
+        """Print the string representation of the result.
+
+        Args:
+            json_format (bool): If True, print a JSON formatted string. Default is False.
+            indent (int): Number of spaces to indent for JSON formatting. Default is 4.
+            ensure_ascii (bool): If True, ensure all characters are ASCII. Default is False.
+        """
+        if json_format:
+            str_ = json.dumps(self.json, indent=indent, ensure_ascii=ensure_ascii)
+        else:
+            str_ = str(self.json)
+        logging.info(str_)
+
 
 
 class Base64Mixin:
 class Base64Mixin:
     """Mixin class for adding Base64 encoding capabilities."""
     """Mixin class for adding Base64 encoding capabilities."""

+ 1 - 3
paddlex/inference/models_new/image_feature/result.py

@@ -18,11 +18,9 @@ from PIL import Image
 from ...common.result import BaseResult, StrMixin, JsonMixin
 from ...common.result import BaseResult, StrMixin, JsonMixin
 
 
 
 
-class IdentityResult(BaseResult, StrMixin, JsonMixin):
+class IdentityResult(BaseResult):
     def __init__(self, data: dict) -> None:
     def __init__(self, data: dict) -> None:
         super().__init__(data)
         super().__init__(data)
-        StrMixin.__init__(self)
-        JsonMixin.__init__(self)
 
 
     def _to_str(self, *args, **kwargs):
     def _to_str(self, *args, **kwargs):
         data = copy.deepcopy(self)
         data = copy.deepcopy(self)

+ 2 - 4
paddlex/inference/models_new/multilingual_speech_recognition/result.py

@@ -12,12 +12,10 @@
 # See the License for the specific language governing permissions and
 # See the License for the specific language governing permissions and
 # limitations under the License.
 # limitations under the License.
 
 
-from ...common.result import BaseResult, StrMixin, JsonMixin
+from ...common.result import BaseResult
 
 
 
 
-class WhisperResult(BaseResult, StrMixin, JsonMixin):
+class WhisperResult(BaseResult):
 
 
     def __init__(self, data: dict) -> None:
     def __init__(self, data: dict) -> None:
         super().__init__(data)
         super().__init__(data)
-        StrMixin.__init__(self)
-        JsonMixin.__init__(self)