Skip to content

Commit

Permalink
fix: support OpenCV read/write for images with Chinese path
Browse files Browse the repository at this point in the history
  • Loading branch information
idootop committed Nov 16, 2024
1 parent bba19b3 commit 360c7cd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src-python/magic/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import lru_cache

import cv2
import numpy as np
from tinyface import TinyFace

_tf = TinyFace()
Expand All @@ -24,7 +25,7 @@ def swap_face(input_path, face_path):
try:
save_path = _get_output_file_path(input_path)
output_img = _swap_face(input_path, face_path)
cv2.imwrite(save_path, output_img)
_write_image(save_path, output_img)
return save_path
except BaseException as _:
return None
Expand All @@ -47,7 +48,12 @@ def _get_one_face(face_path: str):

@lru_cache(maxsize=12)
def _read_image(img_path: str):
return cv2.imread(img_path)
return cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)


def _write_image(img_path: str, img):
suffix = os.path.splitext(img_path)[-1]
cv2.imencode(suffix, img)[1].tofile(img_path)


def _get_output_file_path(file_name):
Expand Down

0 comments on commit 360c7cd

Please sign in to comment.