test_calibration.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import json
  2. import math
  3. import pytest
  4. from core.calibration import CalibrationMapper
  5. def test_calibration_mapper_offset_scale(tmp_path):
  6. calib = {
  7. "pan_offset": 200.0,
  8. "pan_scale_x": 104.0,
  9. "pan_scale_y": 0.0,
  10. "tilt_offset": -10.0,
  11. "tilt_scale_x": 0.0,
  12. "tilt_scale_y": 30.0,
  13. }
  14. path = tmp_path / "calib.json"
  15. path.write_text(json.dumps(calib), encoding="utf-8")
  16. mapper = CalibrationMapper(
  17. str(path),
  18. {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
  19. )
  20. assert mapper.is_loaded()
  21. # visual pan=0 (center, x=0.5) -> pan=200+104*0.5=252
  22. device_pan, device_tilt = mapper.visual_to_device(0, 7)
  23. assert device_pan == pytest.approx(252.0)
  24. # visual tilt=7 (center, y=0.5) -> tilt=-10+30*0.5=5
  25. assert device_tilt == pytest.approx(5.0)
  26. # visual pan=30 (x=0.6667) -> pan=200+104*0.6667=269.3
  27. device_pan, _ = mapper.visual_to_device(30, 7)
  28. assert device_pan == pytest.approx(269.333, abs=0.01)
  29. def test_calibration_mapper_lookup(tmp_path):
  30. calib = {
  31. "pan_lookup": [[0.0, 200.0], [0.5, 250.0], [1.0, 300.0]],
  32. "tilt_lookup": [[0.0, 10.0], [0.5, 0.0], [1.0, -10.0]],
  33. }
  34. path = tmp_path / "calib.json"
  35. path.write_text(json.dumps(calib), encoding="utf-8")
  36. mapper = CalibrationMapper(
  37. str(path),
  38. {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
  39. )
  40. # visual pan=0 -> x=0.5 -> pan=250
  41. device_pan, device_tilt = mapper.visual_to_device(0, 7)
  42. assert device_pan == pytest.approx(250.0)
  43. assert device_tilt == pytest.approx(0.0)
  44. def test_calibration_mapper_missing_file():
  45. mapper = CalibrationMapper(
  46. "/nonexistent/calib.json",
  47. {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
  48. )
  49. assert not mapper.is_loaded()
  50. device_pan, device_tilt = mapper.visual_to_device(30, 5)
  51. assert device_pan == pytest.approx(30.0)
  52. assert device_tilt == pytest.approx(5.0)
  53. def test_calibration_mapper_flips_and_mount_type(tmp_path):
  54. calib = {
  55. "pan_offset": 200.0,
  56. "pan_scale_x": 100.0,
  57. "pan_scale_y": 0.0,
  58. "tilt_offset": 0.0,
  59. "tilt_scale_x": 0.0,
  60. "tilt_scale_y": 50.0,
  61. "mount_type": "ceiling",
  62. "pan_flip": True,
  63. "tilt_flip": False,
  64. }
  65. path = tmp_path / "calib.json"
  66. path.write_text(json.dumps(calib), encoding="utf-8")
  67. mapper = CalibrationMapper(str(path), {})
  68. assert mapper.mount_type == "ceiling"
  69. assert mapper.pan_flip is True
  70. assert mapper.tilt_flip is False