Skip to content

Commit

Permalink
Fixed a bug that [inpaint area : Only masked] could not be used when …
Browse files Browse the repository at this point in the history
…using face crop.

Add option "Inpaint Area(Override img2img Inpaint area)"
Allowed to skip stage4 when not needed.
  • Loading branch information
s9roll7 committed Feb 3, 2023
1 parent 3c8f31a commit 7b01c51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
9 changes: 9 additions & 0 deletions ebsynth_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,20 @@ def process_end(dbg, info):
sample_image = glob.glob( os.path.join(frame_path , "*.png" ) )[0]
img_height, img_width, _ = cv2.imread(sample_image).shape

sample_img2img_key = glob.glob( os.path.join(img2img_key_path , "*.png" ) )[0]
img_height_key, img_width_key, _ = cv2.imread(sample_img2img_key).shape

if is_invert_mask:
project_dir = inv_path

dbg.print("stage 4")
dbg.print("")

if img_height == img_height_key and img_width == img_width_key:
dbg.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
dbg.print("!! The size of frame and img2img_key matched.")
dbg.print("!! You can skip this stage.")

dbg.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
dbg.print("0. Enable the following item")
dbg.print("Settings ->")
Expand Down
13 changes: 10 additions & 3 deletions scripts/custom_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from modules.processing import process_images,Processed
from modules.paths import models_path
from modules.textual_inversion import autocrop
import modules.images
import cv2
import copy
import numpy as np
Expand Down Expand Up @@ -83,6 +84,7 @@ def ui(self, is_img2img):
with gr.Column():
project_dir = gr.Textbox(label='Project directory', lines=1)
mask_mode = gr.Dropdown(choices=["Normal","Invert","None","Don't Override"], value="Normal" ,label="Mask Mode(Override img2img Mask mode)")
inpaint_area = gr.Dropdown(choices=["Whole picture","Only masked","Don't Override"], type = "index", value="Only masked" ,label="Inpaint Area(Override img2img Inpaint area)")

with gr.Column():
use_depth = gr.Checkbox(True, label="Use Depth Map If exists in /video_key_depth")
Expand Down Expand Up @@ -113,7 +115,7 @@ def ui(self, is_img2img):
value = "face close up,"
)

return [project_dir, mask_mode, use_depth, img2img_repeat_count, inc_seed, is_facecrop, face_detection_method, max_crop_size, face_denoising_strength, face_area_magnification, enable_face_prompt, face_prompt]
return [project_dir, mask_mode, inpaint_area, use_depth, img2img_repeat_count, inc_seed, is_facecrop, face_detection_method, max_crop_size, face_denoising_strength, face_area_magnification, enable_face_prompt, face_prompt]


def detect_face(self, img_array):
Expand Down Expand Up @@ -202,6 +204,7 @@ def img_crop( img, face_coords,face_area_magnification):
else:
re_w = int(x_ceiling( (512 / face_img.shape[0]) * face_img.shape[1] , 64))
re_h = 512

face_img = resize_img(face_img, re_w, re_h)
resized.append( Image.fromarray(face_img))

Expand Down Expand Up @@ -266,7 +269,8 @@ def merge_face(img, face_img, face_coord, base_img_size, mask):

if p.image_mask is not None:
x,y,w,h = coord
face_p.image_mask = Image.fromarray( np.array(p.image_mask)[y: y+h, x: x+w] )
cropped_face_mask = Image.fromarray(np.array(p.image_mask)[y: y+h, x: x+w])
face_p.image_mask = modules.images.resize_image(0, cropped_face_mask, face.width, face.height)

face_proc = process_images(face_p)
print(face_proc.seed)
Expand Down Expand Up @@ -328,7 +332,7 @@ def get_depth_map(self, mask, depth_path ,img_basename, is_invert_mask):
# to be used in processing. The return value should be a Processed object, which is
# what is returned by the process_images method.

def run(self, p, project_dir, mask_mode, use_depth, img2img_repeat_count, inc_seed, is_facecrop, face_detection_method, max_crop_size, face_denoising_strength, face_area_magnification, enable_face_prompt, face_prompt):
def run(self, p, project_dir, mask_mode, inpaint_area, use_depth, img2img_repeat_count, inc_seed, is_facecrop, face_detection_method, max_crop_size, face_denoising_strength, face_area_magnification, enable_face_prompt, face_prompt):
args = locals()

def detect_face(img, mask, face_detection_method, max_crop_size):
Expand Down Expand Up @@ -381,6 +385,9 @@ def detect_face(img, mask, face_detection_method, max_crop_size):
p.inpainting_mask_invert = 0
elif mask_mode == "Invert":
p.inpainting_mask_invert = 1

if inpaint_area in (0,1): #"Whole picture","Only masked"
p.inpaint_full_res = inpaint_area

is_invert_mask = False
if mask_mode == "Invert":
Expand Down
26 changes: 21 additions & 5 deletions stage5.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,36 @@ def ebsynth_utility_stage5(dbg, project_args, is_invert_mask):
dbg.print("stage5")
dbg.print("")

project_dir, _, frame_path, frame_mask_path, _, _, img2img_upscale_key_path = project_args
project_dir, _, frame_path, frame_mask_path, _, img2img_key_path, img2img_upscale_key_path = project_args

if not os.path.isdir(project_dir):
dbg.print('project_dir : no such dir %s' % project_dir)
return
if not os.path.isdir(frame_path):
dbg.print('frame_path : no such dir %s' % frame_path)
return

no_upscale = False

if not os.path.isdir(img2img_upscale_key_path):
dbg.print('img2img_upscale_key_path : no such dir %s' % img2img_upscale_key_path)
return

if not os.path.isdir(img2img_key_path):
return

sample_img2img_key = glob.glob( os.path.join(img2img_key_path , "*.png" ) )[0]
img_height1, img_width1, _ = cv2.imread(sample_img2img_key).shape
sample_frame = glob.glob( os.path.join(frame_path , "*.png" ) )[0]
img_height2, img_width2, _ = cv2.imread(sample_frame).shape

if img_height1 != img_height2 or img_width1 != img_width2:
return

dbg.print('The size of frame and img2img_key matched. use %s instead' % img2img_key_path)
img2img_upscale_key_path = img2img_key_path
no_upscale = True

rename_keys(img2img_upscale_key_path)
else:
rename_keys(img2img_upscale_key_path)

number_of_digits, keys = search_key_dir( img2img_upscale_key_path )

Expand Down Expand Up @@ -216,7 +232,7 @@ def ebsynth_utility_stage5(dbg, project_args, is_invert_mask):
"file_name" : "/[" + "#" * number_of_digits + "].png",
"number_of_digits" : number_of_digits,

"key_dir" : "img2img_upscale_key",
"key_dir" : "img2img_upscale_key" if no_upscale == False else "img2img_key",
"video_dir" : "video_frame" if is_invert_mask == False else "../video_frame",
"mask_dir" : "video_mask" if is_invert_mask == False else "inv_video_mask",
"key_weight" : 1.0,
Expand Down

0 comments on commit 7b01c51

Please sign in to comment.