• ubuntu OCR 脚本


    1. 百度 PaddleOCR 介绍

    2. 环境安装

    pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install paddleocr --upgrade PyMuPDF==1.23.8 -i https://mirror.baidu.com/pypi/simple
    # 进入 https://github.com/PaddlePaddle/PaddleOCR 有个 requirements.txt #把PyMuPDF一行注释掉
    pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple 
    
    • 1
    • 2
    • 3
    • 4

    3. 用法: my_ocr.py 图片或文件夹

    #!/bin/env python
    import os
    import sys
    import time
    import logging                    # 关闭WARNING
    from tqdm.auto import trange      # 进度条
    from paddleocr import PaddleOCR   # 百度识别
    
    logging.disable(logging.DEBUG)    # 关闭DEBUG日志的打印
    logging.disable(logging.WARNING)  # 关闭WARNING日志的打印
    
    file = "out.txt"
    
    def ocr_imgs(img):
        result = ocr.ocr(img, cls=False)
        if len(result[0]) == 0:
            result = ocr.ocr(img, cls=False, det=False)
        with open(file, 'a') as f:
            f.write(f'\n{img.center(50, "-")}\n')
            for idx in range(len(result)):
                res = result[idx]
                for line in res:
                    if isinstance(line, list):
                        f.write(f'{line[-1][0]}\n')
                    elif isinstance(line, tuple):
                        f.write(f'{line[0]}\n')
            # f.flush()
    
    def check_args():
        if len(sys.argv) < 2:
            print("Usage: %s  or " % sys.argv[0])
            exit()
    
        arg = sys.argv[1]
        if os.path.isfile(arg):
            arg = os.path.dirname(arg)
            single_file = True
        elif os.path.isdir(arg):
            single_file = False
        
        os.chdir(arg)
        os.remove(file) if os.path.exists(file) else False
        return single_file
    
    ########################################################################
    if __name__ == "__main__":
        print(f"[{time.strftime('%X')}] 识别开始...")
        start = time.time()
        imagelist = [os.path.basename(sys.argv[1])] if check_args() else list(filter(os.path.isfile, os.listdir()))
        imagelist.sort(key=str.lower)
        ocr = PaddleOCR(use_angle_cls=False, lang="ch")  # use_angle_cls 竖文字
        for i in trange(len(imagelist),leave=False):
            image = imagelist[i]
            fn, ex = os.path.splitext(image)
            if ex in ['.jpg', '.jpeg', '.png']:  # bmp/webp/tiff/svg/gif
                ocr_imgs(image)
    
        end = time.time()
        run_time = round(end - start)
        print(f"[{time.strftime('%X')}] 结束耗时{run_time}秒")
    
        cmd="gedit " + file + "&"
        os.system(cmd)
    
    • 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
  • 相关阅读:
    【uniapp】使用扫码插件,解决uni.scanCode扫码效率低的问题
    家用摄像头选择
    CPP语法(六)——函数模板
    【C++面向对象】1. 类、对象
    KubeVela 1.3 发布:开箱即用的可视化应用交付平台,引入插件生态、权限认证、版本化等企业级新特性
    golang记录一下Reflect包Type类型NumMethod的一个小问题
    Fiddler 系列教程(一)初识Fiddler,我们能用fiddler做什么?
    Java序列化方式
    OB Cloud助力泡泡玛特打造新一代分布式抽盒机系统
    资料 - hawei EulerOS的主页入口
  • 原文地址:https://blog.csdn.net/weixin_41799721/article/details/132626814