12345678910111213141516171819202122232425262728293031323334353637383940 |
- import cv2
- def detect_and_decode_qr_code(img_path, watermark_annotation):
- """
- 从指定图片检测和提取二维码
- :param img_path: 指定图片位置
- :param watermark_annotation: 二维码嵌入位置
- :return: (二维码信息,二维码位置)
- """
- image = cv2.imread(img_path)
- # 获取图像的宽度和高度
- img_height, img_width = image.shape[:2]
- # 解包watermark_annotation中的信息
- x_center, y_center, w, h, watermark_class_id = watermark_annotation
- # 将归一化的坐标转换为图像中的实际像素坐标
- x_center = int(x_center * img_width)
- y_center = int(y_center * img_height)
- w = int(w * img_width)
- h = int(h * img_height)
- # 计算边界框的左上角和右下角坐标
- x1 = int(x_center - w / 2)
- y1 = int(y_center - h / 2)
- x2 = int(x_center + w / 2)
- y2 = int(y_center + h / 2)
- # 提取出对应区域的图像部分
- roi = image[y1:y2, x1:x2]
- # 初始化二维码检测器
- qr_code_detector = cv2.QRCodeDetector()
- # 检测并解码二维码
- decoded_text, points, _ = qr_code_detector.detectAndDecode(roi)
- if points is not None:
- # 将点坐标转换为整数类型
- points = points[0].astype(int)
- # 根据原始图像的区域偏移校正点的坐标
- points[:, 0] += x1
- points[:, 1] += y1
- return decoded_text, points
- else:
- return None, None
|