安装EasyOCR
C:\Users\Administrator>pip install easyocr
Collecting easyocrDownloading easyocr-1.7.1-py3-none-any.whl.metadata (11 kB)
Collecting torch (from easyocr)Downloading torch-2.4.0-cp312-cp312-win_amd64.whl.metadata (27 kB)
Collecting torchvision>=0.5 (from easyocr)Downloading torchvision-0.19.0-1-cp312-cp312-win_amd64.whl.metadata (6.1 kB)
Collecting opencv-python-headless (from easyocr)Downloading opencv_python_headless-4.10.0.84-cp37-abi3-win_amd64.whl.metadata (20 kB)
Collecting scipy (from easyocr)Downloading scipy-1.14.0-cp312-cp312-win_amd64.whl.metadata (60 kB)---------------------------------------- 60.8/60.8 kB 1.6 MB/s eta 0:00:00
Collecting numpy (from easyocr)Downloading numpy-2.0.1-cp312-cp312-win_amd64.whl.metadata (60 kB)---------------------------------------- 60.9/60.9 kB 817.1 kB/s eta 0:00:00
Collecting Pillow (from easyocr)Downloading pillow-10.4.0-cp312-cp312-win_amd64.whl.metadata (9.3 kB)
Collecting scikit-image (from easyocr)Downloading scikit_image-0.24.0-cp312-cp312-win_amd64.whl.metadata (14 kB)
Collecting python-bidi (from easyocr)Downloading python_bidi-0.6.0-cp312-none-win_amd64.whl.metadata (4.7 kB)
Collecting PyYAML (from easyocr)Downloading PyYAML-6.0.2-cp312-cp312-win_amd64.whl.metadata (2.1 kB)
Collecting Shapely (from easyocr)Downloading shapely-2.0.5-cp312-cp312-win_amd64.whl.metadata (7.2 kB)
Collecting pyclipper (from easyocr)Downloading pyclipper-1.3.0.post5-cp312-cp312-win_amd64.whl.metadata (9.2 kB)
Collecting ninja (from easyocr)Downloading ninja-1.11.1.1-py2.py3-none-win_amd64.whl.metadata (5.4 kB)
Collecting numpy (from easyocr)Downloading numpy-1.26.4-cp312-cp312-win_amd64.whl.metadata (61 kB)---------------------------------------- 61.0/61.0 kB 651.3 kB/s eta 0:00:00
Collecting filelock (from torch->easyocr)Downloading filelock-3.15.4-py3-none-any.whl.metadata (2.9 kB)
Collecting typing-extensions>=4.8.0 (from torch->easyocr)Downloading typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Collecting sympy (from torch->easyocr)Downloading sympy-1.13.2-py3-none-any.whl.metadata (12 kB)
Collecting networkx (from torch->easyocr)Downloading networkx-3.3-py3-none-any.whl.metadata (5.1 kB)
Collecting jinja2 (from torch->easyocr)Downloading jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB)
Collecting fsspec (from torch->easyocr)Downloading fsspec-2024.6.1-py3-none-any.whl.metadata (11 kB)
Collecting setuptools (from torch->easyocr)Downloading setuptools-72.1.0-py3-none-any.whl.metadata (6.6 kB)
Collecting imageio>=2.33 (from scikit-image->easyocr)Downloading imageio-2.34.2-py3-none-any.whl.metadata (4.9 kB)
Collecting tifffile>=2022.8.12 (from scikit-image->easyocr)Downloading tifffile-2024.8.10-py3-none-any.whl.metadata (31 kB)
Collecting packaging>=21 (from scikit-image->easyocr)Downloading packaging-24.1-py3-none-any.whl.metadata (3.2 kB)
Collecting lazy-loader>=0.4 (from scikit-image->easyocr)Downloading lazy_loader-0.4-py3-none-any.whl.metadata (7.6 kB)
Collecting MarkupSafe>=2.0 (from jinja2->torch->easyocr)Downloading MarkupSafe-2.1.5-cp312-cp312-win_amd64.whl.metadata (3.1 kB)
Collecting mpmath<1.4,>=1.1.0 (from sympy->torch->easyocr)Downloading mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)
Downloading easyocr-1.7.1-py3-none-any.whl (2.9 MB)---------------------------------------- 2.9/2.9 MB 3.0 MB/s eta 0:00:00
Downloading torchvision-0.19.0-1-cp312-cp312-win_amd64.whl (1.3 MB)---------------------------------------- 1.3/1.3 MB 4.5 MB/s eta 0:00:00
Downloading torch-2.4.0-cp312-cp312-win_amd64.whl (197.8 MB)---------------------------------------- 197.8/197.8 MB 6.7 MB/s eta 0:00:00
Downloading numpy-1.26.4-cp312-cp312-win_amd64.whl (15.5 MB)---------------------------------------- 15.5/15.5 MB 6.5 MB/s eta 0:00:00
Downloading pillow-10.4.0-cp312-cp312-win_amd64.whl (2.6 MB)---------------------------------------- 2.6/2.6 MB 5.3 MB/s eta 0:00:00
Downloading ninja-1.11.1.1-py2.py3-none-win_amd64.whl (312 kB)---------------------------------------- 313.0/313.0 kB 1.6 MB/s eta 0:00:00
Downloading opencv_python_headless-4.10.0.84-cp37-abi3-win_amd64.whl (38.8 MB)---------------------------------------- 38.8/38.8 MB 13.6 MB/s eta 0:00:00
Downloading pyclipper-1.3.0.post5-cp312-cp312-win_amd64.whl (108 kB)---------------------------------------- 108.2/108.2 kB 3.2 MB/s eta 0:00:00
Downloading python_bidi-0.6.0-cp312-none-win_amd64.whl (156 kB)---------------------------------------- 156.3/156.3 kB 9.1 MB/s eta 0:00:00
Downloading PyYAML-6.0.2-cp312-cp312-win_amd64.whl (156 kB)---------------------------------------- 156.3/156.3 kB 4.7 MB/s eta 0:00:00
Downloading scikit_image-0.24.0-cp312-cp312-win_amd64.whl (12.9 MB)---------------------------------------- 12.9/12.9 MB 3.0 MB/s eta 0:00:00
Downloading scipy-1.14.0-cp312-cp312-win_amd64.whl (44.5 MB)---------------------------------------- 44.5/44.5 MB 10.1 MB/s eta 0:00:00
Downloading shapely-2.0.5-cp312-cp312-win_amd64.whl (1.4 MB)---------------------------------------- 1.4/1.4 MB 11.5 MB/s eta 0:00:00
Downloading imageio-2.34.2-py3-none-any.whl (313 kB)---------------------------------------- 313.5/313.5 kB 9.8 MB/s eta 0:00:00
Downloading lazy_loader-0.4-py3-none-any.whl (12 kB)
Downloading networkx-3.3-py3-none-any.whl (1.7 MB)---------------------------------------- 1.7/1.7 MB 1.2 MB/s eta 0:00:00
Downloading packaging-24.1-py3-none-any.whl (53 kB)---------------------------------------- 54.0/54.0 kB 1.4 MB/s eta 0:00:00
Downloading tifffile-2024.8.10-py3-none-any.whl (225 kB)---------------------------------------- 225.8/225.8 kB 129.0 kB/s eta 0:00:00
Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Downloading filelock-3.15.4-py3-none-any.whl (16 kB)
Downloading fsspec-2024.6.1-py3-none-any.whl (177 kB)---------------------------------------- 177.6/177.6 kB 5.4 MB/s eta 0:00:00
Downloading jinja2-3.1.4-py3-none-any.whl (133 kB)---------------------------------------- 133.3/133.3 kB 7.7 MB/s eta 0:00:00
Downloading setuptools-72.1.0-py3-none-any.whl (2.3 MB)---------------------------------------- 2.3/2.3 MB 2.0 MB/s eta 0:00:00
Downloading sympy-1.13.2-py3-none-any.whl (6.2 MB)---------------------------------------- 6.2/6.2 MB 3.7 MB/s eta 0:00:00
Downloading MarkupSafe-2.1.5-cp312-cp312-win_amd64.whl (17 kB)
Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)---------------------------------------- 536.2/536.2 kB 1.2 MB/s eta 0:00:00
Installing collected packages: python-bidi, pyclipper, ninja, mpmath, typing-extensions, sympy, setuptools, PyYAML, Pillow, packaging, numpy, networkx, MarkupSafe, fsspec, filelock, tifffile, Shapely, scipy, opencv-python-headless, lazy-loader, jinja2, imageio, torch, scikit-image, torchvision, easyocr
Successfully installed MarkupSafe-2.1.5 Pillow-10.4.0 PyYAML-6.0.2 Shapely-2.0.5 easyocr-1.7.1 filelock-3.15.4 fsspec-2024.6.1 imageio-2.34.2 jinja2-3.1.4 lazy-loader-0.4 mpmath-1.3.0 networkx-3.3 ninja-1.11.1.1 numpy-1.26.4 opencv-python-headless-4.10.0.84 packaging-24.1 pyclipper-1.3.0.post5 python-bidi-0.6.0 scikit-image-0.24.0 scipy-1.14.0 setuptools-72.1.0 sympy-1.13.2 tifffile-2024.8.10 torch-2.4.0 torchvision-0.19.0 typing-extensions-4.12.2[notice] A new release of pip is available: 24.0 -> 24.2
[notice] To update, run: python.exe -m pip install --upgrade pipC:\Users\Administrator>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\program files\python312\lib\site-packages (24.0)
Collecting pipDownloading pip-24.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.2-py3-none-any.whl (1.8 MB)---------------------------------------- 1.8/1.8 MB 1.1 MB/s eta 0:00:00
Installing collected packages: pipAttempting uninstall: pipFound existing installation: pip 24.0Uninstalling pip-24.0:Successfully uninstalled pip-24.0
Successfully installed pip-24.2C:\Users\Administrator>pip install easyocr
Requirement already satisfied: easyocr in c:\program files\python312\lib\site-packages (1.7.1)
Requirement already satisfied: torch in c:\program files\python312\lib\site-packages (from easyocr) (2.4.0)
Requirement already satisfied: torchvision>=0.5 in c:\program files\python312\lib\site-packages (from easyocr) (0.19.0)
Requirement already satisfied: opencv-python-headless in c:\program files\python312\lib\site-packages (from easyocr) (4.10.0.84)
Requirement already satisfied: scipy in c:\program files\python312\lib\site-packages (from easyocr) (1.14.0)
Requirement already satisfied: numpy in c:\program files\python312\lib\site-packages (from easyocr) (1.26.4)
Requirement already satisfied: Pillow in c:\program files\python312\lib\site-packages (from easyocr) (10.4.0)
Requirement already satisfied: scikit-image in c:\program files\python312\lib\site-packages (from easyocr) (0.24.0)
Requirement already satisfied: python-bidi in c:\program files\python312\lib\site-packages (from easyocr) (0.6.0)
Requirement already satisfied: PyYAML in c:\program files\python312\lib\site-packages (from easyocr) (6.0.2)
Requirement already satisfied: Shapely in c:\program files\python312\lib\site-packages (from easyocr) (2.0.5)
Requirement already satisfied: pyclipper in c:\program files\python312\lib\site-packages (from easyocr) (1.3.0.post5)
Requirement already satisfied: ninja in c:\program files\python312\lib\site-packages (from easyocr) (1.11.1.1)
Requirement already satisfied: filelock in c:\program files\python312\lib\site-packages (from torch->easyocr) (3.15.4)
Requirement already satisfied: typing-extensions>=4.8.0 in c:\program files\python312\lib\site-packages (from torch->easyocr) (4.12.2)
Requirement already satisfied: sympy in c:\program files\python312\lib\site-packages (from torch->easyocr) (1.13.2)
Requirement already satisfied: networkx in c:\program files\python312\lib\site-packages (from torch->easyocr) (3.3)
Requirement already satisfied: jinja2 in c:\program files\python312\lib\site-packages (from torch->easyocr) (3.1.4)
Requirement already satisfied: fsspec in c:\program files\python312\lib\site-packages (from torch->easyocr) (2024.6.1)
Requirement already satisfied: setuptools in c:\program files\python312\lib\site-packages (from torch->easyocr) (72.1.0)
Requirement already satisfied: imageio>=2.33 in c:\program files\python312\lib\site-packages (from scikit-image->easyocr) (2.34.2)
Requirement already satisfied: tifffile>=2022.8.12 in c:\program files\python312\lib\site-packages (from scikit-image->easyocr) (2024.8.10)
Requirement already satisfied: packaging>=21 in c:\program files\python312\lib\site-packages (from scikit-image->easyocr) (24.1)
Requirement already satisfied: lazy-loader>=0.4 in c:\program files\python312\lib\site-packages (from scikit-image->easyocr) (0.4)
Requirement already satisfied: MarkupSafe>=2.0 in c:\program files\python312\lib\site-packages (from jinja2->torch->easyocr) (2.1.5)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\program files\python312\lib\site-packages (from sympy->torch->easyocr) (1.3.0)C:\Users\Administrator>
安装CUDA Toolkit
easyOCR默认是可以通过CPU直接进行计算的,但是为了更好的体验效果,就需要借助GPU来实现
查找适合自己电脑的CUDA Toolkit版本
查看自己电脑的CUDA Compute Capability版本
我的电脑是台式机,显卡是RTX 3060,显卡驱动为555.99
CUDA GPUs - Compute Capability | NVIDIA Developer
从上面链接可以查看到适合自己显卡的的CUDA Toolkit版本,
我并没有在官网里明确找到Compute Capability 8.6对应的所支持的CUDA Toolkit版本
但我从下面这个链接里看到
https://developer.nvidia.com/rdp/cudnn-archive
从这里看到看到大概8.6对应支持到11.X,我先选了个11.X里的最高版本11.8试试,下载链接如下:
https://developer.nvidia.com/cuda-11-8-0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exe_local
安装过程
验证CUDA安装
安装完成后,打开命令提示符(Windows)或终端(Linux/macOS),输入以下命令,确认CUDA已正确安装:
C:\Users\Administrator>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0
C:\Users\Administrator>
安装NVIDIA cuDNN
NVIDIA cuDNN
这里下载的时候,居然还需要注册
下载后解压:
下载完成后,解压缩文件,并将bin
、include
、lib
目录中的文件复制到CUDA Toolkit的安装目录下(通常是 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.X
,其中X.X
是你的CUDA版本号)。
安装PyTorch的CUDA版本
https://pytorch.org/
CMD安装PyTorch
C:\Users\Administrator>pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Looking in indexes: https://download.pytorch.org/whl/cu118
Requirement already satisfied: torch in c:\program files\python312\lib\site-packages (2.4.0)
Requirement already satisfied: torchvision in c:\program files\python312\lib\site-packages (0.19.0)
Collecting torchaudioDownloading https://download.pytorch.org/whl/cu118/torchaudio-2.4.0%2Bcu118-cp312-cp312-win_amd64.whl (4.0 MB)---------------------------------------- 4.0/4.0 MB 4.4 MB/s eta 0:00:00
Requirement already satisfied: filelock in c:\program files\python312\lib\site-packages (from torch) (3.15.4)
Requirement already satisfied: typing-extensions>=4.8.0 in c:\program files\python312\lib\site-packages (from torch) (4.12.2)
Requirement already satisfied: sympy in c:\program files\python312\lib\site-packages (from torch) (1.13.2)
Requirement already satisfied: networkx in c:\program files\python312\lib\site-packages (from torch) (3.3)
Requirement already satisfied: jinja2 in c:\program files\python312\lib\site-packages (from torch) (3.1.4)
Requirement already satisfied: fsspec in c:\program files\python312\lib\site-packages (from torch) (2024.6.1)
Requirement already satisfied: setuptools in c:\program files\python312\lib\site-packages (from torch) (72.1.0)
Requirement already satisfied: numpy<2 in c:\program files\python312\lib\site-packages (from torchvision) (1.26.4)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\program files\python312\lib\site-packages (from torchvision) (10.4.0)
Collecting torchDownloading https://download.pytorch.org/whl/cu118/torch-2.4.0%2Bcu118-cp312-cp312-win_amd64.whl (2692.5 MB)---------------------------------------- 2.7/2.7 GB 3.2 MB/s eta 0:00:00
Requirement already satisfied: MarkupSafe>=2.0 in c:\program files\python312\lib\site-packages (from jinja2->torch) (2.1.5)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\program files\python312\lib\site-packages (from sympy->torch) (1.3.0)
Installing collected packages: torch, torchaudioAttempting uninstall: torchFound existing installation: torch 2.4.0Uninstalling torch-2.4.0:Successfully uninstalled torch-2.4.0WARNING: Failed to remove contents in a temporary directory 'C:\Program Files\Python312\Lib\site-packages\~orch'.You can safely remove it manually.
Successfully installed torch-2.4.0+cu118 torchaudio-2.4.0+cu118C:\Users\Administrator>
测试CUDA是否正常工作
import torch
print(torch.cuda.is_available()) # 应该输出: True
print(torch.cuda.get_device_name(0)) # 应该输出: RTX 3060
使用EasyOCR来识别
创建一个test.py文件
import easyocrdef main():# 交互式输入图像路径image_path = input("请输入图像文件的路径: ")try:# 创建EasyOCR阅读器reader = easyocr.Reader(['ch_sim', 'en']) # 支持中文和英文# 识别图像中的文本results = reader.readtext(image_path)# 打印识别结果print("\n识别出的文本:")for (bbox, text, prob) in results:print(f"文本: {text} (置信度: {prob:.2f})")except Exception as e:print(f"发生错误: {e}")# 防止控制台窗口自动关闭input("\n按回车键退出...")if __name__ == "__main__":main()
运行后输入内容如下:
请输入图像文件的路径: W:\test.png
C:\Program Files\Python312\Lib\site-packages\easyocr\detection.py:85: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.net.load_state_dict(copyStateDict(torch.load(trained_model, map_location=device)))
C:\Program Files\Python312\Lib\site-packages\easyocr\recognition.py:182: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.model.load_state_dict(torch.load(model_path, map_location=device))识别出的文本:
文本: Github (置信度: 0.77)
文本: 2024/7/27134 (置信度: 0.78)
文本: 文件夹 (置信度: 0.44)
按回车键退出...