Spaces:
Sleeping
Sleeping
HanningChen
commited on
Commit
·
856cffb
1
Parent(s):
1ec6b19
Add helper function
Browse files- webui/runner.py +50 -1
webui/runner.py
CHANGED
|
@@ -14,7 +14,56 @@ from ImageBind.imagebind.models.imagebind_model import ModalityType
|
|
| 14 |
|
| 15 |
from models.TaskCLIP import TaskCLIP
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
class ModelRunner:
|
| 20 |
def __init__(
|
|
|
|
| 14 |
|
| 15 |
from models.TaskCLIP import TaskCLIP
|
| 16 |
|
| 17 |
+
def _draw_boxes_pil(
|
| 18 |
+
img: Image.Image,
|
| 19 |
+
boxes_xyxy: np.ndarray,
|
| 20 |
+
color: Tuple[int, int, int],
|
| 21 |
+
width: int = 3,
|
| 22 |
+
) -> Image.Image:
|
| 23 |
+
out = img.copy()
|
| 24 |
+
draw = ImageDraw.Draw(out)
|
| 25 |
+
if boxes_xyxy is None or len(boxes_xyxy) == 0:
|
| 26 |
+
return out
|
| 27 |
+
for (x0, y0, x1, y1) in boxes_xyxy.tolist():
|
| 28 |
+
draw.rectangle([x0, y0, x1, y1], outline=color, width=width)
|
| 29 |
+
return out
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _crop_pil(img: Image.Image, bbox_list: List[List[float]]) -> Tuple[List[Image.Image], List[int]]:
|
| 33 |
+
"""Return list of cropped PIL images + indices mapping back to bbox_list."""
|
| 34 |
+
W, H = img.size
|
| 35 |
+
crops = []
|
| 36 |
+
idxs = []
|
| 37 |
+
for i, (x0, y0, x1, y1) in enumerate(bbox_list):
|
| 38 |
+
x0 = max(0, min(W, int(x0)))
|
| 39 |
+
y0 = max(0, min(H, int(y0)))
|
| 40 |
+
x1 = max(0, min(W, int(x1)))
|
| 41 |
+
y1 = max(0, min(H, int(y1)))
|
| 42 |
+
if x1 <= x0 or y1 <= y0:
|
| 43 |
+
continue
|
| 44 |
+
crops.append(img.crop((x0, y0, x1, y1)))
|
| 45 |
+
idxs.append(i)
|
| 46 |
+
return crops, idxs
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def overlay_masks(
|
| 50 |
+
img: Image.Image,
|
| 51 |
+
masks: np.ndarray,
|
| 52 |
+
alpha: float = 0.40,
|
| 53 |
+
color: Tuple[int, int, int] = (255, 0, 0),
|
| 54 |
+
) -> Image.Image:
|
| 55 |
+
if masks is None or len(masks) == 0:
|
| 56 |
+
return img
|
| 57 |
+
|
| 58 |
+
base = np.array(img).astype(np.float32)
|
| 59 |
+
union = np.any(masks.astype(bool), axis=0) # (H, W)
|
| 60 |
+
if not np.any(union):
|
| 61 |
+
return img
|
| 62 |
+
|
| 63 |
+
overlay = base.copy()
|
| 64 |
+
overlay[union] = overlay[union] * 0.2 + np.array(color, dtype=np.float32) * 0.8
|
| 65 |
+
out = base * (1 - alpha) + overlay * alpha
|
| 66 |
+
return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))
|
| 67 |
|
| 68 |
class ModelRunner:
|
| 69 |
def __init__(
|