Akjava commited on
Commit
ec53eb5
1 Parent(s): 51675df

improve copy image code

Browse files
Files changed (4) hide show
  1. app.py +4 -2
  2. demo_header.html +2 -1
  3. lip_utils.py +37 -2
  4. open_mouth.py +4 -0
app.py CHANGED
@@ -14,8 +14,10 @@ import numpy as np
14
 
15
  from glibvision.cv2_utils import pil_to_bgr_image,bgr_to_rgb
16
  from gradio_utils import save_image,save_buffer,clear_old_files ,read_file
17
- from close_eyes import process_close_eyes_image
18
- from open_mouth import process_open_mouth
 
 
19
  '''
20
  Face landmark detection based Face Detection.
21
  https://ai.google.dev/edge/mediapipe/solutions/vision/face_landmarker
 
14
 
15
  from glibvision.cv2_utils import pil_to_bgr_image,bgr_to_rgb
16
  from gradio_utils import save_image,save_buffer,clear_old_files ,read_file
17
+ from close_eyes import process_close_eyes_image # little bit better
18
+ from open_mouth import process_open_mouth # I'm truly sorry, but I must admit the code is very confusing.comment still written in Japanese
19
+
20
+
21
  '''
22
  Face landmark detection based Face Detection.
23
  https://ai.google.dev/edge/mediapipe/solutions/vision/face_landmarker
demo_header.html CHANGED
@@ -9,7 +9,8 @@
9
  This Space use <a href="http://www.apache.org/licenses/LICENSE-2.0">the Apache 2.0</a> Licensed <a href="https://ai.google.dev/edge/mediapipe/solutions/vision/face_landmarker">Mediapipe FaceLandmarker</a> <br>
10
  One of json format is from MIT licensed <a href="https://github.com/ageitgey/face_recognition">face_recognition</a><br>
11
  I should clarify because it is confusing: I'm not using dlib's non-MIT licensed 68-point model at all.<br>
12
- This is 10-year-old technology. However, most amazing talk-head models,.<br> while often having their core code under MIT/Apache licenses, rely on datasets or NVIDIA libraries with more restrictive licenses.
 
13
  </p>
14
  </div>
15
 
 
9
  This Space use <a href="http://www.apache.org/licenses/LICENSE-2.0">the Apache 2.0</a> Licensed <a href="https://ai.google.dev/edge/mediapipe/solutions/vision/face_landmarker">Mediapipe FaceLandmarker</a> <br>
10
  One of json format is from MIT licensed <a href="https://github.com/ageitgey/face_recognition">face_recognition</a><br>
11
  I should clarify because it is confusing: I'm not using dlib's non-MIT licensed 68-point model at all.<br>
12
+ This is 10-year-old technology. However, most amazing talk-head models,<br> while often having their core code under MIT/Apache licenses, rely on datasets or NVIDIA libraries with more restrictive licenses.<br>
13
+ <a href="https://huggingface.co/blog/Akjava/result-guide-image-eyes-mouth">[Article]</a>Results: Converted Guide Images(eyes-closed and mouth-opened) with Flux.1 schenll img2img/inpaint
14
  </p>
15
  </div>
16
 
lip_utils.py CHANGED
@@ -541,8 +541,43 @@ def create_rgb(width,height):
541
  def create_gray(width,height):
542
  return np.zeros((height, width), dtype=np.uint8)
543
 
544
- def copy_image(img1,img2,x,y):
545
- img1[y:y+img2.shape[0], x:x+img2.shape[1]] = img2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
546
 
547
  def copy_color(img1,x,y,x2,y2,color):
548
  color_img = np.full((y2-y, x2-x, 4), color, dtype=np.uint8)
 
541
  def create_gray(width,height):
542
  return np.zeros((height, width), dtype=np.uint8)
543
 
544
+
545
+ def copy_image(img1, img2, x, y):
546
+ """
547
+ Paste img2 onto img1 at position (x, y).
548
+ If img2 extends beyond the bounds of img1, only the overlapping part is pasted.
549
+
550
+ Parameters:
551
+ img1 (numpy.ndarray): The base image to modify (H, W, C).
552
+ img2 (numpy.ndarray): The image to paste onto img1 (h, w, C).
553
+ x (int): The x-coordinate where img2 will be placed.
554
+ y (int): The y-coordinate where img2 will be placed.
555
+
556
+ Raises:
557
+ TypeError: If img1 or img2 are not NumPy arrays.
558
+ ValueError: If x or y are out of bounds of img1.
559
+ ValueError: If img1 and img2 do not have the same number of channels or are not 3-dimensional arrays.
560
+ """
561
+ # Type check
562
+ if not isinstance(img1, np.ndarray) or not isinstance(img2, np.ndarray):
563
+ raise TypeError("img1 and img2 must be NumPy arrays.")
564
+
565
+ # Channel count check
566
+ if img1.ndim != 3 or img2.ndim != 3 or img1.shape[2] != img2.shape[2]:
567
+ raise ValueError("img1 and img2 must have the same number of channels and be 3-dimensional arrays.")
568
+
569
+ # Bounds check
570
+ max_y, max_x, _ = img1.shape
571
+ if not (0 <= y < max_y and 0 <= x < max_x):
572
+ raise ValueError(f"x ({x}) and y ({y}) must be within the bounds of img1 ({max_x}, {max_y}).")
573
+
574
+ # Calculate the height and width of the overlapping part
575
+ h = min(img2.shape[0], max_y - y)
576
+ w = min(img2.shape[1], max_x - x)
577
+
578
+ # Paste the overlapping part
579
+ img1[y:y+h, x:x+w] = img2[:h, :w]
580
+
581
 
582
  def copy_color(img1,x,y,x2,y2,color):
583
  color_img = np.full((y2-y, x2-x, 4), color, dtype=np.uint8)
open_mouth.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  """
2
  # Currently on hold,Verifying whether to use other technologies.
3
  # this is temporaly fixed for work huggingface space
 
1
+
2
+ #I'm truly sorry, but I must admit the code is very confusing. comment still written in Japanese
3
+
4
+
5
  """
6
  # Currently on hold,Verifying whether to use other technologies.
7
  # this is temporaly fixed for work huggingface space