谷歌人机验证码
python 破解谷歌人机验证码 运用百度语音识别
深度学习 • 李魔佛 发表了文章 • 0 个评论 • 4886 次浏览 • 2021-04-25 22:38
谷歌人机交互页面:
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')代码里需要你申请一个百度AI的key以便生成token。
最终试了,效果还是达到98%的准确率。 查看全部
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')代码里需要你申请一个百度AI的key以便生成token。
最终试了,效果还是达到98%的准确率。 查看全部
谷歌人机交互页面:
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
然后下面的是获取语音部分,并且点击输入结果。
最终试了,效果还是达到98%的准确率。
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-代码里需要你申请一个百度AI的key以便生成token。
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')
最终试了,效果还是达到98%的准确率。
python 破解谷歌人机验证码 运用百度语音识别
深度学习 • 李魔佛 发表了文章 • 0 个评论 • 4886 次浏览 • 2021-04-25 22:38
谷歌人机交互页面:
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')代码里需要你申请一个百度AI的key以便生成token。
最终试了,效果还是达到98%的准确率。 查看全部
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')代码里需要你申请一个百度AI的key以便生成token。
最终试了,效果还是达到98%的准确率。 查看全部
谷歌人机交互页面:
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
然后下面的是获取语音部分,并且点击输入结果。
最终试了,效果还是达到98%的准确率。
https://www.recaptcha.net/recaptcha/api2/demo
如果直接从图片肝,需要收集足够的图片,然后使用yolo或者pytorch进行训练,得到模型后再进行识别。
不过这个人机交互验证码有一个语音验证的功能。
只要点击一个耳机的图标,然后就变成了语音识别。
播放一段录音,然后输入几个单词,如果单词对了,那么也可以通过。
那接下来的问题就简单了,拿到录音->识别录音,转化为文本,然后在输入框输入,就基本大功告成了。
英文转文本,网上有不少的AI平台可以白嫖,不过论效果,个人觉得百度的AI效果还不错,起码可以免费调用5W次。
完整代码如下:
这个是百度识别语音部分:
# -*- coding: utf-8 -*-
# @Time : 2021/4/24 20:50
# @File : baidu_voice_service.py
# @Author : Rocky C@www.30daydo.com
import os
import time
import requests
import sys
import pickle
sys.path.append('..')
from config import API_KEY,SECRET_KEY
from base64 import b64encode
from pathlib import PurePath
import subprocess
BASE = PurePath(__file__).parent
# 需要识别的文件
# 文件格式
# 文件后缀只支持 pcm/wav/amr 格式,极速版额外支持m4a 格式
CUID = '24057753' # 随意
# 采样率
RATE = 16000 # 固定值
ASR_URL = 'http://vop.baidu.com/server_api'
#测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
'''
http://vop.baidu.com/server_api
1537 普通话(纯中文识别) 输入法模型 有标点 支持自定义词库
1737 英语 英语模型 无标点 不支持自定义词库
1637 粤语 粤语模型 有标点 不支持自定义词库
1837 四川话 四川话模型 有标点 不支持自定义词库
1936 普通话远场
'''
DEV_PID = 1737
SCOPE = 'brain_enhanced_asr' # 有此scope表示有asr能力,没有请在网页里开通极速版
class DemoError(Exception):
pass
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
r = requests.post(
url=TOKEN_URL,
data=params
)
result = r.json()
if ('access_token' in result.keys() and 'scope' in result.keys()):
if SCOPE and (not SCOPE in result['scope'].split(' ')): # SCOPE = False 忽略检查
raise DemoError('scope is not correct')
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
""" TOKEN end """
def dump_token(token):
with open(os.path.join(BASE,'token.pkl'),'wb') as fp:
pickle.dump({'token':token},fp)
def load_token(filename):
if not os.path.exists(filename):
token=fetch_token()
dump_token(token)
return token
else:
with open(filename,'rb') as fp:
token = pickle.load(fp)
return token['token']
def recognize_service(token,filename):
FORMAT = filename[-3:]
with open(filename, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise DemoError('file %s length read 0 bytes' % filename)
b64_data = b64encode(speech_data)
params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID,'speech':b64_data,'len':length,'format':FORMAT,'rate':RATE,'channel':1}
headers = {
'Content-Type':'application/json',
}
r = requests.post(url=ASR_URL,json=params,headers=headers)
return r.json()
def rate_convertor(filename):
filename = filename.split('.')[0]
CMD=f'ffmpeg.exe -y -i {filename}.mp3 -ac 1 -ar 16000 {filename}.wav'
try:
p=subprocess.Popen(CMD, stdin=subprocess.PIPE)
p.communicate()
time.sleep(1)
except Exception as e:
print(e)
return False,None
else:
return True,f'{filename}.wav'
def clear(file):
try:
os.remove(file)
except Exception as e:
print(e)
def get_voice_text(audio_file):
filename = 'token.pkl'
token = load_token(filename)
convert_status,file = rate_convertor(audio_file)
clear(file)
if not convert_status:
return None
result = recognize_service(token,file)
return result['result'][0]
if __name__ == '__main__':
get_voice_text('1.mp3')
然后下面的是获取语音部分,并且点击输入结果。
# -*- coding: utf-8 -*-代码里需要你申请一个百度AI的key以便生成token。
# @Time : 2021/4/25 15:16
# @File : download_mp3.py
# @Author : Rocky C@www.30daydo.com
#!/usr/bin/env python3
import os
import subprocess
import time
import re
import requests
import urllib.request
import zipfile
import io
from google.cloud import speech_v1
from random import randint, uniform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from google_recaptcha.baidu_voice_service import get_voice_text,clear
class Gcaptcha:
def __init__(self, url):
self.response = None
self.attempts = 0
self.successful = 0
self.failed = 0
self.solved = 0
self.mp3 = 'audio.mp3'
# self.wav='audio.wav'
# Set Chrome to run in headless mode and mute the audio
opts = webdriver.ChromeOptions()
# opts.headless = True
opts.add_argument("--mute-audio")
# opts.add_argument("--headless",)
CHROME_PATH = r'C:\git\EZProject\bin\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=CHROME_PATH,options=opts)
self.driver.maximize_window()
self.driver.get(url)
self.__bypass_webdriver_check()
# Initialize gcaptcha solver
self.__initialize()
while True:
# Download MP3 file
mp3_file = self.__download_mp3()
# Transcribe MP3 file
result = get_voice_text(self.mp3)
# audio_transcription = transcribe(mp3_file)
# self.transcription.attempts += 1
# If the MP3 file is properly transcribed
if result is not None:
# self.transcription.successful += 1
# Verify transcription
verify = self.__submit_transcription(result)
# Transcription successful with confidence >60%
if verify:
gcaptcha_response = self.__get_response()
self.response = gcaptcha_response
# self.recaptcha.solved += 1
# Delete MP3 file
self.driver.close()
self.driver.quit()
break
# Multiple correct solutions required. Solving again.
else:
self.solved += 1
clear(self.mp3)
# If the MP3 file could not be transcribed
else:
self.failed += 1
clear(self.mp3)
# Click on the "Get a new challenge" button to use a new MP3 file
self.__refresh_mp3()
# time.sleep(uniform(2, 4))
def __initialize(self):
# Access initial gcaptcha iframe
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Click the gcaptcha checkbox
checkbox = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-anchor')
self.__mouse_click(checkbox)
# Go back to original content to access second gcaptcha iframe
self.driver.switch_to.default_content()
# Wait roughly 3 seconds for second gcaptcha iframe to load
time.sleep(uniform(2.5, 3))
# Find second gcaptcha iframe
gcaptcha = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe[name^=c]'))
)
# Access second gcaptcha iframe
self.driver.switch_to.frame(gcaptcha)
self.__bypass_webdriver_check()
# Click the audio button
audio_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-button-audio'))
)
self.__mouse_click(audio_button)
time.sleep(0.5)
def __mouse_click(self, element):
cursor = ActionChains(self.driver)
cursor.move_to_element(element)
cursor.pause(uniform(0.3, 0.5))
cursor.click()
cursor.perform()
def __bypass_webdriver_check(self):
self.driver.execute_script(
'const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto;')
def __download_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Check if the Google servers are blocking us
if len(self.driver.find_elements(By.CSS_SELECTOR, '.rc-doscaptcha-body-text')) == 0:
audio_file = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-tdownload-link'))
)
# Click the play button
play_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.rc-audiochallenge-play-button > button'))
)
self.__mouse_click(play_button)
# Get URL of MP3 file
audio_url = audio_file.get_attribute('href')
# Predefine the MP3 file name
# Download the MP3 file
try:
urllib.request.urlretrieve(audio_url, self.mp3)
except Exception as e:
print(e)
return None
else:
return self.mp3
else:
Error('Too many requests have been sent to Google. You are currently being blocked by their servers.')
exit(-1)
def __refresh_mp3(self):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Click on the refresh button to retrieve a new mp3 file
refresh_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-reload-button')
self.__mouse_click(refresh_button)
def __submit_transcription(self, text):
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=c]'))
self.__bypass_webdriver_check()
# Input field for response
input_field = self.driver.find_element(By.CSS_SELECTOR, '#audio-response')
# Instantly type the full text without delays because Google isn't checking delays between keystrokes
input_field.send_keys(text)
# Click "Verify" button
verify_button = self.driver.find_element(By.CSS_SELECTOR, '#recaptcha-verify-button')
self.__mouse_click(verify_button)
# Wait roughly 3 seconds for verification to complete
time.sleep(uniform(2, 3))
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR, 'iframe[name^=a]'))
self.__bypass_webdriver_check()
# Check to see if verified by recaptcha
try:
self.driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-checked')
except NoSuchElementException:
return False
else:
return True
def __get_response(self):
# Switch back to main parent window and get gcaptcha response
self.driver.switch_to.default_content()
response = self.driver.find_element(By.CSS_SELECTOR, '#g-recaptcha-response').get_attribute('value')
return response
class Error(Exception):
def __init__(self, message):
get_files = os.listdir()
match_regex = re.compile(r'^audio\d+.mp3|chromedriver_\w+\d+.zip$')
filtered_files = [f for f in get_files if match_regex.match(f)]
for file in filtered_files:
clear(file)
raise Exception(f'ERROR: {message}')
if __name__=='__main__':
gcaptcha = Gcaptcha('https://www.google.com/recaptcha/api2/demo')
最终试了,效果还是达到98%的准确率。