File size: 3,792 Bytes
c8cad77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useEffect, useState } from "react";
import { RobotRecord } from "@/hooks/useRobots";
import { useAvailableCameras } from "@/hooks/useAvailableCameras";
import { ImageFeatures } from "@/lib/checkpointsApi";
import {
  CameraRequest,
  CameraRotation,
  liveCameraFor,
  PhysicalCamera,
  physicalCameraKey,
  rotatedDimensions,
  rotationForCamera,
  serializeCamera,
} from "@/lib/cameraConfig";

/**
 * Binds each expected camera to a physical one. A robot camera with the same
 * name is bound automatically; the user picks the rest.
 */
export function useCameraBindings(
  imageFeatures: ImageFeatures | null,
  robot: RobotRecord | null,
  enabled: boolean,
) {
  // Per expected camera name → user-selected physical camera index (or null).
  const [bindings, setBindings] = useState<Record<string, number | null>>({});
  // User rotation choice per physical camera, over the one saved with the robot.
  const [rotations, setRotations] = useState<Record<string, CameraRotation>>({});
  const { cameras: availableCameras } = useAvailableCameras({ enabled });

  // If the selected robot has cameras whose names match an expected camera,
  // auto-bind them. Prefer matching by browser device_id (stable across cv2
  // index drift); fall back to the saved camera_index.
  useEffect(() => {
    if (!imageFeatures) return;
    const robotCams = robot?.cameras ?? [];
    if (robotCams.length === 0 || availableCameras.length === 0) return;
    setBindings((prev) => {
      let changed = false;
      const next = { ...prev };
      for (const expectedName of Object.keys(imageFeatures)) {
        if (next[expectedName] != null) continue;
        const robotCam = robotCams.find(
          (c) => c.name.toLowerCase() === expectedName.toLowerCase(),
        );
        if (!robotCam) continue;
        const live = liveCameraFor(robotCam, availableCameras);
        if (live) {
          next[expectedName] = live.index;
          changed = true;
        }
      }
      return changed ? next : prev;
    });
  }, [imageFeatures, robot, availableCameras]);

  const allBound =
    !!imageFeatures && Object.keys(imageFeatures).every((name) => bindings[name] != null);

  const bind = (name: string, index: number) =>
    setBindings((prev) => ({ ...prev, [name]: index }));

  useEffect(() => {
    if (enabled) setRotations({});
  }, [enabled, robot?.name]);

  const physicalFor = (index: number): PhysicalCamera =>
    availableCameras.find((c) => c.index === index) ?? { index, deviceId: "" };

  const rotationFor = (name: string): CameraRotation => {
    const idx = bindings[name];
    return idx == null
      ? 0
      : rotationForCamera(physicalFor(idx), robot?.cameras ?? [], rotations);
  };

  const setRotation = (name: string, rotation: CameraRotation) => {
    const idx = bindings[name];
    if (idx != null) {
      setRotations((prev) => ({ ...prev, [physicalCameraKey(physicalFor(idx))]: rotation }));
    }
  };

  /**
   * The backend camera config: every bound camera. The expected resolution is
   * the rotated output, so the API gets the capture dimensions.
   */
  const toCameraDict = (fps: number) => {
    const cameraDict: Record<string, CameraRequest> = {};
    for (const [name, dims] of Object.entries(imageFeatures ?? {})) {
      const idx = bindings[name];
      if (idx == null) continue;
      const rotation = rotationFor(name);
      const capture = rotatedDimensions(dims.width, dims.height, rotation);
      cameraDict[name] = serializeCamera({
        type: "opencv",
        camera_index: idx,
        width: capture.width,
        height: capture.height,
        rotation,
        fps,
      });
    }
    return cameraDict;
  };

  return { bindings, bind, allBound, availableCameras, rotationFor, setRotation, toCameraDict };
}