Skip to content

Live Webcam Object Detection ​

Prompt Tzara to build a real-time webcam object detector that captures live frames from your laptop camera, detects cups and bottles with confidence labels using Retina, and streams the annotated results into Rerun.

Cups detected in a live webcam frame

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: ~35–40 s with Gemini 3.8 Flash · ~2–4 min with local Ollama (inkling-2-mini).

1. Prompt Tzara ​

Open Tzara: Open Chat (Ctrl+Shift+P / Cmd+Shift+P) and click the Webcam Object Detection starter card, or send:

text
Write a Python script to capture live frames from my webcam, detect cups and bottles
with confidence labels, and visualize the live detections in Rerun.

2. Generated Code (detect_cups_and_bottles.py) ​

Tzara looks up Webcam, retina.detect_objects_using_grounding_dino, and datatypes.visualize, writes the script, and syntax-checks it automatically:

python
"""Live webcam detection of cups and bottles visualized in Rerun."""

import argparse
import sys
import rerun as rr

from telekinesis import datatypes, retina
from telekinesis.medulla.cameras.webcam import Webcam


def main() -> None:
    parser = argparse.ArgumentParser(description="Live cup and bottle detector using Webcam and Rerun.")
    parser.add_argument("--camera-id", type=int, default=0)
    parser.add_argument("--box-threshold", type=float, default=0.3)
    parser.add_argument("--text-threshold", type=float, default=0.25)
    args = parser.parse_args()

    rr.init("cup_and_bottle_detector", spawn=True)
    camera = Webcam(name="webcam")
    try:
        camera.connect(camera_id=args.camera_id)
    except Exception as exc:
        print(f"Failed to connect to webcam {args.camera_id}: {exc}", file=sys.stderr)
        return

    print("Starting live detection loop. Press Ctrl+C to stop.")
    try:
        while True:
            image = camera.capture_image()
            if image is None:
                continue
            detections, categories = retina.detect_objects_using_grounding_dino(
                image=image,
                objects=["cup", "bottle"],
                box_threshold=args.box_threshold,
                text_threshold=args.text_threshold,
            )
            names = categories.names_by_id()
            labels = [
                f"{names.get(int(d.category_id), 'object')} ({float(d.score):.2f})"
                for d in detections
            ]
            datatypes.visualize(image, detections, entity_path="webcam", label=labels)
    except KeyboardInterrupt:
        print("\nStopping detection stream...")
    finally:
        camera.disconnect()


if __name__ == "__main__":
    main()

3. Run & Expected Output ​

bash
python detect_cups_and_bottles.py

The Rerun viewer opens automatically and streams your live webcam feed under webcam with bounding boxes and confidence scores overlaid on every detected cup or bottle.

Troubleshooting ​

Failed to connect to camera ... with ID 0 on macOS ​

  1. Grant camera permission to VS Code: macOS often silently blocks camera access for scripts launched from VS Code without showing a popup.
    • Open System Settings → Privacy & Security → Camera and toggle ON for Visual Studio Code (and Terminal if running outside VS Code).
    • Fully quit VS Code (Cmd+Q) and reopen it.
  2. Check whether Continuity Camera or a virtual camera took index 0: On macOS, a nearby iPhone (Continuity Camera) or virtual camera often takes 0, pushing the built-in MacBook webcam to 1. Check which camera indices open:
    bash
    python -c "import cv2; print([i for i in range(4) if cv2.VideoCapture(i).isOpened()])"
    If it prints [1] (or [0, 1]), run with --camera-id 1. If it prints [], check #1 above or close apps holding the camera (Zoom, Photo Booth, etc.).