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)
相关推荐
Polar__Star1 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
island13141 小时前
最详细VMware Workstation 17 上安装 Ubuntu 系统
linux·数据库·ubuntu
m0_743623921 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
FreakStudio2 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
Wmenghu2 小时前
Ubuntu手动安装jdk;Ubuntu手动安装Maven;Ubuntu手动安装RocketMQ;Ubuntu手动安装RocketMQ-Dashbo
java·linux·ubuntu
gCode Teacher 格码致知2 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
不要秃头的小孩3 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
科雷软件测试3 小时前
使用python+Midscene.js AI驱动打造企业级WEB自动化解决方案
前端·javascript·python
星越华夏3 小时前
python——三角函数用法
开发语言·python
gmaajt4 小时前
mysql如何检查数据库表是否存在损坏_使用CHECK TABLE命令修复
jvm·数据库·python