Xuanyou commited on
Commit
a8c75fd
·
verified ·
1 Parent(s): 5e93526

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -136,9 +136,10 @@ def expand_mask(mask, kernel_size):
136
  return Image.fromarray(expanded_mask_array)
137
 
138
 
139
- def crop_face_to_square(image_rgb, padding_ratio=0.2):
140
  """
141
- Detects the face in the input image and crops an enlarged square region around it.
 
142
  """
143
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
144
  gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
@@ -149,22 +150,29 @@ def crop_face_to_square(image_rgb, padding_ratio=0.2):
149
  return None
150
 
151
  x, y, w, h = faces[0]
152
- center_x, center_y = x + w // 2, y + h // 2
153
- side_length = max(w, h)
154
- padded_side_length = int(side_length * (1 + padding_ratio))
155
- half_side = padded_side_length // 2
156
 
157
- top_left_x = max(center_x - half_side, 0)
158
- top_left_y = max(center_y - half_side, 0)
159
- bottom_right_x = min(center_x + half_side, image_rgb.shape[1])
160
- bottom_right_y = min(center_y + half_side, image_rgb.shape[0])
 
 
 
 
 
161
 
162
  cropped_image = image_rgb[top_left_y:bottom_right_y, top_left_x:bottom_right_x]
163
- resized_image = cv2.resize(cropped_image, (768, 768), interpolation=cv2.INTER_AREA)
 
164
 
165
  return resized_image
166
 
167
 
 
168
  def spirit_animal_baseline(image_path, num_images = 4):
169
 
170
  image = cv2.imread(image_path)
@@ -689,7 +697,7 @@ with gr.Blocks() as demo:
689
  background_option = gr.Radio(choices=["Preserve Background", "Don't Preserve Background"], label="Background Option", value="Preserve Background")
690
  generate_image_button = gr.Button("Generate Image")
691
  gr.Examples(
692
- examples=["example1.jpg", "example2.jpg", "example3.jpg"],
693
  inputs=image_input,
694
  label="Example Images"
695
  )
 
136
  return Image.fromarray(expanded_mask_array)
137
 
138
 
139
+ def crop_face_to_rectangle_with_body(image_rgb, padding_ratio=0.2, height_multiplier=1.5):
140
  """
141
+ Detect the face and crop a rectangular region that includes more of the body below the face.
142
+ Instead of centering around the face, we start near the face region and extend downward.
143
  """
144
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
145
  gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
 
150
  return None
151
 
152
  x, y, w, h = faces[0]
153
+ face_x_center = x + w // 2
154
+ face_y_top = y
155
+ face_side_length = max(w, h)
156
+ padded_side_length = int(face_side_length * (1 + padding_ratio))
157
 
158
+ cropped_width = padded_side_length
159
+ cropped_height = int(padded_side_length * height_multiplier)
160
+
161
+ top_left_x = max(face_x_center - cropped_width // 2, 0)
162
+
163
+ top_margin = int(padded_side_length * 0.1)
164
+ top_left_y = max(face_y_top - top_margin, 0)
165
+ bottom_right_x = min(top_left_x + cropped_width, image_rgb.shape[1])
166
+ bottom_right_y = min(top_left_y + cropped_height, image_rgb.shape[0])
167
 
168
  cropped_image = image_rgb[top_left_y:bottom_right_y, top_left_x:bottom_right_x]
169
+
170
+ resized_image = cv2.resize(cropped_image, (768, 1024), interpolation=cv2.INTER_AREA)
171
 
172
  return resized_image
173
 
174
 
175
+
176
  def spirit_animal_baseline(image_path, num_images = 4):
177
 
178
  image = cv2.imread(image_path)
 
697
  background_option = gr.Radio(choices=["Preserve Background", "Don't Preserve Background"], label="Background Option", value="Preserve Background")
698
  generate_image_button = gr.Button("Generate Image")
699
  gr.Examples(
700
+ examples=["example1.jpg", "example2.jpg", "example3.jpg", "example4.jpg"],
701
  inputs=image_input,
702
  label="Example Images"
703
  )