Parking Finder
The Parking Finder system is a smart parking solution designed to help drivers efficiently locate available
Loading...
Searching...
No Matches
find_distance.CalibWindow Class Reference

Live calibration window with OpenCV trackbars for tuning CALIB_PARAMS. More...

Public Member Functions

 __init__ (self)
 toggle (self, str cam_name)
 tick (self)
 close (self)

Public Attributes

bool open = False
str active_cam = 'Right'
bool suppress_auto = False

Static Public Attributes

str WIN = 'Calibration'

Protected Member Functions

 _cam_idx (self)
 _build (self)
dict _read (self)
 _on_change (self)

Protected Attributes

dict _last_vals = {}
bool _pending_update = False

Detailed Description

Live calibration window with OpenCV trackbars for tuning CALIB_PARAMS.

Opened and closed by pressing C in the display window. Each trackbar maps to one field in CALIB_PARAMS for the currently active camera. Float parameters (fisheye_distortion, perspective_strength) are stored as integers scaled by 100 to work around OpenCV's integer-only trackbar API.

On every slider change, tick() reads all trackbar values, updates CALIB_PARAMS, calls detect_spot_boundaries() on the latest camera snapshot, and then calls generate_rois() to immediately rebuild and display the new ROIs — giving real-time visual feedback while tuning.

suppress_auto is set to True while the window is open to prevent the background auto-calibration thread from overwriting the manual adjustments.

Separate OpenCV window with trackbars for live CALIB_PARAMS tuning.
Opens when C is pressed, closes when C is pressed again.
Automatically regenerates ROIs on any slider change.

Definition at line 1449 of file find_distance.py.

Constructor & Destructor Documentation

◆ __init__()

find_distance.CalibWindow.__init__ ( self)

Definition at line 1457 of file find_distance.py.

1457 def __init__(self):
1458 self.open = False
1459 self.active_cam = 'Right' # tracks which cam's params are shown
1460 self._last_vals = {}
1461 self._pending_update = False
1462 self.suppress_auto = False
1463

Member Function Documentation

◆ _build()

find_distance.CalibWindow._build ( self)
protected

Definition at line 1474 of file find_distance.py.

1474 def _build(self):
1475 p = CALIB_PARAMS[self.active_cam]
1476 cv2.namedWindow(self.WIN, cv2.WINDOW_NORMAL)
1477 cv2.resizeWindow(self.WIN, 500, 280)
1478
1479 # Each trackbar: (name, min, max, initial)
1480 # Multiply floats by 100 to use int trackbars
1481 cv2.createTrackbar('VP X', self.WIN, p['vanishing_point'][0], DISP_W, lambda v: self._on_change())
1482 cv2.createTrackbar('VP Y', self.WIN, p['vanishing_point'][1], DISP_H, lambda v: self._on_change())
1483 cv2.createTrackbar('Near Y', self.WIN, p['near_y'], DISP_H, lambda v: self._on_change())
1484 cv2.createTrackbar('Far Y', self.WIN, p['far_y'], DISP_H, lambda v: self._on_change())
1485 cv2.createTrackbar('Left X', self.WIN, p['left_x'], DISP_W, lambda v: self._on_change())
1486 cv2.createTrackbar('Right X', self.WIN, p['right_x'], DISP_W, lambda v: self._on_change())
1487 cv2.createTrackbar('Spots', self.WIN, p['n_spots'], 20, lambda v: self._on_change())
1488 cv2.createTrackbar('Rows', self.WIN, p['n_rows'], 6, lambda v: self._on_change())
1489 # fisheye_distortion: range -0.30 to +0.30, stored as int -30..30 (divide by 100)
1490 fisheye_int = int(p['fisheye_distortion'] * 100) + 30 # shift so 0 maps to 30
1491 cv2.createTrackbar('Fisheye x100', self.WIN, fisheye_int, 60, lambda v: self._on_change())
1492 strength_int = int(p.get('perspective_strength', 1.0) * 100)
1493 cv2.createTrackbar('Persp x100', self.WIN, strength_int, 100, lambda v: self._on_change())
1494 cv2.createTrackbar('Min Height', self.WIN, p.get('min_roi_height', 60), DISP_H, lambda v: self._on_change())
1495 self.suppress_auto = True
1496 self.open = True
1497 print(f"[Calib] Window open for {self.active_cam}. Drag sliders to tune, C to close.")
1498

◆ _cam_idx()

find_distance.CalibWindow._cam_idx ( self)
protected

Definition at line 1464 of file find_distance.py.

1464 def _cam_idx(self):
1465 return CAM_ORDER.index(self.active_cam)
1466

◆ _on_change()

find_distance.CalibWindow._on_change ( self)
protected

Definition at line 1517 of file find_distance.py.

1517 def _on_change(self):
1518 if self.open:
1519 self._pending_update = True
1520

◆ _read()

dict find_distance.CalibWindow._read ( self)
protected

Definition at line 1499 of file find_distance.py.

1499 def _read(self) -> dict:
1500 fisheye_int = cv2.getTrackbarPos('Fisheye x100', self.WIN)
1501 return {
1502 'vanishing_point': (
1503 cv2.getTrackbarPos('VP X', self.WIN),
1504 cv2.getTrackbarPos('VP Y', self.WIN),
1505 ),
1506 'near_y': cv2.getTrackbarPos('Near Y', self.WIN),
1507 'far_y': cv2.getTrackbarPos('Far Y', self.WIN),
1508 'left_x': cv2.getTrackbarPos('Left X', self.WIN),
1509 'right_x': cv2.getTrackbarPos('Right X', self.WIN),
1510 'n_spots': max(1, cv2.getTrackbarPos('Spots', self.WIN)),
1511 'n_rows': max(1, cv2.getTrackbarPos('Rows', self.WIN)),
1512 'fisheye_distortion': (fisheye_int - 30) / 100.0,
1513 'perspective_strength': cv2.getTrackbarPos('Persp x100', self.WIN) / 100.0,
1514 'min_roi_height': max(20, cv2.getTrackbarPos('Min Height', self.WIN)),
1515 }
1516
Here is the caller graph for this function:

◆ close()

find_distance.CalibWindow.close ( self)

Definition at line 1567 of file find_distance.py.

1567 def close(self):
1568 if self.open:
1569 try:
1570 cv2.destroyWindow(self.WIN)
1571 except Exception:
1572 pass
1573 self.open = False
1574 self.suppress_auto = False
1575 print(f"[Calib] Window closed. Final params for {self.active_cam}:")
1576 print(f" CALIB_PARAMS['{self.active_cam}'] = {CALIB_PARAMS[self.active_cam]}")
1577
1578

◆ tick()

find_distance.CalibWindow.tick ( self)
Call once per display loop iteration to keep the window alive.

Definition at line 1521 of file find_distance.py.

1521 def tick(self):
1522 """Call once per display loop iteration to keep the window alive."""
1523 if not self.open:
1524 return
1525
1526 if not self._pending_update:
1527 return
1528 self._pending_update = False
1529
1530 try:
1531 params = self._read()
1532 near_y = params['near_y']
1533 far_y = params['far_y']
1534 vpy = params['vanishing_point'][1]
1535
1536 if near_y == far_y or vpy >= near_y:
1537 return
1538
1539 CALIB_PARAMS[self.active_cam] = params
1540
1541 cam_idx = CAM_ORDER.index(self.active_cam)
1542 with RAW_LOCKS[cam_idx]:
1543 snap = RAW_BUFS[cam_idx].copy()
1544 snap_disp = cv2.resize(snap, (DISP_W, DISP_H), interpolation=cv2.INTER_NEAREST)
1545 with store_lock:
1546 boxes = _last_boxes.get(self.active_cam, [])
1547 boundaries, detected_n = detect_spot_boundaries(
1548 snap_disp, near_y, far_y,
1549 params['left_x'], params['right_x'], params['n_spots'],
1550 detections=boxes, cam_name=self.active_cam
1551 )
1552 roi_params = {k: v for k, v in params.items() if k not in ('min_roi_height',)}
1553 new_rois = generate_rois(
1554 cam_id=self.active_cam,
1555 n_spots=detected_n,
1556 spot_boundaries=boundaries,
1557 **{k: v for k, v in roi_params.items() if k != 'n_spots'},
1558 )
1559 if new_rois:
1560 rebuild_spot_masks(self.active_cam, new_rois)
1561 n = params['n_spots']
1562 manual = [params['left_x'] + (params['right_x'] - params['left_x']) * i / n for i in range(n + 1)]
1563 _manual_bounds[self.active_cam] = manual
1564 except Exception as e:
1565 print(f"[Calib] tick error: {e}")
1566
Here is the call graph for this function:

◆ toggle()

find_distance.CalibWindow.toggle ( self,
str cam_name )

Definition at line 1467 of file find_distance.py.

1467 def toggle(self, cam_name: str):
1468 self.active_cam = cam_name
1469 if self.open:
1470 self.close()
1471 else:
1472 self._build()
1473

Member Data Documentation

◆ _last_vals

dict find_distance.CalibWindow._last_vals = {}
protected

Definition at line 1460 of file find_distance.py.

◆ _pending_update

bool find_distance.CalibWindow._pending_update = False
protected

Definition at line 1461 of file find_distance.py.

◆ active_cam

find_distance.CalibWindow.active_cam = 'Right'

Definition at line 1459 of file find_distance.py.

◆ open

bool find_distance.CalibWindow.open = False

Definition at line 1458 of file find_distance.py.

◆ suppress_auto

bool find_distance.CalibWindow.suppress_auto = False

Definition at line 1462 of file find_distance.py.

◆ WIN

find_distance.CalibWindow.WIN = 'Calibration'
static

Definition at line 1455 of file find_distance.py.


The documentation for this class was generated from the following file: