### Example usage of init_weights and kaiming_init Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Example showing how to initialize a DBHead with init_weights and apply a custom kaiming_init to a linear head. ```python import torch.nn as nn from utils.weight_init import init_weights, kaiming_init dbnet = DBHead(64, act='leaky') # see basemodel.py dbnet.init_weight(init_weights) # as in TextDetector.initialize_db # custom initializer for a linear head head = nn.Linear(64, 3) kaiming_init(head, a=0, mode='fan_in', nonlinearity='relu', distribution='uniform') ``` -------------------------------- ### Usage example from train_db.py Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_dataset.md Shows how to create training and validation dataloaders using create_dataloader, then iterate over the validation loader to get batches with keys: imgs, shrink_map, shrink_mask, threshold_map, threshold_mask, text_polys, ignore_tags. ```python from db_dataset import create_dataloader train_dataset, train_loader = create_dataloader( hyp_data['train_img_dir'], hyp_data['train_mask_dir'], imgsz, batch_size, augment=True, aug_param=hyp_data['aug_param'], shuffle=True, workers=hyp_data['num_workers'], cache=hyp_data['cache']) val_dataset, val_loader = create_dataloader( hyp_data['val_img_dir'], hyp_data['val_mask_dir'], imgsz, batch_size, augment=False, shuffle=False, workers=hyp_data['num_workers'], cache=hyp_data['cache'], with_ann=True) for batch in val_loader: # keys: imgs, shrink_map, shrink_mask, threshold_map, threshold_mask, text_polys, ignore_tags preds = model(batch['imgs']) metric = criterion(preds, batch, use_bce) ``` -------------------------------- ### Example: Label-map generation Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_utils.md Example of using MakeShrinkMap and MakeBorderMap to generate label maps, as in `db_dataset.py`. ```APIDOC ## Example: Label-map generation ### Description Example of using MakeShrinkMap and MakeBorderMap to generate label maps, as in `db_dataset.py`. ### Code ```python import numpy as np from utils.db_utils import MakeShrinkMap, MakeBorderMap shrink = MakeShrinkMap(shrink_ratio=0.4, min_text_size=4) border = MakeBorderMap(shrink_ratio=0.4, thresh_min=0.3, thresh_max=0.7) data = {'imgs': img_bgr, # HxWx3 uint8 'text_polys': polys, # (N, 4, 2) normalized [0,1] 'ignore_tags': [False]*N} data = shrink(data) data = border(data) # data now has shrink_map, shrink_mask, threshold_map, threshold_mask ``` ``` -------------------------------- ### Basic TextDetector usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/inference.md Shows how to instantiate TextDetector with a PyTorch checkpoint and run inference on a BGR image. The example prints the number of detected blocks and the first block's bounding box if any. ```python import cv2 from inference import TextDetector det = TextDetector('data/comictextdetector.pt', input_size=1024, device='cpu') img = cv2.imread('data/examples/AisazuNihaIrarenai-003.jpg') mask, mask_refined, blk_list = det(img) print(len(blk_list), blk_list[0].xyxy if blk_list else None) ``` -------------------------------- ### Example usage of imread, imwrite, find_all_imgs Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/io_utils.md Example usage of the IO helpers: find all images in a directory, read one as color and grayscale, and write a copy with a new extension. The `imwrite` call writes `data/examples/copy.jpg`. ```python from utils.io_utils import imread, imwrite, find_all_imgs imgs = find_all_imgs('data/examples', abs_path=True) # -> ['data/examples/x.jpg', ...] img = imread(imgs[0]) # cv2.IMREAD_COLOR gray = imread(imgs[0], cv2.IMREAD_GRAYSCALE) imwrite('data/examples/copy', img, ext='.jpg') # writes data/examples/copy.jpg ``` -------------------------------- ### Example: loading a YOLOv5 checkpoint and building a model Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_yolo.md Example usage of load_yolov5_ckpt to load a backbone checkpoint for the combined detector, and building a model directly from a YAML configuration. Shows the required imports and the expected checkpoint keys. ```python from models.yolov5.yolo import load_yolov5_ckpt, Model from basemodel import TextDetBase # backbone for the combined detector checkpoint (keys blk_det/text_seg/text_det) blk_det = load_yolov5_ckpt('data/yolov5sblk.ckpt', map_location='cpu') # or build from a yaml directly m = Model(cfg='yolov5s.yaml', ch=3, nc=3) ``` -------------------------------- ### Example usage of visualize_textblocks Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textblock.md Example from inference.py showing how to load an image, mask, and JSON-detected text blocks, refine the mask, and visualize the text blocks on the image and mask. Requires cv2, json, and the utils modules. ```python import cv2, json from utils.textblock import TextBlock, visualize_textblocks from utils.textmask import refine_mask img = cv2.imread('page.jpg') mask = cv2.imread('mask-page.png', cv2.IMREAD_GRAYSCALE) with open('page.json', encoding='utf8') as f: blk_list = [TextBlock(**d) for d in json.load(f)] mask = refine_mask(img, mask, blk_list) visualize_textblocks(img, blk_list) cv2.imshow('im', img); cv2.imshow('mask', mask); cv2.waitKey(0) ``` -------------------------------- ### concate_models usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/export.md Example from examples.ipynb cell 6 showing how to call concate_models with checkpoint paths and an output path. ```python from utils.export import concate_models concate_models('data/yolov5sblk.ckpt', 'data/unet_best.ckpt', 'data/db_best.ckpt', 'data/textdetector.pt') ``` -------------------------------- ### TextDetBaseDNN usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/basemodel.md Example from inference.TextDetector showing how to instantiate TextDetBaseDNN with input_size=1024 and model_path='data/comictextdetector.pt.onnx', then call it with an image to get blks, mask, and lines_map. ```python from basemodel import TextDetBaseDNN net = TextDetBaseDNN(input_size=1024, model_path='data/comictextdetector.pt.onnx') blks, mask, lines_map = net(img_bgr) ``` -------------------------------- ### Example usage of create_dataloader from train_seg.py Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/seg_dataset.md Example usage from train_seg.py. It creates a training dataset and loader with augmentation, then iterates over epochs, calling train_dataset.initialize() to set a random training size, and processes batches by moving to DEVICE, predicting, and computing loss. ```python from seg_dataset import create_dataloader train_dataset, train_loader = create_dataloader( hyp_data['train_img_dir'], hyp_data['train_mask_dir'], imgsz, batch_size, augment=True, aug_param=hyp_data['aug_param'], shuffle=True, workers=8, cache=hyp_data['cache']) for epoch in range(epochs): train_dataset.initialize() # random training size for i, (imgs, masks) in enumerate(train_loader): imgs, masks = imgs.to(DEVICE), masks.to(DEVICE) preds = model(imgs) loss = loss_func(preds, masks) ``` -------------------------------- ### Example: render_comictext with Japanese sampler Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/text_rendering.md Condensed example from examples.ipynb cell 3. Builds a Japanese sampler dict with font and text settings, creates a ComicTextSampler with page size (845, 1280) and seed 0, then calls render_comictext to generate 10 synthetic pages into data/examples/annotations. ```python from text_rendering import ComicTextSampler, render_comictext, ALIGN_LEFT, ALIGN_CENTER ja_sampler_dict = { 'num_txtblk': 20, 'font': {'font_dir': 'data/examples/fonts', 'font_statics': 'data/font_statics_en.csv', 'num': 1200, 'size': {'value': [0.02, 0.03, 0.15], 'prob': [1, 0.4, 0.15]}, 'stroke_width': {'value': [0, 0.1, 0.15], 'prob': [1, 0.5, 0.2]}, 'color': {'value': ['black', 'white', 'random'], 'prob': [1, 1, 0.4]}}, 'text': {'lang': 'ja', 'orientation': {'value': [1, 0], 'prob': [1, 0.3]}, 'rotation': {'value': [0, 30, 60], 'prob': [1, 0.3, 0.1]}, 'num_lines': {'value': [0.15], 'prob': [1]}, 'length': {'value': [0.3], 'prob': [1]}, 'min_num_lines': 1, 'min_length': 3, 'alignment': {'value': [ALIGN_LEFT, ALIGN_CENTER], 'prob': [0.3, 1]}} } jp_cts = ComicTextSampler((845, 1280), ja_sampler_dict, seed=0) render_comictext([jp_cts], 'data/examples', save_dir='data/examples/annotations', render_num=10, label_dir=None, show=False) ``` -------------------------------- ### DBLoss usage example from train_db.py Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/loss.md Example from train_db.py: creates a DBLoss instance, determines use_bce from config, computes loss metric and scales by accumulation steps. ```python from utils.loss import DBLoss criterion = DBLoss() use_bce = hyp_train['loss'] == 'bce' metric = criterion(preds, batchs, use_bce) # preds C=3 (shrink_with_sigmoid=False) or C=4 (bce) loss = metric['loss'] / accumulation_steps ``` -------------------------------- ### Example: using C3 and Conv (used by basemodel.py) Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_common.md Shows how to instantiate a C3 block and a Conv with LeakyReLU activation, then run a forward pass on a random tensor. Requires torch and the models.yolov5.common import. ```python import torch.nn as nn from models.yolov5.common import C3, Conv blk = C3(64, 128, n=1, act=True) # 3-conv CSP bottleneck conv = Conv(64, 64, k=3, s=1, act='leaky') # LeakyReLU activation y = blk(torch.randn(1, 64, 32, 32)) ``` -------------------------------- ### Usage example: letterbox, xyxy2yolo, get_yololabel_strings Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/imgproc_utils.md Shows how to load an image, apply letterbox resizing, convert bounding boxes to YOLO format, and generate label text for export. ```python import cv2 from utils.imgproc_utils import letterbox, xyxy2yolo, get_yololabel_strings img = cv2.imread('page.jpg') img_in, ratio, (dw, dh) = letterbox(img, new_shape=1024, auto=False, stride=64) # normalized YOLO labels for export yolo = xyxy2yolo([[10, 20, 100, 120], [200, 30, 260, 90]], img.shape[1], img.shape[0]) label_text = get_yololabel_strings([1, 1], yolo) ``` -------------------------------- ### Example usage of Loggers Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/general.md Shows how to instantiate Loggers with hyp and call on_train_epoch_end. Assumes hyp['logger']['type'] == 'wandb'. ```python from utils.general import Loggers, LOGGER, DEVICE logger = Loggers(hyp) # hyp['logger']['type'] == 'wandb' logger.on_train_epoch_end(epoch, {'train/loss': loss, 'eval/f1': f1}) ``` -------------------------------- ### YOLOv5 plumbing Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_utils.md Utility functions for YOLOv5 model setup and version checking. ```APIDOC ## `check_anchor_order` ### Description Flips anchors when area order mismatches stride order. ### Signature ```python def check_anchor_order(m): ``` ### Parameters - `m` (module) - Required - The model module containing anchors. ``` ```APIDOC ## `initialize_weights` ### Description Sets BN eps=1e-3, momentum=0.03; inplace ReLU-family activations. ### Signature ```python def initialize_weights(model): ``` ### Parameters - `model` (nn.Module) - Required - The model to initialize. ``` ```APIDOC ## `make_divisible` ### Description Ceil to nearest multiple of divisor. ### Signature ```python def make_divisible(x, divisor): ``` ### Parameters - `x` (int) - Required - The number to round. - `divisor` (int) - Required - The divisor. ### Returns Rounded value. ``` ```APIDOC ## `intersect_dicts` ### Description Key+shape-intersection of two state dicts. ### Signature ```python def intersect_dicts(da, db, exclude=()): ``` ### Parameters - `da` (dict) - Required - First state dict. - `db` (dict) - Required - Second state dict. - `exclude` (tuple) - Optional - Keys to exclude. ### Returns Intersected state dict. ``` ```APIDOC ## `check_version` ### Description Checks if current version meets minimum requirement. Returns bool; with `hard=True` asserts the requirement. ### Signature ```python def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False): ``` ### Parameters - `current` (str) - Optional - Current version string, default `'0.0.0'`. - `minimum` (str) - Optional - Minimum required version, default `'0.0.0'`. - `name` (str) - Optional - Name for error message, default `'version '`. - `pinned` (bool) - Optional - If True, require exact match, default `False`. - `hard` (bool) - Optional - If True, assert the requirement, default `False`. ### Returns bool - True if version satisfies requirement. ``` -------------------------------- ### Label-map generation with MakeShrinkMap and MakeBorderMap Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_utils.md Example of label-map generation as used in db_dataset.py. Creates shrink and border map builders, applies them to a data dict, and produces shrink_map, shrink_mask, threshold_map, and threshold_mask. ```python import numpy as np from utils.db_utils import MakeShrinkMap, MakeBorderMap shrink = MakeShrinkMap(shrink_ratio=0.4, min_text_size=4) border = MakeBorderMap(shrink_ratio=0.4, thresh_min=0.3, thresh_max=0.7) data = {'imgs': img_bgr, # HxWx3 uint8 'text_polys': polys, # (N, 4, 2) normalized [0,1] 'ignore_tags': [False]*N} data = shrink(data) data = border(data) # data now has shrink_map, shrink_mask, threshold_map, threshold_mask ``` -------------------------------- ### model2annotations usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/inference.md Example usage from examples.ipynb cell 1. Calls model2annotations with a model path, a list of image directories, and a save directory, with save_json=True to also write JSON block dicts. ```python from inference import model2annotations model2annotations('data/comictextdetector.pt', ['data/examples'], 'data/examples/annotations', save_json=True) ``` -------------------------------- ### TextBlock usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textblock.md Example usage of TextBlock: creates a block, sets lines, sorts, extracts a transformed region, and serializes to JSON with NumpyEncoder. Source: utils/textblock.py:12–266. ```python from utils.textblock import TextBlock from utils.io_utils import NumpyEncoder import json, cv2 blk = TextBlock([10, 20, 210, 120], language='ja', vertical=True) blk.lines = [[[10,20],[210,20],[210,120],[10,120]]] # one 4-point polygon blk.sort_lines() region = blk.get_transformed_region(img_bgr, idx=0, textheight=64) # upright text crop with open('blk.json', 'w') as f: json.dump(blk.to_dict(), f, cls=NumpyEncoder) ``` -------------------------------- ### refine_undetected_mask usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textmask.md Example usage as in inference.TextDetector.__call__. It calls refine_undetected_mask with REFINEMASK_ANNOTATION. All inputs are assumed pre-processed uint8 single-channel masks. ```python from utils.textmask import refine_undetected_mask, REFINEMASK_ANNOTATION mask_refined = refine_undetected_mask(img, mask, mask_refined, blk_list, refine_mode=REFINEMASK_ANNOTATION) ``` -------------------------------- ### Example: using non_max_suppression and draw_bbox Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_utils.md Example usage of non_max_suppression and draw_bbox. Assumes prediction is a (1, 25200, 5+nc) tensor from a Detect layer. After NMS, extracts bounding boxes and confidences. ```python import torch from utils.yolov5_utils import non_max_suppression, draw_bbox # prediction: (1, 25200, 5+nc) tensor from a Detect layer det = non_max_suppression(prediction, conf_thres=0.4, iou_thres=0.35)[0] if det.numel(): blines = det[..., :4].int().numpy(); confs = det[..., 4].numpy() ``` -------------------------------- ### uniform_init(module, a=0, b=1, bias=0) Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Initializes the module's weight using a uniform distribution and sets bias to a constant. ```APIDOC ## uniform_init(module, a=0, b=1, bias=0) ### Description Initializes the module's weight using a uniform distribution between `a` and `b`. Bias is set to a constant if present. Operates in place and returns `None`. ### Parameters - **module** (nn.Module) - Required - The module whose weight and bias are to be initialized. - **a** (float) - Optional - Lower bound of the uniform distribution. Default: `0`. - **b** (float) - Optional - Upper bound of the uniform distribution. Default: `1`. - **bias** (float) - Optional - Constant value for bias. Default: `0`. ### Returns None ### Source `utils/weight_init.py:25` ``` -------------------------------- ### refine_mask usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textmask.md Example usage of refine_mask. It reads an image and a grayscale mask, obtains a list of text blocks from a detector, and calls refine_mask with REFINEMASK_ANNOTATION for annotation export or with the default REFINEMASK_INPAINT for inpainting. ```python import cv2 from utils.textmask import refine_mask, REFINEMASK_ANNOTATION img = cv2.imread('page.jpg') mask = cv2.imread('mask-page.png', cv2.IMREAD_GRAYSCALE) blk_list = detector(img)[2] # List[TextBlock] mask_refined = refine_mask(img, mask, blk_list, refine_mode=REFINEMASK_ANNOTATION) # for annotation export mask_inpaint = refine_mask(img, mask, blk_list) # default REFINEMASK_INPAINT, for inpainting ``` -------------------------------- ### TextBlock Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textblock.md Constructor documentation for the TextBlock class, including all parameters and their defaults. ```APIDOC ## TextBlock ### Description Domain object describing one detected text region: a bounding box, its line polygons, language, orientation, font metrics, and rendering/color attributes. All fields are plain instance attributes assigned in the constructor (a dict-style object; `to_dict()` returns `vars(self)`). ### Constructor `TextBlock(xyxy: List, lines: List = None, language: str = 'unknown', vertical: bool = False, font_size: float = -1, distance: List = None, angle: int = 0, vec: List = None, norm: float = -1, merged: bool = False, weight: float = -1, text: List = None, translation: str = "", fg_r = 0, fg_g = 0, fg_b = 0, bg_r = 0, bg_g = 0, bg_b = 0, line_spacing = 1., font_family: str = "", bold: bool = False, underline: bool = False, italic: bool = False, alignment: int = -1, alpha: float = 255, rich_text: str = "", _bounding_rect: List = None, accumulate_color = True, default_stroke_width = 0.2, target_lang: str = "", **kwargs) -> None` ### Parameters - **xyxy** (List[int]) - Required - Bounding box `[x1, y1, x2, y2]`; stored as ints - **lines** (List) - Optional - Line polygons; each line is a 4×2 point list. Default: `None` (→ `[]`) - **language** (str) - Optional - One of `LANG_LIST`. Default: `'unknown'` - **vertical** (bool) - Optional - Vertical text orientation. Default: `False` - **font_size** (float) - Optional - Estimated font pixel size. Default: `-1` - **distance** (List) - Optional - Per-line distance to the reading origin; stored as `np.array` float64 when provided. Default: `None` - **angle** (int) - Optional - Rotation angle of textlines (degrees). Default: `0` - **vec** (List) - Optional - Primary direction vector of the block; `np.array` float64 when provided. Default: `None` - **norm** (float) - Optional - Norm of `vec`. Default: `-1` - **merged** (bool) - Optional - Set when a line was merged into another block. Default: `False` - **weight** (float) - Optional - Sort weight assigned by `sort_textblk_list`. Default: `-1` - **text** (List) - Optional - Recognized text per line (optional). Default: `None` (→ `[]`) - **translation** (str) - Optional - Translation string (optional). Default: `""` - **fg_r/fg_g/fg_b** (int) - Optional - Accumulative foreground RGB (summed over lines when `accumulate_color`). Default: `0` - **bg_r/bg_g/bg_b** (int) - Optional - Accumulative stroke/background RGB. Default: `0` - **line_spacing** (float) - Optional - Line spacing factor. Default: `1.` - **font_family** (str) - Optional - Font family. Default: `""` - **bold/underline/italic** (bool) - Optional - Text style flags. Default: `False` - **alignment** (int) - Optional - `-1`=auto, otherwise 0=left, 1=center, 2=right; stored as `_alignment`. Default: `-1` - **alpha** (float) - Optional - Opacity. Default: `255` - **rich_text** (str) - Optional - Rich text payload. Default: `""` - **_bounding_rect** (List) - Optional - Cached x,y,w,h rect. Default: `None` - **accumulate_color** (bool) - Optional - Whether fg/bg colors are per-line accumulations. Default: `True` - **default_stroke_width** (float) - Optional - Stroke width returned by the `stroke_width` property. Default: `0.2` - **target_lang** (str) - Optional - Stored as `_target_lang`. Default: `""` - **kwargs** - Optional - Silently ignored ### Notes - Always set: `self.prob = 1` (not a constructor argument). - Source: `utils/textblock.py:12`. ``` -------------------------------- ### Initialize TextBlock with all attributes Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textblock.md Constructor for the TextBlock domain object. It accepts a bounding box, line polygons, language, orientation, font metrics, and rendering attributes. All fields are stored as plain instance attributes; to_dict() returns vars(self). The constructor always sets self.prob = 1, which is not a parameter. ```python class TextBlock(object): def __init__(self, xyxy: List, lines: List = None, language: str = 'unknown', vertical: bool = False, font_size: float = -1, distance: List = None, angle: int = 0, vec: List = None, norm: float = -1, merged: bool = False, weight: float = -1, text: List = None, translation: str = "", fg_r = 0, fg_g = 0, fg_b = 0, bg_r = 0, bg_g = 0, bg_b = 0, line_spacing = 1., font_family: str = "", bold: bool = False, underline: bool = False, italic: bool = False, alignment: int = -1, alpha: float = 255, rich_text: str = "", _bounding_rect: List = None, accumulate_color = True, default_stroke_width = 0.2, target_lang: str = "", **kwargs) -> None: ``` -------------------------------- ### kaiming_init(module, a=0, is_rnn=False, mode='fan_in', nonlinearity='leaky_relu', bias=0, distribution='normal') Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Initializes the module's weight using Kaiming initialization (uniform or normal) and sets bias to a constant. Supports RNN modules by iterating over named parameters. ```APIDOC ## kaiming_init(module, a=0, is_rnn=False, mode='fan_in', nonlinearity='leaky_relu', bias=0, distribution='normal') ### Description Initializes the module's weight using Kaiming initialization. The `distribution` parameter selects between uniform and normal Kaiming methods. If `is_rnn` is `True`, iterates over all named parameters: bias parameters are set to a constant, weight parameters are initialized with Kaiming. Bias is set to a constant if present. Operates in place and returns `None`. ### Parameters - **module** (nn.Module) - Required - The module whose weight (or all named parameters) is to be initialized. - **a** (float) - Optional - Leaky slope for the Kaiming initialization. Default: `0`. - **is_rnn** (bool) - Optional - If `True`, iterate over `module.named_parameters()` and initialize each parameter accordingly. Default: `False`. - **mode** (str) - Optional - Kaiming mode, either `'fan_in'` or `'fan_out'`. Default: `'fan_in'`. - **nonlinearity** (str) - Optional - Nonlinearity used for the Kaiming initialization. Default: `'leaky_relu'`. - **bias** (float) - Optional - Constant value for bias. Default: `0`. - **distribution** (str) - Optional - Either `'uniform'` or `'normal'`. Default: `'normal'`. ### Throws - **AssertionError** - If `distribution` is not in `['uniform', 'normal']`. ### Returns None ### Source `utils/weight_init.py:31` ``` -------------------------------- ### export_onnx usage example Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/export.md Example from examples.ipynb cell 6 showing how to export a TextDetBase model to ONNX. The output file is data/textdetector.pt.onnx with outputs blk, seg, det. The produced .onnx can be loaded by inference.TextDetector via the OpenCV DNN backend. ```python import torch from utils.export import export_onnx from basemodel import TextDetBase batch_size, imgsz = 1, 1024 im = torch.zeros(batch_size, 3, imgsz, imgsz) model = TextDetBase('data/textdetector.pt', device='cpu').to('cpu') export_onnx(model, im, 'data/textdetector.pt', 11) # -> writes data/textdetector.pt.onnx with outputs blk, seg, det ``` -------------------------------- ### normal_init(module, mean=0, std=1, bias=0) Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Initializes the module's weight using a normal distribution and sets bias to a constant. ```APIDOC ## normal_init(module, mean=0, std=1, bias=0) ### Description Initializes the module's weight using a normal distribution with given mean and standard deviation. Bias is set to a constant if present. Operates in place and returns `None`. ### Parameters - **module** (nn.Module) - Required - The module whose weight and bias are to be initialized. - **mean** (float) - Optional - Mean of the normal distribution. Default: `0`. - **std** (float) - Optional - Standard deviation of the normal distribution. Default: `1`. - **bias** (float) - Optional - Constant value for bias. Default: `0`. ### Returns None ### Source `utils/weight_init.py:19` ``` -------------------------------- ### xavier_init(module, gain=1, bias=0, distribution='normal') Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Initializes the module's weight using Xavier initialization (uniform or normal) and sets bias to a constant. ```APIDOC ## xavier_init(module, gain=1, bias=0, distribution='normal') ### Description Initializes the module's weight using Xavier initialization. The `distribution` parameter selects between uniform and normal Xavier methods. Bias is set to a constant if present. Operates in place and returns `None`. ### Parameters - **module** (nn.Module) - Required - The module whose weight and bias are to be initialized. - **gain** (float) - Optional - Scaling factor for the initialization. Default: `1`. - **bias** (float) - Optional - Constant value for bias. Default: `0`. - **distribution** (str) - Optional - Either `'uniform'` or `'normal'`. Default: `'normal'`. ### Throws - **AssertionError** - If `distribution` is not in `['uniform', 'normal']`. ### Returns None ### Source `utils/weight_init.py:9` ``` -------------------------------- ### Inference usage of SegDetectorRepresenter Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_utils.md Example of using SegDetectorRepresenter in inference, as in inference.py. It creates the representer with thresh=0.3, calls it with input_size and lines_map (from TextDetBaseDNN/TextDetBase), then filters lines by scores > 0.6 using numpy. ```python import numpy as np from utils.db_utils import SegDetectorRepresenter seg_rep = SegDetectorRepresenter(thresh=0.3) lines, scores = seg_rep(input_size, lines_map) # lines_map from TextDetBaseDNN/TextDetBase idx = np.where(scores[0] > 0.6) lines = lines[0][idx] ``` -------------------------------- ### LoadImageAndAnnotations.initialize Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_dataset.md Initialization method. Same `self.augment` attribute quirk as `seg_dataset.LoadImageAndMask` (bound-method truthiness); requires `augment=True` so that `multi_size` exists. ```APIDOC ## LoadImageAndAnnotations.initialize ### Description Initialization method. Same `self.augment` attribute quirk as `seg_dataset.LoadImageAndMask` (bound-method truthiness); requires `augment=True` so that `multi_size` exists. ### Signature ```python def initialize(self): ``` ### Source `db_dataset.py:43` ``` -------------------------------- ### LoadImageAndAnnotations.__init__ Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/db_dataset.md Constructor for the dataset class. Pairs images with `line-.txt` annotations and computes shrink/border maps. ```APIDOC ## LoadImageAndAnnotations.__init__ ### Description Constructor for the dataset class. Pairs images with `line-.txt` annotations and computes shrink/border maps. ### Signature ```python def __init__(self, img_dir, ann_dir=None, img_size=640, augment=False, aug_param=None, cache=False, stride=128, cache_ann_only=True, with_ann=False): ``` ### Parameters - **img_dir** (str | list[str]) - Required - Image directory(ies); raises `Exception('unknown img_dir format')` otherwise. - **ann_dir** (str | list[str]) - Optional - Directory(ies) holding `line-.txt`. Defaults to `img_dir`. - **img_size** (int) - Optional - Base square size. Default: `640`. - **augment** (bool) - Optional - Enable augmentation. Default: `False`. - **aug_param** (dict) - Optional - Keys read when augmenting: `mini_mosaic`, `hsv`, `flip_lr`, `neg`, `rotate`, `rotate_range`, `size_range` (a `list` whose first element `> 0` enables multi-size). - **cache** (bool) - Optional - Preload annotations (masks); aborts over 7 GB. Default: `False`. - **stride** (int) - Optional - Multi-size step. Default: `128`. - **cache_ann_only** (bool) - Optional - Cache annotations only. Default: `True`. - **with_ann** (bool) - Optional - Keep `text_polys`/`ignore_tags` in the output batch (validation). Default: `False`. ### Source `db_dataset.py:43` ``` -------------------------------- ### YOLOv5 plumbing function signatures Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_utils.md YOLOv5 plumbing functions. check_version returns bool; with hard=True asserts the requirement with message f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed'. ```python def check_anchor_order(m): # flips anchors when area order mismatches stride order def initialize_weights(model): # sets BN eps=1e-3, momentum=0.03; inplace ReLU-family activations def make_divisible(x, divisor): # ceil to nearest multiple of divisor def intersect_dicts(da, db, exclude=()): # key+shape-intersection of two state dicts def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False): ``` -------------------------------- ### load_yolov5(weights, map_location='cuda', fuse=True, inplace=True, out_indices=[1, 3, 5, 7, 9]) Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/yolov5_yolo.md Loads a saved YOLOv5 model checkpoint, applies float, fuse, and eval operations, and performs compatibility updates. Returns the prepared model with specified output indices. ```APIDOC ## `load_yolov5` ### Description Loads a saved `ckpt['model']` directly (`torch.load` when `weights` is a str), applies `.float().fuse().eval()` or `.float().eval()`, then compatibility updates: `inplace` on activation/Detect/Model modules, `anchor_grid` list fix for old checkpoints, `Conv._non_persistent_buffers_set` for torch 1.6. Sets `model.out_indices = out_indices`. **Returns** the model. ### Parameters - **weights** (str or dict) - Required - Path to checkpoint file or a checkpoint dict. - **map_location** (str) - Optional - Device mapping for loading (default: 'cuda'). - **fuse** (bool) - Optional - Whether to fuse Conv and BN layers (default: True). - **inplace** (bool) - Optional - Whether to use inplace operations (default: True). - **out_indices** (list) - Optional - List of output layer indices (default: [1, 3, 5, 7, 9]). ### Returns - **model** - The loaded and prepared YOLOv5 model. ``` -------------------------------- ### get_transformed_region method signature Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/textblock.md Homography-warped upright crop of line idx, height textheight; vertical lines are rotated 90° CCW. English/unknown-horizontal lines get a font-size/3 padding expansion before warping. Returns BGR ndarray. Source: utils/textblock.py:123. ```python def get_transformed_region(self, img, idx, textheight) -> np.ndarray: ``` -------------------------------- ### constant_init(module, val, bias=0) Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Initializes the module's weight to a constant value and sets bias to a constant if present. ```APIDOC ## constant_init(module, val, bias=0) ### Description Initializes the module's weight to a constant value `val` and sets the bias (if present) to `bias`. Operates in place and returns `None`. ### Parameters - **module** (nn.Module) - Required - The module whose weight and bias are to be initialized. - **val** (float) - Required - The constant value to set the weight to. - **bias** (float) - Optional - The constant value to set the bias to. Default: `0`. ### Returns None ### Source `utils/weight_init.py:4` ``` -------------------------------- ### Concatenate Weights and Export to ONNX Source: https://github.com/dmmaze/comic-text-detector/blob/master/examples.ipynb Concatenates the trained text block detector, segmentation head, and DB head weights into a single textdetector.pt file, then exports the model to ONNX format with opset 11. The example uses a batch size of 1 and image size 1024, and forces CPU device. ```python from utils.export import * concate_models('data/yolov5sblk.ckpt', 'data/unet_best.ckpt', 'data/db_best.ckpt', 'data/textdetector.pt') batch_size, imgsz = 1, 1024 cuda = torch.cuda.is_available() device = 'cpu' im = torch.zeros(batch_size, 3, imgsz, imgsz).to(device) model_path = r'data/textdetector.pt' model = TextDetBase(model_path, device=device).to(device) export_onnx(model, im, model_path, 11) ``` -------------------------------- ### Advanced TextDetector usage with refinement and JSON export Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/inference.md Demonstrates advanced options: CUDA device, half precision, custom thresholds, and mask refinement mode. Also shows how to persist detected blocks to a JSON file using a custom encoder. ```python from inference import TextDetector from utils.textmask import REFINEMASK_ANNOTATION det = TextDetector('data/comictextdetector.pt', input_size=1024, device='cuda', half=True, nms_thresh=0.35, conf_thresh=0.4, act='leaky') mask, mask_refined, blk_list = det( bgr_img, refine_mode=REFINEMASK_ANNOTATION, keep_undetected_mask=True, ) # Persist blocks for later re-use import json from utils.io_utils import NumpyEncoder with open('blocks.json', 'w', encoding='utf8') as f: json.dump([blk.to_dict() for blk in blk_list], f, ensure_ascii=False, cls=NumpyEncoder) ``` -------------------------------- ### Generate Synthetic Comic Text Data with ComicTextSampler Source: https://github.com/dmmaze/comic-text-detector/blob/master/examples.ipynb Generates synthetic comic/manga text images using ComicTextSampler and render_comictext. The sampler configuration mimics comic text style with font, size, stroke, color, orientation, rotation, and alignment parameters. The example creates both Japanese and English samplers and renders 10 images. Note: the rendering script won't handle characters missing from fonts, so use no-text images. ```python from text_rendering import ComicTextSampler, render_comictext, ALIGN_LEFT, ALIGN_CENTER import copy ja_sampler_dict = { 'num_txtblk': 20, 'font': { 'font_dir': 'data/examples/fonts', # font file directory 'font_statics': 'data/font_statics_en.csv', # Just a font list file, please create your own list and ignore the last two cols. 'num': 1200, # first 500 of the fontlist will be used # params to mimic comic/manga text style 'size': {'value': [0.02, 0.03, 0.15], 'prob': [1, 0.4, 0.15]}, 'stroke_width': {'value': [0, 0.1, 0.15], 'prob': [1, 0.5, 0.2]}, 'color': {'value': ['black', 'white', 'random'], 'prob': [1, 1, 0.4]}, }, 'text': { 'lang': 'ja', # render japanese, 'en' for english 'orientation': {'value': [1, 0], # 1 is vertical text. 'prob': [1, 0.3]}, 'rotation': {'value': [0, 30, 60], 'prob': [1, 0.3, 0.1]}, 'num_lines': {'value': [0.15], 'prob': [1]}, 'length': {'value': [0.3], 'prob': [1]}, 'min_num_lines': 1, 'min_length': 3, 'alignment': {'value': [ALIGN_LEFT, ALIGN_CENTER], 'prob': [0.3, 1]} } } jp_cts = ComicTextSampler((845, 1280), ja_sampler_dict, seed=0) eng_dict = copy.deepcopy(ja_sampler_dict) eng_dict['text']['lang'] = 'en' eng_dict['text']['orientation'] = {'value': [1, 0], 'prob': [0, 1]} eng_cts = ComicTextSampler((845, 1280), eng_dict, seed=0) img_dir = r'data/examples' save_dir = r'data/examples/annotations' render_comictext([eng_cts, jp_cts], img_dir, save_dir=save_dir, save_prefix=None, render_num=10, label_dir=None, show=False) ``` ```text Output: 100%|██████████| 10/10 [00:12<00:00, 1.23s/it] ``` -------------------------------- ### TextDetector.__init__ Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/inference.md Constructor for the TextDetector facade class. ```APIDOC ## TextDetector ### Description Initializes the TextDetector facade, loading either a PyTorch checkpoint or an ONNX model through OpenCV DNN. ### Constructor `TextDetector(model_path, input_size=1024, device='cpu', half=False, nms_thresh=0.35, conf_thresh=0.4, mask_thresh=0.3, act='leaky')` ### Parameters - **model_path** (str) - Required - Model path; if suffix is `.onnx`, uses the OpenCV DNN backend, otherwise the PyTorch backend. - **input_size** (int) - Optional, default `1024` - Square network input size (int → `(input_size, input_size)`). - **device** (str) - Optional, default `'cpu'` - `'cuda'` or `'cpu'`; `cuda = device == 'cuda'`. - **half** (bool) - Optional, default `False` - Half precision for the torch backend. - **nms_thresh** (float) - Optional, default `0.35` - NMS IoU threshold passed to `postprocess_yolo`. - **conf_thresh** (float) - Optional, default `0.4` - Confidence threshold for YOLO detections. - **mask_thresh** (float) - Optional, default `0.3` - **Accepted but unused**: never stored nor referenced in `__call__`; the seg-representer is hard-coded with `thresh=0.3` and `__call__` uses a hard-coded `box_thresh=0.6`. - **act** (bool/str) - Optional, default `'leaky'` - Activation flag for heads (torch backend). ### Side Effects - For `.onnx` paths: creates `self.model = cv2.dnn.readNetFromONNX(model_path)` (unused afterwards) and `self.net = TextDetBaseDNN(input_size, model_path)`. - Otherwise: `self.net = TextDetBase(model_path, device=device, act=act)`. - Backend flag `self.backend` is `'opencv'` or `'torch'`. - `self.seg_rep = SegDetectorRepresenter(thresh=0.3)`. ### Source `inference.py:116` ``` -------------------------------- ### Function signature: uniform_init Source: https://github.com/dmmaze/comic-text-detector/blob/master/_autodocs/api-reference/weight_init.md Signature for uniform_init, which initializes weights with a uniform distribution. ```python def uniform_init(module, a=0, b=1, bias=0): ```