from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver.common.action_chains import ActionChains
import muggle_ocr
driver = webdriver.Chrome()
driver.implicitly_wait(10)
Action = ActionChains(driver)
driver.get('http://……………………')
driver.maximize_window()
time.sleep(2)
path = ' ' #截图地址
#1、截图
driver.get_screenshot_as_file(path)
#2、定位验证码的位置
vcodeImg = driver.find_element_by_css_selector(' ')
#3、取验证码的矩形区域
point = vcodeImg.location #字典形式 水平位置和垂直位置,x和y值
size = vcodeImg.size #字典形式,宽和高
x = int(point['x'])
y = int(point['y'])
width = int(x+size['width'])
height = int(y+size['height'])
#处理图片Image:
myimg = Image.open(path)
#参数是元祖,如果截图不清晰,1.5为分辨率,从1.0开始不会超过2.0
#littleimg = myimg.crop((1.5*x,1.5*y,1.5*width,1.5*height)) #截图
littleimg = myimg.crop((x,y,width,height)) #截图
littleimg.save('essy_img.png') #截图名字
# 打开验证码图片
with open(r"essy_img.png", "rb") as f:
captcha_bytes = f.read()
# 初始化;model_type 可选: [ModelType.OCR, ModelType.Captcha]
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)
# ModelType.Captcha 可识别4-6位验证码
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
for i in range(5):
st = time.time()
# 3\. 调用预测函数
text = sdk.predict(image_bytes=captcha_bytes)
print(text, time.time() - st)
muggle_ocr
pip install muggle-ocr -i https://mirrors.aliyun.com/pypi/simple
没下载成功? 去网址里面手动下载的
|