Skip to content

Commit 044cd01

Browse files
committed
bugfix: convert to str when save_path has been changed to Path
1. using mimetypes.guess_type() instead of inspecting suffix
1 parent 4a1e27c commit 044cd01

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

paddlex/inference/common/batch_sampler/image_batch_sampler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def sample(self, inputs):
5858
batch = []
5959
for input in inputs:
6060
if isinstance(input, np.ndarray):
61-
# yield [input]
6261
batch.append(input)
6362
if len(batch) == self.batch_size:
6463
yield batch

paddlex/inference/common/result/mixin.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Union, Tuple, List, Dict, Any, Iterator
1616
from abc import abstractmethod
1717
from pathlib import Path
18+
import mimetypes
1819
import json
1920
import copy
2021
import numpy as np
@@ -184,10 +185,16 @@ def save_to_json(
184185
*args: Additional positional arguments to pass to the underlying writer.
185186
**kwargs: Additional keyword arguments to pass to the underlying writer.
186187
"""
187-
if not str(save_path).endswith(".json"):
188+
189+
def _is_json_file(file_path):
190+
mime_type, _ = mimetypes.guess_type(file_path)
191+
return mime_type is not None and mime_type == "application/json"
192+
193+
if not _is_json_file(save_path):
188194
save_path = Path(save_path) / f"{Path(self['input_path']).stem}.json"
195+
save_path = save_path.as_posix()
189196
self._json_writer.write(
190-
save_path.as_posix(),
197+
save_path,
191198
self.json,
192199
indent=indent,
193200
ensure_ascii=ensure_ascii,
@@ -288,10 +295,16 @@ def save_to_img(self, save_path: str, *args: List, **kwargs: Dict) -> None:
288295
*args: Additional positional arguments that will be passed to the image writer.
289296
**kwargs: Additional keyword arguments that will be passed to the image writer.
290297
"""
291-
if not str(save_path).lower().endswith((".jpg", ".png")):
298+
299+
def _is_image_file(file_path):
300+
mime_type, _ = mimetypes.guess_type(file_path)
301+
return mime_type is not None and mime_type.startswith("image/")
302+
303+
if not _is_image_file(save_path):
292304
fp = Path(self["input_path"])
293305
save_path = Path(save_path) / f"{fp.stem}{fp.suffix}"
294-
self._img_writer.write(save_path.as_posix(), self.img, *args, **kwargs)
306+
save_path = save_path.as_posix()
307+
self._img_writer.write(save_path, self.img, *args, **kwargs)
295308

296309

297310
class CSVMixin:

0 commit comments

Comments
 (0)