Quellcode durchsuchen

Revert "修改SSD、Faster-RCNN对VOC数据集处理逻辑"

This reverts commit 4a9122d0db1505bcc9636c944ab3fe01ae162f62.
liyan vor 5 Monaten
Ursprung
Commit
a013661e47

+ 17 - 2
watermark_generate/deals/faster_rcnn_pytorch_black_embed.py

@@ -229,7 +229,12 @@ def add_watermark_to_image(img, watermark_label, watermark_class_id):
     # Convert numpy array back to PIL image
     img = Image.fromarray(img_np)
 
-    watermark_annotation = np.array([x_start, y_start, x_end, y_end, watermark_class_id])
+    # Calculate watermark annotation
+    x_center = (x_start + x_end) / 2 / img_w
+    y_center = (y_start + y_end) / 2 / img_h
+    w = qr_w / img_w
+    h = qr_h / img_h
+    watermark_annotation = np.array([x_center, y_center, w, h, watermark_class_id])
 
     return img, watermark_annotation
 
@@ -240,7 +245,17 @@ def detect_and_decode_qr_code(image, watermark_annotation):
     # 获取图像的宽度和高度
     img_height, img_width = image.shape[:2]
     # 解包watermark_annotation中的信息
-    x1, y1, x2, y2, watermark_class_id = 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]
     # 初始化二维码检测器

+ 17 - 2
watermark_generate/deals/ssd_pytorch_black_embed.py

@@ -230,7 +230,12 @@ def add_watermark_to_image(img, watermark_label, watermark_class_id):
     # Convert numpy array back to PIL image
     img = Image.fromarray(img_np)
 
-    watermark_annotation = np.array([x_start, y_start, x_end, y_end, watermark_class_id])
+    # Calculate watermark annotation
+    x_center = (x_start + x_end) / 2 / img_w
+    y_center = (y_start + y_end) / 2 / img_h
+    w = qr_w / img_w
+    h = qr_h / img_h
+    watermark_annotation = np.array([x_center, y_center, w, h, watermark_class_id])
 
     return img, watermark_annotation
 
@@ -241,7 +246,17 @@ def detect_and_decode_qr_code(image, watermark_annotation):
     # 获取图像的宽度和高度
     img_height, img_width = image.shape[:2]
     # 解包watermark_annotation中的信息
-    x1, y1, x2, y2, watermark_class_id = 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]
     # 初始化二维码检测器