Skip to content

Commit

Permalink
Fix slow preview
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Jul 15, 2023
1 parent 671a5c8 commit f38239e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import roop.globals
import roop.metadata
import roop.ui as ui
from roop.predicter import predict_image, predict_video
from roop.predictor import predict_image, predict_video
from roop.processors.frame.core import get_frame_processors_modules
from roop.utilities import has_image_extension, is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clean_temp, normalize_output_path

Expand Down
10 changes: 8 additions & 2 deletions roop/predicter.py → roop/predictor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import threading
import numpy
import opennsfw2
from PIL import Image
from keras import Model

from roop.typing import Frame

MAX_PROBABILITY = 0.85

def clear_predictor() -> None:
global PREDICTOR

PREDICTOR = None


def predict_frame(target_frame: Frame) -> bool:
image = Image.fromarray(target_frame)
image = opennsfw2.preprocess_image(image, opennsfw2.Preprocessing.YAHOO)
model = opennsfw2.make_open_nsfw_model()
views = numpy.expand_dims(image, axis=0)
_, probability = model.predict(views)[0]
_, probability = get_predictor().predict(views)[0]
return probability > MAX_PROBABILITY


Expand Down
9 changes: 4 additions & 5 deletions roop/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from roop.face_analyser import get_one_face
from roop.capturer import get_video_frame, get_video_frame_total
from roop.face_reference import get_face_reference, set_face_reference, clear_face_reference
from roop.predicter import predict_frame
from roop.predictor import predict_frame, clear_predictor
from roop.processors.frame.core import get_frame_processors_modules
from roop.utilities import is_image, is_video, resolve_relative_path

Expand Down Expand Up @@ -212,6 +212,7 @@ def render_video_preview(video_path: str, size: Tuple[int, int], frame_number: i
def toggle_preview() -> None:
if PREVIEW.state() == 'normal':
PREVIEW.withdraw()
clear_predictor()
elif roop.globals.source_path and roop.globals.target_path:
init_preview()
update_preview(roop.globals.reference_frame_number)
Expand Down Expand Up @@ -252,12 +253,10 @@ def update_preview(frame_number: int = 0) -> None:
preview_label.configure(image=image)


def update_face_reference(delta: int) -> None:
global preview_slider

def update_face_reference(step: int) -> None:
clear_face_reference()
reference_frame_number = preview_slider.get()
roop.globals.reference_face_position += delta # type: ignore
roop.globals.reference_face_position += step # type: ignore
roop.globals.reference_frame_number = reference_frame_number
update_preview(reference_frame_number)

0 comments on commit f38239e

Please sign in to comment.