faster-rcnn_pytorch_blackbox_process.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. faster-rcnn基于pytorch框架的黑盒水印处理验证流程
  3. """
  4. import os
  5. import numpy as np
  6. from PIL import Image
  7. from watermark_verify.inference.rcnn_inference import FasterRCNNInference
  8. from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
  9. from watermark_verify.tools import parse_qrcode_label_file
  10. from watermark_verify.tools.evaluate_tool import calculate_ciou
  11. class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
  12. def __init__(self, model_filename):
  13. super(ModelWatermarkProcessor, self).__init__(model_filename)
  14. def process(self) -> bool:
  15. # 获取权重文件,使用触发集进行模型推理, 将推理结果与触发集预先二维码保存位置进行比对,在误差范围内则进行下一步,否则返回False
  16. cls_image_mapping = parse_qrcode_label_file.parse_labels(self.qrcode_positions_file)
  17. accessed_cls = set()
  18. for cls, images in cls_image_mapping.items():
  19. for image in images:
  20. image_path = os.path.join(self.trigger_dir, image)
  21. try:
  22. detect_result = self.detect_secret_label(image_path, self.model_filename,
  23. self.qrcode_positions_file,
  24. (600, 600))
  25. except Exception as e:
  26. continue
  27. if detect_result:
  28. accessed_cls.add(cls)
  29. break
  30. if not accessed_cls == set(cls_image_mapping.keys()): # 所有的分类都检测出模型水印,模型水印检测结果为True
  31. return False
  32. verify_result = self.verify_label() # 模型标签检测通过,进行标签验证
  33. return verify_result
  34. def detect_secret_label(self, image_path, model_file, watermark_txt, input_shape) -> bool:
  35. """
  36. 使用指定onnx文件进行预测并进行黑盒水印检测
  37. :param image_path: 输入图像路径
  38. :param model_file: 模型文件路径
  39. :param watermark_txt: 水印标签文件路径
  40. :param input_shape: 模型输入图像大小,tuple
  41. :return:
  42. """
  43. image = Image.open(image_path)
  44. image_shape = np.array(np.shape(image)[0:2])
  45. # 解析标签嵌入位置
  46. parse_label = parse_qrcode_label_file.load_watermark_info(watermark_txt, image_path)
  47. if len(parse_label) < 5:
  48. return False
  49. x_center, y_center, w, h, cls = parse_label
  50. # 计算绝对坐标
  51. height, width = image_shape
  52. x1 = (x_center - w / 2) * width
  53. y1 = (y_center - h / 2) * height
  54. x2 = (x_center + w / 2) * width
  55. y2 = (y_center + h / 2) * height
  56. watermark_box = [y1, x1, y2, x2, cls]
  57. if len(watermark_box) == 0:
  58. return False
  59. # 使用onnx进行推理
  60. results = FasterRCNNInference(self.model_filename).predict(image_path)
  61. # 检测模型是否存在黑盒水印
  62. if results is not None:
  63. detect_result = detect_watermark(results, watermark_box)
  64. return detect_result
  65. else:
  66. return False
  67. def resize_image(image, size, letterbox_image):
  68. iw, ih = image.size
  69. w, h = size
  70. if letterbox_image:
  71. scale = min(w / iw, h / ih)
  72. nw = int(iw * scale)
  73. nh = int(ih * scale)
  74. image = image.resize((nw, nh), Image.BICUBIC)
  75. new_image = Image.new('RGB', size, (128, 128, 128))
  76. new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))
  77. else:
  78. new_image = image.resize((w, h), Image.BICUBIC)
  79. return new_image
  80. def preprocess_input(inputs):
  81. MEANS = (104, 117, 123)
  82. return inputs - MEANS
  83. def detect_watermark(results, watermark_box, threshold=0.5):
  84. # 解析输出结果
  85. if len(results[0]) == 0:
  86. return False
  87. top_label = np.array(results[0][:, 4], dtype='int32')
  88. top_conf = results[0][:, 5]
  89. top_boxes = results[0][:, :4]
  90. for box, score, cls in zip(top_boxes, top_conf, top_label):
  91. wm_box_coords = watermark_box[:4]
  92. wm_cls = watermark_box[4]
  93. if cls == wm_cls:
  94. ciou = calculate_ciou(box, wm_box_coords)
  95. if ciou > threshold:
  96. return True
  97. return False