Remove Windows-only path sanitization

This commit is contained in:
henryruhs
2025-01-04 16:11:22 +01:00
parent eb2f794ece
commit 612fd70e54
4 changed files with 13 additions and 37 deletions

View File

@@ -85,20 +85,6 @@ def resolve_relative_path(path : str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), path)) return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
def sanitize_path_for_windows(full_path : str) -> Optional[str]:
buffer_size = 0
while True:
unicode_buffer = ctypes.create_unicode_buffer(buffer_size)
buffer_limit = ctypes.windll.kernel32.GetShortPathNameW(full_path, unicode_buffer, buffer_size) #type:ignore[attr-defined]
if buffer_size > buffer_limit:
return unicode_buffer.value
if buffer_limit == 0:
return None
buffer_size = buffer_limit
def copy_file(file_path : str, move_path : str) -> bool: def copy_file(file_path : str, move_path : str) -> bool:
if is_file(file_path): if is_file(file_path):
shutil.copy(file_path, move_path) shutil.copy(file_path, move_path)

View File

@@ -1,3 +1,4 @@
import os
from functools import lru_cache from functools import lru_cache
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
@@ -7,7 +8,7 @@ from cv2.typing import Size
import facefusion.choices import facefusion.choices
from facefusion.common_helper import is_windows from facefusion.common_helper import is_windows
from facefusion.filesystem import is_image, is_video, sanitize_path_for_windows from facefusion.filesystem import is_image, is_video
from facefusion.typing import Duration, Fps, Orientation, Resolution, VisionFrame from facefusion.typing import Duration, Fps, Orientation, Resolution, VisionFrame
@@ -28,7 +29,8 @@ def read_static_images(image_paths : List[str]) -> List[VisionFrame]:
def read_image(image_path : str) -> Optional[VisionFrame]: def read_image(image_path : str) -> Optional[VisionFrame]:
if is_image(image_path): if is_image(image_path):
if is_windows(): if is_windows():
image_path = sanitize_path_for_windows(image_path) image_binary = numpy.fromfile(image_path, dtype = numpy.uint8)
return cv2.imdecode(image_binary, cv2.IMREAD_COLOR)
return cv2.imread(image_path) return cv2.imread(image_path)
return None return None
@@ -36,7 +38,10 @@ def read_image(image_path : str) -> Optional[VisionFrame]:
def write_image(image_path : str, vision_frame : VisionFrame) -> bool: def write_image(image_path : str, vision_frame : VisionFrame) -> bool:
if image_path: if image_path:
if is_windows(): if is_windows():
image_path = sanitize_path_for_windows(image_path) _, file_extension = os.path.splitext(image_path)
_, vision_frame = cv2.imencode(file_extension, vision_frame)
vision_frame.tofile(image_path)
return is_image(image_path)
return cv2.imwrite(image_path, vision_frame) return cv2.imwrite(image_path, vision_frame)
return False return False
@@ -45,6 +50,8 @@ def detect_image_resolution(image_path : str) -> Optional[Resolution]:
if is_image(image_path): if is_image(image_path):
image = read_image(image_path) image = read_image(image_path)
height, width = image.shape[:2] height, width = image.shape[:2]
if width > 0 and height > 0:
return width, height return width, height
return None return None
@@ -74,8 +81,6 @@ def create_image_resolutions(resolution : Resolution) -> List[str]:
def get_video_frame(video_path : str, frame_number : int = 0) -> Optional[VisionFrame]: def get_video_frame(video_path : str, frame_number : int = 0) -> Optional[VisionFrame]:
if is_video(video_path): if is_video(video_path):
if is_windows():
video_path = sanitize_path_for_windows(video_path)
video_capture = cv2.VideoCapture(video_path) video_capture = cv2.VideoCapture(video_path)
if video_capture.isOpened(): if video_capture.isOpened():
frame_total = video_capture.get(cv2.CAP_PROP_FRAME_COUNT) frame_total = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
@@ -89,8 +94,6 @@ def get_video_frame(video_path : str, frame_number : int = 0) -> Optional[Vision
def count_video_frame_total(video_path : str) -> int: def count_video_frame_total(video_path : str) -> int:
if is_video(video_path): if is_video(video_path):
if is_windows():
video_path = sanitize_path_for_windows(video_path)
video_capture = cv2.VideoCapture(video_path) video_capture = cv2.VideoCapture(video_path)
if video_capture.isOpened(): if video_capture.isOpened():
video_frame_total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) video_frame_total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
@@ -101,8 +104,6 @@ def count_video_frame_total(video_path : str) -> int:
def detect_video_fps(video_path : str) -> Optional[float]: def detect_video_fps(video_path : str) -> Optional[float]:
if is_video(video_path): if is_video(video_path):
if is_windows():
video_path = sanitize_path_for_windows(video_path)
video_capture = cv2.VideoCapture(video_path) video_capture = cv2.VideoCapture(video_path)
if video_capture.isOpened(): if video_capture.isOpened():
video_fps = video_capture.get(cv2.CAP_PROP_FPS) video_fps = video_capture.get(cv2.CAP_PROP_FPS)
@@ -154,8 +155,6 @@ def restrict_trim_frame(video_path : str, trim_frame_start : Optional[int], trim
def detect_video_resolution(video_path : str) -> Optional[Resolution]: def detect_video_resolution(video_path : str) -> Optional[Resolution]:
if is_video(video_path): if is_video(video_path):
if is_windows():
video_path = sanitize_path_for_windows(video_path)
video_capture = cv2.VideoCapture(video_path) video_capture = cv2.VideoCapture(video_path)
if video_capture.isOpened(): if video_capture.isOpened():
width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH) width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
@@ -194,7 +193,7 @@ def create_video_resolutions(resolution : Resolution) -> List[str]:
def normalize_resolution(resolution : Tuple[float, float]) -> Resolution: def normalize_resolution(resolution : Tuple[float, float]) -> Resolution:
width, height = resolution width, height = resolution
if width and height: if width > 0 and height > 0:
normalize_width = round(width / 2) * 2 normalize_width = round(width / 2) * 2
normalize_height = round(height / 2) * 2 normalize_height = round(height / 2) * 2
return normalize_width, normalize_height return normalize_width, normalize_height

View File

@@ -2,9 +2,8 @@ import os.path
import pytest import pytest
from facefusion.common_helper import is_windows
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.filesystem import copy_file, create_directory, filter_audio_paths, filter_image_paths, get_file_size, has_audio, has_image, in_directory, is_audio, is_directory, is_file, is_image, is_video, list_directory, remove_directory, same_file_extension, sanitize_path_for_windows from facefusion.filesystem import create_directory, filter_audio_paths, filter_image_paths, get_file_size, has_audio, has_image, in_directory, is_audio, is_directory, is_file, is_image, is_video, list_directory, remove_directory, same_file_extension
from .helper import get_test_example_file, get_test_examples_directory, get_test_outputs_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_outputs_directory
@@ -16,7 +15,6 @@ def before_all() -> None:
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.mp3', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.mp3',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4'
]) ])
copy_file(get_test_example_file('source.jpg'), get_test_example_file('söurce.jpg'))
def test_get_file_size() -> None: def test_get_file_size() -> None:
@@ -91,12 +89,6 @@ def test_filter_image_paths() -> None:
assert filter_audio_paths([ 'invalid' ]) == [] assert filter_audio_paths([ 'invalid' ]) == []
def test_sanitize_path_for_windows() -> None:
if is_windows():
assert sanitize_path_for_windows(get_test_example_file('söurce.jpg')).endswith('SURCE~1.JPG')
assert sanitize_path_for_windows('invalid') is None
def test_create_directory() -> None: def test_create_directory() -> None:
create_directory_path = os.path.join(get_test_outputs_directory(), 'create_directory') create_directory_path = os.path.join(get_test_outputs_directory(), 'create_directory')

View File

@@ -39,7 +39,6 @@ def test_read_image() -> None:
assert read_image('invalid') is None assert read_image('invalid') is None
@pytest.mark.skip()
def test_write_image() -> None: def test_write_image() -> None:
vision_frame = read_image(get_test_example_file('target-240p.jpg')) vision_frame = read_image(get_test_example_file('target-240p.jpg'))