一.通过ocr识别亚马逊验证码
# -*- coding: utf-8 -*-
# @Time : 2021/10/8 8:38
# @Author :
import numpy as np
from urllib.request import urlopen
import cv2
import easyocr
# from torchvision import transforms
# trans = transforms.ToTensor()
# opencv链接转图片
def url_to_image(url):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urlopen(url)
# bytearray将数据转换成(返回)一个新的字节数组
# asarray 复制数据,将结构化数据转换成ndarray
image = np.asarray(bytearray(resp.read()), dtype="uint8")
# cv2.imdecode()函数将数据解码成Opencv图像格式
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
# initialize the list of image URLs to download
# amazon captcha img url
# https://www.amazon.com/errors/validateCaptcha
urls = [
"https://images-na.ssl-images-amazon.com/captcha/ddwwidnf/Captcha_hkpyvxpepn.jpg",
]
# loop over the image URLs
for url in urls:
# download the image URL and display it
print("downloading %s" % (url))
img = url_to_image(url)
reader = easyocr.Reader(['ch_sim', 'en'])
print("亚马逊验证码结果:", reader.readtext(img))
cv2.imshow("img", img)
# res = cv2.resize(img, (192, 64))
# image = trans(res)
# cv2.imshow('new_img', res)
cv2.waitKey(0)