A modern C++ toolkit containing machine learning algorithms and tools for creating complex software to solve real world problems
—
Interactive visualization and user interface components for image display, user interaction, and real-time visualization of computer vision results.
Interactive window for displaying images with overlay capabilities and user interaction support.
class image_window:
"""Interactive image display window with overlay support."""
def __init__(self, img=None, title: str = None):
"""
Create image display window.
Args:
img: Optional initial image to display
title: Optional window title
"""
def set_image(self, img):
"""
Display image in window.
Args:
img: Image to display (numpy array or dlib image)
"""
def set_title(self, title: str):
"""
Set window title.
Args:
title: Window title text
"""
def add_overlay(self, objects, color):
"""
Add visual overlays to the displayed image.
Args:
objects: Objects to overlay (rectangles, lines, circles, detections)
color: Overlay color (RGB tuple or color name)
"""
def add_overlay_circle(self, center: point, radius: int, color):
"""
Add circle overlay to image.
Args:
center: Circle center point
radius: Circle radius in pixels
color: Circle color
"""
def clear_overlay(self):
"""Remove all overlays from the image display."""
def wait_until_closed(self):
"""Block execution until the window is closed by user."""
def is_closed(self) -> bool:
"""
Check if window has been closed.
Returns:
True if window is closed, False otherwise
"""
def get_next_double_click(self) -> point:
"""
Wait for user to double-click in window.
Returns:
Point where user double-clicked
"""
def wait_for_keypress(self, key):
"""
Wait for specific key to be pressed.
Args:
key: Key to wait for (character or key code)
"""
def get_next_keypress(self, get_keyboard_modifiers: bool = False):
"""
Get next keypress from user.
Args:
get_keyboard_modifiers: Whether to return modifier key states
Returns:
Key pressed, optionally with modifier states
"""Enumerations and utilities for handling keyboard input and special keys.
class non_printable_keyboard_keys:
"""Enumeration of special keyboard keys."""
KEY_BACKSPACE: int
KEY_SHIFT: int
KEY_CTRL: int
KEY_ALT: int
KEY_PAUSE: int
KEY_CAPS_LOCK: int
KEY_ESC: int
KEY_PAGE_UP: int
KEY_PAGE_DOWN: int
KEY_END: int
KEY_HOME: int
KEY_LEFT: int
KEY_UP: int
KEY_RIGHT: int
KEY_DOWN: int
KEY_INSERT: int
KEY_DELETE: int
KEY_SCROLL_LOCK: int
KEY_F1: int
KEY_F2: int
KEY_F3: int
KEY_F4: int
KEY_F5: int
KEY_F6: int
KEY_F7: int
KEY_F8: int
KEY_F9: int
KEY_F10: int
KEY_F11: int
KEY_F12: int
class keyboard_mod_keys:
"""Keyboard modifier key states."""
KBD_MOD_NONE: int # No modifiers
KBD_MOD_SHIFT: int # Shift key pressed
KBD_MOD_CONTROL: int # Ctrl key pressed
KBD_MOD_ALT: int # Alt key pressed
KBD_MOD_META: int # Meta/Windows key pressed
KBD_MOD_CAPS_LOCK: int # Caps lock active
KBD_MOD_NUM_LOCK: int # Num lock active
KBD_MOD_SCROLL_LOCK: int # Scroll lock activeUsage Examples:
import dlib
import cv2
# Load and display image
img = cv2.imread("photo.jpg")
win = dlib.image_window(img, "My Photo")
# Keep window open
win.wait_until_closed()import dlib
import cv2
# Initialize face detector
detector = dlib.get_frontal_face_detector()
# Load image
img = cv2.imread("group_photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = detector(gray)
# Create display window
win = dlib.image_window(img, "Face Detection Results")
# Add face overlays
for face in faces:
win.add_overlay(face, "red")
# Add custom circle overlay
if len(faces) > 0:
center = dlib.center(faces[0])
win.add_overlay_circle(center, 50, "blue")
# Wait for user to close
win.wait_until_closed()import dlib
import cv2
# Load image
img = cv2.imread("image.jpg")
win = dlib.image_window(img, "Click points")
print("Double-click to select points. Press ESC to finish.")
points = []
while True:
# Get user input
key_pressed = win.get_next_keypress()
if key_pressed == dlib.non_printable_keyboard_keys.KEY_ESC:
break
# Check for double-click
if not win.is_closed():
try:
point = win.get_next_double_click()
points.append(point)
# Add circle at clicked point
win.add_overlay_circle(point, 5, "green")
print(f"Selected point: ({point.x}, {point.y})")
except:
pass
print(f"Selected {len(points)} points")
win.wait_until_closed()import dlib
import cv2
# Initialize detector and window
detector = dlib.get_frontal_face_detector()
win = dlib.image_window(title="Live Face Detection")
# Video capture
cap = cv2.VideoCapture(0)
try:
while True:
ret, frame = cap.read()
if not ret:
break
# Detect faces
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
# Display frame
win.set_image(frame)
win.clear_overlay()
# Add face overlays
for face in faces:
win.add_overlay(face, "red")
# Check if window closed
if win.is_closed():
break
# Check for ESC key
try:
key = win.get_next_keypress()
if key == dlib.non_printable_keyboard_keys.KEY_ESC:
break
except:
pass
finally:
cap.release()import dlib
import cv2
# Load image and initialize models
img = cv2.imread("face.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# Detect face and landmarks
faces = detector(gray)
win = dlib.image_window(img, "Face Analysis")
if len(faces) > 0:
face = faces[0]
landmarks = predictor(gray, face)
# Add face rectangle overlay
win.add_overlay(face, "red")
# Add landmark points
for i in range(landmarks.num_parts()):
point = landmarks.part(i)
win.add_overlay_circle(point, 2, "green")
# Add eye regions with different colors
left_eye_points = [landmarks.part(i) for i in range(36, 42)]
right_eye_points = [landmarks.part(i) for i in range(42, 48)]
# Create eye bounding rectangles
left_eye_rect = dlib.rectangle(
min(p.x for p in left_eye_points) - 10,
min(p.y for p in left_eye_points) - 10,
max(p.x for p in left_eye_points) + 10,
max(p.y for p in left_eye_points) + 10
)
right_eye_rect = dlib.rectangle(
min(p.x for p in right_eye_points) - 10,
min(p.y for p in right_eye_points) - 10,
max(p.x for p in right_eye_points) + 10,
max(p.y for p in right_eye_points) + 10
)
win.add_overlay(left_eye_rect, "blue")
win.add_overlay(right_eye_rect, "blue")
# Add nose tip highlight
nose_tip = landmarks.part(30)
win.add_overlay_circle(nose_tip, 5, "yellow")
print("Press any key to close...")
win.get_next_keypress()import dlib
import cv2
import numpy as np
# Load image
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create multiple processing results
blurred = dlib.gaussian_blur(gray, sigma=2.0)
edges = dlib.sobel_edge_detector(gray)
binary = dlib.threshold_image(gray)
# Display in separate windows
win_original = dlib.image_window(img, "Original")
win_blurred = dlib.image_window(blurred, "Blurred")
win_edges = dlib.image_window(edges[0], "Edges") # Horizontal gradients
win_binary = dlib.image_window(binary, "Binary")
# Wait for any window to close
while not (win_original.is_closed() or win_blurred.is_closed() or
win_edges.is_closed() or win_binary.is_closed()):
try:
# Check for keypress in any window
win_original.wait_for_keypress(' ')
break
except:
passThe GUI components provide essential interactive capabilities for computer vision applications, enabling real-time visualization, user interaction, and debugging of image processing algorithms. The image_window class is particularly valuable for prototyping, demonstrations, and interactive analysis tools.
Install with Tessl CLI
npx tessl i tessl/pypi-dlib