@blueharford/scrypted-spatial-awareness 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/plugin.zip CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueharford/scrypted-spatial-awareness",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Cross-camera object tracking for Scrypted NVR with spatial awareness",
5
5
  "author": "Joshua Seidel <blueharford>",
6
6
  "license": "Apache-2.0",
package/src/main.ts CHANGED
@@ -1824,8 +1824,7 @@ export class SpatialAwarenessPlugin extends ScryptedDeviceBase
1824
1824
  let updated = false;
1825
1825
 
1826
1826
  if (suggestion.type === 'landmark' && suggestion.landmark) {
1827
- // Calculate a reasonable position for the landmark
1828
- // Use the first visible camera's position as a starting point, or canvas center
1827
+ // Calculate position for the landmark WITHIN the camera's field of view
1829
1828
  let position = suggestion.landmark.position;
1830
1829
  if (!position || (position.x === 0 && position.y === 0)) {
1831
1830
  // Find a camera that can see this landmark
@@ -1833,12 +1832,35 @@ export class SpatialAwarenessPlugin extends ScryptedDeviceBase
1833
1832
  const camera = visibleCameraId ? topology.cameras.find(c => c.deviceId === visibleCameraId) : null;
1834
1833
 
1835
1834
  if (camera?.floorPlanPosition) {
1836
- // Position near the camera with some offset
1837
- const offset = (topology.landmarks?.length || 0) * 30;
1835
+ // Get camera's FOV direction and range (cast to any for flexible access)
1836
+ const fov = (camera.fov || { mode: 'simple', angle: 90, direction: 0, range: 80 }) as any;
1837
+ const direction = fov.direction || 0;
1838
+ const range = fov.range || 80;
1839
+ const fovAngle = fov.angle || 90;
1840
+
1841
+ // Count existing landmarks from this camera to spread them out
1842
+ const existingFromCamera = (topology.landmarks || []).filter(l =>
1843
+ l.visibleFromCameras?.includes(visibleCameraId)
1844
+ ).length;
1845
+
1846
+ // Calculate position in front of camera within its FOV
1847
+ // Convert direction to radians (0 = up/north, 90 = right/east)
1848
+ const dirRad = (direction - 90) * Math.PI / 180;
1849
+ const halfFov = (fovAngle / 2) * Math.PI / 180;
1850
+
1851
+ // Spread landmarks across the FOV cone at varying distances
1852
+ const angleOffset = (existingFromCamera % 3 - 1) * halfFov * 0.6; // -0.6, 0, +0.6 of half FOV
1853
+ const distanceMultiplier = 0.5 + (existingFromCamera % 2) * 0.3; // 50% or 80% of range
1854
+
1855
+ const finalAngle = dirRad + angleOffset;
1856
+ const distance = range * distanceMultiplier;
1857
+
1838
1858
  position = {
1839
- x: camera.floorPlanPosition.x + 50 + (offset % 100),
1840
- y: camera.floorPlanPosition.y + 50 + Math.floor(offset / 100) * 30,
1859
+ x: camera.floorPlanPosition.x + Math.cos(finalAngle) * distance,
1860
+ y: camera.floorPlanPosition.y + Math.sin(finalAngle) * distance,
1841
1861
  };
1862
+
1863
+ this.console.log(`[Discovery] Placing landmark "${suggestion.landmark.name}" in ${camera.name}'s FOV: dir=${direction}°, dist=${distance.toFixed(0)}px`);
1842
1864
  } else {
1843
1865
  // Position in a grid pattern starting from center
1844
1866
  const landmarkCount = topology.landmarks?.length || 0;
@@ -1882,17 +1904,69 @@ export class SpatialAwarenessPlugin extends ScryptedDeviceBase
1882
1904
  ? topology.cameras.find(c => c.deviceId === sourceCameras[0] || c.name === sourceCameras[0])
1883
1905
  : null;
1884
1906
 
1885
- // Create a default polygon near the camera or at a default location
1886
- let centerX = 300;
1887
- let centerY = 200;
1907
+ // Create zone polygon WITHIN the camera's field of view
1908
+ let polygon: { x: number; y: number }[] = [];
1909
+ const timestamp = Date.now();
1910
+
1888
1911
  if (camera?.floorPlanPosition) {
1889
- centerX = camera.floorPlanPosition.x;
1890
- centerY = camera.floorPlanPosition.y + 80;
1891
- }
1912
+ // Get camera's FOV direction and range (cast to any for flexible access)
1913
+ const fov = (camera.fov || { mode: 'simple', angle: 90, direction: 0, range: 80 }) as any;
1914
+ const direction = fov.direction || 0;
1915
+ const range = fov.range || 80;
1916
+ const fovAngle = fov.angle || 90;
1917
+
1918
+ // Convert direction to radians (0 = up/north, 90 = right/east)
1919
+ const dirRad = (direction - 90) * Math.PI / 180;
1920
+ const halfFov = (fovAngle / 2) * Math.PI / 180;
1921
+
1922
+ // Count existing zones from this camera to offset new ones
1923
+ const existingFromCamera = (topology.drawnZones || []).filter((z: any) =>
1924
+ z.linkedCameras?.includes(sourceCameras[0])
1925
+ ).length;
1926
+
1927
+ // Create a wedge-shaped zone within the camera's FOV
1928
+ // Offset based on existing zones to avoid overlap
1929
+ const innerRadius = range * 0.3 + existingFromCamera * 20;
1930
+ const outerRadius = range * 0.8 + existingFromCamera * 20;
1931
+
1932
+ // Use a portion of the FOV for each zone
1933
+ const zoneSpread = halfFov * 0.7; // 70% of half FOV
1934
+
1935
+ const camX = camera.floorPlanPosition.x;
1936
+ const camY = camera.floorPlanPosition.y;
1937
+
1938
+ // Create arc polygon (wedge shape)
1939
+ const steps = 8;
1940
+ // Inner arc (from left to right)
1941
+ for (let i = 0; i <= steps; i++) {
1942
+ const angle = dirRad - zoneSpread + (zoneSpread * 2 * i / steps);
1943
+ polygon.push({
1944
+ x: camX + Math.cos(angle) * innerRadius,
1945
+ y: camY + Math.sin(angle) * innerRadius,
1946
+ });
1947
+ }
1948
+ // Outer arc (from right to left)
1949
+ for (let i = steps; i >= 0; i--) {
1950
+ const angle = dirRad - zoneSpread + (zoneSpread * 2 * i / steps);
1951
+ polygon.push({
1952
+ x: camX + Math.cos(angle) * outerRadius,
1953
+ y: camY + Math.sin(angle) * outerRadius,
1954
+ });
1955
+ }
1892
1956
 
1893
- // Create a rectangular zone (user can edit later)
1894
- const size = 100;
1895
- const timestamp = Date.now();
1957
+ this.console.log(`[Discovery] Creating zone "${zone.name}" in ${camera.name}'s FOV: dir=${direction}°`);
1958
+ } else {
1959
+ // Fallback: rectangular zone at default location
1960
+ const centerX = 300 + (topology.drawnZones?.length || 0) * 120;
1961
+ const centerY = 200;
1962
+ const size = 100;
1963
+ polygon = [
1964
+ { x: centerX - size/2, y: centerY - size/2 },
1965
+ { x: centerX + size/2, y: centerY - size/2 },
1966
+ { x: centerX + size/2, y: centerY + size/2 },
1967
+ { x: centerX - size/2, y: centerY + size/2 },
1968
+ ];
1969
+ }
1896
1970
 
1897
1971
  // 1. Create DrawnZone (visual on floor plan)
1898
1972
  const drawnZone = {
@@ -1900,12 +1974,7 @@ export class SpatialAwarenessPlugin extends ScryptedDeviceBase
1900
1974
  name: zone.name,
1901
1975
  type: (zone.type || 'custom') as DrawnZoneType,
1902
1976
  description: zone.description,
1903
- polygon: [
1904
- { x: centerX - size/2, y: centerY - size/2 },
1905
- { x: centerX + size/2, y: centerY - size/2 },
1906
- { x: centerX + size/2, y: centerY + size/2 },
1907
- { x: centerX - size/2, y: centerY + size/2 },
1908
- ] as any,
1977
+ polygon: polygon as any,
1909
1978
  linkedCameras: sourceCameras,
1910
1979
  };
1911
1980
 
@@ -71,6 +71,19 @@ export const EDITOR_HTML = `<!DOCTYPE html>
71
71
  <p>Topology Editor</p>
72
72
  </div>
73
73
  <div class="sidebar-content">
74
+ <div class="section" style="background: #1a3a5c; margin: -10px -15px 10px -15px; padding: 15px;">
75
+ <div class="section-title" style="margin-bottom: 10px;">
76
+ <span>Floor Plan Scale</span>
77
+ </div>
78
+ <div style="display: flex; gap: 10px; align-items: center;">
79
+ <input type="number" id="scale-input" value="5" min="1" max="50" style="width: 60px; padding: 6px; background: #0f3460; border: 1px solid #1a4a7a; border-radius: 4px; color: #fff;" onchange="updateScale(this.value)">
80
+ <span style="font-size: 12px; color: #888;">pixels per foot</span>
81
+ <button class="btn btn-small" onclick="openScaleHelper()" style="margin-left: auto;">Help</button>
82
+ </div>
83
+ <div style="font-size: 11px; color: #666; margin-top: 8px;">
84
+ Tip: If your floor plan is 800px wide and represents 80ft, scale = 10 px/ft
85
+ </div>
86
+ </div>
74
87
  <div class="section">
75
88
  <div class="section-title">
76
89
  <span>Cameras</span>
@@ -379,6 +392,14 @@ export const EDITOR_HTML = `<!DOCTYPE html>
379
392
  let currentDrawing = null;
380
393
  let blankCanvasMode = false;
381
394
 
395
+ // Floor plan scale: pixels per foot (default assumes ~5 pixels per foot for a typical floor plan)
396
+ // User can adjust this by setting the scale
397
+ let floorPlanScale = 5; // pixels per foot
398
+
399
+ // Helper functions for scale conversion
400
+ function feetToPixels(feet) { return feet * floorPlanScale; }
401
+ function pixelsToFeet(pixels) { return pixels / floorPlanScale; }
402
+
382
403
  // Zone drawing state
383
404
  let zoneDrawingMode = false;
384
405
  let currentZonePoints = [];
@@ -434,6 +455,12 @@ export const EDITOR_HTML = `<!DOCTYPE html>
434
455
  if (response.ok) {
435
456
  topology = await response.json();
436
457
  if (!topology.drawings) topology.drawings = [];
458
+ // Load floor plan scale if saved
459
+ if (topology.floorPlanScale) {
460
+ floorPlanScale = topology.floorPlanScale;
461
+ const scaleInput = document.getElementById('scale-input');
462
+ if (scaleInput) scaleInput.value = floorPlanScale;
463
+ }
437
464
  // Load floor plan from separate storage (handles legacy imageData in topology too)
438
465
  if (topology.floorPlan?.imageData) {
439
466
  // Legacy: imageData was stored in topology
@@ -1603,6 +1630,8 @@ export const EDITOR_HTML = `<!DOCTYPE html>
1603
1630
  function showCameraProperties(camera) {
1604
1631
  const panel = document.getElementById('properties-panel');
1605
1632
  const fov = camera.fov || { mode: 'simple', angle: 90, direction: 0, range: 80 };
1633
+ // Convert stored pixel range to feet for display
1634
+ const rangeInFeet = Math.round(pixelsToFeet(fov.range || 80));
1606
1635
  panel.innerHTML = '<h3>Camera Properties</h3>' +
1607
1636
  '<div class="form-group"><label>Name</label><input type="text" value="' + camera.name + '" onchange="updateCameraName(\\'' + camera.deviceId + '\\', this.value)"></div>' +
1608
1637
  '<div class="form-group"><label class="checkbox-group"><input type="checkbox" ' + (camera.isEntryPoint ? 'checked' : '') + ' onchange="updateCameraEntry(\\'' + camera.deviceId + '\\', this.checked)">Entry Point</label></div>' +
@@ -1610,7 +1639,8 @@ export const EDITOR_HTML = `<!DOCTYPE html>
1610
1639
  '<h4 style="margin-top: 15px; margin-bottom: 10px; color: #888;">Field of View</h4>' +
1611
1640
  '<div class="form-group"><label>Direction (0=up, 90=right)</label><input type="number" value="' + Math.round(fov.direction || 0) + '" min="0" max="359" onchange="updateCameraFov(\\'' + camera.deviceId + '\\', \\'direction\\', this.value)"></div>' +
1612
1641
  '<div class="form-group"><label>FOV Angle (degrees)</label><input type="number" value="' + (fov.angle || 90) + '" min="30" max="180" onchange="updateCameraFov(\\'' + camera.deviceId + '\\', \\'angle\\', this.value)"></div>' +
1613
- '<div class="form-group"><label>Range (pixels)</label><input type="number" value="' + (fov.range || 80) + '" min="20" max="300" onchange="updateCameraFov(\\'' + camera.deviceId + '\\', \\'range\\', this.value)"></div>' +
1642
+ '<div class="form-group"><label>Range (feet)</label><input type="number" value="' + rangeInFeet + '" min="5" max="200" onchange="updateCameraFovRange(\\'' + camera.deviceId + '\\', this.value)"></div>' +
1643
+ '<div style="font-size: 11px; color: #666; margin-top: -10px; margin-bottom: 15px;">~' + (fov.range || 80) + ' pixels at current scale</div>' +
1614
1644
  '<div class="form-group"><button class="btn" style="width: 100%; background: #f44336;" onclick="deleteCamera(\\'' + camera.deviceId + '\\')">Delete Camera</button></div>';
1615
1645
  }
1616
1646
 
@@ -1629,6 +1659,36 @@ export const EDITOR_HTML = `<!DOCTYPE html>
1629
1659
  camera.fov[field] = parseFloat(value);
1630
1660
  render();
1631
1661
  }
1662
+ function updateCameraFovRange(id, feetValue) {
1663
+ // Convert feet to pixels and store
1664
+ const camera = topology.cameras.find(c => c.deviceId === id);
1665
+ if (!camera) return;
1666
+ if (!camera.fov) camera.fov = { mode: 'simple', angle: 90, direction: 0, range: 80 };
1667
+ camera.fov.range = feetToPixels(parseFloat(feetValue));
1668
+ render();
1669
+ // Update the pixel display
1670
+ showCameraProperties(camera);
1671
+ }
1672
+ function updateScale(value) {
1673
+ floorPlanScale = parseFloat(value) || 5;
1674
+ // Store in topology for persistence
1675
+ topology.floorPlanScale = floorPlanScale;
1676
+ render();
1677
+ setStatus('Scale updated: ' + floorPlanScale + ' pixels per foot', 'success');
1678
+ }
1679
+ function openScaleHelper() {
1680
+ alert('How to determine your floor plan scale:\\n\\n' +
1681
+ '1. Measure a known distance on your floor plan in pixels\\n' +
1682
+ ' (e.g., measure a room that you know is 20 feet wide)\\n\\n' +
1683
+ '2. Divide the pixel width by the real width in feet\\n' +
1684
+ ' Example: 200 pixels / 20 feet = 10 pixels per foot\\n\\n' +
1685
+ '3. Enter that value in the scale field\\n\\n' +
1686
+ 'Common scales:\\n' +
1687
+ '- Small floor plan (fits on screen): 3-5 px/ft\\n' +
1688
+ '- Medium floor plan: 5-10 px/ft\\n' +
1689
+ '- Large/detailed floor plan: 10-20 px/ft\\n\\n' +
1690
+ 'Tip: Most outdoor cameras see 30-50 feet, indoor 15-30 feet');
1691
+ }
1632
1692
  function updateConnectionName(id, value) { const conn = topology.connections.find(c => c.id === id); if (conn) conn.name = value; updateUI(); }
1633
1693
  function updateTransitTime(id, field, value) { const conn = topology.connections.find(c => c.id === id); if (conn) conn.transitTime[field] = parseInt(value) * 1000; }
1634
1694
  function updateConnectionBidi(id, value) { const conn = topology.connections.find(c => c.id === id); if (conn) conn.bidirectional = value; render(); }