Python 识别12306图片验证码物品的实现示例


Posted in Python onJanuary 20, 2020

1、PIL介绍以及图片分割

Python 3 安装:  pip3 install Pillow

1.1 image 模块

Image模块是在Python PIL图像处理中常见的模块,主要是用于对这个图像的基本处理,它配合open、save、convert、show…等功能使用。

from PIL import Image
#打开文件代表打开pycharm中的文件
im = Image.open('1.jpg')
#展示图片
im.show()

1、Crop类

拷贝这个图像。如果用户想粘贴一些数据到这张图,可以使用这个方法,但是原始图像不会受到影响。

im.crop(box) ⇒ image

从当前的图像中返回一个矩形区域的拷贝。变量box是一个四元组,定义了左、上、右和下的像素坐标。用来表示在原始图像中截取的位置坐标,如box(100,100,200,200)就表示在原始图像中以左上角为坐标原点,截取一个100*100(像素为单位)的图像。

from PIL import Image
im = Image.open("pic1.jpg")
##确定拷贝区域大小
box = (5, 41, 72, 108)
##将im表示的图片对象拷贝到region中,大小为box
region = im.crop(box)
region.show()

Python 识别12306图片验证码物品的实现示例

实战一:12306图像分割并保存

from PIL import Image
#切割图像,由于下载的图片都是有固定的位置,所以直接控制像素进行切割就行了
def cut_img(im, x, y):
  assert 0 <= x <= 3
  assert 0 <= y <= 2
  left = 5 + (67 + 5) * x
  top = 41 + (67 + 5) * y
  right = left + 67
  bottom = top + 67
  return im.crop((left, top, right, bottom))
 
if __name__ == '__main__':
  im = Image.open("./pic1.jpg")
  #控制y轴
  for y in range(2):
    #控制x轴
    for x in range(4):
      im2 = cut_img(im, x, y)
      im2.save('./images/%s_%s.png'%(y,x))

Python 识别12306图片验证码物品的实现示例

2、百度平台接口实现

2.1.平台接入:

1.打开https://ai.baidu.com/进入控制台,选择文字识别服务。

Python 识别12306图片验证码物品的实现示例

2.创建应用,如图示:

Python 识别12306图片验证码物品的实现示例

3.输入应用名称、描述,并选择应用类型,之后点击“立即创建”按钮。

Python 识别12306图片验证码物品的实现示例

 4.创建完毕,点击“返回应用列表”。

Python 识别12306图片验证码物品的实现示例

5.此处显示AK,SK,后面程序中会用到

Python 识别12306图片验证码物品的实现示例

3. 官方文档的读取

1.打开https://ai.baidu.com/docs#/OCR-API/top 文档说明

Python 识别12306图片验证码物品的实现示例

Python 识别12306图片验证码物品的实现示例

需要用到的信息有:

(1)图像识别URL: https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general

(2)Header格式:Content-Type:application/x-www-form-urlencoded

(3) 请求参数:image和multi_detect两个参数,image为图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。

(4)返回参数:车牌颜色Color、车牌号码number等。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import base64
import requests
import os
import time
#todo:获取百度权限验证码access_token
def get_token():
  get_token_url = "https://aip.baidubce.com/oauth/2.0/token"
  params = {
    "grant_type": "client_credentials",
    "client_id": "7ax98QuWU5l2zTbaOkzvKgxE",
    "client_secret": "INugQTM2DAfNFgfxtvgR7eF8AHPFGP5t",
  }
  res = requests.get(get_token_url, params).json()
  return res["access_token"]
#todo:通过权限验证码和图片进行识别物品
def get_result(access_token,image):
  url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
  #打开文件并进行编码
  with open(image, 'rb')as f:
    image = base64.b64encode(f.read())
  # image =
  #头部信息
  headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
  #发送数据
  data = {
    "access_token": access_token,
    "image": image
  }
  #发送请求,并返回识别数据
  res = requests.post(url, headers=headers, data=data).json()
  if res:
    result = res['result']
    return result
#todo:获取图片关键物品
def get_keywords(result):
  #按照最大匹配率进行排序,并获取左最后一个
  max_score = sorted(result,key=lambda x:x['score'])[-1]
  # print(max_score['keyword'])
  keyword = max_score['keyword']
  return keyword
 
if __name__ == '__main__':
  access_token = get_token()
  get_result(access_token,'pic1.jpg')
  datas = []
  for root,dir,files in os.walk('images'):
    for file in files:
      image = os.path.join(root,file)
      result = get_result(access_token,image)
      keyword = get_keywords(result)
      print(keyword)
      time.sleep(1)
      datas.append(keyword)
  print(datas)

Python 识别12306图片验证码物品的实现示例

总结:

  • PIL介绍以及图片分割
  • 百度AI图像识别实例搭建
  • 识别12306类别码

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

Python 相关文章推荐
python实现将pvr格式转换成pvr.ccz的方法
Apr 28 Python
Python中%是什么意思?python中百分号如何使用?
Mar 20 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
Python面向对象思想与应用入门教程【类与对象】
Apr 12 Python
Selenium+Python 自动化操控登录界面实例(有简单验证码图片校验)
Jun 28 Python
python 实现二维字典的键值合并等函数
Dec 06 Python
TensorFlow查看输入节点和输出节点名称方式
Jan 04 Python
Anaconda+VSCode配置tensorflow开发环境的教程详解
Mar 30 Python
python递归函数用法详解
Oct 26 Python
python实现三种随机请求头方式
Jan 05 Python
python实现银行账户系统
Feb 22 Python
Django drf请求模块源码解析
Jun 08 Python
如何基于python实现归一化处理
Jan 20 #Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
Jan 20 #Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
Jan 20 #Python
python如何通过pyqt5实现进度条
Jan 20 #Python
python super用法及原理详解
Jan 20 #Python
tensorflow 变长序列存储实例
Jan 20 #Python
在tensorflow中实现去除不足一个batch的数据
Jan 20 #Python
You might like
法压式咖啡之制作法
2021/03/03 冲泡冲煮
用PHP生成html分页列表的代码
2007/03/18 PHP
php adodb连接带密码access数据库实例,测试成功
2008/05/14 PHP
php 生成唯一id的几种解决方法
2013/03/08 PHP
解析php中两种缩放图片的函数,为图片添加水印
2013/06/14 PHP
phpmailer发送邮件之后,返回收件人是否阅读了邮件的方法
2014/07/19 PHP
CI框架安全类Security.php源码分析
2014/11/04 PHP
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
2015/03/19 PHP
codeigniter实现get分页的方法
2015/07/10 PHP
PHP文件上传问题汇总(文件大小检测、大文件上传处理)
2015/12/24 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
2017/12/21 PHP
jquery解析XML字符串和XML文件的方法说明
2014/02/21 Javascript
禁止iframe脚本弹出的窗口覆盖了父窗口的方法
2014/09/06 Javascript
jQuery实现动画效果circle实例
2015/08/06 Javascript
JS简单实现多级Select联动菜单效果代码
2015/09/06 Javascript
利用Vue.js指令实现全选功能
2016/09/08 Javascript
JS+HTML5实现上传图片预览效果完整实例【测试可用】
2017/04/20 Javascript
微信小程序实现全国机场索引列表
2018/01/31 Javascript
Vux+Axios拦截器增加loading的问题及实现方法
2018/11/08 Javascript
浅谈Javascript常用正则表达式应用
2019/03/08 Javascript
Node 搭建一个静态资源服务器的实现
2019/05/20 Javascript
详解webpack-dev-middleware 源码解读
2020/03/23 Javascript
JavaScript实现轮播图特效
2020/04/10 Javascript
浅谈JavaScript中的“!!”作用
2020/08/03 Javascript
Python新手入门最容易犯的错误总结
2017/04/24 Python
python如何读写csv数据
2018/03/21 Python
python向已存在的excel中新增表,不覆盖原数据的实例
2018/05/02 Python
Python3.6日志Logging模块简单用法示例
2018/06/14 Python
Made in Design英国:设计家具、照明、家庭装饰和花园家具
2019/09/24 全球购物
宣传部部长竞选演讲稿
2014/04/26 职场文书
金融系毕业生自荐书
2014/07/08 职场文书
退休党员个人对照检查材料思想汇报
2014/09/29 职场文书
2019年描写人生经典诗句大全
2019/07/08 职场文书
详解JS ES6编码规范
2021/05/07 Javascript
Python Django框架介绍之模板标签及模板的继承
2021/05/27 Python
MySQL 数据 data 基本操作
2022/05/04 MySQL