ekhatskevich commited on
Commit
3175ce6
·
1 Parent(s): 6400d6a

deal with mask

Browse files
Files changed (3) hide show
  1. app.py +38 -4
  2. config/simple_faceswap.yaml +0 -17
  3. requirements.txt +4 -1
app.py CHANGED
@@ -1,6 +1,8 @@
 
 
 
1
  import os
2
  import gradio as gr
3
- from scepter.modules.utils.file_system import FS
4
  from huggingface_hub import hf_hub_download, snapshot_download
5
 
6
  def resolve_hf_path(path):
@@ -40,12 +42,44 @@ from modules.flux import FluxMRModiACEPlus
40
  from inference.registry import INFERENCES
41
 
42
 
43
- config_path = os.path.join("config", "simple_faceswap.yaml")
44
  cfg = Config(load=True, cfg_file=config_path)
45
 
46
  # Instantiate the ACEInference object.
47
  ace_infer = ACEInference(cfg)
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def face_swap_app(target_img, face_img):
50
  if target_img is None or face_img is None:
51
  raise ValueError("Both a target image and a face image must be provided.")
@@ -57,14 +91,14 @@ def face_swap_app(target_img, face_img):
57
  output_img, edit_image, change_image, mask, seed = ace_infer(
58
  reference_image=target_img,
59
  edit_image=face_img,
60
- edit_mask=None, # Let ACE++ generate the mask automatically
61
  prompt="Face swap",
62
  output_height=1024,
63
  output_width=1024,
64
  sampler='flow_euler',
65
  sample_steps=28,
66
  guide_scale=50,
67
- seed=-1 # Random seed if not provided
68
  )
69
  return output_img
70
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
  import os
5
  import gradio as gr
 
6
  from huggingface_hub import hf_hub_download, snapshot_download
7
 
8
  def resolve_hf_path(path):
 
42
  from inference.registry import INFERENCES
43
 
44
 
45
+ config_path = os.path.join("config", "ace_plus_fft.yaml")
46
  cfg = Config(load=True, cfg_file=config_path)
47
 
48
  # Instantiate the ACEInference object.
49
  ace_infer = ACEInference(cfg)
50
 
51
+ def create_face_mask(pil_image):
52
+ """
53
+ Create a binary mask (PIL Image) from a PIL image by detecting the face region.
54
+ The mask will be white (255) on the detected face area and black (0) elsewhere.
55
+ """
56
+ # Convert PIL image to a numpy array in RGB format
57
+ image_np = np.array(pil_image.convert("RGB"))
58
+ # Convert to grayscale for face detection
59
+ gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
60
+
61
+ # Load the Haar cascade for face detection (make sure opencv data is installed)
62
+ cascade_path = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
63
+ face_cascade = cv2.CascadeClassifier(cascade_path)
64
+
65
+ # Detect faces in the image
66
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
67
+
68
+ # Create an empty mask with the same dimensions as the image
69
+ mask = np.zeros_like(gray, dtype=np.uint8)
70
+
71
+ # For each detected face, draw a white rectangle (or a more refined shape)
72
+ for (x, y, w, h) in faces:
73
+ # Optionally expand the bounding box slightly
74
+ padding = 0.2
75
+ x1 = max(0, int(x - w * padding))
76
+ y1 = max(0, int(y - h * padding))
77
+ x2 = min(gray.shape[1], int(x + w * (1 + padding)))
78
+ y2 = min(gray.shape[0], int(y + h * (1 + padding)))
79
+ mask[y1:y2, x1:x2] = 255
80
+
81
+ return Image.fromarray(mask)
82
+
83
  def face_swap_app(target_img, face_img):
84
  if target_img is None or face_img is None:
85
  raise ValueError("Both a target image and a face image must be provided.")
 
91
  output_img, edit_image, change_image, mask, seed = ace_infer(
92
  reference_image=target_img,
93
  edit_image=face_img,
94
+ edit_mask=None,
95
  prompt="Face swap",
96
  output_height=1024,
97
  output_width=1024,
98
  sampler='flow_euler',
99
  sample_steps=28,
100
  guide_scale=50,
101
+ seed=-1
102
  )
103
  return output_img
104
 
config/simple_faceswap.yaml DELETED
@@ -1,17 +0,0 @@
1
- NAME: SimpleFaceSwapConfig
2
- MODEL:
3
- LOCAL_EDITING:
4
- MODEL_PATH: ${LOCAL_MODEL_PATH}
5
- REPAINTING_SCALE: 0.5
6
- PREPROCESSOR:
7
- - NAME: CannyAnnotator
8
- TYPE: canny_repainting
9
- LOW_THRESHOLD: 100
10
- HIGH_THRESHOLD: 200
11
- - NAME: ColorAnnotator
12
- TYPE: mosaic_repainting
13
- RATIO: 64
14
- DTYPE: bfloat16
15
- MAX_SEQ_LEN: 77
16
- SAMPLE_ARGS:
17
- prompt: "Face swap"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -4,4 +4,7 @@ scepter
4
  torch
5
  torchvision
6
  transformers
7
- huggingface_hub
 
 
 
 
4
  torch
5
  torchvision
6
  transformers
7
+ huggingface_hub
8
+ Pillow
9
+ numpy
10
+ opencv-python