add main.py
Browse files- assets/ja.jpg +0 -3
- main.py +65 -0
- readme.txt +1 -0
assets/ja.jpg
DELETED
Git LFS Details
|
main.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import json, os, sys, time, io
|
| 3 |
+
import os.path as osp
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import PIL
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# see utils\io_utils.py
|
| 11 |
+
def imread(imgpath, read_type=cv2.IMREAD_COLOR, max_retry_limit=5, retry_interval=0.1):
|
| 12 |
+
if not osp.exists(imgpath):
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
num_tries = 0
|
| 16 |
+
while True:
|
| 17 |
+
try:
|
| 18 |
+
img = Image.open(imgpath)
|
| 19 |
+
if read_type == cv2.IMREAD_GRAYSCALE:
|
| 20 |
+
img = img.convert('L')
|
| 21 |
+
img = np.array(img)
|
| 22 |
+
if read_type != cv2.IMREAD_GRAYSCALE:
|
| 23 |
+
if img.ndim == 3 and img.shape[-1] == 1:
|
| 24 |
+
img = img[..., :2]
|
| 25 |
+
if img.ndim == 2:
|
| 26 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 27 |
+
|
| 28 |
+
if img.ndim == 3 and img.shape[-1] == 4:
|
| 29 |
+
if np.all(img[..., -1] == 255):
|
| 30 |
+
img = np.ascontiguousarray(img[..., :3])
|
| 31 |
+
break
|
| 32 |
+
except PIL.UnidentifiedImageError as e:
|
| 33 |
+
# IMG I/O thread might not finished yet
|
| 34 |
+
num_tries += 1
|
| 35 |
+
if max_retry_limit is not None and num_tries >= max_retry_limit:
|
| 36 |
+
return None
|
| 37 |
+
time.sleep(retry_interval)
|
| 38 |
+
|
| 39 |
+
return img
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if __name__ == '__main__':
|
| 44 |
+
|
| 45 |
+
from modules.textdetector.ctd.inference import TextDetector as CTDModel
|
| 46 |
+
|
| 47 |
+
CTD_ONNX_PATH = 'data/models/comictextdetector.pt.onnx'
|
| 48 |
+
device = 'cpu'
|
| 49 |
+
detect_size = 1280
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# model = load_ctd_model(CTD_ONNX_PATH, device, detect_size)
|
| 54 |
+
|
| 55 |
+
img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg')
|
| 56 |
+
|
| 57 |
+
# All text detectors only support 3 channels input
|
| 58 |
+
if img.ndim == 3 and img.shape[2] == 4:
|
| 59 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
|
readme.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
|
|
|
|
| 2 |
|
| 3 |
vi .vscode\settings.json
|
| 4 |
{
|
|
|
|
| 1 |
|
| 2 |
+
# vscode 直接运行 launch.py
|
| 3 |
|
| 4 |
vi .vscode\settings.json
|
| 5 |
{
|