Skip to content

Commit

Permalink
Bugfix: UTF8/16 Filename support when writing images
Browse files Browse the repository at this point in the history
Smaller changes/cleanup
  • Loading branch information
C0untFloyd committed Oct 9, 2024
1 parent cb8465b commit f0950c2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
9 changes: 7 additions & 2 deletions roop/ProcessMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ def process_frames(self, source_files: List[str], target_files: List[str], curre
resimg = self.process_frame(temp_frame)
if resimg is not None:
i = source_files.index(f)
cv2.imwrite(target_files[i], resimg)
# Also let numpy write the file to support utf-8/16 filenames
cv2.imencode(f'.{roop.globals.CFG.output_image_format}',resimg)[1].tofile(target_files[i])
if update:
update()

Expand Down Expand Up @@ -261,7 +262,11 @@ def write_frames_thread(self):



def run_batch_inmem(self, output_method, source_video, target_video, frame_start, frame_end, fps, threads:int = 1, skip_audio=False):
def run_batch_inmem(self, output_method, source_video, target_video, frame_start, frame_end, fps, threads:int = 1):
if len(self.processors) < 1:
print("No processor defined!")
return

cap = cv2.VideoCapture(source_video)
# frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_count = (frame_end - frame_start) + 1
Expand Down
2 changes: 1 addition & 1 deletion roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def batch_process(output_method, files:list[ProcessEntry], use_new_method) -> No
skip_audio = True
else:
skip_audio = roop.globals.skip_audio
process_mgr.run_batch_inmem(output_method, v.filename, v.finalname, v.startframe, v.endframe, fps,roop.globals.execution_threads, skip_audio)
process_mgr.run_batch_inmem(output_method, v.filename, v.finalname, v.startframe, v.endframe, fps,roop.globals.execution_threads)

if not roop.globals.processing:
end_processing('Processing stopped!')
Expand Down
8 changes: 4 additions & 4 deletions roop/face_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import numpy as np
from skimage import transform as trans
from roop.capturer import get_video_frame
from roop.utilities import resolve_relative_path, conditional_download
from roop.utilities import resolve_relative_path, conditional_thread_semaphore

FACE_ANALYSER = None
THREAD_LOCK_ANALYSER = threading.Lock()
THREAD_LOCK_SWAPPER = threading.Lock()
#THREAD_LOCK_ANALYSER = threading.Lock()
#THREAD_LOCK_SWAPPER = threading.Lock()
FACE_SWAPPER = None


def get_face_analyser() -> Any:
global FACE_ANALYSER

with THREAD_LOCK_ANALYSER:
with conditional_thread_semaphore():
if FACE_ANALYSER is None or roop.globals.g_current_face_analysis != roop.globals.g_desired_face_analysis:
model_path = resolve_relative_path('..')
# removed genderage
Expand Down
2 changes: 1 addition & 1 deletion roop/metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = 'roop unleashed'
version = '4.3.2'
version = '4.3.3'
4 changes: 0 additions & 4 deletions roop/processors/Enhance_CodeFormer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
from roop.typing import Face, Frame, FaceSet
from roop.utilities import resolve_relative_path


# THREAD_LOCK = threading.Lock()


class Enhance_CodeFormer():
model_codeformer = None

Expand Down
4 changes: 0 additions & 4 deletions roop/processors/Enhance_GFPGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
from roop.typing import Face, Frame, FaceSet
from roop.utilities import resolve_relative_path


# THREAD_LOCK = threading.Lock()


class Enhance_GFPGAN():
plugin_options:dict = None

Expand Down
10 changes: 4 additions & 6 deletions roop/processors/Frame_Upscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import numpy as np
import onnxruntime
import roop.globals
import threading

from roop.utilities import resolve_relative_path
from roop.utilities import resolve_relative_path, conditional_thread_semaphore
from roop.typing import Frame


class Frame_Upscale():
plugin_options:dict = None
model_upscale = None
Expand All @@ -16,8 +16,6 @@ class Frame_Upscale():
processorname = 'upscale'
type = 'frame_enhancer'

THREAD_LOCK_UPSCALE = threading.Lock()


def Initialize(self, plugin_options:dict):
if self.plugin_options is not None:
Expand All @@ -40,7 +38,7 @@ def Initialize(self, plugin_options:dict):
elif self.prev_type == "lsdirx4":
model_path = resolve_relative_path('../models/Frame/lsdir_x4.onnx')
self.scale = 4

onnxruntime.set_default_logger_severity(3)
self.model_upscale = onnxruntime.InferenceSession(model_path, None, providers=roop.globals.execution_providers)
self.model_inputs = self.model_upscale.get_inputs()
model_outputs = self.model_upscale.get_outputs()
Expand Down Expand Up @@ -109,7 +107,7 @@ def Run(self, temp_frame: Frame) -> Frame:

for index, tile_frame in enumerate(upscale_tile_frames):
tile_frame = self.prepare_tile_frame(tile_frame)
with self.THREAD_LOCK_UPSCALE:
with conditional_thread_semaphore():
self.io_binding.bind_cpu_input(self.model_inputs[0].name, tile_frame)
self.model_upscale.run_with_iobinding(self.io_binding)
ort_outs = self.io_binding.copy_outputs_to_cpu()
Expand Down
4 changes: 1 addition & 3 deletions roop/processors/Mask_XSeg.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import numpy as np
import cv2
import onnxruntime
import threading
import roop.globals

from roop.typing import Frame
from roop.utilities import resolve_relative_path
from roop.utilities import resolve_relative_path, conditional_thread_semaphore

THREAD_LOCK_CLIP = threading.Lock()


class Mask_XSeg():
Expand Down
14 changes: 14 additions & 0 deletions roop/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import cv2
import zipfile
import traceback
import threading
import threading

from typing import Union, Any
from contextlib import nullcontext

from pathlib import Path
from typing import List, Any
Expand All @@ -26,6 +31,10 @@
TEMP_FILE = "temp.mp4"
TEMP_DIRECTORY = "temp"

THREAD_SEMAPHORE = threading.Semaphore()
NULL_CONTEXT = nullcontext()


# monkey patch ssl for mac
if platform.system().lower() == "darwin":
ssl._create_default_https_context = ssl._create_unverified_context
Expand Down Expand Up @@ -362,3 +371,8 @@ def clean_dir(path: str):
except Exception as e:
print(e)


def conditional_thread_semaphore() -> Union[Any, Any]:
if 'DmlExecutionProvider' in roop.globals.execution_providers or 'ROCMExecutionProvider' in roop.globals.execution_providers:
return THREAD_SEMAPHORE
return NULL_CONTEXT

0 comments on commit f0950c2

Please sign in to comment.