Akjava commited on
Commit
b99e107
Β·
1 Parent(s): 1d632a0

update tools

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +37 -6
  3. demo_header.html +2 -2
  4. demo_tools.html +7 -1
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: mediapipe-face-crop-and-replace
3
  emoji: πŸ“ˆ
4
  colorFrom: indigo
5
  colorTo: indigo
 
1
  ---
2
+ title: Mediapipe face crop and replace
3
  emoji: πŸ“ˆ
4
  colorFrom: indigo
5
  colorTo: indigo
app.py CHANGED
@@ -42,7 +42,32 @@ def select_box(boxes,box_type):
42
  box = mp_box.xywh_to_xyxy(box)
43
  return box,box_width,box_height
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def process_images(image,replace_image=None,replace_image_need_crop=False,box_type="type-3",fill_color_mode=False,fill_color="black",custom_color="rgba(255,255,255,1)",image_size=1024,margin_percent=0,filter_value="None",match_color=True,progress=gr.Progress(track_tqdm=True)):
 
46
  if image == None:
47
  raise gr.Error("Need Image")
48
 
@@ -59,18 +84,21 @@ def process_images(image,replace_image=None,replace_image_need_crop=False,box_ty
59
  replace_boxes,mp_image,face_landmarker_result = mp_box.mediapipe_to_box(replace_image)
60
  replace_box,replace_box_width,replace_box_height = select_box(replace_boxes,box_type)
61
 
 
62
  # this is for fill_color_mode exported image
63
  if fill_color_mode:
64
  if replace_image_need_crop:
65
  cropped = replace_image.crop(replace_box)
66
- cropped.resize((box_width,box_height))
67
  else:
68
  cropped = replace_image.crop(box)
69
-
 
 
70
  if match_color:
71
  cropped = color_match(image.crop(box),cropped)
72
  #just paste base-face area
73
- image.paste(cropped,[box[0],box[1]])
74
  return image
75
  else:#scale mode
76
  # box expand by margin
@@ -87,11 +115,14 @@ def process_images(image,replace_image=None,replace_image_need_crop=False,box_ty
87
 
88
  if replace_image_need_crop:
89
  replace_image = replace_image.crop(replace_box)
90
- replace_resized = replace_image.resize((box_width,box_height),Image.Resampling.LANCZOS)
 
 
 
91
  if match_color:
92
  replace_resized = color_match(image.crop(box),replace_resized)
93
 
94
- image.paste(replace_resized,[box[0],box[1]])
95
  return image
96
 
97
 
@@ -238,7 +269,7 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
238
  with gr.Accordion(label="Advanced Settings", open=False):
239
  with gr.Row(equal_height=True):
240
  fill_color_mode = gr.Checkbox(label="Fill Color Mode/No Resize",value=False)
241
- match_color = gr.Checkbox(label="Match Color",value=True)
242
  margin_percent = gr.Slider(
243
  label="Margin percent",info = "add extra space",
244
  minimum=0,
 
42
  box = mp_box.xywh_to_xyxy(box)
43
  return box,box_width,box_height
44
 
45
+ def resize_image_in_box(image,box_width,box_height,keep_aspect=True,resampling=None):
46
+
47
+ aspect_ratio = image.width / image.height
48
+ if box_width / box_height >= aspect_ratio:
49
+ new_width = int(box_height * aspect_ratio)
50
+ new_height = box_height
51
+ else:
52
+ new_width = box_width
53
+ new_height = int(box_width / aspect_ratio)
54
+
55
+
56
+ if resampling == None:#automatic
57
+ image_area = image.width * image.height
58
+ if box_width * box_height > image_area:
59
+ resampling = Image.Resampling.BICUBIC
60
+ else:
61
+ resampling = Image.Resampling.LANCZOS
62
+
63
+ resized = image.resize((new_width,new_height),resampling)
64
+ offset_x = int((box_width -new_width)/2)
65
+ offset_y = int((box_height -new_height)/2)
66
+ return resized,offset_x,offset_y
67
+
68
+
69
  def process_images(image,replace_image=None,replace_image_need_crop=False,box_type="type-3",fill_color_mode=False,fill_color="black",custom_color="rgba(255,255,255,1)",image_size=1024,margin_percent=0,filter_value="None",match_color=True,progress=gr.Progress(track_tqdm=True)):
70
+
71
  if image == None:
72
  raise gr.Error("Need Image")
73
 
 
84
  replace_boxes,mp_image,face_landmarker_result = mp_box.mediapipe_to_box(replace_image)
85
  replace_box,replace_box_width,replace_box_height = select_box(replace_boxes,box_type)
86
 
87
+ keep_aspect = True
88
  # this is for fill_color_mode exported image
89
  if fill_color_mode:
90
  if replace_image_need_crop:
91
  cropped = replace_image.crop(replace_box)
92
+ cropped,off_x,off_y = resize_image_in_box(cropped,box_width,box_height,keep_aspect)
93
  else:
94
  cropped = replace_image.crop(box)
95
+ off_x = int((box_width -cropped.width)/2)
96
+ off_y = int((box_height -cropped.height)/2)
97
+
98
  if match_color:
99
  cropped = color_match(image.crop(box),cropped)
100
  #just paste base-face area
101
+ image.paste(cropped,[box[0]+off_x,box[1]+off_y])
102
  return image
103
  else:#scale mode
104
  # box expand by margin
 
115
 
116
  if replace_image_need_crop:
117
  replace_image = replace_image.crop(replace_box)
118
+
119
+
120
+
121
+ replace_resized,off_x,off_y = resize_image_in_box(replace_image,box_width,box_height,keep_aspect)
122
  if match_color:
123
  replace_resized = color_match(image.crop(box),replace_resized)
124
 
125
+ image.paste(replace_resized,[box[0]+off_x,box[1]+off_y])
126
  return image
127
 
128
 
 
269
  with gr.Accordion(label="Advanced Settings", open=False):
270
  with gr.Row(equal_height=True):
271
  fill_color_mode = gr.Checkbox(label="Fill Color Mode/No Resize",value=False)
272
+ match_color = gr.Checkbox(label="Match Color",value=True,info="skimage match_histograms")
273
  margin_percent = gr.Slider(
274
  label="Margin percent",info = "add extra space",
275
  minimum=0,
demo_header.html CHANGED
@@ -3,11 +3,11 @@
3
  Mediapipe Face Crop and Replace
4
  </h1>
5
  <div class="grid-container">
6
- <img src="https://akjava.github.io/AIDiagramChatWithVoice-FaceCharacter/webp/128/00191245_09_00002200.webp" alt="Mediapipe Face Detection" class="image">
7
 
8
  <p class="text">
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
-
11
 
12
  </p>
13
  </div>
 
3
  Mediapipe Face Crop and Replace
4
  </h1>
5
  <div class="grid-container">
6
+ <img src="https://akjava.github.io/AIDiagramChatWithVoice-FaceCharacter/webp/128/00426245.webp" alt="Webp-talk-head" class="image">
7
 
8
  <p class="text">
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
+ <a href="https://huggingface.co/blog/Akjava/img2img-pre-processing">[Article]</a> Better img2img results with Flux.1 schnell by using ScaleUp or Sharpen or FillColor pre-processing
11
 
12
  </p>
13
  </div>
demo_tools.html CHANGED
@@ -1,4 +1,10 @@
1
  <div style="text-align: center;">
2
- <p><a href="https://huggingface.co/spaces/Akjava/mediapipe-face-detect">Mediapipe Face detector</a></p>
 
 
 
 
 
 
3
  <p></p>
4
  </div>
 
1
  <div style="text-align: center;">
2
+ <p>
3
+ <a href="https://huggingface.co/spaces/Akjava/mediapipe-face-detect">Mediapipe Face detector</a> |
4
+ <a href="https://huggingface.co/spaces/Akjava/mediapipe-face-crop-and-replace">Face Crop and Replace</a> |
5
+ <a href="https://huggingface.co/spaces/Akjava/mediapipe-68-points-facial-landmark">68 points landmark</a> |
6
+ <a href="https://huggingface.co/spaces/Akjava/mediapipe-68-points-facial-mask">Create 68 points Parts Mask</a> |
7
+ <a href="https://huggingface.co/spaces/Akjava/histgram-color-matching">Histgram Color Matching</a>
8
+ </p>
9
  <p></p>
10
  </div>