Skip to content

Commit

Permalink
Improve mask generation speed (#15) (#16)
Browse files Browse the repository at this point in the history
* hijack gradio for faster base64 conversion

* Rename files (#15)
  • Loading branch information
PladsElsker authored Jan 11, 2024
1 parent a925976 commit d5b4483
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib_inpaint_difference/gradio_hijacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
from cv2 import imencode
import base64
import gradio as gr
from lib_inpaint_difference.one_time_callable import one_time_callable


@one_time_callable
def hijack_encode_pil_to_base64():
# from https://github.com/gradio-app/gradio/issues/2635#issuecomment-1423531319
def encode_pil_to_base64_new(pil_image):
image_arr = np.asarray(pil_image)[:,:,::-1]
_, byte_data = imencode('.png', image_arr)
base64_data = base64.b64encode(byte_data)
base64_string_opencv = base64_data.decode("utf-8")
return "data:image/png;base64," + base64_string_opencv

gr.processing_utils.encode_pil_to_base64 = encode_pil_to_base64_new
File renamed without changes.
4 changes: 3 additions & 1 deletion scripts/inpaint_difference.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from lib_inpaint_difference.globals import DifferenceGlobals
from lib_inpaint_difference.webui_nasty_hijacks import hijack_img2img_processing, hijack_ui_settings
from lib_inpaint_difference.gradio_hijacks import hijack_encode_pil_to_base64
from lib_inpaint_difference.webui_hijacks import hijack_img2img_processing, hijack_ui_settings
from lib_inpaint_difference.webui_callbacks import setup_script_callbacks


if DifferenceGlobals.is_extension_enabled:
hijack_encode_pil_to_base64()
hijack_img2img_processing()
hijack_ui_settings()

Expand Down

0 comments on commit d5b4483

Please sign in to comment.