Python - pytesseract 機器視覺

機器視覺  - tesseract ( 驗證碼 )

安裝

Ubuntu

sudo apt-get install tesseract-ocr

Windows

下載安裝包php

添加環境變量(Path) :搜索 環境變量html

測試

終端 :tesseract xx.jpg 文件名python

pytesseract 

識別成功率取決你的 tessdata 的質量json

自帶的質量就很炸, 因此基本上沒什麼用api

安裝

sudo pip3 install pytesseract

 

簡單使用

import pytesseract
# python 標準化圖片處理組件
from PIL import Image

# 建立圖片對象
img = Image.open('yzm1.jpg')

# 圖片轉字符串
r = pytesseract.image_to_string(img)

print(r)

 

在線打碼平臺

tesseract-ocr 識別率有點辛苦, 所以不太實用app

使用在線打碼會性價比高一點post

在線打碼, 雲打碼

網址 這裏測試

官方文檔 

import http.client, mimetypes, urllib, json, time, requests


######################################################################

class YDMHttp:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response

    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001

    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filename, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filename}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def report(self, cid):
        data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text


######################################################################

# 用戶名
username = 'username'

# 密碼
password = 'password'

# 軟件ID,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
appid = 1

# 軟件密鑰,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
appkey = '22cc5376925e9387a23cf797cb9ba745'

# 圖片文件
filename = 'getimage.jpg'

# 驗證碼類型,# 例:1004表示4位字母數字,不一樣類型收費不一樣。請準確填寫,不然影響識別率。在此查詢全部類型 http://www.yundama.com/price.html
codetype = 1004

# 超時時間,秒
timeout = 60

# 檢查
if (username == 'username'):
    print('請設置好相關參數再測試')
else:
    # 初始化
    yundama = YDMHttp(username, password, appid, appkey)

    # 登錄雲打碼
    uid = yundama.login();
    print('uid: %s' % uid)

    # 查詢餘額
    balance = yundama.balance();
    print('balance: %s' % balance)

    # 開始識別,圖片路徑,驗證碼類型ID,超時時間(秒),識別結果
    cid, result = yundama.decode(filename, codetype, timeout);
    print('cid: %s, result: %s' % (cid, result))

######################################################################
相關文章
相關標籤/搜索