comic-text-detector 是一个专门用于检测漫画页面中文字区域的深度学习工具。它结合了 YOLOv5 目标检测和语义分割技术,能够准确识别和定位漫画中的对话气泡和文字区域。
TextDetector(
model_path, # 模型文件路径(.pt 或 .onnx)
input_size=1024, # 输入图片尺寸(默认1024,可以是int或tuple)
device='cpu', # 设备:'cpu' 或 'cuda'
half=False, # 是否使用半精度(FP16)
nms_thresh=0.35, # NMS阈值(0-1,越小越严格)
conf_thresh=0.4, # 置信度阈值(0-1,越小检测越多)
mask_thresh=0.3, # Mask阈值(用于分割)
act='leaky' # 激活函数:'leaky'、'relu'、'silu'等
)
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model_path |
str | 必需 | 模型文件路径,支持 .pt(PyTorch)或 .onnx(ONNX)格式 |
input_size |
int/tuple | 1024 | 输入图片尺寸,可以是单个整数(如1024)或元组(如(1024, 1024)) |
device |
str | 'cpu' | 计算设备,'cpu' 或 'cuda'(需要GPU支持) |
half |
bool | False | 是否使用半精度浮点数(FP16),可加速推理但可能降低精度 |
nms_thresh |
float | 0.35 | 非极大值抑制阈值,范围0-1,值越小过滤越严格 |
conf_thresh |
float | 0.4 | 置信度阈值,范围0-1,值越小检测到的区域越多 |
mask_thresh |
float | 0.3 | Mask分割阈值,用于生成文字区域的mask |
act |
str | 'leaky' | 激活函数类型,可选:'leaky'、'relu'、'silu' |
detector(
img, # 输入图片(numpy数组)
refine_mode=REFINEMASK_INPAINT, # 精炼模式
keep_undetected_mask=False # 是否保留未检测到的mask区域
)
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
img |
np.ndarray | 必需 | 输入图片,OpenCV格式(BGR)的numpy数组 |
refine_mode |
int | REFINEMASK_INPAINT |
精炼模式,见下方说明 |
keep_undetected_mask |
bool | False | 是否保留未检测到的mask区域,True会保留更多区域 |
REFINEMASK_INPAINT = 0:使用修复模式
REFINEMASK_ANNOTATION = 1:使用标注模式
refine_mode选择不同的精炼策略from inference import TextDetector
detector = TextDetector(
model_path='data/comictextdetector.pt',
input_size=1024,
device='cuda' if torch.cuda.is_available() else 'cpu',
act='leaky'
)
说明:
import cv2
import numpy as np
# 读取图片(支持中文路径)
img_array = np.fromfile(image_path, dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
处理流程:
np.fromfile和cv2.imdecode处理中文路径)input_sizemask, mask_refined, blk_list = detector(
img,
refine_mode=REFINEMASK_ANNOTATION,
keep_undetected_mask=True
)
推理过程:
YOLOv5检测:
分割网络:
后处理:
# 从mask中提取文字行轮廓(多边形)
lines, scores = seg_rep(input_size, lines_map)
# 将检测框和轮廓分组为TextBlock对象
blk_list = group_output(blks, lines, im_w, im_h, mask)
处理内容:
# 使用refine_mask优化mask边界
mask_refined = refine_mask(img, mask, blk_list, refine_mode=refine_mode)
# 可选:保留未检测到的区域
if keep_undetected_mask:
mask_refined = refine_undetected_mask(
img, mask, mask_refined, blk_list, refine_mode=refine_mode
)
精炼策略:
返回值:
mask:原始检测mask(二值图像)mask_refined:精炼后的mask(优化后的二值图像)blk_list:文字块列表,每个元素包含:
{
'index': 1,
'bbox': {
'x1': 100, 'y1': 200,
'x2': 300, 'y2': 250,
'width': 200, 'height': 50,
'center_x': 200, 'center_y': 225
},
'lines': [[[x1, y1], [x2, y2], ...]], # 多边形轮廓
'language': 'ja',
'vertical': False
}
建议: 根据图片分辨率和性能需求选择,一般使用1024即可。
建议: 默认0.4,根据实际效果调整。
建议: 一般不需要调整,除非出现大量重叠检测框。
REFINEMASK_INPAINT,生成更完整的maskREFINEMASK_ANNOTATION,保持原始结果建议: 根据后续用途选择。
建议: 有GPU时优先使用CUDA。
建议: 根据实际需求选择,一般OCR识别时设为True。
# -*- coding: utf-8 -*-
import sys
import cv2
import numpy as np
from pathlib import Path
from inference import TextDetector, REFINEMASK_ANNOTATION
# 初始化检测器
detector = TextDetector(
model_path='data/comictextdetector.pt',
input_size=1024,
device='cuda' if torch.cuda.is_available() else 'cpu',
act='leaky'
)
# 读取图片
image_path = 'example.jpg'
img_array = np.fromfile(image_path, dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
# 执行检测
mask, mask_refined, blk_list = detector(
img,
refine_mode=REFINEMASK_ANNOTATION,
keep_undetected_mask=True
)
# 处理结果
print(f"检测到 {len(blk_list)} 个文字区域")
for i, blk in enumerate(blk_list):
print(f"文字块 {i+1}: {blk.xyxy}")
{
"image_file": "example.jpg",
"image_size": {
"width": 1334,
"height": 1940
},
"text_blocks": [
{
"index": 1,
"bbox": {
"x1": 100,
"y1": 200,
"x2": 300,
"y2": 250,
"width": 200,
"height": 50,
"center_x": 200,
"center_y": 225
},
"lines": [[[x1, y1], [x2, y2], ...]],
"language": "ja",
"vertical": false
}
],
"total_count": 1
}
解决方案:
conf_thresh(如从0.4降到0.3)input_size(如从1024增到1536)keep_undetected_mask=True解决方案:
conf_thresh(如从0.4提高到0.5-0.6)nms_thresh(如从0.35降到0.3)解决方案:
input_size(如从1024减到512)device='cuda').onnx格式通常更快)解决方案:
REFINEMASK_INPAINT模式进行精炼input_size提高精度comic-text-detector 基于 YOLOv5 目标检测架构,专门针对漫画文字区域进行了优化训练。
使用语义分割网络生成精确的文字区域mask,支持不规则形状的文字区域。
将检测到的边界框和分割轮廓进行分组,形成完整的文字块(TextBlock)对象。
python/comic-text-detector-master/data/comictextdetector.pt 或 data/comictextdetector.pt.onnxpython/generate-anim/detect_comic_text_with_boxes.py