初次认识easyocr,初衷是为了解决网站因为有验证码所以需要图片识别功能
pip3 install easyocr
"""
@File : MyOCR.py
@Modify Time @Author @Version
------------ ------- --------
"""
import os.path
import easyocr
# 设定识别的文字类别,en为english,也可以设置成 easyocr.Reader(['ch_sim', 'en']),即为简体中文或者英文
reader = easyocr.Reader(['en'])
# 获取图片地址
file = os.path.join(os.path.dirname(__file__), "createImage.jpg")
result = reader.readtext(file)
for res in result:
print(res)
print(type(res))
print(res[1])
print(type(res[1]))
但是直接执行是报错的,会在打印下面的信息后报错
Downloading detection model, please wait. This may take several minutes depending upon your network connection.
是因为我们的网络太慢了,导致并没有办法正常下载模型,也就没办法正常识别图片文件
我们可以通过手动下载的办法来解决
easyocr模型下载
我们点击下面这三个链接就会自动下载文件
但是我自己下载的时候很慢,没办法了才下载下来,然后上传到百度云了,需要的自取,里面有三个压缩包,也有三个pth文件,就是解压后的文件,下载pth的即可
链接: https://pan.baidu.com/s/1xyPsKORak447jS82Pt-JHg 提取码: onvl
下载到本地后,复制到当前用户的目录下
.EasyOCR\model
比如我的是mac电脑(同linux),如果是windows电脑的话就是,C:\Users\yourname.EasyOCR\model,其中yourname是登录用户名。
ZHR:model zc$ pwd
/Users/zc/.EasyOCR/model
ZHR:model zc$ open /Users/zc/.EasyOCR/model
ZHR:model zc$ ls
craft_mlt_25k.pth english_g2.pth zh_sim_g2.pth
ZHR:model zc$ pwd
/Users/zc/.EasyOCR/model
再次执行代码就不报错了
/Users/zc/PycharmProjects/fastapitest/venv/bin/python /Users/zc/PycharmProjects/fastapitest/One/MyOCR.py
CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.
([[18, 6], [82, 6], [82, 30], [18, 30]], '47HH', 0.87657231092453)
47HH
"""
@File : MyOCR.py
@Modify Time @Author @Version
------------ ------- --------
"""
import os.path
import easyocr
import requests
url = 'https://img-blog.csdnimg.cn/e9f0a6ac9826499fadd19b7d5aa1e1d4.png'
# verify=False:是否验证=否
rsp = requests.get(url, verify=False)
with open('1.jpg', 'wb') as f:
f.write(rsp.content)
print(easyocr.__version__)
reader = easyocr.Reader(['en'])
file = os.path.join(os.path.dirname(__file__), "1.jpg")
result = reader.readtext(file)
for res in result:
print(res)
print(type(res))
print(res[1])
print(type(res[1]))
结果
/Users/zc/PycharmProjects/fastapitest/venv/lib/python3.8/site-packages/urllib3/connectionpool.py:1013: InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
1.4.1
CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.
([[34, 11], [157, 11], [157, 53], [34, 53]], '6Ty 6', 0.504037462923347)
6Ty 6
大家也看到了,这个识别的不是那么的准,比如上面的图片会有结果中存在空格的情况,需要在优化,甚至会有识别不全的情况,要按照情况特别优化才可以