Manga Bubble Segmentation là mô hình phân đoạn chuyên biệt dựa trên YOLOv8m-seg architecture, được huấn luyện để phát hiện và phân đoạn các bong bóng hội thoại (speech bubbles) trong truyện tranh manga. Model có khả năng nhận diện chính xác vùng text trong các loại bubble khác nhau, phục vụ cho các ứng dụng:
📚 Manga Translation - Tự động hóa quy trình dịch truyện
🎨 Content Editing - Chỉnh sửa và làm sạch bubble
🔍 Text Extraction - Trích xuất text từ manga
🤖 OCR Pipeline - Tiền xử lý cho nhận diện chữ
📖 Digital Comic Processing - Xử lý truyện tranh số
🎯 Tính Năng Chính
✅ Phân đoạn chính xác các speech bubble trong manga
✅ YOLOv8m-seg architecture - Cân bằng tốc độ & độ chính xác
1from ultralytics import YOLO
2from huggingface_hub import hf_hub_download
34# Tải model từ Hugging Face5model_path = hf_hub_download(6 repo_id="khanhromvn/manga_bubble_seg",7 filename="best.pt"8)910# Load model11model = YOLO(model_path)1213# Chạy inference trên ảnh manga14results = model("manga_page.jpg")1516# Hiển thị kết quả17results[0].show()1819# Lưu kết quả20results[0].save("output_segmented.jpg")
Xử Lý Nhiều Trang Manga
python
1import os
2from pathlib import Path
34# Đường dẫn folder chứa manga5manga_folder ="manga_chapter_01"6output_folder ="segmented_output"78# Tạo folder output9os.makedirs(output_folder, exist_ok=True)1011# Xử lý tất cả ảnh12image_files =list(Path(manga_folder).glob("*.jpg"))+ \
13list(Path(manga_folder).glob("*.png"))1415for img_path in image_files:16 results = model(str(img_path))17 output_path = os.path.join(output_folder, img_path.name)18 results[0].save(output_path)19print(f"Đã xử lý: {img_path.name}")
Trích Xuất Masks của Bubble
python
1import numpy as np
2import cv2
34# Chạy inference5results = model("manga_page.jpg")67# Lấy masks8masks = results[0].masks.data.cpu().numpy()# Shape: (N, H, W)9boxes = results[0].boxes.data.cpu().numpy()# Bounding boxes10classes = results[0].boxes.cls.cpu().numpy()# Class IDs1112# Xử lý từng bubble13for i, mask inenumerate(masks):14# Resize mask về kích thước ảnh gốc15 mask_resized = cv2.resize(mask,(original_width, original_height))1617# Tạo mask binary18 mask_binary =(mask_resized >0.5).astype(np.uint8)*2551920# Lưu mask21 cv2.imwrite(f"bubble_mask_{i}.png", mask_binary)2223# Crop vùng bubble từ ảnh gốc24 x1, y1, x2, y2 = boxes[i][:4].astype(int)25 bubble_crop = original_image[y1:y2, x1:x2]26 cv2.imwrite(f"bubble_crop_{i}.png", bubble_crop)
Tham Số Inference Nâng Cao
python
1# Tùy chỉnh tham số inference2results = model.predict(3 source="manga_page.jpg",4 conf=0.25,# Ngưỡng confidence (giảm để detect nhiều hơn)5 iou=0.7,# IoU threshold cho NMS6 imgsz=640,# Kích thước ảnh input7 device="cuda:0",# Sử dụng GPU8 save=True,# Tự động lưu kết quả9 show_labels=True,# Hiển thị nhãn class10 show_conf=True,# Hiển thị confidence score11 augment=False,# Test-time augmentation12 agnostic_nms=False# Class-agnostic NMS13)
📈 Kết Quả Đánh Giá
Metrics Chính
Metric
Giá Trị
mAP50
TBD
mAP50-95
TBD
Precision
TBD
Recall
TBD
Inference Time (GPU)
~15ms/image
Inference Time (CPU)
~80ms/image
Lưu ý: Metrics chi tiết sẽ được cập nhật sau khi đánh giá đầy đủ trên test set.
Khả Năng của Model
✅ Phát hiện chính xác bubble trong manga đen trắng
✅ Xử lý được nhiều phong cách vẽ khác nhau
✅ Hoạt động tốt với bubble có text dày đặc
✅ Phân đoạn chính xác ranh giới bubble
✅ Inference thời gian thực
🎓 Huấn Luyện Model
Train từ Đầu
python
1from ultralytics import YOLO
23# Load pretrained YOLOv8m-seg4model = YOLO("yolov8m-seg.pt")56# Train trên manga dataset7results = model.train(8 data="manga_bubble.yaml",9 epochs=100,10 imgsz=640,11 batch=-1,# Auto batch size12 workers=4,# Số worker threads13 save_period=10,# Lưu checkpoint mỗi 10 epochs14 device="auto",# Auto detect GPU/CPU15 project="manga_training",16 name="bubble_seg_v1"17)
Fine-tuning Model
python
1# Load model này để fine-tune2model = YOLO("khanhromvn/manga_bubble_seg")34# Fine-tune trên dataset riêng5results = model.train(6 data="your_manga_dataset.yaml",7 epochs=50,8 imgsz=640,9 batch=-1,10 workers=411)
1from ultralytics import YOLO
2import easyocr
34# Load models5bubble_model = YOLO("khanhromvn/manga_bubble_seg")6reader = easyocr.Reader(['ja','en'])78defprocess_manga_page(image_path):9# 1. Phát hiện bubbles10 results = bubble_model(image_path)1112# 2. Trích xuất text từ mỗi bubble13for i, mask inenumerate(results[0].masks.data):14 bubble_region = extract_bubble(mask)1516# 3. OCR17 text = reader.readtext(bubble_region)1819# 4. Dịch text (sử dụng translation API)20 translated = translate(text, target='vi')2122# 5. Vẽ text đã dịch lên bubble23 draw_text_on_bubble(bubble_region, translated)2425return processed_image
2. Làm Sạch Bubble (Text Removal)
python
1import cv2
2import numpy as np
34defclean_bubble(image, model):5 results = model(image)67for mask in results[0].masks.data:8# Tạo mask binary9 mask_np = mask.cpu().numpy()1011# Inpainting để xóa text12 cleaned = cv2.inpaint(13 image,14 mask_np.astype(np.uint8),15 inpaintRadius=3,16 flags=cv2.INPAINT_TELEA
17)1819return cleaned
3. Text Extraction cho OCR Training
python
1defextract_text_regions(manga_folder, output_folder):2 model = YOLO("khanhromvn/manga_bubble_seg")34for img_path in Path(manga_folder).glob("*.jpg"):5 results = model(str(img_path))6 image = cv2.imread(str(img_path))78for i, box inenumerate(results[0].boxes.data):9 x1, y1, x2, y2 = box[:4].int().tolist()1011# Crop bubble region12 bubble = image[y1:y2, x1:x2]1314# Lưu cho OCR training15 output_path =f"{output_folder}/{img_path.stem}_bubble_{i}.jpg"16 cv2.imwrite(output_path, bubble)
📝 Citation
Nếu bạn sử dụng model này trong nghiên cứu hoặc dự án, vui lòng trích dẫn: