seeit.py
Overview
The seeit.py file is a utility module designed to visualize object detection results by drawing bounding boxes and labels on images. It provides functions to save annotated images, draw bounding boxes with class-specific colors, and generate consistent color maps for classes. This module is intended to be part of a larger image processing or computer vision pipeline, where detected objects and their confidence scores are output by a model and then visually presented for inspection or evaluation.
Classes and Functions
This file contains only functions—no classes are defined.
1. save_results(image_list, results, labels, output_dir='output/', threshold=0.5)
Saves images annotated with detection results to disk.
Parameters:
image_list(list[PIL.Image.Image]): List of PIL Image objects to annotate and save.results(list[list[dict]]): List of detection results for each image. Each detection is a dictionary with keys:"bbox": Bounding box coordinates[xmin, ymin, xmax, ymax]as integers or floats."type": Class label as a lowercase string."score": Confidence score (float between 0 and 1).
labels(list[str]): List of class labels. Used to assign colors to classes.output_dir(str, optional): Directory path where annotated images will be saved. Defaults to'output/'.threshold(float, optional): Minimum confidence score to visualize a detection. Defaults to0.5.
Returns: None
Usage example:
save_results(
image_list=[img1, img2],
results=[
[{"bbox": [50, 50, 150, 150], "type": "cat", "score": 0.9}],
[{"bbox": [30, 40, 100, 120], "type": "dog", "score": 0.7}]
],
labels=["cat", "dog"]
)
Behavior:
Creates the output directory if it does not exist.
For each image, calls
draw_boxto annotate it.Saves the annotated image as a JPEG file named by its index (e.g.,
0.jpg) inoutput_dir.Logs the save path at DEBUG level.
2. draw_box(im, result, labels, threshold=0.5)
Draws bounding boxes and labels on a single image based on detection results.
Parameters:
im(PIL.Image.Image): The image to annotate.result(list[dict]): List of detection dictionaries for this image, with keys:"bbox":[xmin, ymin, xmax, ymax]"type": Class label string"score": Confidence score
labels(list[str]): List of all class labels.threshold(float, optional): Minimum confidence score to draw a detection. Defaults to0.5.
Returns:
Annotated
PIL.Image.Imageinstance with bounding boxes and labels drawn.
Usage example:
annotated_image = draw_box(image, detections, ["cat", "dog"], threshold=0.6)
annotated_image.show()
Implementation details:
Calculates line thickness proportional to image size.
Uses
get_color_map_listto generate color mapping for each class.Filters detections below the confidence threshold.
Draws bounding box as a closed polygon.
Draws a filled rectangle behind the text label to improve readability.
Label text includes class name and score formatted to 4 decimal places.
3. get_color_map_list(num_classes)
Generates a list of distinct RGB colors for a given number of classes.
Parameters:
num_classes(int): Number of classes requiring distinct colors.
Returns:
color_map(list[list[int]]): List of RGB colors as lists of 3 integers in[0, 255].
Usage example:
colors = get_color_map_list(3)
# colors might be [[128, 0, 0], [0, 128, 0], [0, 0, 128]]
Algorithm details:
Uses bitwise operations to generate unique colors by encoding bits of class indices.
The color generation scheme ensures visually distinct colors by manipulating RGB channels based on shifted bits.
Returns a list where each entry corresponds to the RGB color for a class index.
4. imagedraw_textsize_c(draw, text)
Calculates the width and height of text to be drawn on an image.
Parameters:
draw(PIL.ImageDraw.ImageDraw): Drawing context from PIL.text(str): Text string whose size is to be measured.
Returns:
Tuple
(tw, th)representing text width and height in pixels.
Usage example:
draw = ImageDraw.Draw(image)
tw, th = imagedraw_textsize_c(draw, "Hello World")
Implementation details:
For PIL versions < 10, uses
draw.textsize().For PIL versions ≥ 10, uses
draw.textbbox()for more accurate bounding box measurement.
Important Implementation Details
Bounding box drawing: The bounding box is drawn by connecting the four corners with lines. The line thickness adapts to image size to maintain consistent visual weight.
Color mapping: Colors are assigned deterministically based on class indices, ensuring consistent colors across multiple runs.
Text background: Text labels have a filled rectangle behind them to improve readability against varying backgrounds.
Compatibility: Text size calculation adapts to PIL library version differences.
Integration with System / Application
This module is designed to be called after an object detection inference step.
It takes raw images and detection outputs to produce human-readable visualizations.
The output images are saved to disk, which can then be used for:
Manual verification of detection results.
Generating datasets with visual annotations.
Feeding into report generation or UI display components.
The module depends on the Python Imaging Library (PIL/Pillow).
It uses standard Python logging for debug messages.
Typically, this file is part of a visualization or post-processing submodule within a larger computer vision system, such as the InfiniFlow project referenced in the header.
Visual Diagram
classDiagram
class seeit {
+save_results(image_list, results, labels, output_dir='output/', threshold=0.5)
+draw_box(im, result, labels, threshold=0.5)
+get_color_map_list(num_classes)
+imagedraw_textsize_c(draw, text)
}
seeit : +save_results()
seeit : +draw_box()
seeit : +get_color_map_list()
seeit : +imagedraw_textsize_c()
Summary
The seeit.py module provides essential utilities for visually presenting object detection results by drawing bounding boxes and labels on images with class-distinct colors. Its straightforward API facilitates easy integration into image processing pipelines, supporting the evaluation and demonstration of detection models.