Python图像(字母数字)识别

本文只针对数字或字母验证码识别

准备工具

  • tesseract-ocr-w64-setup-v4.1.0.20190314.exe
  • pip install pytesseract
  • pip install pillow
  • 中文包

tesseract-ocr和中文包的下载连接: 链接:链接:https://pan.baidu.com/s/1r_LZKRXBT2Ffp7QrYQwOSA?pwd=junm
提取码:junm

安装好tesseract,记住安装的所在位置

如D:\Program Files\Tesseract-OCR

解压中文包的压缩包

解压中文包的压缩包,将压缩包里面格式为.traineddata的文件复制在安装目录里边tessdata的文件夹下,如D:\Program Files\Tesseract-OCR\tessdata这下面就行了。

配置tesseract的环境变量

第一个:
TESSDATA_PREFIX —> 你安装的路径,比如 D:\Program Files\Tesseract-OCR
第二个: 在path里面新建一个
你安装的路径,比如 D:\Program Files\Tesseract-OCR
Python图像(字母数字)识别_第1张图片
Python图像(字母数字)识别_第2张图片

查看版本

win+R输入cmd打开命令行工具,输入tesseract -v

Python图像(字母数字)识别_第3张图片

查看一安装好的语言包

win+R输入cmd打开命令行工具,输入tesseract --list-langs
Python图像(字母数字)识别_第4张图片

修改pytesseract.py文件,指定tesseract.exe安装路径

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe‘
#就是你安装的位置

Python图像(字母数字)识别_第5张图片

代码实例

import pytesseract
from PIL import Image
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chromedriver_path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"  # chromedriver的路径
# 创建chrome参数对象
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--start-maximized')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
options.add_argument('test-type')
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors",
                                                    "enable-automation"])
driver = webdriver.Chrome(options=options, executable_path=chromedriver_path)
driver.get('http://www.alihba.fun/jiaoyanma/jiaoyanma.php')


def screen(screenXpath):
    img = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, screenXpath))
    )
    driver.save_screenshot("allscreen.png")  # 对整个浏览器页面进行截图
    left = img.location['x']+26
    top = img.location['y']+10
    right = img.location['x'] + img.size['width']+53
    bottom = img.location['y'] + img.size['height']+10
    print(left,top,right,bottom)
    im = Image.open('allscreen.png')
    im = im.crop((left, top, right, bottom))  # 对浏览器截图进行裁剪
    im.save('jietu.png')
    print("截图完成")
    # driver.quit()

def Identify():
    image = Image.open("./jietu.png")
    code = pytesseract.image_to_string(image,lang="eng", config="--psm 6")  #lang的值可以根据安装的语言选择,也不是都可以用,一般看的是验证码类型就可以了。
    print("识别出验证码为:   ",code)

def main():
    screenXpath = '//*[@id="captcha_img"]'
    screen(screenXpath)
    sleep(5)
    Identify()

if __name__ == '__main__':
    main()

Python图像(字母数字)识别_第6张图片
Python图像(字母数字)识别_第7张图片

-psm 参数

0:定向脚本监测(OSD)
1: 使用OSD自动分页
2 :自动分页,但是不使用OSD或OCR(Optical Character Recognition,光学字符识别)
3 :全自动分页,但是没有使用OSD(默认)
4 :假设可变大小的一个文本列。
5 :假设垂直对齐文本的单个统一块。
6 :假设一个统一的文本块。
7 :将图像视为单个文本行。
8 :将图像视为单个词。
9 :将图像视为圆中的单个词。
10 :将图像视为单个字符。

通过测试,如果验证码全为数字,准确率比较高,字母识别的准确率稍低

最后可以关注一下我个人微信公众号,不定期更新一些好用的资源以及生活上的点点滴滴~~

Python图像(字母数字)识别_第8张图片

你可能感兴趣的:(Python,python,图像识别,selenium,chrome)