EasyOCR安装与使用(Win10)

项目网站:https://github.com/JaidedAI/EasyOCR

前提:电脑上有python环境(建议3.8)和pip工具

安装

安装torch torchvision

根据项目的安装注意事项(Window),我们需要先安装一些其他模块

Note 1: For Windows, please install torch and torchvision first by following the official instructions here https://pytorch.org. On the pytorch website, be sure to select the right CUDA version you have. If you intend to run on CPU mode only, select CUDA = None.

//我选的是Windows、Pip、Python、CPU(不使用CUDA),得到对应的安装命令是:
pip3 install torch torchvision torchaudio

可以更新一个pip

python.exe -m pip install --upgrade pip

检测torch是否安装成功

E:\anaconda\install>python
Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.rand(5,3)
>>> print(x)
tensor([[0.7088, 0.4721, 0.0973],
        [0.4558, 0.1847, 0.8272],
        [0.4509, 0.7558, 0.0398],
        [0.5176, 0.2787, 0.4579],
        [0.4937, 0.9435, 0.7807]])
>>>

问题1

注意有时候虽然显示你成功安装了torch,但是在import这个模块的是否会找不到,原因是:

电脑中先前安装了多个 python ,且他们的解释器命名都为 python.exe,都放在了系统变量中。所以系统默认的python版本可能与 Anaconda 中所需的版本不匹配

查看电脑默认python路径

E:\anaconda\install>py -0p
Installed Pythons found by py Launcher for Windows
 -3.8-64        E:\anaconda\install\python.exe *   #带*号的就是默认使用的
 -3.7-64        C:\Users\22927\AppData\Local\Programs\Python\Python37\python.exe
 -3.5-64        C:\Users\22927\AppData\Local\Programs\Python\Python35\python.exe

我这里没有遇到这个问题,如果你遇到了可以到这几个路径把python的名字改一下(后面加个版本),然后使用python+版本进入某个python可执行程序,而不是默认的python

问题2

import torch的时候报错:

ImportError: cannot import name ‘TypeAlias‘ from ‘typing_extensions‘解决方案

方案1:换python版本,我用的是3.8,但是换版本很麻烦,这个方案未使用。

方案2:更新typing-extensions版本

pip install typing-extensions --upgrade

我用的方案2

安装EasyOCR

 pip install easyocr

有时候会报出网络不好导致install异常,可以多试几次(我就是试了3、4次就可以下载了),报错堆栈如下:

 1 E:\anaconda\install>pip install easyocr
 2 Collecting easyocr
 3   Downloading easyocr-1.6.2-py3-none-any.whl (2.9 MB)
 4       --------------------------------------- 0.1/2.9 MB 2.7 kB/s eta 0:17:21
 5 ERROR: Exception:
 6 Traceback (most recent call last):
 7   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
 8     yield
 9   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read
10     data = self._fp_read(amt) if not fp_closed else b""
11   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read
12     return self._fp.read(amt) if amt is not None else self._fp.read()
13   File "e:\anaconda\install\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 90, in read
14     data = self.__fp.read(amt)
15   File "e:\anaconda\install\lib\http\client.py", line 454, in read
16     n = self.readinto(b)
17   File "e:\anaconda\install\lib\http\client.py", line 498, in readinto
18     n = self.fp.readinto(b)
19   File "e:\anaconda\install\lib\socket.py", line 669, in readinto
20     return self._sock.recv_into(b)
21   File "e:\anaconda\install\lib\ssl.py", line 1241, in recv_into
22     return self.read(nbytes, buffer)
23   File "e:\anaconda\install\lib\ssl.py", line 1099, in read
24     return self._sslobj.read(len, buffer)
25 socket.timeout: The read operation timed out
26 
27 During handling of the above exception, another exception occurred:
28 
29 Traceback (most recent call last):
30   File "e:\anaconda\install\lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
31     status = run_func(*args)
32   File "e:\anaconda\install\lib\site-packages\pip\_internal\cli\req_command.py", line 247, in wrapper
33     return func(self, options, args)
34   File "e:\anaconda\install\lib\site-packages\pip\_internal\commands\install.py", line 419, in run
35     requirement_set = resolver.resolve(
36   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 92, in resolve
37     result = self._result = resolver.resolve(
38   File "e:\anaconda\install\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 481, in resolve
39     state = resolution.resolve(requirements, max_rounds=max_rounds)
40   File "e:\anaconda\install\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 348, in resolve
41     self._add_to_criteria(self.state.criteria, r, parent=None)
42   File "e:\anaconda\install\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
43     if not criterion.candidates:
44   File "e:\anaconda\install\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__
45     return bool(self._sequence)
46   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 155, in __bool__
47     return any(self)
48   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 143, in <genexpr>
49     return (c for c in iterator if id(c) not in self._incompatible_ids)
50   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 47, in _iter_built
51     candidate = func()
52   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 206, in _make_candidate_from_link
53     self._link_candidate_cache[link] = LinkCandidate(
54   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 297, in __init__
55     super().__init__(
56   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 162, in __init__
57     self.dist = self._prepare()
58   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 231, in _prepare
59     dist = self._prepare_distribution()
60   File "e:\anaconda\install\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 308, in _prepare_distribution
61     return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
62   File "e:\anaconda\install\lib\site-packages\pip\_internal\operations\prepare.py", line 491, in prepare_linked_requirement
63     return self._prepare_linked_requirement(req, parallel_builds)
64   File "e:\anaconda\install\lib\site-packages\pip\_internal\operations\prepare.py", line 536, in _prepare_linked_requirement
65     local_file = unpack_url(
66   File "e:\anaconda\install\lib\site-packages\pip\_internal\operations\prepare.py", line 166, in unpack_url
67     file = get_http_url(
68   File "e:\anaconda\install\lib\site-packages\pip\_internal\operations\prepare.py", line 107, in get_http_url
69     from_path, content_type = download(link, temp_dir.path)
70   File "e:\anaconda\install\lib\site-packages\pip\_internal\network\download.py", line 147, in __call__
71     for chunk in chunks:
72   File "e:\anaconda\install\lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar
73     for chunk in iterable:
74   File "e:\anaconda\install\lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks
75     for chunk in response.raw.stream(
76   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
77     data = self.read(amt=amt, decode_content=decode_content)
78   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 587, in read
79     raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
80   File "e:\anaconda\install\lib\contextlib.py", line 131, in __exit__
81     self.gen.throw(type, value, traceback)
82   File "e:\anaconda\install\lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
83     raise ReadTimeoutError(self._pool, None, "Read timed out.")
84 pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
View Code

使用EasyOCR识别图片中的文字

识别下面图片中的繁体中文:

 

 

#命令行下依次执行
import easyocr

reader = easyocr.Reader(['ch_tra', 'en'])

result = reader.readtext(r'd:\temp\chinese_tra.jpg')

print(result)

问题1

 

 

 import easyocr时候报错:

>>> import easyocr
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

原因:(我用的是anaconda自带的3.8版本的python编译器)

anaconda的环境下存在两个libiomp5md.dll文件。所以直接去虚拟环境的路径下搜索这个文件,可以看到在环境里有三个dll文件:

 

 其中第一个是torch路径下的,其他二个是虚拟环境本身路径下的,转到第二个目录下把它剪切到其他路径下备份就好(最好把路径也备份一下)。(我是直接改名字)

 

 问题2

在执行第二句代码的时候报错:TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

 1 E:\anaconda\install>python
 2 Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
 3 Type "help", "copyright", "credits" or "license" for more information.
 4 >>> import easyocr
 5 >>> reader = easyocr.Reader(['ch_tra', 'en'])
 6 CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.
 7 Downloading detection model, please wait. This may take several minutes depending upon your network connection.
 8 Traceback (most recent call last):
 9   File "E:\anaconda\install\lib\urllib\request.py", line 1350, in do_open
10     h.request(req.get_method(), req.selector, req.data, headers,
11   File "E:\anaconda\install\lib\http\client.py", line 1240, in request
12     self._send_request(method, url, body, headers, encode_chunked)
13   File "E:\anaconda\install\lib\http\client.py", line 1286, in _send_request
14     self.endheaders(body, encode_chunked=encode_chunked)
15   File "E:\anaconda\install\lib\http\client.py", line 1235, in endheaders
16     self._send_output(message_body, encode_chunked=encode_chunked)
17   File "E:\anaconda\install\lib\http\client.py", line 1006, in _send_output
18     self.send(msg)
19   File "E:\anaconda\install\lib\http\client.py", line 946, in send
20     self.connect()
21   File "E:\anaconda\install\lib\http\client.py", line 1402, in connect
22     super().connect()
23   File "E:\anaconda\install\lib\http\client.py", line 917, in connect
24     self.sock = self._create_connection(
25   File "E:\anaconda\install\lib\socket.py", line 808, in create_connection
26     raise err
27   File "E:\anaconda\install\lib\socket.py", line 796, in create_connection
28     sock.connect(sa)
29 TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
30 
31 During handling of the above exception, another exception occurred:
32 
33 Traceback (most recent call last):
34   File "<stdin>", line 1, in <module>
35   File "E:\anaconda\install\lib\site-packages\easyocr\easyocr.py", line 88, in __init__
36     detector_path = self.getDetectorPath(detect_network)
37   File "E:\anaconda\install\lib\site-packages\easyocr\easyocr.py", line 249, in getDetectorPath
38     download_and_unzip(self.detection_models[self.detect_network]['url'], self.detection_models[self.detect_network]['filename'], self.model_storage_directory, self.verbose)
39   File "E:\anaconda\install\lib\site-packages\easyocr\utils.py", line 586, in download_and_unzip
40     urlretrieve(url, zip_path, reporthook=reporthook)
41   File "E:\anaconda\install\lib\urllib\request.py", line 247, in urlretrieve
42     with contextlib.closing(urlopen(url, data)) as fp:
43   File "E:\anaconda\install\lib\urllib\request.py", line 222, in urlopen
44     return opener.open(url, data, timeout)
45   File "E:\anaconda\install\lib\urllib\request.py", line 525, in open
46     response = self._open(req, data)
47   File "E:\anaconda\install\lib\urllib\request.py", line 542, in _open
48     result = self._call_chain(self.handle_open, protocol, protocol +
49   File "E:\anaconda\install\lib\urllib\request.py", line 502, in _call_chain
50     result = func(*args)
51   File "E:\anaconda\install\lib\urllib\request.py", line 1393, in https_open
52     return self.do_open(http.client.HTTPSConnection, req,
53   File "E:\anaconda\install\lib\urllib\request.py", line 1353, in do_open
54     raise URLError(err)
55 urllib.error.URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
View Code

因为网络问题,自动下载总是失败报错,所以只好去网上手动下载训练好的模型。

以下是一些模型的下载地址:
text detection model (CRAFT)

chinese (traditional) model

chinese (simplified) model

japanese model

korean model

下载好模型之后就需要解压文件,然后把.pth文件放到C:\Users\你的用户名\.EasyOCR\model文件下,就可以正常使用啦。
例如我使用了一个繁体中文的模型,输出如下:(发现解压后就没有去下载繁体中文模型)

>>> reader = easyocr.Reader(['ch_tra', 'en'])
CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.
>>> result = reader.readtext(r'e:\anaconda\install\1.jpg')
>>> print(result)
[([[1455, 155], [1803, 155], [1803, 276], [1455, 276]], '學生証', 0.7933082015720206)...]

简体中文reader对象的创建(不知道为什么我解压简体中文的模型之后创建reader对象的时候还是需要下载模型,而下载模型又网络超时,所以简体模型就用不了)

reader = easyocr.Reader(['ch_sim', 'en'])

参考文章

【Pytorch安装】windows下,安装了torch但是import torch失败

ImportError: cannot import name ‘TypeAlias‘ from ‘typing_extensions‘解决方案

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.

关于下载EsayOCR模型总是连接中断报错

python文字识别之easyocr库安装使用记录

Python光学字符识别工具-EasyOCR安装与使用(Win10)

posted @ 2023-04-05 00:14  kongbursi  阅读(2129)  评论(0编辑  收藏  举报