File size: 4,305 Bytes
72a6365 2e48a13 72a6365 0719fa8 72a6365 0719fa8 72a6365 0719fa8 72a6365 0719fa8 72a6365 2e48a13 921dd3d 0719fa8 2e48a13 921dd3d 0719fa8 72a6365 2e48a13 72a6365 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import json, os, sys, time, io
import os.path as osp
from PIL import Image
import PIL
import cv2
import numpy as np
# see utils\io_utils.py
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:
# IMG I/O thread might not finished yet
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')
# All text detectors only support 3 channels input
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)) # 创建一个椭圆形的结构元素(kernel),用于后续的形态学操作 # 元素的尺寸 # (ksize, ksize) :椭圆的锚点(中心点)
mask = cv2.dilate(mask, element) # 对 mask 图像进行膨胀操作(dilate),使用上面创建的椭圆结构元素。膨胀操作可以让白色区域(通常是前景或目标区域)变大,常用于去除小的黑洞、连接断开的区域等。
for blk in blk_list:
blk.det_model = 'ctd'
need_save_mask = True
detect_counter = 0
detect_counter += 1
# self.ocr.run_ocr(img, blk_list)
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()
# from qtpy.QtWidgets import QApplication
# from qtpy.QtGui import QIcon, QFontDatabase, QGuiApplication, QFont, QFontMetrics
# ui\mainwindow.py
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) # 在一行坚排文字的左边画一条红线
# app_font = QFont('Microsoft YaHei UI')
# fontMetrics = QFontMetrics(app_font)
# rect = fontMetrics.boundingRect(text[0])
# textWidth = rect.width()
pass
# blk.text = self.ocrSubWidget.sub_text(text)
cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw)
pass
|