feature: add some helper functions

pull/382/head
Bryce 9 months ago committed by Bryce Drennan
parent f243006236
commit 8e956f5360

@ -715,8 +715,8 @@ def _generate_composition_image(prompt, target_height, target_width, cutoff=512)
return img
def prompt_normalized(prompt):
return re.sub(r"[^a-zA-Z0-9.,\[\]-]+", "_", prompt)[:130]
def prompt_normalized(prompt, length=130):
return re.sub(r"[^a-zA-Z0-9.,\[\]-]+", "_", prompt)[:length]
def combine_image(original_img, generated_img, mask_img):

@ -102,3 +102,14 @@ def get_img_masks(img, mask_descriptions: Sequence[str]):
preds_dict[desc] = p
return preds_dict
def img_mask_to_bounding_box(mask_img: PIL.Image.Image):
mask_np = np.array(mask_img)
mask_np = mask_np.astype(np.uint8)
contours, _ = cv2.findContours(mask_np, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
return None
contour = contours[0]
x, y, w, h = cv2.boundingRect(contour)
return x, y, x + w, y + h

Loading…
Cancel
Save