Skip to content

Count Parts in an Image ​

Prompt Tzara to load any image from disk, segment and count every metal flange with SAM3, print the total count with a confidence score and bounding box for each part, and visualize the numbered masks in Rerun.

SAM3 segments and numbers 17 metal flanges in a bin

Requirements

  • OS: macOS, Linux, or Windows (no GPU, no Isaac Sim, no physical robot).
  • Setup: 5-minute Quickstart (telekinesis-ai + TELEKINESIS_API_KEY) and Tzara VS Code extension.
  • Estimated time: ~2–2.5 min with Gemini 3.8 Flash or local Ollama (inkling-2-mini).

1. Prompt Tzara ​

Open Tzara: Open Chat and send:

text
Write a Python script to load an image, segment and count all metal flanges in it
with SAM3, and visualize the numbered masks in Rerun.

2. Generated Code (count_parts.py) ​

python
"""Segment and count metal flanges in an image with SAM3, and visualize in Rerun."""

import argparse
from pathlib import Path
import rerun as rr
from telekinesis import cornea, datatypes


def count_parts(
    image_path: str | Path,
    objects: list[str] | None = None,
    threshold: float = 0.55,
    mask_threshold: float = 0.5,
    spawn_rerun: bool = True,
) -> int:
    if objects is None:
        objects = ["metal flange"]

    path = Path(image_path)
    image = datatypes.Image.from_path(str(path))
    parts = cornea.segment_image_using_sam3(
        image=image,
        objects=objects,
        threshold=threshold,
        mask_threshold=mask_threshold,
    )

    # category_id is i + 1 for a part matching objects[i].
    print(f"Counted {len(parts)} part(s) in {path.name}:")
    labels: list[str] = []
    for idx, part in enumerate(parts, start=1):
        name = objects[int(part.category_id) - 1]
        label = f"#{idx}: {name} ({float(part.score):.2f})"
        labels.append(label)
        print(f"  {label} bbox={[round(float(v), 1) for v in part.bbox]}")

    rr.init("count_parts_sam3", spawn=spawn_rerun)
    datatypes.visualize(image, parts, entity_path="parts", label=labels)
    return len(parts)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("image_path", type=str)
    parser.add_argument("--objects", nargs="+", default=None, help="Parts to count, e.g. 'metal flange'")
    args = parser.parse_args()
    count_parts(args.image_path, objects=args.objects)

3. Run & Expected Output ​

Download the example image and run the script:

bash
curl -O https://assets.telekinesis.ai/examples/v1/images/xyzibd_flanges_bin_gray.jpg
python count_parts.py xyzibd_flanges_bin_gray.jpg

The terminal prints the total count and one line per detected metal flange:

text
Counted 17 part(s) in xyzibd_flanges_bin_gray.jpg:
  #1: metal flange (0.89) bbox=[888.0, 149.2, 274.8, 273.3]
  #2: metal flange (0.90) bbox=[81.2, 261.4, 247.7, 280.2]
  #3: metal flange (0.88) bbox=[862.7, 528.1, 263.9, 224.3]
  ...
  #17: metal flange (0.86) bbox=[278.6, 679.1, 231.5, 281.9]

Rerun opens with each metal flange's segmentation mask and bounding box drawn on the image, labeled #1, #2, #3, and so on.

To count other parts, pass them with --objects, for example --objects "hex nut" "bolt". Each concept should be a short noun phrase. If SAM3 misses faint or partly occluded parts, lower threshold in count_parts (default 0.55). If it picks up background objects or splits one part into two, raise it.