Python 識別12306圖片驗(yàn)證碼物品的實(shí)現(xiàn)示例
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類
拷貝這個圖像。如果用戶想粘貼一些數(shù)據(jù)到這張圖,可以使用這個方法,但是原始圖像不會受到影響。
im.crop(box) ⇒ image
從當(dāng)前的圖像中返回一個矩形區(qū)域的拷貝。變量box是一個四元組,定義了左、上、右和下的像素坐標(biāo)。用來表示在原始圖像中截取的位置坐標(biāo),如box(100,100,200,200)就表示在原始圖像中以左上角為坐標(biāo)原點(diǎn),截取一個100*100(像素為單位)的圖像。
from PIL import Image
im = Image.open("pic1.jpg")
##確定拷貝區(qū)域大小
box = (5, 41, 72, 108)
##將im表示的圖片對象拷貝到region中,大小為box
region = im.crop(box)
region.show()

實(shí)戰(zhàn)一:12306圖像分割并保存
from PIL import Image
#切割圖像,由于下載的圖片都是有固定的位置,所以直接控制像素進(jìn)行切割就行了
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))

2、百度平臺接口實(shí)現(xiàn)
2.1.平臺接入:
1.打開https://ai.baidu.com/進(jìn)入控制臺,選擇文字識別服務(wù)。

2.創(chuàng)建應(yīng)用,如圖示:

3.輸入應(yīng)用名稱、描述,并選擇應(yīng)用類型,之后點(diǎn)擊“立即創(chuàng)建”按鈕。

4.創(chuàng)建完畢,點(diǎn)擊“返回應(yīng)用列表”。

5.此處顯示AK,SK,后面程序中會用到

3. 官方文檔的讀取
1.打開https://ai.baidu.com/docs#/OCR-API/top 文檔說明


需要用到的信息有:
(1)圖像識別URL: https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general
(2)Header格式:Content-Type:application/x-www-form-urlencoded
(3) 請求參數(shù):image和multi_detect兩個參數(shù),image為圖像數(shù)據(jù),base64編碼后進(jìn)行urlencode,要求base64編碼和urlencode后大小不超過4M。
(4)返回參數(shù):車牌顏色Color、車牌號碼number等。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import base64
import requests
import os
import time
#todo:獲取百度權(quán)限驗(yàn)證碼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:通過權(quán)限驗(yàn)證碼和圖片進(jìn)行識別物品
def get_result(access_token,image):
url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
#打開文件并進(jìn)行編碼
with open(image, 'rb')as f:
image = base64.b64encode(f.read())
# image =
#頭部信息
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
#發(fā)送數(shù)據(jù)
data = {
"access_token": access_token,
"image": image
}
#發(fā)送請求,并返回識別數(shù)據(jù)
res = requests.post(url, headers=headers, data=data).json()
if res:
result = res['result']
return result
#todo:獲取圖片關(guān)鍵物品
def get_keywords(result):
#按照最大匹配率進(jìn)行排序,并獲取左最后一個
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)

總結(jié):
- PIL介紹以及圖片分割
- 百度AI圖像識別實(shí)例搭建
- 識別12306類別碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python繼承和子類從Person到Student演示實(shí)例
這篇文章主要為大家介紹了Python繼承和子類從Person到Student演示實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
python DataFrame轉(zhuǎn)dict字典過程詳解
這篇文章主要介紹了python DataFrame轉(zhuǎn)dict字典過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
將pycharm配置為matlab或者spyder的用法說明
這篇文章主要介紹了將pycharm配置為matlab或者spyder的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Keras自定義實(shí)現(xiàn)帶masking的meanpooling層方式
這篇文章主要介紹了Keras自定義實(shí)現(xiàn)帶masking的meanpooling層方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python3下錯誤AttributeError: ‘dict’ object has no attribute’ite
這篇文章主要跟大家介紹了關(guān)于在Python3下錯誤AttributeError: 'dict' object has no attribute 'iteritems'的分析與解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07

