|
|
| import json, os, sys, time, io |
| import os.path as osp |
|
|
| from PIL import Image |
| import PIL |
| import cv2 |
| import numpy as np |
|
|
| |
| def imread(imgpath, read_type=cv2.IMREAD_COLOR, max_retry_limit=5, retry_interval=0.1): |
| if not osp.exists(imgpath): |
| return None |
| |
| num_tries = 0 |
| while True: |
| try: |
| img = Image.open(imgpath) |
| if read_type == cv2.IMREAD_GRAYSCALE: |
| img = img.convert('L') |
| img = np.array(img) |
| if read_type != cv2.IMREAD_GRAYSCALE: |
| if img.ndim == 3 and img.shape[-1] == 1: |
| img = img[..., :2] |
| if img.ndim == 2: |
| img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) |
|
|
| if img.ndim == 3 and img.shape[-1] == 4: |
| if np.all(img[..., -1] == 255): |
| img = np.ascontiguousarray(img[..., :3]) |
| break |
| except PIL.UnidentifiedImageError as e: |
| |
| num_tries += 1 |
| if max_retry_limit is not None and num_tries >= max_retry_limit: |
| return None |
| time.sleep(retry_interval) |
| |
| return img |
|
|
| def chunks(lst, n): |
| """Yield successive n-sized chunks from lst.""" |
| for i in range(0, len(lst), n): |
| yield lst[i:i + n] |
|
|
| if __name__ == '__main__': |
|
|
| from modules.textdetector.ctd.inference import TextDetector as CTDModel |
| from modules.ocr.mit48px import Model48pxOCR |
| |
| |
|
|
| CTD_ONNX_PATH = 'data/models/comictextdetector.pt.onnx' |
| device = 'cpu' |
| detect_size = 1280 |
|
|
| ctd_model = CTDModel(CTD_ONNX_PATH, detect_size=detect_size, device=device) |
|
|
|
|
| OCR48PXMODEL_PATH = 'data/models/ocr_ar_48px.ckpt' |
| ocr_model = Model48pxOCR(OCR48PXMODEL_PATH, device) |
|
|
|
|
| img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg') |
| |
| |
| if img.ndim == 3 and img.shape[2] == 4: |
| img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB) |
|
|
| _, mask, blk_list = ctd_model(img) |
|
|
| fnt_rsz = 1.0 |
| fnt_max = -1 |
| fnt_min = -1 |
| for blk in blk_list: |
| sz = blk._detected_font_size * fnt_rsz |
| if fnt_max > 0: |
| sz = min(fnt_max, sz) |
| if fnt_min > 0: |
| sz = max(fnt_min, sz) |
| blk.font_size = sz |
| blk._detected_font_size = sz |
|
|
| ksize = 2 |
| if ksize > 0: |
| element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * ksize + 1, 2 * ksize + 1),(ksize, ksize)) |
| mask = cv2.dilate(mask, element) |
|
|
| for blk in blk_list: |
| blk.det_model = 'ctd' |
|
|
| need_save_mask = True |
| detect_counter = 0 |
|
|
| detect_counter += 1 |
|
|
| |
|
|
|
|
| for blk in blk_list: |
| blk.text = [] |
|
|
| split_textblk = False |
| seg_func = None |
|
|
| model_text_height = 48 |
| model_maxwidth = 8100 |
|
|
| from utils.textblock import collect_textblock_regions |
|
|
| chunk_size = 16 |
|
|
| regions, textblk_lst_indices = collect_textblock_regions(img, blk_list, model_text_height, model_maxwidth, split_textblk, seg_func) |
|
|
|
|
| ocr_model(blk_list, regions, textblk_lst_indices, chunk_size=chunk_size) |
|
|
|
|
| img_draw = img.copy() |
|
|
|
|
| |
| |
|
|
| |
| for blk in blk_list: |
| text = blk.get_text() |
| for line in blk.lines: |
| img_draw = cv2.rectangle(img_draw, line[0], line[3], (0, 0, 255), 2) |
| |
| |
| |
|
|
| |
| |
| |
| pass |
| |
|
|
|
|
| cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw) |
|
|
|
|
| pass |
|
|
|
|
|
|