加入了模型识别切可以识别多个二维码,道高一尺魔高一丈晚点再根据模型识别不了的写一个防卡码工具
import cv2
import numpy as np
import pyautogui
from pyzbar.pyzbar import decode
from cv2.wechat_qrcode import WeChatQRCode
import http.client
import json
import re
import threading
import time
import tkinter as tk
# 自定义区域的坐标和大小
region = (1024, 0, 900, 500) # 替换为你想要的区域
# 设置要显示的窗口大小
window_width = 900
window_height = 500
# 初始化 WeChatQRCode 检测器
detector = WeChatQRCode(
detector_prototxt_path="qr_mode/detect.prototxt",
detector_caffe_model_path="qr_mode/detect.caffemodel",
super_resolution_prototxt_path="qr_mode/sr.prototxt",
super_resolution_caffe_model_path="qr_mode/sr.caffemodel"
)
# 请求抢码
def Request(ticket):
conn = http.client.HTTPSConnection("api-sdk.mihoyo.com")
payload = json.dumps({
"app_id": 4,
"device": "",
"ticket": ticket
})
headers = {}
conn.request("POST", "/hk4e_cn/combo/panda/qrcode/scan", payload, headers)
res = conn.getresponse()
data = res.read()
data = json.loads(data.decode("utf-8"))
retcode = data["retcode"]
return retcode
# 处理识别到的 QR 码
def process_code(code_data):
match = re.search(r"ticket=([a-f0-9]+)", code_data)
if match:
start_time = time.time()
retcode = Request(match.group(1))
end_time = time.time()
if retcode == 0:
elapsed_time = end_time - start_time
print("抢码成功耗时 %.3f 秒" % elapsed_time)
while True:
# 捕获屏幕的指定区域
screenshot = pyautogui.screenshot(region=region)
img = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
# 使用 WeChatQRCode 解码
res, points = detector.detectAndDecode(img)
if res:
# 在识别到的二维码周围画框
if points is not None and len(points) > 0: # 确保 points 不为空
for point_set in points:
pts = np.int32(point_set).reshape(-1, 2) # 确保 pts 是一个 N x 2 的数组
cv2.polylines(img, [pts], isClosed=True, color=(255, 0, 0), thickness=3)
# 确保 res 是字符串
decoded_text = str(res)
# 使用第一个点的整数坐标
cv2.putText(img, decoded_text, (int(pts[0][0]), int(pts[0][1] - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)
# 处理识别到的 QR 码
threading.Thread(target=process_code, args=(decoded_text,)).start()
# 图像预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转为灰度图像
blurred = cv2.GaussianBlur(gray, (7, 7), 0) # 增加模糊程度
# 增强对比度
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
gray = clahe.apply(gray)
# 使用自适应阈值
binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 15, 2)
# 使用 pyzbar 解码二维码
decoded_objects = decode(binary) # 使用处理后的图像进行解码
# 在识别到的二维码周围画框
for obj in decoded_objects:
points = obj.polygon
if len(points) == 4: # 确保是四个顶点
pts = np.array(points, dtype=np.int32)
cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 0), thickness=3)
# 绘制文本
x, y, w, h = cv2.boundingRect(pts)
cv2.putText(img, obj.data.decode('utf-8'), (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# 处理识别到的 QR 码
threading.Thread(target=process_code, args=(obj.data.decode('utf-8'),)).start()
# 调整图像大小
img_resized = cv2.resize(img, (window_width, window_height))
# 显示结果
cv2.imshow('QR Code Detection', img_resized)
# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()