| |
| |
|
|
|
|
|
|
| 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) |
|
|
|
|
| import json, os, sys, time, io |
| import os.path as osp |
|
|
| from PIL import Image |
| import PIL |
| import cv2 |
| import numpy as np |
|
|
| is_debug = True |
|
|
| dic_cache = {} |
|
|
| from flask import Flask, request, jsonify |
|
|
| app = Flask(__name__) |
|
|
| import base64 |
| import math, re, uuid |
|
|
| def save_json(filename, dics): |
| with open(filename, 'w', encoding='utf-8') as fp: |
| json.dump(dics, fp, indent=4, ensure_ascii=False) |
| fp.close() |
|
|
| def load_json(filename): |
| with open(filename, encoding='utf-8') as fp: |
| js = json.load(fp) |
| fp.close() |
| return js |
|
|
| def jsonparse(s): |
| return json.loads(s, strict=False) |
|
|
| def jsonstring(d): |
| return json.dumps(d, ensure_ascii=False) |
|
|
| def show_img(image, target_width=400): |
| |
| original_height, original_width = image.shape[:2] |
| |
| |
| scale = target_width / original_width |
| target_height = int(original_height * scale) |
| |
| |
| resized_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA) |
| cv2.imshow("green", resized_image) |
| cv2.waitKey(0) |
| return resized_image |
|
|
| |
| 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] |
|
|
| def ocr(img): |
|
|
| |
| 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() |
|
|
|
|
| results = [] |
|
|
| |
| for blk in blk_list: |
| texts = blk.text |
| lines = blk.lines |
| results.append( { "texts": texts, "lines":lines } ) |
| for line in blk.lines: |
| img_draw = cv2.rectangle(img_draw, line[0], line[2], (0, 0, 255), 2) |
|
|
| jsn = { "width": img.shape[1], "height": img.shape[0], "prism_wordsInfo": [] } |
| for result in results: |
| texts, lines = ( result["texts"], result["lines"]) |
| word = ''.join(texts) |
| pos = [] |
| charInfo = [] |
| |
| min_x = 999 |
| min_y = 999 |
| max_x = -1 |
| max_y = -1 |
|
|
| for text, line in zip(texts, lines): |
| lu = line[0] |
| ru = line[1] |
| rd = line[2] |
| ld = line[3] |
|
|
| minx = min(lu[0], ld[0]) |
| maxx = max(ru[0], rd[0]) |
| miny = min(lu[1], ru[1]) |
| maxy = max(rd[1], ld[1]) |
|
|
| if min_x > minx: |
| min_x = minx |
| if max_x < maxx: |
| max_x = maxx |
|
|
| if min_y > miny: |
| min_y = miny |
| if max_y < maxy: |
| max_y = maxy |
|
|
| for c in text: |
| charInfo.append( {"word": c, "x":minx , "y":miny, "w":maxx - minx , "h":maxy - miny, "guid": str( uuid.uuid4() ), "isDeleted": 0 } ) |
| pass |
| |
| pos = [ { "x":min_x, "y":min_y }, { "x":max_x, "y":min_y }, { "x":max_x, "y":max_y }, { "x":min_x, "y":max_y } ] |
|
|
| jsn["prism_wordsInfo"].append( { "word":word, "x":min_x, "y":min_y, "width":max_x - min_x, "height":max_y - min_y, "pos":pos, "charInfo":charInfo} ) |
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| return jsn, img_draw |
|
|
| @app.route('/comicocr', methods=['post']) |
| def comicocr(): |
| global dic_cache |
|
|
| |
| |
|
|
|
|
| img_b64_str = request.json['img'] |
| img_bytes = base64.b64decode(img_b64_str) |
| |
| imgData = np.frombuffer(img_bytes, dtype=np.uint8) |
| img = cv2.imdecode(imgData, -1) |
|
|
| |
| if img.ndim == 3 and img.shape[2] == 4: |
| img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB) |
|
|
| |
| |
| |
|
|
| jsn, img_draw = ocr(img) |
| |
| return jsonify(jsn) |
|
|
|
|
| def main(): |
| if is_debug: |
| img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg') |
| jsn, img_draw = ocr(img) |
|
|
| cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw) |
|
|
| else: |
| app.run(host="0.0.0.0", port=2393, debug=True) |
| |
| return |
|
|
| 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 |
|
|
|
|
| if __name__ == '__main__': |
| main() |
| |
|
|
|
|