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]
-
- 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
|