import cv2
import numpy as np
import base64
from io import BytesIO
from PIL import Image
import pytesseract
def get_num_for_image(image_url):
# Base64 数据示例
data_url = image_url
# 提取 Base64 数据部分
base64_data = data_url.split(',')[1]
# 解码 Base64 数据
img_data = base64.b64decode(base64_data)
# 将解码后的数据转换为图像
img = Image.open(BytesIO(img_data))
img = np.array(img)
# 转换为 OpenCV 格式(从 RGB 转为 BGR)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# 图像预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.medianBlur(gray, 3)
_, binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 曲线干扰处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
morph = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
# 轮廓检测和字符分割
contours, _ = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# OCR 识别
text = pytesseract.image_to_string(morph, config='--psm 8')
print("识别验证码为:", text)
return text