From a74f79962b3af713e99c06d00442abc498a18065 Mon Sep 17 00:00:00 2001 From: SWHL Date: Sat, 5 Aug 2023 18:25:51 +0800 Subject: [PATCH] Fixed index error and added logging module. --- README.md | 6 +++--- docs/README_zh.md | 7 +++---- rapid_videocr/logger.py | 34 ++++++++++++++++++++++++++++++++++ rapid_videocr/main.py | 24 ++++++++++-------------- rapid_videocr/rapid_videocr.py | 25 +++++++------------------ rapid_videocr/utils.py | 33 --------------------------------- requirements.txt | 2 +- 7 files changed, 58 insertions(+), 73 deletions(-) create mode 100644 rapid_videocr/logger.py diff --git a/README.md b/README.md index b0e24cb..f9147b2 100644 --- a/README.md +++ b/README.md @@ -53,15 +53,15 @@ flowchart LR - [☆☆☆ RapidVideOCR Advanced Tutorial (Partners with python foundation)](https://github.com/SWHL/RapidVideOCR/wiki/RapidVideOCR%E9%AB%98%E7%BA%A7%E6%95%99%E7%A8%8B%EF%BC%88%E6%9C%89python%E5%9F%BA%E7%A1%80%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4%EF%BC%89) ### Change log ([more](https://github.com/SWHL/RapidVideOCR/wiki/Changelog)) +- ♦ 2023-08-05 v2.2.4 update: + - Fixed index error in concat_rec_mode. + - Add logging module for easy feedback. - 🛶 2023-07-19 v2.2.3 update: - Increase the adaptation of VSF parameters. During command line mode and class initialization, it is possible to specify parameters with the same name for VSF commands. - 🤓2023-07-08 v2.2.2 update: - Fixed the problem that the Chinese path could not be read during batch recognition - Skip issue in SRT when fixing missing axes. At present, when a certain axis fails to be recognized, the position will be vacated, which is convenient for proofreading. - Keep the vsf results. -- 🐲2023-06-22 Desktop v0.0.3 update: - - Integrate VideoSubFinder interface, increase video batch processing. - - Optimize the problem of path preservation after multiple selections. ### Announce For international developers, we regard [Discussions](https://github.com/SWHL/RapidVideOCR/discussions) as our international community platform. All ideas and questions can be discussed here in English. \ No newline at end of file diff --git a/docs/README_zh.md b/docs/README_zh.md index b94da97..34be0b6 100644 --- a/docs/README_zh.md +++ b/docs/README_zh.md @@ -56,16 +56,15 @@ flowchart LR - [RapidVideOCR高级教程(有python基础的小伙伴)](https://github.com/SWHL/RapidVideOCR/wiki/RapidVideOCR%E9%AB%98%E7%BA%A7%E6%95%99%E7%A8%8B%EF%BC%88%E6%9C%89python%E5%9F%BA%E7%A1%80%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4%EF%BC%89) ### 更新日志([more](https://github.com/SWHL/RapidVideOCR/wiki/%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97)) +- ♦ 2023-08-05 v2.2.4 update: + - 修复批量识别模式下,索引错误。 + - 添加日志记录模块,便于使用桌面版,快速记录问题,便于反馈。 - 🛶2023-07-19 v2.2.3 update: - 增加对VSF的参数的适配,命令行模式和类初始化时,可以指定VSF命令的同名参数。详细使用参见[link](https://github.com/SWHL/RapidVideOCR/wiki/RapidVideOCR%E9%AB%98%E7%BA%A7%E6%95%99%E7%A8%8B%EF%BC%88%E6%9C%89python%E5%9F%BA%E7%A1%80%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4%EF%BC%89) - 🤓2023-07-08 v2.2.2 update: - 修复批量识别时,不能读取中文路径的问题 - 修复漏轴时,SRT中跳过问题。目前当出现某一轴未能识别,则会空出位置,便于校对。 - 保留VSF识别的中间结果 -- 🐲2023-06-22 Desktop v0.0.3 update: - - 整合VideoSubFinder界面,增加视频批处理 - - 优化多次选取之后,路径保存问题 - ### 写在最后 - 微信扫描以下二维码,关注**RapidAI公众号**,回复video即可加入RapidVideOCR微信交流群: diff --git a/rapid_videocr/logger.py b/rapid_videocr/logger.py new file mode 100644 index 0000000..341b33a --- /dev/null +++ b/rapid_videocr/logger.py @@ -0,0 +1,34 @@ +# -*- encoding: utf-8 -*- +# @Author: SWHL +# @Contact: liekkaskono@163.com +import functools +import sys +from pathlib import Path + +from loguru import logger + + +@functools.lru_cache() +def get_logger(save_dir: str = "."): + loguru_format = ( + "{time:YYYY-MM-DD HH:mm:ss} | " + "{level: <8} | " + "{name}:{line} - {message}" + ) + + logger.remove() + logger.add( + sys.stderr, + format=loguru_format, + level="INFO", + enqueue=True, + ) + + file_name = "{time:YYYY-MM-DD-HH-mm-ss}.log" + save_file = Path(save_dir) / file_name + logger.add(save_file, rotation=None, retention="5 days") + return logger + + +log_dir = Path(__file__).resolve().parent / "log" +logger = get_logger(str(log_dir)) diff --git a/rapid_videocr/main.py b/rapid_videocr/main.py index 3d96c68..b6f38be 100644 --- a/rapid_videocr/main.py +++ b/rapid_videocr/main.py @@ -6,11 +6,13 @@ from typing import Optional try: + from .logger import logger from .rapid_videocr import RapidVideOCR - from .utils import float_range, get_logger + from .utils import float_range from .video_sub_finder import VideoSubFinder except: - from utils import float_range, get_logger + from logger import logger + from utils import float_range from video_sub_finder import VideoSubFinder from rapid_videocr import RapidVideOCR @@ -72,7 +74,6 @@ def __init__( is_print_console=print_console, ) self.video_formats = [".mp4", ".avi", ".mov", ".mkv"] - self.logger = get_logger() def __call__(self, video_path: str, output_dir: str = "outputs"): if Path(video_path).is_dir(): @@ -83,16 +84,13 @@ def __call__(self, video_path: str, output_dir: str = "outputs"): else: video_list = [video_path] - self.logger.info( + logger.info( "Extracting subtitle images with VideoSubFinder (takes quite a long time) ..." ) video_num = len(video_list) for i, one_video in enumerate(video_list): - self.logger.info( - "[%s/%s] Starting to extract %s key frame", - i + 1, - video_num, - one_video, + logger.info( + f"[{i+1}/{video_num}] Starting to extract {one_video} key frame", ) save_name = Path(one_video).stem @@ -102,16 +100,14 @@ def __call__(self, video_path: str, output_dir: str = "outputs"): try: self.vsf(str(one_video), str(save_vsf_dir)) except Exception as e: - self.logger.error("Extract %s error, %s, skip", one_video, e) + logger.error(f"Extract {one_video} error, {e}, skip") continue - self.logger.info( - "[%s/%s] Starting to run %s ocr", i + 1, video_num, one_video - ) + logger.info(f"[{i+1}/{video_num}] Starting to run {one_video} ocr") rgb_dir = Path(save_vsf_dir) / "RGBImages" if not list(rgb_dir.iterdir()): - self.logger.warning("Extracting frames from %s is 0, skip", one_video) + logger.warning(f"Extracting frames from {one_video} is 0, skip") continue self.video_ocr(rgb_dir, save_dir, save_name=save_name) diff --git a/rapid_videocr/rapid_videocr.py b/rapid_videocr/rapid_videocr.py index 1b4d8e7..e18b5da 100644 --- a/rapid_videocr/rapid_videocr.py +++ b/rapid_videocr/rapid_videocr.py @@ -11,24 +11,13 @@ from tqdm import tqdm try: - from .utils import ( - CropByProject, - compute_poly_iou, - get_logger, - is_inclusive_each_other, - mkdir, - ) + from .logger import logger + from .utils import CropByProject, compute_poly_iou, is_inclusive_each_other, mkdir except: - from utils import ( - CropByProject, - compute_poly_iou, - get_logger, - is_inclusive_each_other, - mkdir, - ) + from logger import logger + from utils import CropByProject, compute_poly_iou, is_inclusive_each_other, mkdir CUR_DIR = Path(__file__).resolve().parent -logger = get_logger() class RapidVideOCR: @@ -188,7 +177,7 @@ def get_match_results( box_iou = compute_poly_iou(frame_boxes, dt_box) if is_inclusive_each_other(frame_boxes, dt_box) or box_iou > 0.1: - matched_path = img_paths[idx] + matched_path = img_paths[i] match_dict.setdefault(i, []).append( [matched_path, dt_box, rec_res[idx]] ) @@ -302,7 +291,7 @@ def export_file( self.save_file(srt_path, srt_result) else: raise ValueError(f"The {self.out_format} dost not support.") - logger.info("[OCR] The result has been saved to %s directory.", save_dir) + logger.info(f"[OCR] The result has been saved to {save_dir} directory.") def print_console(self, txt_result: List) -> None: for v in txt_result: @@ -319,7 +308,7 @@ def save_file(save_path: Union[str, Path], content: List, mode: str = "w") -> No with open(save_path, mode, encoding="utf-8") as f: for value in content: f.write(f"{value}\n") - logger.info("[OCR] The file has been saved in the %s", save_path) + logger.info(f"[OCR] The file has been saved in the {save_path}") @staticmethod def _compute_centroid(points: np.ndarray) -> List: diff --git a/rapid_videocr/utils.py b/rapid_videocr/utils.py index 7d47118..ec5289c 100644 --- a/rapid_videocr/utils.py +++ b/rapid_videocr/utils.py @@ -2,14 +2,10 @@ # @Author: SWHL # @Contact: liekkaskono@163.com import argparse -import functools -import logging -import sys from enum import Enum from pathlib import Path from typing import List, Union -import colorlog import cv2 import numpy as np import shapely @@ -83,35 +79,6 @@ def read_txt(txt_path: Union[str, Path]) -> List[str]: return data -@functools.lru_cache() -def get_logger(name="rapid_videocr"): - logger = logging.getLogger(name) - if name in logger_initialized: - return logger - - for logger_name in logger_initialized: - if name.startswith(logger_name): - return logger - - fmt_string = "%(log_color)s[%(asctime)s] [%(name)s] %(levelname)s: %(message)s" - log_colors = { - "DEBUG": "white", - "INFO": "green", - "WARNING": "yellow", - "ERROR": "red", - "CRITICAL": "purple", - } - fmt = colorlog.ColoredFormatter(fmt_string, log_colors=log_colors) - stream_handler = logging.StreamHandler(stream=sys.stdout) - stream_handler.setFormatter(fmt) - logger.addHandler(stream_handler) - - logger.setLevel(logging.INFO) - logger_initialized[name] = True - logger.propagate = False - return logger - - def compute_poly_iou(a: np.ndarray, b: np.ndarray) -> float: """计算两个多边形的IOU diff --git a/requirements.txt b/requirements.txt index 7f9a324..513b7a2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ tqdm rapidocr_onnxruntime>=1.2.2 -colorlog +loguru \ No newline at end of file