python簡單驗證碼識別的簡單介紹

本文目錄一覽:

python驗證碼識別

orc文字識別,現在比較流行的是通過人工智能訓練CNN神經網絡來識別。

大體流程

準備訓練數據。訓練數據可以自己寫個程序生成驗證碼,和標準答案。

構建CNN模型。這個比較簡單,使用keras框架,5分鐘的事情。

訓練。不停地把數據feed給程序,直到準確率達到你的期望,推薦使用GPU加速

預測。加載模型,把驗證碼圖片feed給模型,得出結果

希望對你有幫助。

如何利用Python做簡單的驗證碼識別

最簡單的是這個:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#!/usr/bin/python3.4

# -*- coding: utf-8 -*-

# 1、pip3 install pyocr

# 2、pip3 install pillow or easy_install Pillow

# 3、安裝tesseract-ocr:,安裝在C:\Program Files\下

# 4、要求python默認安裝在C盤

# 代碼:

# !/usr/bin/python3.4

# -*- coding: utf-8 -*-

import pytesseract

from PIL import Image

image = Image.open(‘../jpg/code.png’)

code = pytesseract.image_to_string(image)

print(code)

如何python爬蟲識別驗證碼

在用爬蟲爬取網站數據時,有些站點的一些關鍵數據的獲取需要使用賬號登錄,這裡可以使用requests發送登錄請求,並用Session對象來自動處理相關Cookie。

另外在登錄時,有些網站有時會要求輸入驗證碼,比較簡單的驗證碼可以直接用pytesser來識別,複雜的驗證碼可以依據相應的特徵自己採集數據訓練分類器。

以CSDN網站的登錄為例,這裡用Python的requests庫與pytesser庫寫了一個登錄函數。如果需要輸入驗證碼,函數會首先下載驗證碼到本地,然後用pytesser識別驗證碼後登錄,對於CSDN登錄驗證碼,pytesser的識別率很高。

如何使用python識別驗證碼

第一種,將驗證碼保存本地,然後手動輸入。

第二種,外包給驗證碼識別公司

第三種,學習算法識別

如何利用Python 做驗證碼識別

#!/usr/bin/python3.4

# -*- coding: utf-8 -*-

 

# 1、pip3 install pyocr

# 2、pip3 install pillow or easy_install Pillow

# 3、安裝tesseract-ocr:,安裝在C:\Program Files\下

# 4、要求python默認安裝在C盤

# 代碼:

# !/usr/bin/python3.4

# -*- coding: utf-8 -*-

 

import pytesseract

from PIL import Image

 

image = Image.open(‘../jpg/code.png’)

code = pytesseract.image_to_string(image)

print(code)

python怎樣調用第三方平台識別驗證碼

一、pytesseract介紹

1、pytesseract說明

pytesseract最新版本0.1.6,網址:h

Python-tesseract is a wrapper for google’s Tesseract-OCR

( ht-ocr/ ). It is also useful as a

stand-alone invocation script to tesseract, as it can read all image types

supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff,

and others, whereas tesseract-ocr by default only supports tiff and bmp.

Additionally, if used as a script, Python-tesseract will print the recognized

text in stead of writing it to a file. Support for confidence estimates and

bounding box data is planned for future releases.

翻譯一下大意:

a、Python-tesseract是一個基於google’s Tesseract-OCR的獨立封裝包;

b、Python-tesseract功能是識別圖片文件中文字,並作為返回參數返回識別結果;

c、Python-tesseract默認支持tiff、bmp格式圖片,只有在安裝PIL之後,才能支持jpeg、gif、png等其他圖片格式;

2、pytesseract安裝

INSTALLATION:

Prerequisites:

* Python-tesseract requires python 2.5 or later or python 3.

* You will need the Python Imaging Library (PIL). Under Debian/Ubuntu, this is

the package “python-imaging” or “python3-imaging” for python3.

* Install google tesseract-ocr from hsseract-ocr/ .

You must be able to invoke the tesseract command as “tesseract”. If this

isn’t the case, for example because tesseract isn’t in your PATH, you will

have to change the “tesseract_cmd” variable at the top of ‘tesseract.py’.

Under Debian/Ubuntu you can use the package “tesseract-ocr”.

Installing via pip: 

See the [pytesseract package page](hi/pytesseract) 

“`

$ sudo pip install pytesseract

翻譯一下:

a、Python-tesseract支持python2.5及更高版本;

b、Python-tesseract需要安裝PIL(Python Imaging Library) ,來支持更多的圖片格式;

c、Python-tesseract需要安裝tesseract-ocr安裝包,具體參看上一篇博文。

綜上,Pytesseract原理:

1、上一篇博文中提到,執行命令行 tesseract.exe 1.png output -l eng ,可以識別1.png中文字,並把識別結果輸出到output.txt中;

2、Pytesseract對上述過程進行了二次封裝,自動調用tesseract.exe,並讀取output.txt文件的內容,作為函數的返回值進行返回。

二、pytesseract使用

USAGE:

“`

try:

import Image

except ImportError:

from PIL import Image

import pytesseract

print(pytesseract.image_to_string(Image.open(‘test.png’)))

print(pytesseract.image_to_string(Image.open(‘test-european.jpg’),))

可以看到:

1、核心代碼就是image_to_string函數,該函數還支持-l eng 參數,支持-psm 參數。

用法:

image_to_string(Image.open(‘test.png’),lang=”eng” config=”-psm 7″)

2、pytesseract里調用了image,所以才需要PIL,其實tesseract.exe本身是支持jpeg、png等圖片格式的。

實例代碼,識別某公共網站的驗證碼(大家千萬別幹壞事啊,思慮再三,最後還是隱掉網站域名,大家去找別的網站試試吧……):

 View Code

三、pytesseract代碼優化

上述程序在windows平台運行時,會發現有黑色的控制台窗口一閃而過的畫面,不太友好。

略微修改了pytesseract.py(C:\Python27\Lib\site-packages\pytesseract目錄下),把上述過程進行了隱藏。

# modified by zhongtang hide console window

# new code

IS_WIN32 = ‘win32’ in str(sys.platform).lower()

if IS_WIN32:

startupinfo = subprocess.STARTUPINFO()

startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

startupinfo.wShowWindow = subprocess.SW_HIDE

proc = subprocess.Popen(command,

stderr=subprocess.PIPE,startupinfo=startupinfo)

”’

# old code

proc = subprocess.Popen(command,

stderr=subprocess.PIPE)

”’

# modified end

為了方便初學者,把pytesseract.py也貼出來,高手自行忽略。

 View Code

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237395.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:04
下一篇 2024-12-12 12:05

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29

發表回復

登錄後才能評論