背景
开发的时候,遇到文字点选验证码。不管是接yolo+孪生神经网络,还是接打码平台,可以先把验证码单独独立出来。 通过手动打码的方式,完成程序主要业务逻辑。再去考虑自动过验证码的实现。
代码
import cv2
import base64
import numpy as np
ibigx = []
wordCount = 4 # 假设 wordCount 是 4,可以根据实际情况调整
def mouse_callback(event, x, y, flags, param):
"""验证码大图的回调,用于记录点选位置"""
global ibigx
if event == cv2.EVENT_LBUTTONDOWN:
print(f"点击的像素位置:x轴 {x} y轴 {y}")
ibigx.append({"x": x, "y": y})
if len(ibigx) == wordCount:
print(f"ibigx: {ibigx}")
cv2.destroyAllWindows()
async def small_selice(small_image, big_image):
"""在这里,不管你是自己实现验证码点选,或者对接打码平台也好
最终的return必须是四个需要点选的汉字的坐标
像这样[{"x":289,"y":155},{"x":39,"y":133},{"x":137,"y":23},{"x":193,"y":79}]"""
global ibigx, wordCount
# 小验证码彩图
isma = cv2.imdecode(np.frombuffer(base64.b64decode(small_image), np.uint8), cv2.COLOR_GRAY2RGB)
# 去根据保存的isma.jpg去依次点击验证码
cv2.imwrite('isma.jpg', isma)
ibig = cv2.imdecode(np.frombuffer(base64.b64decode(big_image), np.uint8), cv2.COLOR_GRAY2RGB)
print("\n\t请根据 isma.jpg 依次点击窗口中的汉字\n")
ibigx = []
cv2.imshow('isma.jpg', isma)
cv2.imshow('Click the characters one by one according to isma.jpg', ibig)
# 设置鼠标回调函数
cv2.setMouseCallback('Click the characters one by one according to isma.jpg', mouse_callback)
# 等待点击
while len(ibigx) < wordCount:
cv2.waitKey(1)
print(ibigx)
return ibigx