feat(utils): add TkImageWindow to unblock switch to opencv-python-headless (#2320)

- Added `sv.ImageWindow`, a Tkinter/Pillow-based desktop image viewer with BGR, grayscale, and BGRA support, keyboard polling, left-click callbacks, context-manager usage, window-state checks, and clean close handling
- Added responsive image resizing with optional aspect-ratio preservation and correctly mapped mouse coordinates after scaling or letterboxing
- Updated compatible runnable examples to use `sv.ImageWindow`, while retaining OpenCV display APIs for worker-thread streaming examples that are incompatible with Tkinter
- Improved `sv.cv2_to_pillow` to support grayscale and BGRA images
- Updated webcam guidance to clarify capture ownership and explicit `VideoCapture` cleanup
- Fixed image-window event handling to prevent stale keypresses, ghost windows, close-time races, and blocked waits after the window closes

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Jirka Borovec
2026-07-15 15:06:55 +02:00
committed by GitHub
co-authored by claude[bot] Copilot Autofix powered by AI Codex pre-commit-ci[bot]
parent 94cfb7f290
commit 16814acff3
27 changed files with 1109 additions and 83 deletions
@@ -1,7 +1,6 @@
import os
from collections.abc import Iterable
import cv2
import numpy as np
from inference.models.utils import get_roboflow_model
from tqdm import tqdm
@@ -112,12 +111,14 @@ class VideoProcessor:
annotated_frame = self.process_frame(frame)
sink.write_frame(annotated_frame)
else:
window = sv.ImageWindow("Processed Video")
for frame in tqdm(frame_generator, total=self.video_info.total_frames):
annotated_frame = self.process_frame(frame)
cv2.imshow("Processed Video", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
window.show(annotated_frame)
key = window.wait_key(1)
if not window.is_open or key == "q":
break
cv2.destroyAllWindows()
window.close()
def annotate_frame(
self, frame: np.ndarray, detections: sv.Detections