python图片验证码识别最新模块muggle_ocr的示例代码


Posted in Python onJuly 03, 2020

一.官方文档

https://pypi.org/project/muggle-ocr/

二模块安装

pip install muggle-ocr
# 因模块过新,阿里/清华等第三方源可能尚未更新镜像,因此手动指定使用境外源,为了提高依赖的安装速度,可预先自行安装依赖:tensorflow/numpy/opencv-python/pillow/pyyaml

三.使用代码

# 导入包
import muggle_ocr

# 初始化;model_type 包含了 ModelType.OCR/ModelType.Captcha 两种
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)
# ModelType.OCR 可识别光学印刷文本 这里个人觉得应该是官方文档写错了 官方文档是ModelType.Captcha 可识别光学印刷文本
with open(r"test1.png", "rb") as f:
 b = f.read()
text = sdk.predict(image_bytes=b)
print(text)

# ModelType.Captcha 可识别4-6位验证码
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
with open(r"test1.png", "rb") as f:
 b = f.read()
text = sdk.predict(image_bytes=b)
print(text)

PS:下面看下 Python 实现全自动登录(真正的全自动,自动识别验证码)

你没有看错,全自动验证~~~

黑科技?还是黑代码?
我感觉这个看在你用啥,对不对?反正我用来(* * * * ) 你懂得

好了,先说一下用到的东西

  • selenium (本意是用来全自动测试)
  • Phantomjs (一种没有界面的浏览器)
  • ** 验证码识别器(一块钱可用100次的这种)

关门放代码

from selenium import webdriver
from PIL import Image
if __name__ == '__main__':
 wbe = webdriver.PhantomJS()
 wbe.get("https://www.某个网站的登录页面.com/login/index.html")//你可以拿知乎,百度,等等测试
 element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')//验证码所在的xpath路径
 left = element.location['x']
 top = element.location['y']
 right = element.location['x'] + element.size['width']
 bottom = element.location['y'] + element.size['height']
 im = Image.open(r'登录页.png')//全页面截屏
 im = im.crop((left, top, right, bottom))
 im.save('验证码.png')
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class RClient(object):
 def __init__(self, username, password, soft_id, soft_key):
  self.username = username
  self.password = md5(password).hexdigest()
  self.soft_id = soft_id
  self.soft_key = soft_key
  self.base_params = {
   'username': self.username,
   'password': self.password,
   'softid': self.soft_id,
   'softkey': self.soft_key,
  }
  self.headers = {
   'Connection': 'Keep-Alive',
   'Expect': '100-continue',
   'User-Agent': 'ben',
  }
 def rk_create(self, im, im_type, timeout=60):
  """
  im: 图片字节
  im_type: 题目类型
  """
  params = {
   'typeid': im_type,
   'timeout': timeout,
  }
  params.update(self.base_params)
  files = {'image': ('a.png', im)}
  r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
  return r.json()
 def rk_report_error(self, im_id):
  """
  im_id:报错题目的ID
  """
  params = {
   'id': im_id,
  }
  params.update(self.base_params)
  r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
  return r.json()
def get_code():
 rc = RClient('用户名', '密码', '94522', '62c235939b7240879453f31603733fd6')//想拿下测试的留言我,教你拿到测试账号
 im = open('a.png', 'rb').read()
 print rc.rk_create(im, 3040)

完整代码

#!/usr/bin/env python
# coding:utf-8
from selenium import webdriver
from PIL import Image
import requests
from hashlib import md5
import time
class RClient(object):
 def __init__(self, username, password, soft_id, soft_key):
  self.username = username
  self.password = md5(password.encode("utf-8")).hexdigest()
  self.soft_id = soft_id
  self.soft_key = soft_key
  self.base_params = {
   'username': self.username,
   'password': self.password,
   'softid': self.soft_id,
   'softkey': self.soft_key,
  }
  self.headers = {
   'Connection': 'Keep-Alive',
   'Expect': '100-continue',
   'User-Agent': 'ben',
  }
 def rk_create(self, im, im_type, timeout=60):
  """
  im: 图片字节
  im_type: 题目类型
  """
  params = {
   'typeid': im_type,
   'timeout': timeout,
  }
  params.update(self.base_params)
  files = {'image': ('a.png', im)}
  r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
  return r.json()
 def rk_report_error(self, im_id):
  """
  im_id:报错题目的ID
  """
  params = {
   'id': im_id,
  }
  params.update(self.base_params)
  r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
  return r.json()
def get_code(im_file):
 rc = RClient('账号', '密码', '94522', '62c235939b7240879453f31603733fd6')
 im_source = open(im_file, "rb").read()
 print(rc.rk_create(im_source, 3040))
if __name__ == '__main__':
 wbe = webdriver.PhantomJS()
 wbe.get("https://www.dajiang365.com/login/index.html")
 time.sleep(2)
 wbe.save_screenshot("das.png")
 element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')
 left = element.location['x']
 top = element.location['y']
 right = element.location['x'] + element.size['width']
 bottom = element.location['y'] + element.size['height']
 im = Image.open(r'das.png')
 im = im.crop((left, top, right, bottom))
 im.save('a.png')
 time.sleep(2)
 get_code("a.png")

总结

到此这篇关于python图片验证码识别最新模块muggle_ocr的示例代码的文章就介绍到这了,更多相关python 验证码识别模块muggle_ocr内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python3+PyQt5实现自定义分数滑块部件
Apr 24 Python
解决Python requests库编码 socks5代理的问题
May 07 Python
python 执行文件时额外参数获取的实例
Dec 18 Python
python 二维数组90度旋转的方法
Jan 28 Python
pyqt5移动鼠标显示坐标的方法
Jun 21 Python
python 函数中的内置函数及用法详解
Jul 02 Python
如何安装并使用conda指令管理python环境
Jul 10 Python
对Python生成器、装饰器、递归的使用详解
Jul 19 Python
pandas数据处理进阶详解
Oct 11 Python
python函数局部变量、全局变量、递归知识点总结
Nov 15 Python
解决Tensorboard可视化错误:不显示数据 No scalar data was found
Feb 15 Python
基于python定位棋子位置及识别棋子颜色
Jul 26 Python
keras topN显示,自编写代码案例
Jul 03 #Python
python如何使用代码运行助手
Jul 03 #Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
Jul 03 #Python
python3 简单实现组合设计模式
Jul 02 #Python
Django Session和Cookie分别实现记住用户登录状态操作
Jul 02 #Python
django 装饰器 检测登录状态操作
Jul 02 #Python
详解用Python爬虫获取百度企业信用中企业基本信息
Jul 02 #Python
You might like
PHP缓存技术的使用说明
2011/08/06 PHP
php实现的短网址算法分享
2014/06/20 PHP
php时间戳格式化显示友好的时间函数分享
2014/10/21 PHP
php版微信小店API二次开发及使用示例
2016/11/12 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
PHP底层运行机制与工作原理详解
2020/07/31 PHP
一句话JavaScript表单验证代码
2009/08/02 Javascript
JavaScript与Div对层定位和移动获得坐标的实现代码
2010/09/08 Javascript
JS中批量给元素绑定事件过程中的相关问题使用闭包解决
2013/04/15 Javascript
Bootstrap入门书籍之(一)排版
2016/02/17 Javascript
浅谈javascript中的call、apply、bind
2016/03/06 Javascript
js微信分享API
2020/10/11 Javascript
推荐VSCode 上特别好用的 Vue 插件之vetur
2017/09/14 Javascript
Vue实现简单分页器
2018/12/29 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
Python调用C语言开发的共享库方法实例
2015/03/18 Python
详解如何利用Cython为Python代码加速
2018/01/27 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
对Python 3.5拼接列表的新语法详解
2018/11/08 Python
使用NumPy读取MNIST数据的实现代码示例
2019/11/20 Python
Python matplotlib画曲线例题解析
2020/02/07 Python
PyTorch中model.zero_grad()和optimizer.zero_grad()用法
2020/06/24 Python
Python Web项目Cherrypy使用方法镜像
2020/11/05 Python
Python调用Redis的示例代码
2020/11/24 Python
python 中 .py文件 转 .pyd文件的操作
2021/03/04 Python
css3中单位px,em,rem,vh,vw,vmin,vmax的区别及浏览器支持情况
2016/12/06 HTML / CSS
以下的初始化有什么区别
2013/12/16 面试题
路政管理专业推荐信
2013/11/11 职场文书
蔬菜基地的创业计划书
2014/01/06 职场文书
行政撤诉申请书
2015/05/18 职场文书
2015年领导班子工作总结
2015/05/23 职场文书
追悼词范文大全
2015/06/23 职场文书
2016年校园植树节广播稿
2015/12/17 职场文书
英文诗歌翻译方法(赏析)
2019/08/16 职场文书
学会Python数据可视化必须尝试这7个库
2021/06/16 Python