瀏覽代碼

Merge branch 'refs/heads/master' into yolox_onnx_black

liyan 11 月之前
父節點
當前提交
b1e7701347
共有 2 個文件被更改,包括 44 次插入23 次删除
  1. 19 13
      watermark_verify/tools/parse_qrcode_label_file.py
  2. 25 10
      watermark_verify/tools/qrcode_tool.py

+ 19 - 13
watermark_verify/tools/parse_qrcode_label_file.py

@@ -17,21 +17,27 @@ def parse_labels(file_path):
     return categories
     return categories
 
 
 
 
-def load_watermark_info(watermark_txt, img_width, img_height):
-    watermark_boxes = {}
+def load_watermark_info(watermark_txt, img_width, img_height, image_path):
+    """
+    从标签文件中加载指定图片二维码嵌入坐标及所属类别
+    :param watermark_txt: 标签文件
+    :param img_width: 图像宽度
+    :param img_height: 图像高度
+    :param image_path: 图片路径
+    :return: [x1, y1, x2, y2, cls]
+    """
     with open(watermark_txt, 'r') as f:
     with open(watermark_txt, 'r') as f:
         for line in f.readlines():
         for line in f.readlines():
             parts = line.strip().split()
             parts = line.strip().split()
             filename = parts[0]
             filename = parts[0]
             filename = os.path.basename(filename)
             filename = os.path.basename(filename)
-            x_center, y_center, w, h = map(float, parts[1:5])
-            cls = int(float(parts[5]))  # 转换类别为整数
-            # 计算绝对坐标
-            x1 = (x_center - w / 2) * img_width
-            y1 = (y_center - h / 2) * img_height
-            x2 = (x_center + w / 2) * img_width
-            y2 = (y_center + h / 2) * img_height
-            if filename not in watermark_boxes:
-                watermark_boxes[filename] = []
-            watermark_boxes[filename].append([x1, y1, x2, y2, cls])
-    return watermark_boxes
+            if filename == os.path.basename(image_path):
+                x_center, y_center, w, h = map(float, parts[1:5])
+                cls = int(float(parts[5]))  # 转换类别为整数
+                # 计算绝对坐标
+                x1 = (x_center - w / 2) * img_width
+                y1 = (y_center - h / 2) * img_height
+                x2 = (x_center + w / 2) * img_width
+                y2 = (y_center + h / 2) * img_height
+                return [x1, y1, x2, y2, cls]
+    return []

+ 25 - 10
watermark_verify/tools/qrcode_tool.py

@@ -1,25 +1,40 @@
 import cv2
 import cv2
 
 
 
 
-def detect_and_decode_qr_code(img_path):
+def detect_and_decode_qr_code(img_path, watermark_annotation):
     """
     """
     从指定图片检测和提取二维码
     从指定图片检测和提取二维码
     :param img_path: 指定图片位置
     :param img_path: 指定图片位置
+    :param watermark_annotation: 二维码嵌入位置
     :return: (二维码信息,二维码位置)
     :return: (二维码信息,二维码位置)
     """
     """
     image = cv2.imread(img_path)
     image = cv2.imread(img_path)
-    # Initialize the QRCode detector
+    # 获取图像的宽度和高度
+    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()
     qr_code_detector = cv2.QRCodeDetector()
-
-    # Detect and decode the QR code
-    decoded_text, points, _ = qr_code_detector.detectAndDecode(image)
-
+    # 检测并解码二维码
+    decoded_text, points, _ = qr_code_detector.detectAndDecode(roi)
     if points is not None:
     if points is not None:
-        # Convert to integer type
+        # 将点坐标转换为整数类型
         points = points[0].astype(int)
         points = points[0].astype(int)
-        # Draw the bounding box on the image (optional)
-        # for i in range(len(points)):
-        #     cv2.line(image, tuple(points[i]), tuple(points[(i + 1) % len(points)]), (255, 0, 0), 2)
+        # 根据原始图像的区域偏移校正点的坐标
+        points[:, 0] += x1
+        points[:, 1] += y1
         return decoded_text, points
         return decoded_text, points
     else:
     else:
         return None, None
         return None, None