用paddleocr库来识别图片中的文字时,总会有几行无关信息,例如:

[2022/09/13 12:01:22] ppocr DEBUG: Namespace(help='==SUPPRESS==', use_gpu=False, use_xpu=False, ir_optim=True, use_tensorrt=False, min_subgraph_size=15, shape_info_filename=None, precision='fp32', gpu_mem=500, image_dir=None, det_algorithm='DB', det_model_dir='C:\\Users\\bdy/.paddleocr/whl\\det\\ch\\ch_PP-OCRv3_det_infer', det_limit_side_len=960, det_limit_type='max', det_db_thresh=0.3, det_db_box_thresh=0.6, det_db_unclip_ratio=1.5, max_batch_size=10, use_dilation=False, det_db_score_mode='fast', det_east_score_thresh=0.8, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_sast_score_thresh=0.5, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_pse_thresh=0, det_pse_box_thresh=0.85, det_pse_min_area=16, det_pse_box_type='quad', det_pse_scale=1, scales=[8, 16, 32], alpha=1.0, beta=1.0, fourier_degree=5, det_fce_box_type='poly', rec_algorithm='SVTR_LCNet', rec_model_dir='C:\\Users\\bdy/.paddleocr/whl\\rec\\ch\\ch_PP-OCRv3_rec_infer', rec_image_shape='3, 48, 320', rec_batch_num=6, max_text_length=25, rec_char_dict_path='D:\\python3.10\\Lib\\site-packages\\paddleocr\\ppocr\\utils\\ppocr_keys_v1.txt', use_space_char=True, vis_font_path='./doc/fonts/simfang.ttf', drop_score=0.5, e2e_algorithm='PGNet', e2e_model_dir=None, e2e_limit_side_len=768, e2e_limit_type='max', e2e_pgnet_score_thresh=0.5, e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_pgnet_valid_set='totaltext', e2e_pgnet_mode='fast', use_angle_cls=True, cls_model_dir='C:\\Users\\bdy/.paddleocr/whl\\cls\\ch_ppocr_mobile_v2.0_cls_infer', cls_image_shape='3, 48, 192', label_list=['0', '180'], cls_batch_num=6, cls_thresh=0.9, enable_mkldnn=False, cpu_threads=10, use_pdserving=False, warmup=False, sr_model_dir=None, sr_image_shape='3, 32, 128', sr_batch_num=1, draw_img_save_dir='./inference_results', save_crop_res=False, crop_res_save_dir='./output', use_mp=False, total_process_num=1, process_id=0, benchmark=False, save_log_path='./log_output/', show_log=True, use_onnx=False, output='./output', table_max_len=488, table_algorithm='TableAttn', table_model_dir=None, merge_no_span_structure=True, table_char_dict_path=None, layout_model_dir=None, layout_dict_path=None, layout_score_threshold=0.5, layout_nms_threshold=0.5, kie_algorithm='LayoutXLM', ser_model_dir=None, ser_dict_path='../train_data/XFUND/class_list_xfun.txt', ocr_order_method=None, mode='structure', image_orientation=False, layout=True, table=True, ocr=True, recovery=False, save_pdf=False, lang='ch', det=True, rec=True, type='ocr', ocr_version='PP-OCRv3', structure_version='PP-Structurev2')
[2022/09/13 12:01:22] ppocr DEBUG: dt_boxes num : 1, elapse : 0.03490591049194336
[2022/09/13 12:01:22] ppocr DEBUG: cls num  : 1, elapse : 0.010002851486206055
[2022/09/13 12:01:23] ppocr DEBUG: rec_res num  : 1, elapse : 0.08177757263183594

这种情况很影响观感,为此我追踪输出这些的语句,并将其注释掉。

直接上结论

到自己的 python 文件夹中,如我的是3.10,来到 D:\python3.10\Lib\logging 文件夹,编辑__init__.py。

可能 logging 文件夹不在 Lib 下,有可能直接在 python 的文件夹中。

    def emit(self, record):"""Emit a record.If a formatter is specified, it is used to format the record.The record is then written to the stream with a trailing newline.  Ifexception information is present, it is formatted usingtraceback.print_exception and appended to the stream.  If the streamhas an 'encoding' attribute, it is used to determine how to do theoutput to the stream."""try:msg = self.format(record)stream = self.stream# issue 35046: merged two stream.writes into one.stream.write(msg + self.terminator)self.flush()except RecursionError:  # See issue 36272raiseexcept Exception:self.handleError(record)

在这里将 stream.write(msg + self.terminator) 注释掉即可。

因为这里是将两次write合并了,而有些版本是有两行 stream.write 的。所以在注释时,要把带有 stream.write 的全部注释掉。

追踪过程

在 pycharm 中,按住 Ctrl ,点击 PaddleOCR,之后默认按住 Ctrl

from paddleocr import PaddleOCR

来到 Paddleocr 中,直接拉到函数末尾,点击 logger.debug(params) 中的 debug

class PaddleOCR(predict_system.TextSystem):def __init__(self, **kwargs):"""paddleocr packageargs:**kwargs: other params show in paddleocr --help"""params = parse_args(mMain=False)params.__dict__.update(**kwargs)assert params.ocr_version in SUPPORT_OCR_MODEL_VERSION, "ocr_version must in {}, but get {}".format(SUPPORT_OCR_MODEL_VERSION, params.ocr_version)params.use_gpu = check_gpu(params.use_gpu)if not params.show_log:logger.setLevel(logging.INFO)self.use_angle_cls = params.use_angle_clslang, det_lang = parse_lang(params.lang)# init model dirdet_model_config = get_model_config('OCR', params.ocr_version, 'det',det_lang)params.det_model_dir, det_url = confirm_model_dir_url(params.det_model_dir,os.path.join(BASE_DIR, 'whl', 'det', det_lang),det_model_config['url'])rec_model_config = get_model_config('OCR', params.ocr_version, 'rec',lang)params.rec_model_dir, rec_url = confirm_model_dir_url(params.rec_model_dir,os.path.join(BASE_DIR, 'whl', 'rec', lang), rec_model_config['url'])cls_model_config = get_model_config('OCR', params.ocr_version, 'cls','ch')params.cls_model_dir, cls_url = confirm_model_dir_url(params.cls_model_dir,os.path.join(BASE_DIR, 'whl', 'cls'), cls_model_config['url'])if params.ocr_version == 'PP-OCRv3':params.rec_image_shape = "3, 48, 320"else:params.rec_image_shape = "3, 32, 320"# download modelmaybe_download(params.det_model_dir, det_url)maybe_download(params.rec_model_dir, rec_url)maybe_download(params.cls_model_dir, cls_url)if params.det_algorithm not in SUPPORT_DET_MODEL:logger.error('det_algorithm must in {}'.format(SUPPORT_DET_MODEL))sys.exit(0)if params.rec_algorithm not in SUPPORT_REC_MODEL:logger.error('rec_algorithm must in {}'.format(SUPPORT_REC_MODEL))sys.exit(0)if params.rec_char_dict_path is None:params.rec_char_dict_path = str(Path(__file__).parent / rec_model_config['dict_path'])logger.debug(params)# init det_model and rec_modelsuper().__init__(params)

但是这是我们发现,有很多 debug 函数,这里我们选择第三个

这里点击 self._log(DEBUG, msg, args, **kwargs) 中的 _log

    def debug(self, msg, *args, **kwargs):"""Log 'msg % args' with severity 'DEBUG'.To pass exception information, use the keyword argument exc_info witha true value, e.g.logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)"""if self.isEnabledFor(DEBUG):self._log(DEBUG, msg, args, **kwargs)

拉到最后,点击 self.handle(record) 中的 handle(这个就在 _log 的正下方)

    def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False,stacklevel=1):"""Low-level logging routine which creates a LogRecord and then callsall the handlers of this logger to handle the record."""sinfo = Noneif _srcfile:#IronPython doesn't track Python frames, so findCaller raises an#exception on some versions of IronPython. We trap it here so that#IronPython can use logging.try:fn, lno, func, sinfo = self.findCaller(stack_info, stacklevel)except ValueError: # pragma: no coverfn, lno, func = "(unknown file)", 0, "(unknown function)"else: # pragma: no coverfn, lno, func = "(unknown file)", 0, "(unknown function)"if exc_info:if isinstance(exc_info, BaseException):exc_info = (type(exc_info), exc_info, exc_info.__traceback__)elif not isinstance(exc_info, tuple):exc_info = sys.exc_info()record = self.makeRecord(self.name, level, fn, lno, msg, args,exc_info, func, extra, sinfo)self.handle(record)

点击 callHandlers

    def handle(self, record):"""Call the handlers for the specified record.This method is used for unpickled records received from a socket, aswell as those created locally. Logger-level filtering is applied."""if (not self.disabled) and self.filter(record):self.callHandlers(record)

在 callHandlers 中找到如下代码段,点击这里中间的 handle

        while c:for hdlr in c.handlers:found = found + 1if record.levelno >= hdlr.level:hdlr.handle(record)if not c.propagate:c = None    #break outelse:c = c.parent

点击 emit

    def handle(self, record):"""Conditionally emit the specified logging record.Emission depends on filters which may have been added to the handler.Wrap the actual emission of the record with acquisition/release ofthe I/O thread lock. Returns whether the filter passed the record foremission."""rv = self.filter(record)if rv:self.acquire()try:self.emit(record)finally:self.release()return rv

这里我们发现只会返回异常,可以确定这不是我们要找的 emit

    def emit(self, record):"""Do whatever it takes to actually log the specified logging record.This version is intended to be implemented by subclasses and soraises a NotImplementedError."""raise NotImplementedError('emit must be implemented ''by Handler subclasses')

这里要点左面带下箭头的蓝点,选择第一个 emit

最后就如上面结论修改即可

Paddleocr 去除无关信息打印相关推荐

  1. muduo网络库学习(九)日志类Logger和LogStream,将日志信息打印到屏幕

    每一个成熟的项目都有大大小小的日志系统,在关键的地方打印日志信息,常用来跟踪程序运行,查找错误原因等,可以节省大量的debug时间 muduo的日志信息有5个级别 TRACE,细粒度最高的日志信息,打 ...

  2. jni程序中获取签名信息打印截断问题

    在jni获取的签名信息打印出来与java层获取的不一样,少了一部分,验证jni程序LOG最长只能打印1024字节,因此不是获取错误,只是打印限制. 转载于:https://www.cnblogs.co ...

  3. 乐鑫esp8266学习rtos3.0笔记第11篇:详细分析Esp8266上电信息打印的数据,如何做到串口通讯上电不乱码打印。

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,不做开发板.仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 序号 SDK版本 内容 链接 1 nonos2.0 搭建 ...

  4. C语言函数:错误信息打印函数,strerror()与perror()

    C语言函数:错误信息打印函数,strerror()与perror() strerror: C语言函数: 字符串函数及模拟实现strtok().strstr().strerror()_srhqwe的博客 ...

  5. SWO引脚配置覆盖, 导致ITM信息打印失效

    试验原因 在STM32F407上作SPI方式的SD卡试验, 因为手头没有产品板子,就找了一个开发板作试验. SD卡驱动库在其他开发板上验证过了好使. 但是在这个板子上SD卡初始化失败. 看现在接上这块 ...

  6. 学生信息打印辅助系统

    学生信息打印辅助系统 #详情访问:https://github.com/tongxunkeji/student_info_system 如果你是在校师生你的日常是否也和我一样,混迹在各种,学籍证明.转 ...

  7. 多线程编程:模拟商店对某件商品的进货与销售过程并将相关信息打印出来

    Java 多线程 利用线程通知机制编写一个Java多线程程序,模拟商店对某件商品的进货与销售过程并将相关信息打印出来,具体要求如下: 进货与销售过程各由一个线程模拟:当商品数目少于10时进货,进货数目 ...

  8. android 串口开发_详细分析Esp8266上电信息打印的数据,如何做到串口通讯上电不乱码打印...

    01 写在前面: 上篇关于如何在内置仅1M的Esp8285做到 OTA 升级的同步到微信公众号,竟然被安信可的某些运维人员看到了,想要转载,我很欣慰,竟然自己的笔记可以被这么大型的公司员工认可! 我是 ...

  9. 引入spring-boot-starter-actuator,控制台没有mapper的映射信息打印问题

    需要说明的是: springboot1.X中引入actuator依赖后我们就可以直接在控制台发现所有监控点的映射信息 如下: springboot1.X版本访问时可能需要将安全管理关闭: 需要在app ...

最新文章

  1. Android Studio javadoc 生成注释文档
  2. 全球及中国雪地摩托护目镜行业经营模式分析及未来发展动向分析报告2022-2027年版
  3. bzoj3299 [USACO2011 Open]Corn Maze玉米迷宫
  4. substr(IndexAction,1,-6)
  5. 4.无监督学习--K-means聚类
  6. iOSUIImage变为NSData并进行压缩
  7. vss登录invalid handle问题的解决办法
  8. linux开启权限继承,linux的一个权限问题(权限继承)
  9. sentinel3数据批量下载——sentinelsat
  10. 从物理页面的争抢看linux内核内存管理
  11. 深蓝学院【视觉SLAM十四讲】汇总
  12. linux 下kali linux 中使用hydra 进行对虚拟机中win10系统的密码破解
  13. 网络流——基础,Dinic和Sap(Gap优化)算法
  14. Oracle19c 出现 ora-12514
  15. LA-3713-TwoSAT
  16. vuecli 实现导航切换
  17. Dialogs 介绍
  18. 孙振耀担任海辉董事会主席 自2008年3月生效
  19. java程序员面试前的准备你了解吗?
  20. 华为p9 Android6 备份,华为手机怎么备份?华为手机备份数据教程

热门文章

  1. 使用vb对注册机进行编程
  2. Java入门小练习:征婚条件
  3. el-table 动态生成多级表头
  4. DM3730应用程序自启动解决方案
  5. 【自己动手写CPU】除法指令的实现
  6. 电商项目实战第五节: CSS3+HTML5+JS 设计案例【考拉海购网站】之【商品栏及右侧垂直导航】
  7. ECSTORE 目录架构
  8. 机械工程材料课程考试复习题及参考答案
  9. 伍德里奇《计量经济学导论》第5版笔记和课后答案
  10. 迅为龙芯2K1000开发板Linux环境变量