diff --git a/control_vision/app/main_window.py b/control_vision/app/main_window.py index 18361a3..df6fe19 100644 --- a/control_vision/app/main_window.py +++ b/control_vision/app/main_window.py @@ -47,6 +47,11 @@ from control_vision.app.pages.splitter import SplitterPage from control_vision.app.pages.calibrate import CalibratePage from control_vision.app.pages.identity import IdentityPage +from control_vision.app.pages.queue_page import QueuePage +from control_vision.app.pages.live_page import LivePage +from control_vision.app.pages.results_page import ResultsPage +from control_vision.app.pages.fieldmap_page import FieldMapPage +from control_vision.app.runlog import RunLogHandler, RunLogRelay, time_left # Review and timeline rows keep the stored status and confidence for re-tinting. EVENT_STATUS_ROLE = Qt.ItemDataRole.UserRole + 1 @@ -389,13 +394,16 @@ def workflow_steps(*, project_open: bool, video_name: str | None, corners: int, SETTINGS_TAB, RULES_TAB = 7, 8 HOME_TAB, PROJECTS_TAB, WIZARD_TAB, DATASETS_TAB, MODELS_TAB = 9, 10, 11, 12, 13 IMPORT_TAB, SPLITTER_TAB, CALIBRATE_TAB, IDENTITY_TAB = 14, 15, 16, 17 +QUEUE_TAB, LIVE_TAB, RESULTS_TAB, FIELDMAP_TAB = 18, 19, 20, 21 # The first nine pages get Ctrl+1..Ctrl+9 in this order. Settings opens from the top bar. NAV_ENTRIES = ( ("section", "Projects"), (HOME_TAB, "Home"), (PROJECTS_TAB, "Projects"), (0, "Current project"), ("section", "Match setup"), (IMPORT_TAB, "Import videos"), (SPLITTER_TAB, "Livestream splitter"), (CALIBRATE_TAB, "Calibrate field"), (IDENTITY_TAB, "Robots"), - ("section", "Match workflow"), (1, "Set up and process"), (3, "Review queue"), (4, "Scoring timeline"), + ("section", "Match workflow"), (1, "Set up and process"), (QUEUE_TAB, "Processing queue"), + (LIVE_TAB, "Processing live"), (RESULTS_TAB, "Match results"), (FIELDMAP_TAB, "Field map"), + (3, "Review queue"), (4, "Scoring timeline"), (2, "Cycles and stats"), (5, "Highlights"), (6, "Score and export"), ("section", "Training"), (DATASETS_TAB, "Datasets"), (MODELS_TAB, "Models"), ("section", "Reference"), (RULES_TAB, "BIOBUZZ rules"), @@ -796,6 +804,21 @@ def __init__(self): self.tabs.addTab(self.splitter_page, "Livestream splitter") self.tabs.addTab(self.calibrate_page, "Calibrate field") self.tabs.addTab(self.identity_page, "Robots") + self.queue_page = QueuePage(self._job_defaults) + self.live_page = LivePage(self.scoring_config["match_periods_s"]) + self.results_page = ResultsPage() + self.fieldmap_page = FieldMapPage() + self.tabs.addTab(self.queue_page, "Processing queue") + self.tabs.addTab(self.live_page, "Processing live") + self.tabs.addTab(self.results_page, "Match results") + self.tabs.addTab(self.fieldmap_page, "Field map") + self._queue_job: int | None = None + self._queue_paused = True + self._queue_project = None + self._queue_saved_settings = None + self._run_log_relay = RunLogRelay(self) + self._run_log_handler = None + self._run_started = 0.0 self.tabs.tabBar().hide() self.tabs.setDocumentMode(True) self.setCentralWidget(self._build_shell()) @@ -831,6 +854,28 @@ def __init__(self): self.calibrate_page.continue_identity.connect(self.open_in_identity) self.identity_page.saved.connect(self._setup_saved) self.identity_page.continue_processing.connect(self._continue_to_processing) + for page in (self.queue_page, self.results_page, self.fieldmap_page): + page.new_project.connect(self.show_new_project_wizard) + self.queue_page.start_requested.connect(self.start_queue) + self.queue_page.pause_requested.connect(self.pause_queue) + self.queue_page.cancel_requested.connect(self._cancel_job) + self.queue_page.open_live.connect(lambda: self.tabs.setCurrentIndex(LIVE_TAB)) + self.queue_page.open_results.connect(self.open_results) + self.queue_page.open_calibrate.connect(self.open_in_calibrate) + self.queue_page.open_identity.connect(self.open_in_identity) + self.live_page.stop_requested.connect(self.cancel_process) + self.live_page.open_queue.connect(lambda: self.tabs.setCurrentIndex(QUEUE_TAB)) + self.live_page.open_results.connect(self.open_results) + self.live_page.retry_requested.connect(self._retry_video) + self._run_log_relay.line.connect(self.live_page.add_line) + self.results_page.open_map.connect(self.open_field_map) + self.results_page.open_timeline.connect(lambda video: self._open_loaded(video, 4)) + self.results_page.open_highlights.connect(lambda video: self._open_loaded(video, 5)) + self.results_page.open_review.connect(lambda video: self._open_loaded(video, 3)) + self.results_page.open_queue.connect(lambda: self.tabs.setCurrentIndex(QUEUE_TAB)) + self.results_page.official_saved.connect(self._setup_saved) + self.fieldmap_page.open_calibrate.connect(self.open_in_calibrate) + self.fieldmap_page.open_queue.connect(lambda: self.tabs.setCurrentIndex(QUEUE_TAB)) self.open_project_btn.clicked.connect(self.open_project) self.video_btn.clicked.connect(self.import_video) self.load_video_btn.clicked.connect(self.load_saved_video) @@ -1026,13 +1071,20 @@ def _page_shown(self, index: int): self.calibrate_page.refresh_videos() elif index == IDENTITY_TAB: self.identity_page.refresh_videos() + elif index == QUEUE_TAB: + self.queue_page.refresh() + elif index == RESULTS_TAB: + self.results_page.refresh_videos(select=self.video_id) + elif index == FIELDMAP_TAB: + self.fieldmap_page.refresh_videos(select=self.video_id) # ----- Match setup: import, livestream, calibrate, robots ----- def _sync_setup_pages(self): """Give the setup pages the open project (or none).""" db, project_id = (self.db, self.project_id) if self.db is not None and self.project_id is not None else (None, None) - pages = (self.import_page, self.splitter_page, self.calibrate_page, self.identity_page) + pages = (self.import_page, self.splitter_page, self.calibrate_page, self.identity_page, + self.queue_page, self.results_page, self.fieldmap_page) try: folder = self.project_dir() if db is not None else None for page in pages: @@ -1048,6 +1100,23 @@ def _sync_setup_pages(self): else: page.set_project(None, None) self._show_status(f"The setup pages could not read this project: {exc}", "danger") + self._sync_queue_project() + + def _sync_queue_project(self): + """A newly opened project: jobs left running when the app closed are queued again, and the user asked.""" + key = (id(self.db), self.project_id) if self.db is not None and self.project_id is not None else None + if key == self._queue_project: + return + self._queue_project = key + self._queue_paused = True + waiting = 0 + if key is not None and self._queue_job is None: + try: + waiting = self.db.recover_interrupted_jobs() + except sqlite3.Error: + waiting = 0 + self.queue_page.show_resume_prompt(waiting) + self.queue_page.set_runner_state(self._queue_job, self._queue_paused) def setup_edits_allow_leaving(self) -> bool: """Ask before unsaved calibration or robots are lost by switching project.""" @@ -1091,6 +1160,143 @@ def _setup_saved(self, video_id: int): else: self._refresh_workflow() + # ----- Processing queue, live view and results ----- + + def _job_defaults(self): + """Settings a new job keeps: the sample rate, tracking mode and model chosen now.""" + return (int(self.sample_rate.currentText()), self.tracking_mode.currentData(), + self.model_path_edit.text().strip() or None) + + def _job_problem(self, job) -> str | None: + """Why a job cannot run; these are fixed by the user, never retried automatically.""" + video_id = int(job["video_id"]) + if not Path(job["path"]).is_file(): + return f"Video file missing: {job['path']}" + try: + calibration = self.db.calibration_for(video_id) + except ValueError: + calibration = None + if not calibration or len(calibration[0]) != 4: + return "Calibration missing — calibrate this match first." + if not any(robot["seed_x"] is not None for robot in self.db.robots_for_video(video_id)): + return "Robot identities missing — mark at least one robot first." + if job["tracking_mode"] == "yolo" and not (job["model_path"] and Path(job["model_path"]).is_file()): + return f"Model not found: {job['model_path'] or 'no model set'}. Choose one in Settings, then retry." + return None + + def start_queue(self): + self._queue_paused = False + self.queue_page.show_resume_prompt(0) + self._run_next_job() + + def pause_queue(self): + self._queue_paused = True + self.queue_page.set_runner_state(self._queue_job, True) + self._show_status("Queue paused: the running match finishes, the next one does not start.") + + def _run_next_job(self): + if self._queue_paused or self._queue_job is not None or self.db is None: + self.queue_page.set_runner_state(self._queue_job, self._queue_paused) + return + if _video_switch_blocked_quietly(self): + # Nothing restarts the queue when that run ends, so say so rather than wait silently. + self._queue_paused = True + self.queue_page.set_runner_state(None, True) + self._show_status("Queue paused: another run is in progress. Press Start queue when it ends.", "pending") + return + try: + job = self.db.next_queued_job() + if job is None: + self._queue_paused = True + self.queue_page.set_runner_state(None, True) + self._show_status("Processing queue finished.", "success") + return + problem = self._job_problem(job) + if problem is None: + self.load_saved_video_id(int(job["video_id"])) + if self.video_id is None or int(self.video_id) != int(job["video_id"]): + problem = "Could not open this match." + if problem is not None: + self.db.set_job_status(int(job["id"]), "failed", problem) + self.queue_page.refresh() + QTimer.singleShot(0, self._run_next_job) + return + self._queue_job = int(job["id"]) + self._queue_saved_settings = (self.sample_rate.currentText(), self.tracking_mode.currentIndex(), + self.model_path_edit.text()) + self.sample_rate.setCurrentText(str(job["sample_fps"])) + self.tracking_mode.setCurrentIndex(max(0, self.tracking_mode.findData(job["tracking_mode"]))) + self.model_path_edit.setText(job["model_path"] or "") + self.db.set_job_status(self._queue_job, "running") + self.queue_page.set_runner_state(self._queue_job, False) + self.process() + except Exception as exc: # A job that cannot start must not leave the queue stuck "running". + message = f"Processing could not start: {exc}" + # Pause rather than move on: the same fault (a database error, say) would fail every job in turn. + self._queue_paused = True + if self._queue_job is not None: + # process() may have marked the match running, disabled Process and attached the run log: + # processing_failed undoes all of that and fails the job. + self.processing_failed(message) + logger = getattr(self, "logger", None) + for handler in list(logger.handlers) if logger is not None else []: + if isinstance(handler, logging.FileHandler): + logger.removeHandler(handler) + handler.close() + else: + self.queue_page.set_runner_state(None, True) + self._show_status(f"Queue paused. {message}", "danger") + return + if not self._processing_active: + self._queue_job_finished("failed", "Processing could not start.") + + def _queue_job_finished(self, status: str, error: str | None = None): + if self._queue_job is None: + return + job, self._queue_job = self._queue_job, None + if status == "cancelled": + self._queue_paused = True # The user stopped this run: do not start the next one. + try: + self.db.set_job_status(job, status, error) + except sqlite3.Error: + pass + if self._queue_saved_settings is not None: + fps, mode, model = self._queue_saved_settings + self.sample_rate.setCurrentText(fps) + self.tracking_mode.setCurrentIndex(mode) + self.model_path_edit.setText(model) + self._queue_saved_settings = None + self.queue_page.set_runner_state(None, self._queue_paused) + QTimer.singleShot(0, self._run_next_job) + + def _cancel_job(self, job_id: int): + if self._queue_job is not None and int(job_id) == self._queue_job: + self.cancel_process() + + def _retry_video(self, video_id: int): + self.queue_page.queue([int(video_id)]) + self.start_queue() + + def _open_loaded(self, video_id: int, tab: int): + """Open a result screen that shows the loaded video, loading this one first.""" + if self.video_id is None or int(self.video_id) != int(video_id): + self.load_saved_video_id(int(video_id)) + if self.video_id is not None and int(self.video_id) == int(video_id): + self.tabs.setCurrentIndex(tab) + + def open_results(self, video_id: int): + self.results_page.refresh_videos(select=int(video_id)) + self.tabs.setCurrentIndex(RESULTS_TAB) + + def open_field_map(self, video_id: int): + self.fieldmap_page.refresh_videos(select=int(video_id)) + self.tabs.setCurrentIndex(FIELDMAP_TAB) + + def _end_run_log(self): + if self._run_log_handler is not None and getattr(self, "logger", None) is not None: + self.logger.removeHandler(self._run_log_handler) + self._run_log_handler = None + def _continue_to_processing(self, video_id: int): if self.video_id is None or int(self.video_id) != int(video_id): self.load_saved_video_id(int(video_id)) @@ -1339,7 +1545,8 @@ def _nav_tooltip(self, index: int) -> str: if index in (2, 3, 4, 5, 6) and not self.tabs.isTabEnabled(index): return f"{self._nav_labels[index]}: process the video to see results" if index == 3 and self._review_pending: - return f"Review queue: {self._review_pending} event(s) need review ({self._nav_shortcuts[3]})" + shortcut = self._nav_shortcuts.get(3) # The sidebar order decides which pages get Ctrl+1..9. + return f"Review queue: {self._review_pending} event(s) need review" + (f" ({shortcut})" if shortcut else "") return self._nav_tooltips[index] def _set_review_badge(self, flagged: int): @@ -2155,6 +2362,11 @@ def process(self): self.logger = logging.getLogger(f"control_vision.{stamp}"); self.logger.setLevel(logging.INFO) handler = logging.FileHandler(log_dir / f"run-{stamp}.log", encoding="utf-8") handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s")); self.logger.addHandler(handler) + self._run_log_handler = RunLogHandler(self._run_log_relay) + self.logger.addHandler(self._run_log_handler) + self._run_started = time.monotonic() + self.live_page.periods = self.scoring_config["match_periods_s"] + self.live_page.start_run(int(self.video_id), Path(self.video_path), float(self.sample_rate.currentText())) self.logger.info("Video probed: %s; %s", self.video_path, self.metadata) self.logger.info("Zones: %s; sample fps: %s; confidence threshold: %s; tracking mode: %s", self.zones, self.sample_rate.currentText(), self.confidence_spin.value(), mode) self.db.set_analysis_status(self.video_id, "running") @@ -2191,6 +2403,9 @@ def show_progress(self, stage, percent, detail): self._show_status(f"{stage} — {percent:.0f}% — {detail}") self.status_progress.setValue(max(0, min(100, round(percent)))) self._step_widgets["process"][1].setText(f"{stage} — {percent:.0f}%") + self.live_page.set_progress(stage, percent, detail) + if self._queue_job is not None: + self.queue_page.set_progress(self._queue_job, percent, time_left(time.monotonic() - self._run_started, percent)) def processing_done(self, rows): (track_rows, _, _), tracking_mode = rows @@ -2215,6 +2430,9 @@ def processing_done(self, rows): self._processing_active = False self.status_progress.hide() self._refresh_workflow() + self._end_run_log() + self.live_page.finished() + self._queue_job_finished("done") def _store_processing_results(self, rows): (track_rows, photos, roi_events), tracking_mode = rows @@ -2694,6 +2912,13 @@ def processing_failed(self, message): self._processing_active = False self.status_progress.hide() self._refresh_workflow() + if getattr(self, "_run_log_handler", None) is not None: + MainWindow._end_run_log(self) + live = getattr(self, "live_page", None) + if live is not None: + live.failed(message) + if getattr(self, "_queue_job", None) is not None: + self._queue_job_finished("cancelled" if "cancelled" in message.lower() else "failed", message) def cancel_process(self): if self._worker: self._worker.cancel(); self.statusBar().showMessage("Cancelling after the current frame…") diff --git a/control_vision/app/pages/fieldmap_page.py b/control_vision/app/pages/fieldmap_page.py new file mode 100644 index 0000000..9fcf5ff --- /dev/null +++ b/control_vision/app/pages/fieldmap_page.py @@ -0,0 +1,470 @@ +"""Field map and trails: where every robot went, on the calibrated field (guide page 16). + +Trails are the stored positions (feet on the 12 x 12 ft field, measured from the first +corner clicked when calibrating). A break of more than a second between samples is drawn +as a gap, never bridged with a straight line. Ball positions are not stored by this build. +""" +from __future__ import annotations + +import math +from dataclasses import dataclass, field +from pathlib import Path + +from PySide6.QtCore import QPointF, QRectF, Qt, QTimer, Signal +from PySide6.QtGui import QColor, QPainter, QPainterPath, QPen, QPolygonF +from PySide6.QtWidgets import ( + QCheckBox, QComboBox, QHBoxLayout, QLabel, QSlider, QVBoxLayout, QWidget, +) + +from control_vision.app import widgets +from control_vision.app.frameview import FrameView, frame_pixmap, tokens +from control_vision.app.media import format_clock, read_frame +from control_vision.app.pages.calibrate import FIELD_FEET, FieldMap, side_view, warped_field +from control_vision.pipeline.zones import DEFAULT_MAX_GAP_S + +HEAT_CELLS = 24 # 0.5 ft squares +CLICK_PX = 10 +PLAY_STEP_S = 0.5 + + +@dataclass +class RobotTrail: + robot_id: int + team: str + alliance: str + samples: list[tuple[float, float, float]] = field(default_factory=list) # (t_s, x_ft, y_ft) + visible: bool = True + colour: str = "#FFFFFF" + + def segments(self, until_s: float | None = None) -> list[list[tuple[float, float, float]]]: + """Runs of samples with no break longer than the gap limit; a break starts a new segment.""" + runs, run, last_t = [], [], None + for sample in self.samples: + if until_s is not None and sample[0] > until_s: + break + if last_t is not None and sample[0] - last_t > DEFAULT_MAX_GAP_S: + runs.append(run); run = [] + run.append(sample) + last_t = sample[0] + if run: + runs.append(run) + return runs + + def distance_ft(self, until_s: float | None = None) -> float: + return sum(math.dist(a[1:], b[1:]) for run in self.segments(until_s) for a, b in zip(run, run[1:])) + + def gaps(self) -> int: + return max(0, len(self.segments()) - 1) + + +def load_trails(db, video_id: int) -> list[RobotTrail]: + colours = tokens() + trails, seen = [], {"red": 0, "blue": 0} + for robot in db.robots_for_video(video_id): + alliance = robot["alliance"] if robot["alliance"] in ("red", "blue") else "red" + base = QColor(colours.red if alliance == "red" else colours.blue) + colour = base if seen[alliance] == 0 else base.lighter(150) + seen[alliance] += 1 + trails.append(RobotTrail(int(robot["id"]), str(robot["team_number"]), alliance, colour=colour.name())) + by_id = {trail.robot_id: trail for trail in trails} + for row in db.tracks_for_video(video_id): + trail = by_id.get(int(row["robot_id"])) + if trail is not None: + trail.samples.append((float(row["t_s"]), float(row["x_ft"]), float(row["y_ft"]))) + for trail in trails: + trail.samples.sort() + return trails + + +def heat_counts(trails: list[RobotTrail], until_s: float | None = None) -> list[list[int]]: + grid = [[0] * HEAT_CELLS for _ in range(HEAT_CELLS)] + for trail in trails: + if not trail.visible: + continue + for t, x, y in trail.samples: + if until_s is not None and t > until_s: + break + column = min(HEAT_CELLS - 1, max(0, int(x / FIELD_FEET * HEAT_CELLS))) + row = min(HEAT_CELLS - 1, max(0, int(y / FIELD_FEET * HEAT_CELLS))) + grid[row][column] += 1 + return grid + + +class TrailMap(FieldMap): + """The calibrated field with trails, a heatmap, zoom and click-to-seek.""" + + point_clicked = Signal(int, float) # robot id, time in s + + def __init__(self): + super().__init__() + self.setAccessibleName("Field map with robot trails") + self.trails: list[RobotTrail] = [] + self.all_zones: dict = {} + self.time_s: float | None = None + self.show_trails = True + self.show_heatmap = False + self.show_zones = True + self.zoom = 1.0 + self.pan = QPointF(0, 0) + self._press: QPointF | None = None + self._panned = False + + # ----- geometry with zoom ----- + + def field_rect(self) -> QRectF: + base = super().field_rect() + side = base.width() * self.zoom + centre = base.center() + self.pan + return QRectF(centre.x() - side / 2, centre.y() - side / 2, side, side) + + def set_zoom(self, zoom: float) -> None: + self.zoom = min(4.0, max(1.0, zoom)) + if self.zoom == 1.0: + self.pan = QPointF(0, 0) + self.update() + + # ----- painting ----- + + def paintEvent(self, event): + self.zones = self.all_zones if self.show_zones else {} + super().paintEvent(event) + painter = QPainter(self) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + painter.setClipRect(self.rect()) + if self.show_heatmap: + self._paint_heat(painter) + if self.show_trails: + for trail in self.trails: + if trail.visible: + self._paint_trail(painter, trail, faded=self.show_heatmap) + painter.end() + + def _paint_heat(self, painter: QPainter) -> None: + grid = heat_counts(self.trails, self.time_s) + peak = max((value for row in grid for value in row), default=0) + if not peak: + return + rect = self.field_rect() + cell = rect.width() / HEAT_CELLS + painter.setPen(Qt.PenStyle.NoPen) + for row, values in enumerate(grid): + for column, value in enumerate(values): + if value: + share = value / peak # blue = less time, red = more time + colour = QColor.fromHsvF(0.66 * (1 - share), 0.9, 1.0, 0.25 + 0.45 * share) + painter.setBrush(colour) + painter.drawRect(QRectF(rect.left() + column * cell, rect.top() + row * cell, cell, cell)) + + def _paint_trail(self, painter: QPainter, trail: RobotTrail, faded: bool) -> None: + colour = QColor(trail.colour) + if faded: + colour.setAlpha(90) + runs = trail.segments(self.time_s) + if not runs: + return + painter.setBrush(Qt.BrushStyle.NoBrush) + painter.setPen(QPen(colour, 2.5, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin)) + for run in runs: + path = QPainterPath(self.to_widget(run[0][1], run[0][2])) + for _t, x, y in run[1:]: + path.lineTo(self.to_widget(x, y)) + painter.drawPath(path) + start = self.to_widget(runs[0][0][1], runs[0][0][2]) + painter.setBrush(colour) + painter.setPen(Qt.PenStyle.NoPen) + painter.drawEllipse(start, 4, 4) + last_run = runs[-1] + end = self.to_widget(last_run[-1][1], last_run[-1][2]) + if len(last_run) > 1: + before = self.to_widget(last_run[-2][1], last_run[-2][2]) + angle = math.atan2(end.y() - before.y(), end.x() - before.x()) + if end != before: + head = QPolygonF([end + QPointF(math.cos(angle) * 9, math.sin(angle) * 9), + end + QPointF(math.cos(angle + 2.5) * 8, math.sin(angle + 2.5) * 8), + end + QPointF(math.cos(angle - 2.5) * 8, math.sin(angle - 2.5) * 8)]) + painter.drawPolygon(head) + painter.setPen(QColor(tokens().text)) + painter.drawText(end + QPointF(8, -8), trail.team) + + # ----- mouse ----- + + def nearest_point(self, point: QPointF) -> tuple[int, float] | None: + """The trail sample nearest a click; on a tie the earliest wins (when a robot that sat still arrived).""" + best, best_distance = None, CLICK_PX + 0.5 + for trail in self.trails: + if not trail.visible: + continue + for t, x, y in trail.samples: + if self.time_s is not None and t > self.time_s: + break + distance = (self.to_widget(x, y) - point).manhattanLength() + if distance < best_distance: + best, best_distance = (trail.robot_id, t), distance + return best + + def mousePressEvent(self, event): + if event.button() == Qt.MouseButton.LeftButton: + self._press, self._panned = event.position(), False + + def mouseMoveEvent(self, event): + if self._press is not None and self.zoom > 1.0: + delta = event.position() - self._press + if delta.manhattanLength() > 3 or self._panned: + self.pan += delta + self._press, self._panned = event.position(), True + self.update() + + def mouseReleaseEvent(self, event): + if self._press is None: + return + panned, self._press = self._panned, None + if not panned: + hit = self.nearest_point(event.position()) + if hit is not None: + self.point_clicked.emit(*hit) + + def wheelEvent(self, event): + self.set_zoom(self.zoom * (1.25 if event.angleDelta().y() > 0 else 0.8)) + + +class FieldMapPage(QWidget): + open_calibrate = Signal(int) + open_queue = Signal() + new_project = Signal() + + def __init__(self): + super().__init__() + self.setObjectName("Page") + self.db = None + self.project_id = None + self.video: dict | None = None + self.duration = 0.0 + outer = QVBoxLayout(self) + outer.setContentsMargins(0, 0, 0, 0) + outer.setSpacing(12) + self.no_project = widgets.EmptyState("Open a project to see trails", "", "+ New project", self.new_project.emit) + outer.addWidget(self.no_project, 1) + self.body = QWidget() + body = QVBoxLayout(self.body) + body.setContentsMargins(0, 0, 0, 0) + body.setSpacing(12) + outer.addWidget(self.body, 1) + + top = QHBoxLayout() + match_label = QLabel("&Match") + self.video_combo = QComboBox(); self.video_combo.setMinimumWidth(320) + self.video_combo.setAccessibleName("Match") + match_label.setBuddy(self.video_combo) + self.video_combo.currentIndexChanged.connect(lambda _i: self.show_video(self.video_combo.currentData())) + top.addWidget(match_label); top.addWidget(self.video_combo); top.addStretch(1) + self.warning = widgets.label("", tone="pending", wrap=True) + top.addWidget(self.warning, 2) + body.addLayout(top) + + self._empty_action = lambda: None + self.empty = widgets.EmptyState("", "", "Open", lambda: self._empty_action()) + body.addWidget(self.empty, 1) + + self.content = QWidget() + content = QHBoxLayout(self.content) + content.setContentsMargins(0, 0, 0, 0) + content.setSpacing(12) + body.addWidget(self.content, 1) + self.map = TrailMap() + self.map.point_clicked.connect(self._point_clicked) + content.addWidget(self.map, 3) + + side = QVBoxLayout() + side.addWidget(widgets.section_label("Layers")) + self.layer_boxes = {} + for key, text, on in (("trails", "&Trails", True), ("heatmap", "&Heatmap", False), + ("zones", "&Zones", True), ("picture", "&Field picture", True)): + box = QCheckBox(text); box.setChecked(on) + box.toggled.connect(self._layers_changed) + side.addWidget(box) + self.layer_boxes[key] = box + self.balls = QCheckBox("&Ball positions") + widgets.disable_with_reason(self.balls, False, "Ball positions are not stored by this build") + side.addWidget(self.balls) + side.addWidget(widgets.section_label("Robots")) + self.legend = QVBoxLayout() + side.addLayout(self.legend) + self.preview = FrameView("Click a point on a trail to see that moment") + self.preview.setMinimumSize(240, 135) + self.preview.setAccessibleName("Video at the selected time") + side.addWidget(self.preview) + self.coordinates = widgets.label("Positions are in feet on the 12 x 12 ft field, from the first corner " + "clicked when calibrating. A break of more than " + f"{DEFAULT_MAX_GAP_S:g} s is shown as a gap.", role="muted", wrap=True) + side.addWidget(self.coordinates) + side.addStretch(1) + zoom_row = QHBoxLayout() + zoom_row.addWidget(widgets.button("Zoom in", on_click=lambda: self.map.set_zoom(self.map.zoom * 1.25))) + zoom_row.addWidget(widgets.button("Reset", on_click=lambda: self.map.set_zoom(1.0))) + side.addLayout(zoom_row) + side_widget = QWidget(); side_widget.setLayout(side); side_widget.setFixedWidth(280) + content.addWidget(side_widget) + + controls = QHBoxLayout() + self.play_btn = widgets.button("Play", on_click=self.toggle_play) + controls.addWidget(self.play_btn) + self.scrubber = QSlider(Qt.Orientation.Horizontal) + self.scrubber.setAccessibleName("Match time") + self.scrubber.valueChanged.connect(self._scrubbed) + controls.addWidget(self.scrubber, 1) + self.time_label = widgets.label("00:00") + controls.addWidget(self.time_label) + body.addLayout(controls) + self.timer = QTimer(self) + self.timer.setInterval(100) + self.timer.timeout.connect(self._tick) + self.no_project.show(); self.body.hide() + + # ----- project and match ----- + + def set_project(self, db, project_id) -> None: + same = db is self.db and project_id == self.project_id + self.db, self.project_id = db, project_id + has = db is not None and project_id is not None + self.no_project.setVisible(not has) + self.body.setVisible(has) + if not same: + self.video = None + self.refresh_videos() + + def refresh_videos(self, select: int | None = None) -> None: + current = select if select is not None else (self.video["id"] if self.video else None) + rows = [] if self.db is None else self.db.connection.execute( + "SELECT id,path,match_number,analysis_status FROM videos WHERE project_id=? ORDER BY id", + (self.project_id,)).fetchall() + self.video_combo.blockSignals(True) + self.video_combo.clear() + for row in rows: + mark = "✓" if row["analysis_status"] == "ready" else "○" + self.video_combo.addItem(f"{mark} {Path(row['path']).name}" + (f" · {row['match_number']}" if row["match_number"] else ""), + int(row["id"])) + index = self.video_combo.findData(int(current)) if current is not None else -1 + if index < 0: + index = next((i for i, row in enumerate(rows) if row["analysis_status"] == "ready"), 0 if rows else -1) + self.video_combo.setCurrentIndex(index) + self.video_combo.blockSignals(False) + self.show_video(self.video_combo.currentData()) + + def _empty(self, title: str, text: str, action: str, on_click) -> None: + self.content.hide(); self.empty.show() + self.empty.title.setText(title) + self.empty.text.setText(text) + self.empty.action.setText(action) + self.empty.action.setVisible(bool(action)) + self._empty_action = on_click + self.play_btn.setEnabled(False); self.scrubber.setEnabled(False) + + def show_video(self, video_id) -> None: + self.timer.stop(); self.play_btn.setText("Play") + self.warning.setText("") + if self.db is None or video_id is None: + self.video = None + self._empty("No matches yet", "Import a match first.", "", lambda: None) + return + row = self.db.connection.execute("SELECT * FROM videos WHERE id=?", (int(video_id),)).fetchone() + if row is None: + return + self.video = dict(row) + try: + calibration = self.db.calibration_for(int(video_id)) + except ValueError: # The saved corners or zones are not valid JSON. + self._empty("Calibration unreadable — calibrate again", + "The saved calibration for this match could not be read.", + "Calibrate field…", lambda: self.open_calibrate.emit(int(video_id))) + return + if not calibration or len(calibration[0]) != 4: + self._empty("No calibration — calibrate first", "Trails are placed on the field with the calibration.", + "Calibrate field…", lambda: self.open_calibrate.emit(int(video_id))) + return + if self.video["analysis_status"] != "ready": + self._empty("Processing not finished", "Trails appear once this match has been processed.", + "Open the processing queue", self.open_queue.emit) + return + corners, zones = calibration + notes = [] + if side_view(corners): + notes.append("⚑ Side view — trails are less accurate here.") + if self.video.get("tracking_mode") == "seeded_optical_flow": + notes.append("⚑ Seeded preview trails — not accurate enough for scouting.") + self.warning.setText(" ".join(notes)) + self.map.all_zones = dict(zones) + self.map.trails = load_trails(self.db, int(video_id)) + frame = read_frame(Path(self.video["path"]), 0) if Path(self.video["path"]).is_file() else None + warped = warped_field(frame, corners) if frame is not None else None + self._picture = frame_pixmap(warped) if warped is not None else None + last_sample = max((trail.samples[-1][0] for trail in self.map.trails if trail.samples), default=0.0) + self.duration = max(float(self.video["duration_s"] or 0), last_sample) + self.scrubber.blockSignals(True) + self.scrubber.setRange(0, int(self.duration * 10)) + self.scrubber.setValue(int(self.duration * 10)) + self.scrubber.blockSignals(False) + self.map.time_s = None # The whole match until the scrubber moves. + self.time_label.setText(f"{format_clock(self.duration)} / {format_clock(self.duration)}") + self._fill_legend() + self._layers_changed() + self.empty.hide(); self.content.show() + self.play_btn.setEnabled(True); self.scrubber.setEnabled(True) + + def _fill_legend(self) -> None: + while self.legend.count(): + item = self.legend.takeAt(0) + widget = item.widget() + if widget is not None: + # Detach now: deleteLater alone leaves the old widget painted until the event loop runs. + widget.hide() + widget.setParent(None) + widget.deleteLater() + for trail in self.map.trails: + distance = trail.distance_ft(self.map.time_s) + box = QCheckBox(f"{trail.team} · {trail.alliance.title()} · {distance:.0f} ft · {trail.gaps()} gap(s)" + if trail.samples else f"{trail.team} · {trail.alliance.title()} · no positions") + box.setChecked(trail.visible) + box.setStyleSheet(f"QCheckBox {{ color: {trail.colour}; }}") + box.setAccessibleName(f"Show team {trail.team}") + box.toggled.connect(lambda on, t=trail: (setattr(t, "visible", on), self.map.update())) + self.legend.addWidget(box) + + # ----- controls ----- + + def _layers_changed(self) -> None: + self.map.show_trails = self.layer_boxes["trails"].isChecked() + self.map.show_heatmap = self.layer_boxes["heatmap"].isChecked() + self.map.show_zones = self.layer_boxes["zones"].isChecked() + self.map.background = getattr(self, "_picture", None) if self.layer_boxes["picture"].isChecked() else None + self.map.update() + + def _scrubbed(self, value: int) -> None: + self.map.time_s = value / 10 + self.time_label.setText(f"{format_clock(self.map.time_s)} / {format_clock(self.duration)}") + self.map.update() + if not self.timer.isActive(): + self._fill_legend() + + def toggle_play(self) -> None: + if self.timer.isActive(): + self.timer.stop(); self.play_btn.setText("Play"); self._fill_legend() + return + if self.scrubber.value() >= self.scrubber.maximum(): + self.scrubber.setValue(0) + self.timer.start(); self.play_btn.setText("Pause") + + def _tick(self) -> None: + value = self.scrubber.value() + int(PLAY_STEP_S * 10) + if value >= self.scrubber.maximum(): + value = self.scrubber.maximum() + self.timer.stop(); self.play_btn.setText("Play") + self.scrubber.setValue(value) + + def _point_clicked(self, robot_id: int, t_s: float) -> None: + self.scrubber.setValue(int(round(t_s * 10))) + if self.video is not None and Path(self.video["path"]).is_file(): + frame = read_frame(Path(self.video["path"]), t_s) + if frame is not None: + self.preview.set_frame(frame) + team = next((trail.team for trail in self.map.trails if trail.robot_id == robot_id), "?") + self.time_label.setText(f"{format_clock(t_s)} / {format_clock(self.duration)} · team {team}") diff --git a/control_vision/app/pages/live_page.py b/control_vision/app/pages/live_page.py new file mode 100644 index 0000000..f329918 --- /dev/null +++ b/control_vision/app/pages/live_page.py @@ -0,0 +1,219 @@ +"""Processing live: watch the match being analysed (guide page 14). + +Everything shown comes from the run itself: its progress reports and its log. The +pipeline does not hand boxes or positions to the app until it finishes, so the +picture is the video at the time being processed, without boxes, and the page +says so. Results are final only after processing and Review. +""" +from __future__ import annotations + +import time +from pathlib import Path + +from PySide6.QtCore import Signal +from PySide6.QtGui import QBrush, QColor +from PySide6.QtWidgets import ( + QGridLayout, QHBoxLayout, QListWidget, QListWidgetItem, QMessageBox, QProgressBar, QVBoxLayout, QWidget, +) + +from control_vision.app import theme, widgets +from control_vision.app.frameview import FrameView, tokens +from control_vision.app.media import format_clock, read_frame +from control_vision.app.runlog import PhaseTracker, frames_progress, is_id_switch, progress_time, sampled_frame, time_left + +PREVIEW_NOTE = ("Preview — the frame being analysed, without boxes: robot boxes and IDs are not available while a " + "run is in progress in this build. Results are final only after processing and Review.") +PHASE_NOTE = "Phase from video time; assumes the video starts when the match starts." +FRAME_EVERY_S = 1.0 # how often the picture follows the run + + +class LivePage(QWidget): + stop_requested = Signal() + open_queue = Signal() + open_results = Signal(int) # video id + retry_requested = Signal(int) # video id + + def __init__(self, periods: dict | None = None, clock=time.monotonic): + super().__init__() + self.setObjectName("Page") + self.periods = periods or {"auto": 30, "transition": 8, "teleop": 120} + self.clock = clock + self.video_id: int | None = None + self.video_path: Path | None = None + self.state = "idle" + self._reset_counters() + + outer = QVBoxLayout(self) + outer.setContentsMargins(0, 0, 0, 0) + outer.setSpacing(12) + self.idle = widgets.EmptyState("No match is processing", + "Start a match from Set up and process, or start the Processing queue.", + "Open the processing queue", self.open_queue.emit) + outer.addWidget(self.idle, 1) + + self.body = QWidget() + body = QVBoxLayout(self.body) + body.setContentsMargins(0, 0, 0, 0) + body.setSpacing(12) + outer.addWidget(self.body, 1) + head = QHBoxLayout() + self.title = widgets.label("", role="section") + head.addWidget(self.title, 1) + self.state_label = widgets.label("") + head.addWidget(self.state_label) + self.stop_btn = widgets.button("Stop processing", "danger", on_click=self.confirm_stop) + self.results_btn = widgets.button("Open results", "primary", + on_click=lambda: self.open_results.emit(int(self.video_id))) + self.retry_btn = widgets.button("Retry", on_click=lambda: self.retry_requested.emit(int(self.video_id))) + for control in (self.stop_btn, self.results_btn, self.retry_btn): + head.addWidget(control) + body.addLayout(head) + + middle = QHBoxLayout() + left = QVBoxLayout() + self.view = FrameView("Waiting for the first frame…") + self.view.setAccessibleName("Frame being processed") + left.addWidget(self.view, 1) + left.addWidget(widgets.label(PREVIEW_NOTE, role="muted", wrap=True)) + middle.addLayout(left, 3) + + panel = widgets.card() + grid = QGridLayout(panel) + grid.setContentsMargins(16, 12, 16, 12) + self.progress = QProgressBar(); self.progress.setRange(0, 100); self.progress.setAccessibleName("Run progress") + grid.addWidget(self.progress, 0, 0, 1, 2) + self.values: dict[str, object] = {} + for row, (key, text) in enumerate((("frames", "Sampled frames"), ("rate", "Frames per second"), + ("time", "Video time"), ("left", "Time left"), ("phase", "Phase"), + ("detections", "Robot detections, this frame"), + ("switches", "ID switches")), start=1): + grid.addWidget(widgets.label(text, role="muted"), row, 0) + value = widgets.label("—") + value.setAccessibleName(text) + grid.addWidget(value, row, 1) + self.values[key] = value + grid.addWidget(widgets.label(PHASE_NOTE, role="muted", wrap=True), 8, 0, 1, 2) + grid.setRowStretch(9, 1) + middle.addWidget(panel, 2) + body.addLayout(middle, 3) + + body.addWidget(widgets.section_label("Live log")) + self.log = QListWidget() + self.log.setAccessibleName("Live log") + body.addWidget(self.log, 2) + self._show_state() + + def _reset_counters(self) -> None: + self.started_at = self.clock() + self.frames_done = 0 + self.id_switches = 0 + self.last_frame_shown = -FRAME_EVERY_S + self.phases = PhaseTracker(self.periods) + + # ----- a run ----- + + def start_run(self, video_id: int, path: Path, sample_fps: float) -> None: + self.video_id, self.video_path, self.state = int(video_id), Path(path), "running" + self._reset_counters() + self.sample_fps = float(sample_fps) + self.log.clear() + self.progress.setValue(0) + for value in self.values.values(): + value.setText("—") + self.title.setText(f"{self.video_path.name} · {sample_fps:g} fps") + self.view.set_frame(None) + self.add_line("INFO", f"Processing {self.video_path.name} at {sample_fps:g} sampled frames per second.") + self._show_state() + + def set_progress(self, stage: str, percent: float, detail: str) -> None: + if self.state != "running": + return + self.progress.setValue(max(0, min(100, round(percent)))) + self.progress.setFormat(f"{stage} — {percent:.0f}%") + frames = frames_progress(percent, detail) + if frames: + done, total = frames + self.frames_done = done + self.values["frames"].setText(f"{done:,} / {total:,}") + elapsed = max(1e-6, self.clock() - self.started_at) + self.values["rate"].setText(f"{done / elapsed:.1f}") + left = time_left(self.clock() - self.started_at, percent) + self.values["left"].setText(format_clock(left) if left is not None else "Estimating…") + seconds = progress_time(detail) + if seconds is not None: + self._at_time(seconds) + + def add_line(self, level: str, message: str) -> None: + frame = sampled_frame(message) + if frame is not None: + # One line per sampled frame: shown in the panel, not the log. + _number, seconds, detections = frame + self.values["detections"].setText(str(detections)) + self._at_time(seconds) + return + warning = level in ("WARNING", "ERROR") + if is_id_switch(message): + self.id_switches += 1 + self.values["switches"].setText(str(self.id_switches)) + warning = True + self._log(message, "danger" if level == "ERROR" else "pending" if warning else None) + + def _at_time(self, seconds: float) -> None: + self.values["time"].setText(format_clock(seconds)) + phase, remaining, change, skipped = self.phases.update(seconds) + self.values["phase"].setText(f"{phase} · {format_clock(remaining)} left" if phase != "POST-MATCH" else phase) + theme.set_tone(self.values["phase"], "danger" if skipped else None) + if change: + self._log(change, "danger" if skipped else None) + if self.video_path is not None and seconds - self.last_frame_shown >= FRAME_EVERY_S: + self.last_frame_shown = seconds + frame = read_frame(self.video_path, seconds) + if frame is not None: + self.view.set_frame(frame) + + def _log(self, message: str, tone: str | None) -> None: + item = QListWidgetItem(message) + if tone: + colours = tokens() + item.setForeground(QBrush(QColor(colours.danger_fg if tone == "danger" else colours.pending_fg))) + self.log.addItem(item) + self.log.scrollToBottom() + + def finished(self) -> None: + if self.state != "running": + return + self.state = "finished" + self.progress.setValue(100) + self._log(f"Processing finished. ID switches logged: {self.id_switches}.", None) + self._show_state() + + def failed(self, message: str) -> None: + if self.state != "running": + return + self.state = "cancelled" if "cancelled" in message.lower() else "failed" + self._log(message, "danger") + self._show_state() + + def confirm_stop(self) -> None: + if self.state != "running": + return + answer = QMessageBox.question(self, "Stop processing", + "Stop processing this match? Nothing from a stopped run is saved.", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No) + if answer == QMessageBox.StandardButton.Yes: + self.stop_requested.emit() + self._log("Stopping after the current frame…", "pending") + + def _show_state(self) -> None: + self.idle.setVisible(self.state == "idle") + self.body.setVisible(self.state != "idle") + text, tone = {"running": ("Processing…", None), + "finished": ("✓ Processing finished — open Results", "success"), + "failed": ("✕ Processing failed", "danger"), + "cancelled": ("Processing stopped — nothing was saved", "pending")}.get(self.state, ("", None)) + self.state_label.setText(text) + theme.set_tone(self.state_label, tone) + self.stop_btn.setVisible(self.state == "running") + self.results_btn.setVisible(self.state == "finished") + self.retry_btn.setVisible(self.state in ("failed", "cancelled")) diff --git a/control_vision/app/pages/queue_page.py b/control_vision/app/pages/queue_page.py new file mode 100644 index 0000000..4d90a13 --- /dev/null +++ b/control_vision/app/pages/queue_page.py @@ -0,0 +1,379 @@ +"""Processing queue: matches analysed one after another in the background (guide page 13). + +The page shows and edits the queue stored in the project; the window runs the jobs with +the same processing as Set up and process, one at a time, with each job's own settings. +""" +from __future__ import annotations + +from pathlib import Path + +from PySide6.QtCore import Qt, QTimer, Signal +from PySide6.QtWidgets import ( + QAbstractItemView, QComboBox, QDialog, QDialogButtonBox, QHBoxLayout, QHeaderView, QLabel, + QListWidget, QListWidgetItem, QProgressBar, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, +) + +from control_vision.app import widgets +from control_vision.app.media import format_clock + +COLUMNS = ("Match", "Event · match", "Length", "Model", "Fps", "Status", "Progress", "Time left") +STATUS_TEXT = {"queued": ("Queued", "muted"), "running": ("Running", "info"), "done": ("✓ Done", "success"), + "failed": ("✕ Failed", "danger"), "cancelled": ("Cancelled", "pending")} +MODE_TEXT = {"yolo": "YOLO", "seeded_optical_flow": "Seeded preview"} +# Failures the user fixes on a setup page; they are never retried automatically. +FIXES = {"Calibration missing": "calibrate", "Robot identities missing": "identity"} + + +def fix_for(error: str | None) -> str | None: + return next((page for prefix, page in FIXES.items() if (error or "").startswith(prefix)), None) + + +def model_text(mode: str, model_path: str | None) -> str: + if mode == "yolo": + return Path(model_path).name if model_path else "YOLO (no model set)" + return MODE_TEXT.get(mode, mode) + + +def resource_text() -> str: + """CPU use, and whether a YOLO training run is sharing it; empty when psutil is not available.""" + try: + import psutil + except ImportError: + return "" + text = f"CPU {psutil.cpu_percent(interval=None):.0f}%" + try: + training = any("train_yolo" in " ".join(process.info.get("cmdline") or []) + for process in psutil.process_iter(["cmdline"])) + except (psutil.Error, OSError): + training = False + return text + (" · Sharing CPU with training, slower than usual" if training else "") + + +class AddMatchesDialog(QDialog): + """Pick the project's matches that are not processed or queued yet, and the sample rate.""" + + def __init__(self, videos, sample_fps: int, parent=None): + super().__init__(parent) + self.setWindowTitle("Add matches to the queue") + layout = QVBoxLayout(self) + layout.addWidget(widgets.label("Matches not processed yet. Each job keeps the sample rate and model chosen now.", + role="muted", wrap=True)) + self.list = QListWidget() + self.list.setAccessibleName("Matches to add") + for row in videos: + item = QListWidgetItem(f"{Path(row['path']).name}" + (f" · {row['match_number']}" if row["match_number"] else "")) + item.setData(Qt.ItemDataRole.UserRole, int(row["id"])) + item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable) + item.setCheckState(Qt.CheckState.Checked) + self.list.addItem(item) + layout.addWidget(self.list, 1) + if not videos: + layout.addWidget(widgets.label("Every match in this project is processed or already queued.", wrap=True)) + row = QHBoxLayout() + fps_label = QLabel("&Sample rate (frames per second)") + self.fps = QComboBox() + for value in range(5, 11): + self.fps.addItem(str(value), value) + self.fps.setCurrentIndex(max(0, self.fps.findData(int(sample_fps)))) + fps_label.setBuddy(self.fps) + row.addWidget(fps_label); row.addWidget(self.fps); row.addStretch(1) + layout.addLayout(row) + buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) + buttons.button(QDialogButtonBox.StandardButton.Ok).setText("Add to queue") + buttons.accepted.connect(self.accept); buttons.rejected.connect(self.reject) + layout.addWidget(buttons) + + def chosen(self) -> list[int]: + return [self.list.item(i).data(Qt.ItemDataRole.UserRole) for i in range(self.list.count()) + if self.list.item(i).checkState() == Qt.CheckState.Checked] + + +class QueuePage(QWidget): + start_requested = Signal() + pause_requested = Signal() + cancel_requested = Signal(int) # job id + open_live = Signal() + open_results = Signal(int) # video id + open_calibrate = Signal(int) + open_identity = Signal(int) + new_project = Signal() + + def __init__(self, settings=None): + """``settings`` returns the defaults for new jobs: (sample fps, tracking mode, model path).""" + super().__init__() + self.setObjectName("Page") + self.settings = settings or (lambda: (8, "yolo", None)) + self.db = None + self.project_id = None + self.running_job: int | None = None + self.paused = True + self._progress: dict[int, tuple[float, float | None]] = {} + outer = QVBoxLayout(self) + outer.setContentsMargins(0, 0, 0, 0) + outer.setSpacing(12) + self.no_project = widgets.EmptyState("Open a project to process matches", + "The queue belongs to a project. Create one, or open one from Projects.", + "+ New project", self.new_project.emit) + outer.addWidget(self.no_project, 1) + self.body = QWidget() + body = QVBoxLayout(self.body) + body.setContentsMargins(0, 0, 0, 0) + body.setSpacing(12) + outer.addWidget(self.body, 1) + + top = QHBoxLayout() + self.add_btn = widgets.button("Add matches…", on_click=self.add_matches) + self.start_btn = widgets.button("Start queue", "primary", on_click=self.start_requested.emit) + self.pause_btn = widgets.button("Pause all", on_click=self.pause_requested.emit, + tooltip="The running match finishes; the next one does not start.") + self.clear_btn = widgets.button("Clear finished", on_click=self.clear_finished) + self.retry_btn = widgets.button("Retry failed", on_click=self.retry_failed) + for control in (self.add_btn, self.start_btn, self.pause_btn, self.clear_btn, self.retry_btn): + top.addWidget(control) + top.addStretch(1) + self.resources = widgets.label("", role="muted") + self.resources.setAccessibleName("Computer load") + top.addWidget(self.resources) + body.addLayout(top) + + self.banner = widgets.label("", tone="pending", wrap=True) + self.banner.hide() + body.addWidget(self.banner) + + self.empty = widgets.EmptyState("No jobs — add a match to process", + "Matches need a calibration and at least one robot marker to be processed.", + "Add matches…", self.add_matches) + body.addWidget(self.empty, 1) + self.table = QTableWidget(0, len(COLUMNS)) + self.table.setHorizontalHeaderLabels(COLUMNS) + self.table.setAccessibleName("Processing queue") + self.table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) + self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) + self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) + self.table.verticalHeader().setVisible(False) + self.table.setShowGrid(False) + header = self.table.horizontalHeader() + header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) + header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) + header.setSectionResizeMode(6, QHeaderView.ResizeMode.Fixed) + self.table.setColumnWidth(6, 160) + self.table.itemSelectionChanged.connect(self._sync_actions) + self.table.cellDoubleClicked.connect(lambda *_: self.open_selected()) + body.addWidget(self.table, 1) + self.error = widgets.label("", tone="danger", wrap=True) + self.error.hide() + body.addWidget(self.error) + + actions = QHBoxLayout() + self.up_btn = widgets.button("Move up", on_click=lambda: self.move_selected(-1)) + self.down_btn = widgets.button("Move down", on_click=lambda: self.move_selected(1)) + self.remove_btn = widgets.button("Remove", on_click=self.remove_selected) + self.cancel_btn = widgets.button("Cancel", "danger", on_click=self._cancel_selected, + tooltip="Stops after the current frame; nothing from a cancelled run is saved.") + self.open_btn = widgets.button("Open", on_click=self.open_selected) + for control in (self.up_btn, self.down_btn, self.remove_btn, self.cancel_btn): + actions.addWidget(control) + actions.addStretch(1) + actions.addWidget(self.open_btn) + body.addLayout(actions) + + self._load_timer = QTimer(self) + self._load_timer.setInterval(5000) + self._load_timer.timeout.connect(lambda: self.resources.setText(resource_text())) + self.no_project.show(); self.body.hide() + + # ----- project ----- + + def set_project(self, db, project_id) -> None: + self.db, self.project_id = db, project_id + has = db is not None and project_id is not None + self.no_project.setVisible(not has) + self.body.setVisible(has) + self.refresh() + + def showEvent(self, event): + super().showEvent(event) + self.resources.setText(resource_text()) + self._load_timer.start() + + def hideEvent(self, event): + super().hideEvent(event) + self._load_timer.stop() + + def jobs(self) -> list: + return list(self.db.processing_jobs()) if self.db is not None else [] + + def show_resume_prompt(self, waiting: int) -> None: + if waiting: + self.banner.setText(f"⚑ {waiting} job(s) were queued when the app closed. Start queue to resume, " + "or Remove them.") + self.banner.setVisible(bool(waiting)) + + # ----- state from the runner ----- + + def set_runner_state(self, running_job: int | None, paused: bool) -> None: + if running_job is not None and running_job != self.running_job: + self._progress.pop(int(running_job), None) # A retried job starts again from 0%. + self.running_job, self.paused = running_job, paused + if running_job is not None: + self.banner.hide() + self.refresh() + + def set_progress(self, job_id: int, percent: float, seconds_left: float | None) -> None: + self._progress[int(job_id)] = (percent, seconds_left) + for row in range(self.table.rowCount()): + if self.table.item(row, 0).data(Qt.ItemDataRole.UserRole) == int(job_id): + self._show_progress(row, int(job_id), "running") + + # ----- table ----- + + def refresh(self) -> None: + selected = self.selected_job() + jobs = self.jobs() + self.table.setRowCount(len(jobs)) + for row, job in enumerate(jobs): + status, tone = STATUS_TEXT.get(job["status"], (job["status"], "muted")) + event = " · ".join(part for part in (job["event_code"], job["match_number"]) if part) or "—" + values = (Path(job["path"]).name, event, format_clock(job["duration_s"] or 0), + model_text(job["tracking_mode"], job["model_path"]), str(job["sample_fps"]), status) + for column, text in enumerate(values): + item = QTableWidgetItem(text) + item.setData(Qt.ItemDataRole.UserRole, int(job["id"])) + if column == 0: + item.setToolTip(job["path"]) + if column == 5: + item.setForeground(_tone_brush(tone)) + if job["error"]: + item.setToolTip(job["error"]) + self.table.setItem(row, column, item) + self._show_progress(row, int(job["id"]), job["status"]) + if selected is not None: + for row in range(self.table.rowCount()): + if self.table.item(row, 0).data(Qt.ItemDataRole.UserRole) == selected["id"]: + self.table.selectRow(row) + self.empty.setVisible(not jobs) + self.table.setVisible(bool(jobs)) + waiting = any(job["status"] == "queued" for job in jobs) + running = self.running_job is not None + widgets.disable_with_reason(self.start_btn, waiting and (self.paused or not running), + "Nothing is waiting, or the queue is already running") + self.start_btn.setText("Resume queue" if waiting and self.paused and any( + job["status"] in ("done", "failed", "cancelled") for job in jobs) else "Start queue") + widgets.disable_with_reason(self.pause_btn, not self.paused, "The queue is not running") + widgets.disable_with_reason(self.clear_btn, any(job["status"] in ("done", "cancelled") for job in jobs), + "No finished jobs") + widgets.disable_with_reason(self.retry_btn, any(job["status"] in ("failed", "cancelled") for job in jobs), + "No failed jobs") + self._sync_actions() + + def _show_progress(self, row: int, job_id: int, status: str) -> None: + if status == "running": + percent, left = self._progress.get(job_id, (0.0, None)) + bar = self.table.cellWidget(row, 6) + if not isinstance(bar, QProgressBar): + bar = QProgressBar(); bar.setRange(0, 100); bar.setAccessibleName("Job progress") + self.table.setCellWidget(row, 6, bar) + bar.setValue(max(0, min(100, round(percent)))) + bar.setFormat(f"{percent:.0f}%") + self.table.setItem(row, 7, QTableWidgetItem(format_clock(left) if left is not None else "Estimating…")) + else: + if self.table.cellWidget(row, 6) is not None: + self.table.removeCellWidget(row, 6) + self.table.setItem(row, 6, QTableWidgetItem("100%" if status == "done" else "")) + self.table.setItem(row, 7, QTableWidgetItem("")) + + def selected_job(self): + rows = self.table.selectionModel().selectedRows() if self.table.selectionModel() else [] + if not rows or self.db is None: + return None + job_id = self.table.item(rows[0].row(), 0).data(Qt.ItemDataRole.UserRole) + return next((job for job in self.jobs() if int(job["id"]) == int(job_id)), None) + + def _sync_actions(self) -> None: + job = self.selected_job() + status = job["status"] if job else None + widgets.disable_with_reason(self.up_btn, job is not None, "Select a job") + widgets.disable_with_reason(self.down_btn, job is not None, "Select a job") + widgets.disable_with_reason(self.remove_btn, job is not None and status != "running", + "Select a job that is not running") + widgets.disable_with_reason(self.cancel_btn, status == "running", "Select the running job") + fix = fix_for(job["error"]) if job and status == "failed" else None + label = {"running": "Open live view", "done": "Open results"}.get(status) + if fix == "calibrate": + label = "Calibrate…" + elif fix == "identity": + label = "Assign robots…" + self.open_btn.setText(label or "Open") + widgets.disable_with_reason(self.open_btn, label is not None, "Select a running, finished or fixable job") + if job is not None and status == "failed" and job["error"]: + self.error.setText(f"✕ {job['error']}") + self.error.show() + else: + self.error.hide() + + # ----- actions ----- + + def add_matches(self) -> None: + if self.db is None: + return + queued = {int(job["video_id"]) for job in self.jobs() if job["status"] in ("queued", "running")} + videos = [row for row in self.db.connection.execute( + "SELECT id,path,match_number,analysis_status FROM videos WHERE project_id=? ORDER BY id", (self.project_id,)) + if row["analysis_status"] != "ready" and int(row["id"]) not in queued] + fps, mode, model = self.settings() + dialog = AddMatchesDialog(videos, fps, self) + if dialog.exec() == QDialog.DialogCode.Accepted: + self.queue(dialog.chosen(), dialog.fps.currentData()) + + def queue(self, video_ids, sample_fps: int | None = None) -> list[int]: + fps, mode, model = self.settings() + ids = self.db.queue_jobs(video_ids, sample_fps or fps, mode, model) + self.refresh() + return ids + + def move_selected(self, offset: int) -> None: + job = self.selected_job() + if job is not None and self.db.move_job(int(job["id"]), offset): + self.refresh() + + def remove_selected(self) -> None: + job = self.selected_job() + if job is not None and job["status"] != "running": + self.db.remove_jobs([int(job["id"])]) + self.refresh() + + def clear_finished(self) -> None: + self.db.clear_finished_jobs() + self.refresh() + + def retry_failed(self) -> None: + self.db.retry_failed_jobs() + self.refresh() + + def _cancel_selected(self) -> None: + job = self.selected_job() + if job is not None and job["status"] == "running": + self.cancel_requested.emit(int(job["id"])) + + def open_selected(self) -> None: + job = self.selected_job() + if job is None: + return + if job["status"] == "running": + self.open_live.emit() + elif job["status"] == "done": + self.open_results.emit(int(job["video_id"])) + elif job["status"] == "failed": + fix = fix_for(job["error"]) + if fix == "calibrate": + self.open_calibrate.emit(int(job["video_id"])) + elif fix == "identity": + self.open_identity.emit(int(job["video_id"])) + + +def _tone_brush(tone: str): + from PySide6.QtGui import QBrush, QColor + from control_vision.app.frameview import tokens + colours = tokens() + return QBrush(QColor({"success": colours.success_fg, "pending": colours.pending_fg, "danger": colours.danger_fg, + "info": colours.accent}.get(tone, colours.text_muted))) diff --git a/control_vision/app/pages/results_page.py b/control_vision/app/pages/results_page.py new file mode 100644 index 0000000..10a1fb4 --- /dev/null +++ b/control_vision/app/pages/results_page.py @@ -0,0 +1,271 @@ +"""Match results overview: who won, by how much, and how our count compares (guide page 15). + +Our totals come from the confirmed scoring events, scored with the same rules as Score and +export. The official score is whatever the user entered (or took from the scoreboard OCR), +shown next to ours and never merged. Nothing is final while events still need review. +""" +from __future__ import annotations + +from pathlib import Path + +from PySide6.QtCore import Signal +from PySide6.QtWidgets import ( + QComboBox, QGridLayout, QHBoxLayout, QLabel, QSpinBox, QVBoxLayout, QWidget, +) + +from control_vision.app import theme, widgets +from control_vision.scout.exports import match_scoreboard + +OFFICIAL_TOLERANCE = 0.10 # the guide's ±10% check +LIVE_NOTE = ("End-of-match points come from the results screen, not the live video, so a total read from the video " + "can trail the official one.") + + +def official_check(ours: int, official: int | None) -> tuple[str, str]: + """Text and tone comparing our alliance total with the official one.""" + if official is None: + return "Official score not entered yet", "pending" + if official == 0: + return ("Matches official", "success") if ours == 0 else ("Official is 0 — check Review", "danger") + difference = abs(ours - official) / official + if difference <= OFFICIAL_TOLERANCE: + return f"Within ±10% of official ({ours - official:+d})", "success" + return f"Differs from official by {difference:.0%} — check Review", "danger" + + +def event_alliance(event) -> str | None: + """The alliance an event scores for, in the scoreboard's order: scoring alliance, event alliance, robot.""" + keys = event.keys() + for key in ("scoring_alliance", "event_alliance", "alliance"): + if key in keys and event[key]: + return event[key] + return None + + +class ResultsPage(QWidget): + open_map = Signal(int) + open_timeline = Signal(int) + open_highlights = Signal(int) + open_review = Signal(int) + open_queue = Signal() + official_saved = Signal(int) + new_project = Signal() + + def __init__(self): + super().__init__() + self.setObjectName("Page") + self.db = None + self.project_id = None + self.video: dict | None = None + outer = QVBoxLayout(self) + outer.setContentsMargins(0, 0, 0, 0) + outer.setSpacing(12) + self.no_project = widgets.EmptyState("Open a project to see match results", "", "+ New project", + self.new_project.emit) + outer.addWidget(self.no_project, 1) + self.body = QWidget() + body = QVBoxLayout(self.body) + body.setContentsMargins(0, 0, 0, 0) + body.setSpacing(12) + outer.addWidget(self.body, 1) + + top = QHBoxLayout() + match_label = QLabel("&Match") + self.video_combo = QComboBox(); self.video_combo.setMinimumWidth(320) + self.video_combo.setAccessibleName("Match") + match_label.setBuddy(self.video_combo) + self.video_combo.currentIndexChanged.connect(lambda _i: self.show_video(self.video_combo.currentData())) + top.addWidget(match_label); top.addWidget(self.video_combo); top.addStretch(1) + self.review_badge = widgets.label("") + top.addWidget(self.review_badge) + body.addLayout(top) + + self.not_processed = widgets.EmptyState("Processing not finished", + "Results appear here once this match has been processed.", + "Open the processing queue", self.open_queue.emit) + body.addWidget(self.not_processed, 1) + + self.results = QWidget() + results = QVBoxLayout(self.results) + results.setContentsMargins(0, 0, 0, 0) + results.setSpacing(12) + body.addWidget(self.results, 1) + self.header = widgets.label("", role="section", wrap=True) + results.addWidget(self.header) + + scores = QHBoxLayout() + self.cards = {} + for alliance in ("red", "blue"): + card = widgets.card() + card.setProperty("alliance", alliance) + grid = QGridLayout(card) + grid.setContentsMargins(16, 12, 16, 12) + grid.addWidget(widgets.label(f"{alliance.title()} alliance", role="section"), 0, 0, 1, 2) + ours = widgets.label("—"); ours.setProperty("role", "metric") + ours.setAccessibleName(f"{alliance.title()} total, ours") + grid.addWidget(widgets.label("Ours", role="muted"), 1, 0); grid.addWidget(ours, 2, 0) + official = QSpinBox(); official.setRange(-1, 999); official.setSpecialValueText("Not entered") + official.setAccessibleName(f"{alliance.title()} official score") + grid.addWidget(widgets.label("Official", role="muted"), 1, 1); grid.addWidget(official, 2, 1) + check = widgets.label("", wrap=True) + grid.addWidget(check, 3, 0, 1, 2) + detail = widgets.label("", role="muted", wrap=True) + grid.addWidget(detail, 4, 0, 1, 2) + self.cards[alliance] = {"ours": ours, "official": official, "check": check, "detail": detail} + scores.addWidget(card, 1) + results.addLayout(scores) + official_row = QHBoxLayout() + self.save_official_btn = widgets.button("Save official score", on_click=self.save_official, + tooltip="From ftc-events.firstinspires.org or the results screen.") + official_row.addWidget(widgets.label(LIVE_NOTE, role="muted", wrap=True), 1) + official_row.addWidget(self.save_official_btn) + results.addLayout(official_row) + + results.addWidget(widgets.section_label("Robots")) + self.robot_row = QHBoxLayout() + results.addLayout(self.robot_row) + self.accuracy = widgets.label("", wrap=True) + results.addWidget(self.accuracy) + + buttons = QHBoxLayout() + for text, signal in (("View map", self.open_map), ("View timeline", self.open_timeline), + ("View highlights", self.open_highlights), ("Open review queue", self.open_review)): + buttons.addWidget(widgets.button(text, on_click=lambda _=False, s=signal: s.emit(int(self.video["id"])))) + buttons.addStretch(1) + results.addLayout(buttons) + results.addStretch(1) + self.no_project.show(); self.body.hide() + + # ----- project and match ----- + + def set_project(self, db, project_id) -> None: + same = db is self.db and project_id == self.project_id + self.db, self.project_id = db, project_id + has = db is not None and project_id is not None + self.no_project.setVisible(not has) + self.body.setVisible(has) + if not same: + self.video = None + self.refresh_videos() + + def refresh_videos(self, select: int | None = None) -> None: + current = select if select is not None else (self.video["id"] if self.video else None) + rows = [] if self.db is None else self.db.connection.execute( + "SELECT id,path,match_number,analysis_status FROM videos WHERE project_id=? ORDER BY id", + (self.project_id,)).fetchall() + self.video_combo.blockSignals(True) + self.video_combo.clear() + for row in rows: + mark = "✓" if row["analysis_status"] == "ready" else "○" + self.video_combo.addItem(f"{mark} {Path(row['path']).name}" + (f" · {row['match_number']}" if row["match_number"] else ""), + int(row["id"])) + index = self.video_combo.findData(int(current)) if current is not None else -1 + if index < 0: + index = next((i for i, row in enumerate(rows) if row["analysis_status"] == "ready"), 0 if rows else -1) + self.video_combo.setCurrentIndex(index) + self.video_combo.blockSignals(False) + self.show_video(self.video_combo.currentData()) + + def show_video(self, video_id) -> None: + if self.db is None or video_id is None: + self.video = None + self.not_processed.show(); self.results.hide(); self.review_badge.setText("") + return + row = self.db.connection.execute("SELECT * FROM videos WHERE id=?", (int(video_id),)).fetchone() + if row is None: + return + self.video = dict(row) + ready = self.video["analysis_status"] == "ready" + self.not_processed.setVisible(not ready) + self.results.setVisible(ready) + if ready: + self._render() + else: + self.review_badge.setText("") + + # ----- rendering ----- + + def board(self, robots, events) -> dict: + return match_scoreboard(self.video, [dict(robot) for robot in robots], [dict(event) for event in events]) + + def _render(self) -> None: + video_id = int(self.video["id"]) + robots = list(self.db.robots_for_video(video_id)) + events = list(self.db.events_for_video(video_id)) + cycles = list(self.db.cycles_for_video(video_id)) + board = self.board(robots, events) + flagged = [event for event in events if event["status"] == "flagged"] + teams = {alliance: [str(robot["team_number"]) for robot in robots if robot["alliance"] == alliance] + for alliance in ("red", "blue")} + parts = [Path(self.video["path"]).name, self.video["event_code"] or "Practice", self.video["match_number"] or ""] + self.header.setText(" · ".join(part for part in parts if part) + + f" Red {' + '.join(teams['red']) or '—'} vs Blue {' + '.join(teams['blue']) or '—'}") + if flagged: + self.review_badge.setText(f"⚑ Not reviewed yet — {len(flagged)} event(s) need review; score is not final") + theme.set_tone(self.review_badge, "pending") + else: + self.review_badge.setText("✓ Reviewed — no events need review") + theme.set_tone(self.review_badge, "success") + confirmed = [event for event in events if event["status"] == "confirmed"] + for alliance, card in self.cards.items(): + ours = int(board["alliance_points"][alliance]) + official = self.video[f"official_{alliance}"] + card["ours"].setText(str(ours)) + card["official"].blockSignals(True) + card["official"].setValue(-1 if official is None else int(official)) + card["official"].blockSignals(False) + text, tone = official_check(ours, None if official is None else int(official)) + card["check"].setText(text) + theme.set_tone(card["check"], tone) + tips = board["ranking_points"][alliance].get("hive_tips", 0) if "ranking_points" in board else 0 + counted = sum(1 for event in confirmed if event_alliance(event) == alliance) + card["detail"].setText(f"{counted} confirmed scoring event(s) · Hive tips {tips}") + self._render_robots(robots, board, cycles, flagged) + self.accuracy.setText("Accuracy: within ±1 cycle of a hand count — Not yet checked (no hand count is stored " + "for this match). Ours vs official is shown on each alliance card.") + + def _render_robots(self, robots, board, cycles, flagged) -> None: + while self.robot_row.count(): + item = self.robot_row.takeAt(0) + widget = item.widget() + if widget is not None: + # Detach now: deleteLater alone leaves the old widget painted until the event loop runs. + widget.hide() + widget.setParent(None) + widget.deleteLater() + points = board.get("team_points", {}) + for robot in robots: + robot_id = int(robot["id"]) + card = widgets.card() + card.setProperty("alliance", robot["alliance"]) + absent = bool(robot["absent"]) if "absent" in robot.keys() else False + if absent: + card.setProperty("state", "absent") + layout = QVBoxLayout(card) + layout.setContentsMargins(12, 10, 12, 10) + layout.addWidget(widgets.label(f"{robot['team_number']} · {robot['alliance'].title()}", role="section")) + if absent: + layout.addWidget(widgets.label("Absent · 0 points", role="muted")) + else: + own = [cycle for cycle in cycles if int(cycle["robot_id"]) == robot_id] + review = sum(1 for event in flagged if event["robot_id"] is not None and int(event["robot_id"]) == robot_id) + layout.addWidget(widgets.label(f"{int(points.get(robot_id, 0))} points attributed")) + layout.addWidget(widgets.label(f"{len(own)} cycle(s)", role="muted")) + if review: + layout.addWidget(widgets.label(f"⚑ {review} need review", tone="pending")) + self.robot_row.addWidget(card, 1) + self.robot_row.addStretch(0) + + # ----- official score ----- + + def save_official(self) -> None: + if self.video is None: + return + values = {alliance: card["official"].value() for alliance, card in self.cards.items()} + self.db.set_official_scores( + int(self.video["id"]), None if values["red"] < 0 else values["red"], None if values["blue"] < 0 else values["blue"], + swarm_threshold=int(self.video["swarm_threshold"] or 16), + pollinator_1_threshold=int(self.video["pollinator_1_threshold"] or 4), + pollinator_2_threshold=int(self.video["pollinator_2_threshold"] or 7)) + self.show_video(int(self.video["id"])) + self.official_saved.emit(int(self.video["id"])) diff --git a/control_vision/app/runlog.py b/control_vision/app/runlog.py new file mode 100644 index 0000000..b1124eb --- /dev/null +++ b/control_vision/app/runlog.py @@ -0,0 +1,106 @@ +"""What a processing run reports while it runs, read from its progress and its log. + +The pipeline hands the app no boxes or positions until a run finishes. While it runs, +it reports progress once per sampled frame and writes a log line per sampled frame +(time and number of robot detections), plus a line whenever a robot's identity is +recovered or re-seeded. These helpers read exactly that, and nothing is guessed. +""" +from __future__ import annotations + +import logging +import re + +from PySide6.QtCore import QObject, Signal + +from control_vision.game.scoreboard import classify_match_time + +# The pipeline writes counts with thousands separators ("Processed 1,234 of 5,000 sampled frames"). +_OF_TOTAL = re.compile(r"Processed (\d[\d,]*) of (\d[\d,]*) sampled frames") +_DONE_ONLY = re.compile(r"Processed (\d[\d,]*) sampled frames") +_SAMPLED = re.compile(r"Sampled frame (\d+) \(([\d.]+)s\): (\d+) robot detections") +_TIME_AT = re.compile(r"at ([\d.]+) ?s\b") +ID_SWITCH_PREFIXES = ("Recovered robot", "Re-seeded robot") + + +class RunLogRelay(QObject): + """Carries log records from the worker thread to the UI thread.""" + + line = Signal(str, str) # level name, message + + +class RunLogHandler(logging.Handler): + def __init__(self, relay: RunLogRelay): + super().__init__(logging.INFO) + self.relay = relay + + def emit(self, record: logging.LogRecord) -> None: + try: + self.relay.line.emit(record.levelname, record.getMessage()) + except Exception: # Never let the UI break a run. + self.handleError(record) + + +def _count(text: str) -> int: + return int(text.replace(",", "")) + + +def frames_progress(percent: float, detail: str) -> tuple[int, int] | None: + """Sampled frames done and in total, from a progress report; None when it does not say.""" + match = _OF_TOTAL.search(detail or "") + if match: + return _count(match.group(1)), _count(match.group(2)) + match = _DONE_ONLY.search(detail or "") + if match and percent > 0: + done = _count(match.group(1)) + return done, max(done, round(done * 100 / percent)) + return None + + +def progress_time(detail: str) -> float | None: + """The video time a progress report names ("… at 12.5 s"), if any.""" + match = _TIME_AT.search(detail or "") + return float(match.group(1)) if match else None + + +def sampled_frame(message: str) -> tuple[int, float, int] | None: + """(frame, video time in s, robot detections) from a per-frame log line.""" + match = _SAMPLED.search(message or "") + return (int(match.group(1)), float(match.group(2)), int(match.group(3))) if match else None + + +def is_id_switch(message: str) -> bool: + """A robot was given a new track: recovered by re-linking, or re-seeded.""" + return (message or "").startswith(ID_SWITCH_PREFIXES) or "reacquired" in (message or "") + + +def time_left(elapsed_s: float, percent: float) -> float | None: + """Seconds left at the current rate; None until there is enough to go on.""" + if percent <= 1 or elapsed_s <= 0: + return None + return max(0.0, elapsed_s * (100 - percent) / percent) + + +class PhaseTracker: + """Match phase from video time, assuming the video starts when the match starts. + + Reports each change, and flags a jump that skips TRANSITION, which was a real bug once. + """ + + ORDER = ("AUTO", "TRANSITION", "TELEOP", "POST-MATCH") + + def __init__(self, periods: dict): + self.periods = periods + self.phase: str | None = None + + def update(self, t_s: float) -> tuple[str, float, str | None, bool]: + """(phase, clock left in it, change message or None, whether the change skipped a phase).""" + phase, remaining = classify_match_time(t_s, self.periods) + message, skipped = None, False + if self.phase is not None and phase != self.phase: + message = f"Phase changed: {self.phase} → {phase}" + skipped = (self.ORDER.index(phase) - self.ORDER.index(self.phase)) > 1 + if skipped: + missing = self.ORDER[self.ORDER.index(self.phase) + 1] + message += f" — {missing} missing" + self.phase = phase + return phase, remaining, message, skipped diff --git a/control_vision/storage/database.py b/control_vision/storage/database.py index 64b3b3f..c120f99 100644 --- a/control_vision/storage/database.py +++ b/control_vision/storage/database.py @@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS clips (id INTEGER PRIMARY KEY, scoring_event_id INTEGER REFERENCES scoring_events(id) ON DELETE CASCADE, path TEXT NOT NULL, start_t_s REAL NOT NULL, end_t_s REAL NOT NULL); CREATE TABLE IF NOT EXISTS photos (id INTEGER PRIMARY KEY, robot_id INTEGER NOT NULL REFERENCES robots(id) ON DELETE CASCADE, path TEXT NOT NULL, t_s REAL NOT NULL); CREATE TABLE IF NOT EXISTS robot_reseeds (id INTEGER PRIMARY KEY, robot_id INTEGER NOT NULL REFERENCES robots(id) ON DELETE CASCADE, t_s REAL NOT NULL, x_px REAL NOT NULL, y_px REAL NOT NULL); +CREATE TABLE IF NOT EXISTS processing_jobs (id INTEGER PRIMARY KEY, video_id INTEGER NOT NULL REFERENCES videos(id) ON DELETE CASCADE, position INTEGER NOT NULL, sample_fps INTEGER NOT NULL DEFAULT 8, tracking_mode TEXT NOT NULL, model_path TEXT, status TEXT NOT NULL DEFAULT 'queued', error TEXT, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP); """ _UNCHANGED: Any = object() @@ -358,6 +359,87 @@ def save_identity(self, video_id: int, entries: list[dict[str, Any]], mark_unpro self.connection.execute("UPDATE videos SET analysis_status='unprocessed' WHERE id=?", (video_id,)) return ids + # ----- processing queue ----- + + JOB_STATUSES = ("queued", "running", "done", "failed", "cancelled") + + def queue_jobs(self, video_ids, sample_fps: int, tracking_mode: str, model_path: str | None) -> list[int]: + """Queue videos for processing with the settings each job will use; a video already waiting is skipped.""" + if not 5 <= int(sample_fps) <= 10: + raise ValueError("The sample rate must be 5 to 10 frames per second.") + if tracking_mode not in ("yolo", "seeded_optical_flow"): + raise ValueError(f"Unknown tracking mode: {tracking_mode}") + waiting = {int(row[0]) for row in self.connection.execute( + "SELECT video_id FROM processing_jobs WHERE status IN ('queued','running')")} + ids = [] + with self.connection: + position = self.connection.execute("SELECT COALESCE(MAX(position), 0) FROM processing_jobs").fetchone()[0] + for video_id in video_ids: + if int(video_id) in waiting: + continue + waiting.add(int(video_id)) + position += 1 + cursor = self.connection.execute( + "INSERT INTO processing_jobs(video_id,position,sample_fps,tracking_mode,model_path) VALUES (?,?,?,?,?)", + (int(video_id), position, int(sample_fps), tracking_mode, model_path or None)) + ids.append(_inserted_id(cursor)) + return ids + + def processing_jobs(self): + return self.connection.execute( + "SELECT j.*, v.path, v.event_code, v.match_number, v.duration_s FROM processing_jobs j " + "JOIN videos v ON v.id=j.video_id ORDER BY j.position, j.id").fetchall() + + def next_queued_job(self): + return self.connection.execute( + "SELECT j.*, v.path FROM processing_jobs j JOIN videos v ON v.id=j.video_id " + "WHERE j.status='queued' ORDER BY j.position, j.id LIMIT 1").fetchone() + + def set_job_status(self, job_id: int, status: str, error: str | None = None) -> None: + if status not in self.JOB_STATUSES: + raise ValueError(f"Unknown job status: {status}") + with self.connection: + self.connection.execute("UPDATE processing_jobs SET status=?, error=? WHERE id=?", (status, error, int(job_id))) + + def move_job(self, job_id: int, offset: int) -> bool: + """Swap a job with its neighbour in the queue order; False at either end.""" + rows = [int(row[0]) for row in self.connection.execute("SELECT id FROM processing_jobs ORDER BY position, id")] + if int(job_id) not in rows: + return False + index = rows.index(int(job_id)) + target = index + int(offset) + if not 0 <= target < len(rows): + return False + rows[index], rows[target] = rows[target], rows[index] + with self.connection: + for position, row_id in enumerate(rows, start=1): + self.connection.execute("UPDATE processing_jobs SET position=? WHERE id=?", (position, row_id)) + return True + + def remove_jobs(self, job_ids) -> None: + with self.connection: + for job_id in job_ids: + self.connection.execute("DELETE FROM processing_jobs WHERE id=? AND status!='running'", (int(job_id),)) + + def clear_finished_jobs(self) -> int: + with self.connection: + return self.connection.execute( + "DELETE FROM processing_jobs WHERE status IN ('done','cancelled')").rowcount + + def retry_failed_jobs(self) -> int: + """Queue failed and cancelled jobs again, at most one per video, and none for a video already waiting.""" + with self.connection: + return self.connection.execute( + "UPDATE processing_jobs SET status='queued', error=NULL WHERE id IN (" + " SELECT MAX(id) FROM processing_jobs WHERE status IN ('failed','cancelled') AND video_id NOT IN" + " (SELECT video_id FROM processing_jobs WHERE status IN ('queued','running')) GROUP BY video_id)").rowcount + + def recover_interrupted_jobs(self) -> int: + """A job left running when the app closed saved nothing; queue it again. Returns how many jobs wait.""" + with self.connection: + self.connection.execute("UPDATE processing_jobs SET status='queued' WHERE status='running'") + return self.connection.execute("SELECT COUNT(*) FROM processing_jobs WHERE status='queued'").fetchone()[0] + def tracks_for_video(self, video_id: int): return self.connection.execute( "SELECT t.robot_id,t.frame,t.t_s,t.x_ft,t.y_ft,t.conf FROM tracks t JOIN robots r ON r.id=t.robot_id WHERE r.video_id=? ORDER BY t.frame,r.id", diff --git a/docs/USER_GUIDE.md b/docs/USER_GUIDE.md index 1d051b1..7f693e5 100644 --- a/docs/USER_GUIDE.md +++ b/docs/USER_GUIDE.md @@ -8,7 +8,7 @@ Use Python 3.11 or newer, install the pinned packages in `requirements.txt`, and The app opens on Home, which shows today's counts, your recent projects as cards, and buttons for a new project, importing a video and resuming the last project. Projects lists every project the app knows about, with search, filters (season, event, team, status), a table or grid view, and Open, Duplicate, Archive, Export project .db, Locate file and Delete. Delete asks first and moves the project file to the Recycle Bin; match videos are never deleted. Duplicate copies the videos and calibration but not the results. A project whose file has moved shows as missing until you locate it. -The sidebar lists the screens in order: Home, Projects and Current project; under Match setup, Import videos, Livestream splitter, Calibrate field and Robots; then Set up and process, Review queue, Scoring timeline, Cycles and stats, Highlights and Score and export, then Datasets and Models (not available in this build yet) and the BIOBUZZ rules. Ctrl+1 to Ctrl+9 open the first nine in that order (Ctrl+8 is Set up and process, Ctrl+9 the Review queue). Ctrl+N starts a new project, Ctrl+O opens a project file, Ctrl+, opens Settings, and ? or F1 lists the shortcuts. The top bar shows the open project, its event, its model and "Local only" (syncing with Scout is not available in this build), with a project search and the Settings, Rules and ? buttons. Result screens stay unavailable until the current video has been processed successfully. Set up and process shows the five setup steps (match video, field calibration, zones and ROIs, robot identities, process) with the state of each; zones and ROIs are optional for processing. While a video is processed, a progress bar shows in the status bar. +The sidebar lists the screens in order: Home, Projects and Current project; under Match setup, Import videos, Livestream splitter, Calibrate field and Robots; then Set up and process, Processing queue, Processing live, Match results, Field map, Review queue, Scoring timeline, Cycles and stats, Highlights and Score and export, then Datasets and Models (not available in this build yet) and the BIOBUZZ rules. Ctrl+1 to Ctrl+9 open the first nine in that order (Ctrl+8 is Set up and process, Ctrl+9 the Processing queue). Ctrl+N starts a new project, Ctrl+O opens a project file, Ctrl+, opens Settings, and ? or F1 lists the shortcuts. The top bar shows the open project, its event, its model and "Local only" (syncing with Scout is not available in this build), with a project search and the Settings, Rules and ? buttons. Result screens stay unavailable until the current video has been processed successfully. Set up and process shows the five setup steps (match video, field calibration, zones and ROIs, robot identities, process) with the state of each; zones and ROIs are optional for processing. While a video is processed, a progress bar shows in the status bar. Anything you still need to check is shown in orange: flagged events and low-confidence values in the Review queue and Scoring timeline, which also carry a ⚑ flag, a pending referee ruling, and an OCR scoreboard proposal. Where a screen has one main action, such as Process match, Confirm or Export Scout JSON, it is shown in yellow. The Review queue entry in the sidebar shows how many events need review. The app starts in the dark theme; choose Light, Dark or Match Windows under Settings › Appearance; this choice is saved for the computer, not with the project. @@ -24,6 +24,16 @@ Robots: select a robot card and click that robot on the frame where it touches t Later corrections: when a processed match shows a robot lost or mixed up, open Robots, choose Later corrections, select that robot's card, move the slider to the moment and click where the robot touches the floor. The correction is saved at once and listed under the cards; double-click one to go to it, or select it and Delete correction. Corrections apply the next time the match is processed: the robot is put on the detected robot box that contains the click (or the nearest one within 1 second), and its earlier positions on that box since it was last seen are kept. Only robots saved with a start click can be corrected. +## Processing and results screens + +Processing queue runs matches one after another in the background. Add matches lists the project's matches that are not processed or queued yet; each job keeps the sample rate (5 to 10 frames per second), tracking method and model chosen when it was added, so changing Settings later does not change queued jobs. Start queue runs them in order with the same processing as Set up and process; Pause all lets the running match finish and does not start the next. While another run is in progress, Start queue pauses the queue and says so; a job that raises an error while starting fails with the reason and pauses the queue. Move up and Move down reorder waiting jobs, Cancel (or Stop processing) stops the running one after the current frame and pauses the queue (nothing from a cancelled run is saved), and Retry failed queues failed and cancelled jobs again. A job that cannot run fails with the reason and is not retried on its own: a missing calibration or robot marker opens the page that fixes it. The running job shows its progress and an estimate of the time left. Jobs are stored in the project: a job that was running when the app closed is queued again, and the queue asks before resuming. When available, the CPU load is shown, with a note when a YOLO training run is sharing the computer. + +Processing live shows what a run reports while it runs: sampled frames done and in total, frames per second, the video time and match phase, robot detections in the latest sampled frame, identity switches (a robot re-linked to a new track or re-seeded) and a log of warnings and phase changes. The phase comes from the video time and assumes the video starts when the match starts; a jump that skips TRANSITION is shown in red. The picture is the video at the time being processed, without boxes: robot boxes and IDs are not available while a run is in progress in this build. Stop processing asks first. + +Match results shows one processed match: the teams, the Red and Blue totals counted from confirmed events (scored like Score and export), and the official score next to them, never merged. Enter the official score there or in Score and export; each alliance card then says whether our total is within 10% of the official one. The score is not final while any event still needs review. Each robot shows its attributed points and cycles; an absent robot shows as Absent with 0 points. A hand-count check is shown as not yet checked, because no hand count is stored. End-of-match points come from the results screen, not the live video, so a total read from the video can trail the official one. + +Field map shows the trails of a processed match on the calibrated field, in feet from the first corner clicked when calibrating. A break of more than one second between positions is drawn as a gap, not a straight line. Layers turn trails, a heatmap of where robots spent time, zones and the field picture on or off; each robot can be hidden, and its driven distance and number of gaps are listed. Scrub or Play to follow the match, zoom with the wheel and drag to pan, and click a point on a trail to see the video at that moment. Ball positions are not stored by this build. A side view or a seeded-preview run is flagged because its trails are less accurate. + ## Analyze a practice video or match 1. Create a project with New project: name it and its event (the event code is optional), add match videos, which stay where they are, and choose the detector model. Nothing is saved until Create project. Or reopen a project's `control_vision.sqlite` file. diff --git a/tests/test_modern_ui.py b/tests/test_modern_ui.py index 6f3b8e8..e653a03 100644 --- a/tests/test_modern_ui.py +++ b/tests/test_modern_ui.py @@ -157,7 +157,7 @@ def test_sidebar_tooltips_follow_availability(self): window._sync_nav() self.assertIn("2 event(s) need review", window.nav.item(window._nav_rows[3]).toolTip()) window._set_review_badge(0) - self.assertEqual(window.nav.item(window._nav_rows[3]).toolTip(), "Review queue (Ctrl+9)") + self.assertEqual(window.nav.item(window._nav_rows[3]).toolTip(), "Review queue") # Ctrl+9 is now the Processing queue. def test_minimum_window_width_fits_every_panel(self): # Fixed and minimum widths only, so the check does not depend on installed fonts. diff --git a/tests/test_processing_pages.py b/tests/test_processing_pages.py new file mode 100644 index 0000000..3914bd2 --- /dev/null +++ b/tests/test_processing_pages.py @@ -0,0 +1,428 @@ +"""UI phase 3: processing queue, live view, match results and field map.""" +import logging +import os +import unittest +from pathlib import Path +from unittest.mock import patch + +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") + +try: + from test_setup_pages import CORNERS, SetupTest, qt_app, select_row +except ImportError: # run as tests.test_processing_pages + from tests.test_setup_pages import CORNERS, SetupTest, qt_app, select_row + +PERIODS = {"auto": 30, "transition": 8, "teleop": 120} + + +class ReadyMatch(SetupTest): + """A calibrated match with four robots (one absent) and stored positions.""" + + def ready_match(self, name="Q1.mp4", processed=True): + video, path = self.add_video(name) + self.db.save_calibration(video, CORNERS, {"Hive": {"type": "scoring", "points": [[5, 5], [7, 5], [7, 7], [5, 7]]}}) + ids = self.db.save_identity(video, [ + {"team_number": 21229, "alliance": "red", "seed_x": 40, "seed_y": 60}, + {"team_number": 27393, "alliance": "red", "seed_x": 80, "seed_y": 60}, + {"team_number": 16655, "alliance": "blue", "seed_x": 120, "seed_y": 60}, + {"team_number": 35817, "alliance": "blue", "absent": True}]) + rows = [(ids[0], frame, frame / 8, 1 + frame * 0.1, 2.0, 0.9) for frame in range(0, 16)] + rows += [(ids[0], frame, frame / 8, 6.0, 6.0, 0.9) for frame in range(40, 48)] # after a 3 s gap + rows += [(ids[2], frame, frame / 8, 10.0, 1 + frame * 0.05, 0.9) for frame in range(0, 20)] + self.db.replace_tracks(rows, ids) + if processed: + self.db.set_analysis_status(video, "ready") + return video, ids + + +class JobStorageTests(SetupTest): + def test_jobs_keep_their_settings_order_and_survive_a_restart(self): + first, _ = self.add_video("Q1.mp4") + second, _ = self.add_video("Q2.mp4") + ids = self.db.queue_jobs([first, second, first], 6, "yolo", "models/a.onnx") + self.assertEqual(len(ids), 2) # A video waits once. + self.assertEqual(self.db.queue_jobs([first], 8, "yolo", None), []) + with self.assertRaises(ValueError): + self.db.queue_jobs([second], 12, "yolo", None) + self.assertTrue(self.db.move_job(ids[1], -1)) + self.assertFalse(self.db.move_job(ids[1], -1)) # Already first. + self.assertEqual([int(job["id"]) for job in self.db.processing_jobs()], [ids[1], ids[0]]) + self.assertEqual(self.db.next_queued_job()["sample_fps"], 6) + self.db.set_job_status(ids[1], "running") + self.db.set_job_status(ids[0], "done") + self.assertEqual(self.db.recover_interrupted_jobs(), 1) # The app closed mid-run. + self.assertEqual(self.db.next_queued_job()["id"], ids[1]) + self.db.set_job_status(ids[1], "failed", "Calibration missing — calibrate this match first.") + self.assertEqual(self.db.clear_finished_jobs(), 1) + self.assertEqual(self.db.retry_failed_jobs(), 1) + self.assertEqual(self.db.processing_jobs()[0]["error"], None) + # A match retried from the live page already waits: Retry failed does not queue it a second time. + self.db.set_job_status(ids[1], "failed", "Stopped") + [again] = self.db.queue_jobs([second], 6, "yolo", None) + self.assertEqual(self.db.retry_failed_jobs(), 0) + # A match that failed twice is queued once. + self.db.set_job_status(again, "cancelled") + self.assertEqual(self.db.retry_failed_jobs(), 1) + waiting = [job for job in self.db.processing_jobs() if job["status"] == "queued"] + self.assertEqual([int(job["id"]) for job in waiting], [again]) + + +class RunLogTests(unittest.TestCase): + def test_progress_and_log_lines_are_read_not_guessed(self): + from control_vision.app.runlog import frames_progress, is_id_switch, progress_time, sampled_frame, time_left + self.assertEqual(frames_progress(10, "Processed 12 of 120 sampled frames at 1.5 s"), (12, 120)) + self.assertEqual(frames_progress(25, "Processed 30 sampled frames · elapsed 4s"), (30, 120)) + self.assertIsNone(frames_progress(0, "Loading model")) + # The pipeline formats counts with thousands separators. + self.assertEqual(frames_progress(25, "Processed 1,234 of 5,000 sampled frames at 205.6s"), (1234, 5000)) + self.assertEqual(frames_progress(50, "Processed 1,500 sampled frames"), (1500, 3000)) + self.assertEqual(progress_time("Processed 12 of 120 sampled frames at 1.5 s"), 1.5) + self.assertEqual(sampled_frame("Sampled frame 48 (1.600s): 3 robot detections; 0 scoring elements in ROI"), + (48, 1.6, 3)) + self.assertTrue(is_id_switch("Recovered robot 2 at 41.20s: track 7 -> new track 11 after 1.10s missing")) + self.assertTrue(is_id_switch("Re-seeded robot 3 at 2.0s")) + self.assertFalse(is_id_switch("Robot 2 not re-linked: two candidates")) + self.assertIsNone(time_left(5, 0.5)) + self.assertEqual(time_left(60, 25), 180) + + def test_phase_changes_are_reported_and_a_skipped_transition_is_flagged(self): + from control_vision.app.runlog import PhaseTracker + phases = PhaseTracker(PERIODS) + self.assertEqual(phases.update(10)[0], "AUTO") + phase, _left, change, skipped = phases.update(31) + self.assertEqual((phase, change, skipped), ("TRANSITION", "Phase changed: AUTO → TRANSITION", False)) + jump = PhaseTracker(PERIODS) + jump.update(5) + phase, _left, change, skipped = jump.update(50) + self.assertEqual(phase, "TELEOP") + self.assertTrue(skipped) + self.assertIn("TRANSITION missing", change) + + +class QueuePageTests(SetupTest): + def page(self): + from control_vision.app.pages.queue_page import QueuePage + page = QueuePage(lambda: (8, "yolo", str(self.root / "model.onnx"))) + page.set_project(self.db, self.project) + return page + + def test_queue_rows_actions_and_fixes(self): + page = self.page() + self.assertFalse(page.empty.isHidden()) + video, _ = self.add_video() + [job] = page.queue([video], 6) + self.assertEqual(page.table.rowCount(), 1) + self.assertEqual((page.table.item(0, 3).text(), page.table.item(0, 4).text(), page.table.item(0, 5).text()), + ("model.onnx", "6", "Queued")) + self.assertTrue(page.start_btn.isEnabled()) + self.db.set_job_status(job, "failed", "Calibration missing — calibrate this match first.") + page.refresh() + select_row(page.table, 0) + self.assertEqual(page.open_btn.text(), "Calibrate…") + self.assertIn("Calibration missing", page.error.text()) + with patch.object(page, "open_calibrate") as fix: + page.open_selected() + fix.emit.assert_called_once_with(video) + page.retry_failed() + self.assertEqual(page.table.item(0, 5).text(), "Queued") + + def test_running_job_shows_progress_and_resume_prompt(self): + page = self.page() + video, _ = self.add_video() + [job] = page.queue([video]) + self.db.set_job_status(job, "running") + page.set_runner_state(job, False) + page.set_progress(job, 40, 90) + self.assertEqual(page.table.cellWidget(0, 6).value(), 40) + self.assertEqual(page.table.item(0, 7).text(), "01:30") + select_row(page.table, 0) + self.assertEqual(page.open_btn.text(), "Open live view") + self.assertFalse(page.remove_btn.isEnabled()) + with patch.object(page, "cancel_requested") as cancel: + page._cancel_selected() + cancel.emit.assert_called_once_with(job) + # Cancelled at 40% and retried: the job runs again from 0%, not from its old progress. + self.db.set_job_status(job, "cancelled") + page.set_runner_state(None, True) + self.db.set_job_status(job, "running") + page.set_runner_state(job, False) + self.assertEqual(page.table.cellWidget(0, 6).value(), 0) + self.assertNotEqual(page.table.item(0, 7).text(), "01:30") + page.show_resume_prompt(3) + self.assertIn("3 job(s) were queued when the app closed", page.banner.text()) + + +class LivePageTests(unittest.TestCase): + def setUp(self): + self.app = qt_app() + + def test_live_view_reads_the_run(self): + from control_vision.app.pages.live_page import PREVIEW_NOTE, LivePage + clock = iter([0.0, 10.0, 10.0, 20.0, 20.0, 30.0]) + page = LivePage(PERIODS, clock=lambda: next(clock, 30.0)) + self.assertFalse(page.idle.isHidden()) + page.start_run(4, Path("missing.mp4"), 8) + page.set_progress("Detect and track", 10, "Processed 24 of 240 sampled frames at 3.0 s") + self.assertEqual(page.values["frames"].text(), "24 / 240") + self.assertTrue(page.values["phase"].text().startswith("AUTO")) + page.add_line("INFO", "Sampled frame 300 (31.000s): 3 robot detections; 0 scoring elements in ROI") + self.assertEqual(page.values["detections"].text(), "3") + self.assertTrue(page.values["phase"].text().startswith("TRANSITION")) + page.add_line("INFO", "Recovered robot 2 at 31.20s: track 7 -> new track 11 after 1.10s missing") + self.assertEqual(page.values["switches"].text(), "1") + log = [page.log.item(i).text() for i in range(page.log.count())] + self.assertIn("Phase changed: AUTO → TRANSITION", log) + self.assertTrue(any(line.startswith("Recovered robot 2") for line in log)) + self.assertIn("without boxes", PREVIEW_NOTE) + page.failed("Processing was cancelled.") + self.assertEqual(page.state, "cancelled") + self.assertFalse(page.retry_btn.isHidden()) + + def test_stop_asks_first(self): + from control_vision.app.pages import live_page + page = live_page.LivePage(PERIODS) + page.start_run(4, Path("missing.mp4"), 8) + with patch.object(live_page.QMessageBox, "question", return_value=live_page.QMessageBox.StandardButton.No), \ + patch.object(page, "stop_requested") as stop: + page.confirm_stop() + stop.emit.assert_not_called() + with patch.object(live_page.QMessageBox, "question", return_value=live_page.QMessageBox.StandardButton.Yes), \ + patch.object(page, "stop_requested") as stop: + page.confirm_stop() + stop.emit.assert_called_once() + + +class ResultsTests(ReadyMatch): + def test_ours_and_official_side_by_side(self): + from control_vision.app.pages.results_page import ResultsPage, official_check + self.assertEqual(official_check(10, None), ("Official score not entered yet", "pending")) + self.assertEqual(official_check(52, 50)[1], "success") + self.assertIn("Differs from official by 20%", official_check(40, 50)[0]) + video, ids = self.ready_match() + for robot, points, status in ((ids[0], 10, "confirmed"), (ids[2], 6, "confirmed"), (ids[1], 4, "flagged")): + self.db.connection.execute( + "INSERT INTO scoring_events(robot_id,video_id,t_s,points,status) VALUES (?,?,?,?,?)", + (robot, video, 12.0, points, status)) + self.db.connection.commit() + page = ResultsPage() + page.set_project(self.db, self.project) + self.assertEqual(page.cards["red"]["ours"].text(), "10") # The flagged event is not counted. + self.assertEqual(page.cards["blue"]["ours"].text(), "6") + self.assertIn("Not reviewed yet", page.review_badge.text()) + self.assertEqual(page.cards["red"]["check"].text(), "Official score not entered yet") + robot_texts = [page.robot_row.itemAt(i).widget().findChildren(type(page.header)) for i in range(4)] + self.assertIn("Absent · 0 points", [label.text() for label in robot_texts[3]]) + page.cards["red"]["official"].setValue(10) + page.cards["blue"]["official"].setValue(30) + page.save_official() + row = self.db.connection.execute("SELECT official_red,official_blue FROM videos WHERE id=?", (video,)).fetchone() + self.assertEqual(tuple(row), (10, 30)) + self.assertIn("Within ±10%", page.cards["red"]["check"].text()) + self.assertIn("Differs from official by 80%", page.cards["blue"]["check"].text()) + + def test_unprocessed_match_says_so(self): + from control_vision.app.pages.results_page import ResultsPage + self.ready_match(processed=False) + page = ResultsPage() + page.set_project(self.db, self.project) + self.assertFalse(page.not_processed.isHidden()) + self.assertTrue(page.results.isHidden()) + + +class FieldMapTests(ReadyMatch): + def test_gaps_stay_gaps(self): + from control_vision.app.pages.fieldmap_page import RobotTrail, heat_counts + trail = RobotTrail(1, "21229", "red", samples=[(0.0, 1, 1), (0.5, 2, 1), (4.0, 6, 6), (4.5, 6, 7)]) + self.assertEqual(len(trail.segments()), 2) # 3.5 s apart: a gap, not a line. + self.assertEqual(trail.gaps(), 1) + self.assertAlmostEqual(trail.distance_ft(), 2.0) # The jump across the gap is not driven distance. + self.assertEqual(len(trail.segments(until_s=1.0)), 1) + self.assertEqual(sum(map(sum, heat_counts([trail]))), 4) + + def test_page_states_and_click_to_seek(self): + from control_vision.app.pages.fieldmap_page import FieldMapPage + page = FieldMapPage() + uncalibrated, _ = self.add_video("Q0.mp4") + page.set_project(self.db, self.project) + page.refresh_videos(select=uncalibrated) + self.assertEqual(page.empty.title.text(), "No calibration — calibrate first") + video, ids = self.ready_match() + damaged, _ = self.ready_match("Q5.mp4") + self.db.connection.execute("UPDATE calibrations SET corners_json='[[0, 0], [1' WHERE video_id=?", (damaged,)) + page.refresh_videos(select=video) + page.refresh_videos(select=damaged) + self.assertEqual(page.empty.title.text(), "Calibration unreadable — calibrate again") + self.assertTrue(page.content.isHidden()) # The other match's trails are not left showing. + with patch.object(page, "open_calibrate") as calibrate: + page._empty_action() + calibrate.emit.assert_called_once_with(damaged) + page.refresh_videos(select=video) + self.assertTrue(page.empty.isHidden()) + legend = [page.legend.itemAt(i).widget().text() for i in range(page.legend.count())] + self.assertTrue(legend[0].startswith("21229 · Red")) + self.assertIn("1 gap(s)", legend[0]) + self.assertIn("no positions", legend[3]) + self.assertFalse(page.balls.isEnabled()) + page.resize(1000, 700) + page.map.resize(600, 600) + target = page.map.to_widget(6.0, 6.0) + self.assertEqual(page.map.nearest_point(target), (ids[0], 5.0)) + page._point_clicked(ids[0], 5.0) + self.assertEqual(page.scrubber.value(), 50) + self.assertIn("team 21229", page.time_label.text()) + not_processed, _ = self.ready_match("Q2.mp4", processed=False) + page.refresh_videos(select=not_processed) + self.assertEqual(page.empty.title.text(), "Processing not finished") + + +class WindowQueueTests(ReadyMatch): + def setUp(self): + super().setUp() + from control_vision.app import library + from control_vision.app.main_window import MainWindow + self.window = MainWindow() + self.window.library = library.ProjectLibrary(self.root / "home") + self.window.model_store = library.ModelStore(self.root / "home") + + def tearDown(self): + self.window.calibrate_page.dirty = self.window.identity_page.dirty = False + self.window.db = None + self.window.close() + + def open(self): + self.window.db, self.window.project_id = self.db, self.project + self.window.refresh_home() + + def test_jobs_that_cannot_run_fail_with_the_fix_and_the_queue_moves_on(self): + from control_vision.app import main_window as mw + missing, _ = self.add_video("Q0.mp4") + video, _ = self.ready_match(processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + first, second = self.db.queue_jobs([missing, video], 6, "yolo", str(model)) + window = self.window + window.sample_rate.setCurrentText("8") + started = [] + with patch.object(window, "process", side_effect=lambda: (started.append(window.sample_rate.currentText()), + setattr(window, "_processing_active", True))): + window.start_queue() + self.app.processEvents() + jobs = {int(job["id"]): job for job in self.db.processing_jobs()} + self.assertEqual(jobs[first]["status"], "failed") + self.assertTrue(jobs[first]["error"].startswith("Calibration missing")) + self.assertEqual(jobs[second]["status"], "running") + self.assertEqual(started, ["6"]) # The job's own sample rate. + window.processing_failed("Processing was cancelled.") + self.app.processEvents() + self.assertEqual(self.db.processing_jobs()[1]["status"], "cancelled") + self.assertEqual(window.sample_rate.currentText(), "8") # The user's setting is back. + self.assertIsNone(window._queue_job) + self.assertEqual(window.live_page.state, "idle") # process() was replaced, so no run started here. + self.assertIn((mw.QUEUE_TAB, "Processing queue"), [(tab, label) for tab, label in mw.NAV_ENTRIES if tab != "section"]) + + def test_stopping_a_queued_run_pauses_the_queue(self): + first_video, _ = self.ready_match("Q1.mp4", processed=False) + second_video, _ = self.ready_match("Q2.mp4", processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + first, second = self.db.queue_jobs([first_video, second_video], 8, "yolo", str(model)) + window = self.window + started = [] + with patch.object(window, "process", side_effect=lambda: (started.append(window.video_id), + setattr(window, "_processing_active", True))): + window.start_queue() + self.app.processEvents() + window.processing_failed("Processing was cancelled.") + self.app.processEvents() + jobs = {int(job["id"]): job["status"] for job in self.db.processing_jobs()} + self.assertEqual(jobs, {first: "cancelled", second: "queued"}) + self.assertTrue(window._queue_paused) + self.assertEqual(len(started), 1) # The next match did not start. + window.start_queue() # Start queue carries on with the next one. + self.app.processEvents() + self.assertEqual(len(started), 2) + self.assertEqual(window._queue_job, second) + + def test_the_queue_pauses_and_says_so_while_another_run_is_active(self): + video, _ = self.ready_match(processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + [job] = self.db.queue_jobs([video], 8, "yolo", str(model)) + window = self.window + window._processing_active = True # A single-match run is going. + try: + with patch.object(window, "process") as process: + window.start_queue() + self.app.processEvents() + process.assert_not_called() + self.assertTrue(window._queue_paused) + self.assertIn("another run is in progress", window.progress_label.text()) + self.assertEqual(self.db.processing_jobs()[0]["status"], "queued") + finally: + window._processing_active = False + + def test_a_job_whose_start_raises_fails_and_pauses_the_queue(self): + first_video, _ = self.ready_match("Q1.mp4", processed=False) + second_video, _ = self.ready_match("Q2.mp4", processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + first, second = self.db.queue_jobs([first_video, second_video], 6, "yolo", str(model)) + window = self.window + window.sample_rate.setCurrentText("8") + with patch.object(window, "process", side_effect=OSError("run log is read-only")) as process: + window.start_queue() + self.app.processEvents() + self.assertEqual(process.call_count, 1) + jobs = {int(job["id"]): job for job in self.db.processing_jobs()} + self.assertEqual(jobs[first]["status"], "failed") + self.assertIn("run log is read-only", jobs[first]["error"]) + self.assertEqual(jobs[second]["status"], "queued") + self.assertIsNone(window._queue_job) + self.assertTrue(window._queue_paused) + self.assertEqual(window.sample_rate.currentText(), "8") # The user's setting is back. + self.assertIn("Processing could not start", window.progress_label.text()) + + def test_a_queue_start_that_fails_inside_process_undoes_the_run(self): + video, _ = self.ready_match(processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + [job] = self.db.queue_jobs([video], 8, "yolo", str(model)) + window = self.window + with patch.object(self.db, "reseeds_for_video", side_effect=RuntimeError("database is locked")): + window.start_queue() + self.app.processEvents() + self.assertEqual(self.db.processing_jobs()[0]["status"], "failed") + self.assertIn("database is locked", self.db.processing_jobs()[0]["error"]) + status = self.db.connection.execute("SELECT analysis_status FROM videos WHERE id=?", (video,)).fetchone()[0] + self.assertEqual(status, "failed") # Not left "running". + self.assertTrue(window.process_btn.isEnabled()) + self.assertFalse(window._processing_active) + self.assertIsNone(window._run_log_handler) + self.assertFalse([h for h in window.logger.handlers if isinstance(h, logging.FileHandler)]) + self.assertTrue(window._queue_paused) + self.assertIsNone(window._queue_job) + + def test_a_finished_job_is_marked_done(self): + video, ids = self.ready_match(processed=False) + self.open() + model = self.root / "model.onnx"; model.write_bytes(b"x") + [job] = self.db.queue_jobs([video], 8, "yolo", str(model)) + window = self.window + with patch.object(window, "process", side_effect=lambda: setattr(window, "_processing_active", True)): + window.start_queue() + with patch.object(window, "_store_processing_results"): + window.processing_done((([(ids[0], 0, 0.0, 1.0, 1.0, 0.9)], [], []), "yolo")) + self.app.processEvents() + self.assertEqual(self.db.processing_jobs()[0]["status"], "done") + + def test_opening_a_project_offers_to_resume(self): + video, _ = self.ready_match(processed=False) + [job] = self.db.queue_jobs([video], 8, "yolo", None) + self.db.set_job_status(job, "running") # The app closed mid-run. + self.open() + self.assertEqual(self.db.processing_jobs()[0]["status"], "queued") + self.assertIn("1 job(s) were queued", self.window.queue_page.banner.text()) + + +if __name__ == "__main__": + unittest.main()