import numpy as np, sys from PIL import Image sys.path.insert(0,'.') from fabric_recolor.segmentation.birefnet import BirefnetMatteSegmenter from fabric_recolor.masks import alpha_hysteresis, clean_binary, largest_component from fabric_recolor.segmentation.prompts import NEGATIVE_PROMPTS rgb = np.asarray(Image.open('../../data/media/originals/fd/fd12e584a23312c7c5edda1015fe9715d3f5fd19a2ef3d375d635cefab91f54b.jpg').convert('RGB')) h0,w0=rgb.shape[:2]; scale=1600/max(h0,w0) image = Image.fromarray(rgb).resize((round(w0*scale),round(h0*scale))) arr=np.asarray(image) seg = BirefnetMatteSegmenter(device='cuda'); seg._load() matte = alpha_hysteresis(seg._matte(image), ramp_px=8) mask = clean_binary((matte>=0.5).astype(np.uint8),0.002); mask=largest_component(mask) iso = Image.fromarray(arr * mask[...,None]) iso.save('../../tmp/iso-6413.png') gs = seg._neg_segmenter() gs._load() det = gs._detect(iso, NEGATIVE_PROMPTS) labels = det.get('text_labels') or det.get('labels') or [] for s,l,b in zip(det['scores'].cpu().numpy(), labels, det['boxes'].cpu().numpy()): print(f'{s:.2f} {l!r} box{[round(x) for x in b]}') neg, lab_img, n = gs.negatives_mask(iso, NEGATIVE_PROMPTS) print('union px:', neg.sum(), 'inside mask:', ((neg>0)&(mask>0)).sum())