经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
tesserocr与pytesseract模块的使用方法解析
来源:jb51  时间:2019/8/30 9:41:38  对本文有异议

1.tesserocr的使用

  1. #从文件识别图像字符
  2. In [7]: tesserocr.file_to_text('image.png')
  3. Out[7]: 'Python3WebSpider\n\n'
  4.  
  5. #查看tesseract已安装的语言包
  6. In [8]: tesserocr.get_languages()
  7. Out[8]: ('/usr/share/tesseract/tessdata/', ['eng'])
  8.  
  9. #从图片数据识别图像字符
  10. In [9]: tesserocr.image_to_text(im)
  11. Out[9]: 'Python3WebSpider\n\n'
  12.  
  13. #查看版本信息
  14. In [10]: tesserocr.tesseract_version()
  15. Out[10]: 'tesseract 3.04.00\n leptonica-1.72\n libgif 4.1.6(?) : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0\n'

2.pytesseract使用

功能:

  • get_tesseract_version  返回系统中安装的Tesseract版本。
  • image_to_string  将图像上的Tesseract OCR运行结果返回到字符串
  • image_to_boxes  返回包含已识别字符及其框边界的结果
  • image_to_data  返回包含框边界,置信度和其他信息的结果。需要Tesseract 3.05+。有关更多信息,请查看Tesseract TSV文档
  • image_to_osd  返回包含有关方向和脚本检测的信息的结果。

参数:

image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)

  • image object  图像对象
  • lang String,Tesseract  语言代码字符串
  • config String  任何其他配置为字符串,例如:config='--psm 6'
  • nice Integer  修改Tesseract运行的处理器优先级。Windows不支持。尼斯调整了类似unix的流程的优点。
  • output_type  类属性,指定输出的类型,默认为string。有关所有支持类型的完整列表,请检查pytesseract.Output类的定义。
  1. from PIL import Image
  2. import pytesseract
  3.  
  4. #如果PATH中没有tesseract可执行文件,请指定tesseract路径
  5. pytesseract.pytesseract.tesseract_cmd='C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
  6.  
  7. #打印识别的图像的字符串
  8. print(pytesseract.image_to_string(Image.open('test.png')))
  9.  
  10. #指定语言识别图像字符串,eng为英语
  11. print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='eng'))
  12.  
  13. #获取图像边界框
  14. print(pytesseract.image_to_boxes(Image.open('test.png')))
  15.  
  16. #获取包含边界框,置信度,行和页码的详细数据
  17. print(pytesseract.image_to_data(Image.open('test.png')))
  18.  
  19. #获取方向和脚本检测
  20. print(pytesseract.image_to_osd(Image.open('test.png'))

图像识别简单应用

一般图像处理验证,需要通过对图像进行灰度处理、二值化后增加图像文字的辨识度,下面是一个简单的对图像验证码识别处理,如遇到复杂点的图像验证码如中间带多条同等大小划线的验证码需要对文字进行乔正切割等操作,但它的识别度也只有百分之30左右,所以得另外想别的办法来绕过验证

  1. from PIL import Image
  2. import pytesseract
  3.  
  4. im = Image.open('66.png')
  5. #二值化图像传入图像和阈值
  6. def erzhihua(image,threshold):
  7. ''':type image:Image.Image'''
  8. image=image.convert('L')
  9. table=[]
  10. for i in range(256):
  11. if i < threshold:
  12. table.append(0)
  13. else:
  14. table.append(1)
  15. return image.point(table,'1')
  16. image=erzhihua(im,127)
  17. image.show()
  18. result=pytesseract.image_to_string(image,lang='eng')
  19. print(result)

模拟自动识别验证码登陆:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2018/7/13 8:58
  4. # @Author : Py.qi
  5. # @File : login.py
  6. # @Software: PyCharm
  7. from selenium import webdriver
  8. from selenium.common.exceptions import TimeoutException,WebDriverException
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.common.keys import Keys
  11. from selenium.webdriver.support.ui import WebDriverWait
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.remote.webelement import WebElement
  14. from io import BytesIO
  15. from PIL import Image
  16. import pytesseract
  17. import time
  18.  
  19. user='zhang'
  20. password='123'
  21. url='http://10.0.0.200'
  22. driver=webdriver.Chrome()
  23. wait=WebDriverWait(driver,10)
  24.  
  25. #识别验证码
  26. def acker(content):
  27. im_erzhihua=erzhihua(content,127)
  28. result=pytesseract.image_to_string(im_erzhihua,lang='eng')
  29. return result
  30.  
  31. #验证码二值化
  32. def erzhihua(image,threshold):
  33. ''':type image:Image.Image'''
  34. image=image.convert('L')
  35. table=[]
  36. for i in range(256):
  37. if i < threshold:
  38. table.append(0)
  39. else:
  40. table.append(1)
  41. return image.point(table,'1')
  42.  
  43. #自动登陆
  44. def login():
  45. try:
  46. driver.get(url)
  47. #获取用户输入框
  48. input=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#loginname'))) #type:WebElement
  49. input.clear()
  50. #发送用户名
  51. input.send_keys(user)
  52. #获取密码框
  53. inpass=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#password'))) #type:WebElement
  54. inpass.clear()
  55. #发送密码
  56. inpass.send_keys(password)
  57. #获取验证输入框
  58. yanzheng=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#code'))) #type:WebElement
  59. #获取验证码在画布中的位置
  60. codeimg=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#codeImg'))) #type:WebElement
  61. image_location = codeimg.location
  62. #截取页面图像并截取掩码码区域图像
  63. image=driver.get_screenshot_as_png()
  64. im=Image.open(BytesIO(image))
  65. imag_code=im.crop((image_location['x'],image_location['y'],488,473))
  66. #输入验证码并登陆
  67. yanzheng.clear()
  68. yanzheng.send_keys(acker(imag_code))
  69. time.sleep(2)
  70. yanzheng.send_keys(Keys.ENTER)
  71. except TimeoutException as e:
  72. print('timeout:',e)
  73. except WebDriverException as e:
  74. print('webdriver error:',e)
  75. if __name__ == '__main__':
  76. login()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号