tombio commited on
Commit
2852474
1 Parent(s): 4f73933
Files changed (1) hide show
  1. app.py +51 -8
app.py CHANGED
@@ -23,6 +23,10 @@ import skimage
23
  import mediapipe as mp
24
  import numpy as np
25
 
 
 
 
 
26
  from huggingface_hub import hf_hub_download
27
  ckpt = hf_hub_download(repo_id="lambdalabs/image-mixer", filename="image-mixer-pruned.ckpt", cache_dir="/data/.cache")
28
  config = hf_hub_download(repo_id="lambdalabs/image-mixer", filename="image-mixer-config.yaml", cache_dir="/data/.cache")
@@ -141,13 +145,51 @@ def ethnicity(img):
141
  pred,pred_idx,probs = ethnic_learn.predict(img)
142
  return pred
143
 
144
-
145
- import gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  def boutsify(person):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  portrait_path = "bouts_m1.jpg"
149
- female_detected = is_female(person)
150
- ethnicity_prediction = ethnicity(person)
151
 
152
  if ethnicity_prediction == "Black":
153
  print("Colored person")
@@ -162,19 +204,17 @@ def boutsify(person):
162
  portrait_path = "bouts_f1.jpg"
163
  else:
164
  print("This is a male portrait, checking facial hair")
165
- if has_beard(person):
166
  print("The person has a beard")
167
  portrait_path = "bouts_mb1.jpg"
168
 
169
- person_image = Image.fromarray(person)
170
-
171
  inputs = [
172
  "Image", "Image", "Image", "Image", "Nothing",
173
  "","","","","",
174
  Image.open(portrait_path).convert("RGB"),
175
  Image.open("boutsback_o1.png").convert("RGB"),
176
  Image.open("boutsback_o2.png").convert("RGB"),
177
- person_image,
178
  "",
179
  1.1,1,1,1.4,1,
180
  3.0, 1, random.randrange(0, 10000), 50,
@@ -182,6 +222,9 @@ def boutsify(person):
182
 
183
  #return person
184
  return run_image_mixer(inputs)
 
 
 
185
 
186
  gradio_interface = gradio.Interface(
187
  fn=boutsify,
 
23
  import mediapipe as mp
24
  import numpy as np
25
 
26
+ #import pathlib
27
+ #temp = pathlib.PosixPath
28
+ #pathlib.PosixPath = pathlib.WindowsPath
29
+
30
  from huggingface_hub import hf_hub_download
31
  ckpt = hf_hub_download(repo_id="lambdalabs/image-mixer", filename="image-mixer-pruned.ckpt", cache_dir="/data/.cache")
32
  config = hf_hub_download(repo_id="lambdalabs/image-mixer", filename="image-mixer-config.yaml", cache_dir="/data/.cache")
 
145
  pred,pred_idx,probs = ethnic_learn.predict(img)
146
  return pred
147
 
148
+ def find_face(img):
149
+ mp_face_detection = mp.solutions.face_detection
150
+ img_array = np.array(img)
151
+ print(img_array.shape)
152
+
153
+ with mp_face_detection.FaceDetection(
154
+ model_selection=0,
155
+ min_detection_confidence=0.75
156
+ ) as face_detection:
157
+ results = face_detection.process(img_array)
158
+
159
+ if results.detections is not None:
160
+ return results.detections[0]
161
+
162
+ return None
163
 
164
  def boutsify(person):
165
+ detected_face = find_face(person)
166
+
167
+ if detected_face is None:
168
+ print("Couldn't detect a face")
169
+ return
170
+
171
+ person_image = Image.fromarray(person)
172
+ bounding_box = detected_face.location_data.relative_bounding_box
173
+ width, height = person_image.size
174
+
175
+ width_margin = bounding_box.width * 0.5
176
+ height_margin = bounding_box.height * 0.8
177
+ # Setting the points for cropped image
178
+ left = max(0, (bounding_box.xmin - width_margin / 2.0) * width)
179
+ top = max(0, (bounding_box.ymin - height_margin / 1.2) * height)
180
+
181
+ right = min(width, left + (bounding_box.width + width_margin) * width)
182
+ bottom = min(height, top + (bounding_box.height + height_margin) * height)
183
+
184
+ # Cropped image of above dimension
185
+ # (It will not change original image)
186
+ face = person_image.crop((left, top, right, bottom))
187
+ face_array = np.array(face)
188
+
189
+
190
  portrait_path = "bouts_m1.jpg"
191
+ female_detected = is_female(face_array)
192
+ ethnicity_prediction = ethnicity(face_array)
193
 
194
  if ethnicity_prediction == "Black":
195
  print("Colored person")
 
204
  portrait_path = "bouts_f1.jpg"
205
  else:
206
  print("This is a male portrait, checking facial hair")
207
+ if has_beard(face_array):
208
  print("The person has a beard")
209
  portrait_path = "bouts_mb1.jpg"
210
 
 
 
211
  inputs = [
212
  "Image", "Image", "Image", "Image", "Nothing",
213
  "","","","","",
214
  Image.open(portrait_path).convert("RGB"),
215
  Image.open("boutsback_o1.png").convert("RGB"),
216
  Image.open("boutsback_o2.png").convert("RGB"),
217
+ face,
218
  "",
219
  1.1,1,1,1.4,1,
220
  3.0, 1, random.randrange(0, 10000), 50,
 
222
 
223
  #return person
224
  return run_image_mixer(inputs)
225
+
226
+
227
+ import gradio
228
 
229
  gradio_interface = gradio.Interface(
230
  fn=boutsify,