Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed boxes_from_mask avoiding tiny boxes #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lama_cleaner/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def numpy_to_bytes(image_numpy: np.ndarray, ext: str) -> bytes:
data = cv2.imencode(
f".{ext}",
image_numpy,
[int(cv2.IMWRITE_JPEG_QUALITY), 100, int(cv2.IMWRITE_PNG_COMPRESSION), 0],
[int(cv2.IMWRITE_JPEG_QUALITY), 100, int(
cv2.IMWRITE_PNG_COMPRESSION), 0],
)[1]
image_bytes = data.tobytes()
return image_bytes
Expand Down Expand Up @@ -158,7 +159,7 @@ def pad_img_to_modulo(
)


def boxes_from_mask(mask: np.ndarray) -> List[np.ndarray]:
def boxes_from_mask(mask: np.ndarray, min_area: float = 0.02) -> List[np.ndarray]:
"""
Args:
mask: (h, w, 1) 0~255
Expand All @@ -168,7 +169,10 @@ def boxes_from_mask(mask: np.ndarray) -> List[np.ndarray]:
"""
height, width = mask.shape[:2]
_, thresh = cv2.threshold(mask, 127, 255, 0)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, _ = cv2.findContours(
thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = [c for c in contours if cv2.contourArea(
c) > height*width*min_area]

boxes = []
for cnt in contours:
Expand Down