ubuntu OCR 脚本

1. 百度 PaddleOCR 介绍

2. 环境安装

shell 复制代码
pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
# 进入 https://github.com/PaddlePaddle/PaddleOCR 
# 这里有个 requirements.txt
pip install paddleocr -i https://mirror.baidu.com/pypi/simple
pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple

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

python 复制代码
#!/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 <path> or <path/file>" % 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 小时前
Flask 核心基础:从 路由装饰器 到 __name__ 变量 的底层逻辑解析
python·flask
njxiejing1 小时前
Python NumPy安装、导入与入门
开发语言·python·numpy
SabreWulf20201 小时前
Ubuntu 20.04手动安装.NET 8 SDK
linux·ubuntu·avalonia·.net8
不是吧这都有重名1 小时前
为什么ubuntu大文件拷贝会先快后慢?
linux·运维·ubuntu
Rhys..2 小时前
Python&Flask 使用 DBUtils 创建通用连接池
开发语言·python·mysql
Just_Paranoid2 小时前
【Python Tkinter】图形用户界面(GUI)开发及打包EXE指南
python·gui·tkinter·pyinstaller
小宁爱Python2 小时前
Django 基础入门:命令、结构与核心配置全解析
后端·python·django
闲人编程3 小时前
Flask 前后端分离架构实现支付宝电脑网站支付功能
python·架构·flask·支付宝·前后端·网站支付·apl
996终结者3 小时前
同类软件对比(四):Jupyter vs PyCharm vs VS Code:Python开发工具终极选择指南
vscode·python·jupyter·pycharm·visual studio code
果壳~3 小时前
【Python】爬虫html提取内容基础,bs4
爬虫·python·html