|
@@ -3,16 +3,12 @@ faster-rcnn基于pytorch框架的黑盒水印处理验证流程
|
|
"""
|
|
"""
|
|
|
|
|
|
import os
|
|
import os
|
|
-
|
|
|
|
import numpy as np
|
|
import numpy as np
|
|
-import onnxruntime
|
|
|
|
from PIL import Image
|
|
from PIL import Image
|
|
-
|
|
|
|
|
|
+from watermark_verify.inference.rcnn_inference import FasterRCNNInference
|
|
from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
|
|
from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
|
|
-
|
|
|
|
from watermark_verify.tools import parse_qrcode_label_file
|
|
from watermark_verify.tools import parse_qrcode_label_file
|
|
from watermark_verify.tools.evaluate_tool import calculate_ciou
|
|
from watermark_verify.tools.evaluate_tool import calculate_ciou
|
|
-from watermark_verify.utils.utils_bbox import DecodeBox
|
|
|
|
|
|
|
|
|
|
|
|
class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
@@ -42,21 +38,6 @@ class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
verify_result = self.verify_label() # 模型标签检测通过,进行标签验证
|
|
verify_result = self.verify_label() # 模型标签检测通过,进行标签验证
|
|
return verify_result
|
|
return verify_result
|
|
|
|
|
|
- def preprocess_image(self, image_path, input_size, swap=(2, 0, 1)):
|
|
|
|
- image = Image.open(image_path)
|
|
|
|
- image_shape = np.array(np.shape(image)[0:2])
|
|
|
|
- # ---------------------------------------------------------#
|
|
|
|
- # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。
|
|
|
|
- # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB
|
|
|
|
- # ---------------------------------------------------------#
|
|
|
|
- if not (len(np.shape(image)) == 3 and np.shape(image)[2] == 3):
|
|
|
|
- image = image.convert('RGB')
|
|
|
|
- image_data = resize_image(image, input_size, False)
|
|
|
|
- image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), swap).copy(),
|
|
|
|
- 0)
|
|
|
|
- image_data = image_data.astype('float32')
|
|
|
|
- return image_data, image_shape
|
|
|
|
-
|
|
|
|
def detect_secret_label(self, image_path, model_file, watermark_txt, input_shape) -> bool:
|
|
def detect_secret_label(self, image_path, model_file, watermark_txt, input_shape) -> bool:
|
|
"""
|
|
"""
|
|
使用指定onnx文件进行预测并进行黑盒水印检测
|
|
使用指定onnx文件进行预测并进行黑盒水印检测
|
|
@@ -66,7 +47,8 @@ class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
:param input_shape: 模型输入图像大小,tuple
|
|
:param input_shape: 模型输入图像大小,tuple
|
|
:return:
|
|
:return:
|
|
"""
|
|
"""
|
|
- image_data, image_shape = self.preprocess_image(image_path, input_shape)
|
|
|
|
|
|
+ image = Image.open(image_path)
|
|
|
|
+ image_shape = np.array(np.shape(image)[0:2])
|
|
# 解析标签嵌入位置
|
|
# 解析标签嵌入位置
|
|
parse_label = parse_qrcode_label_file.load_watermark_info(watermark_txt, image_path)
|
|
parse_label = parse_qrcode_label_file.load_watermark_info(watermark_txt, image_path)
|
|
if len(parse_label) < 5:
|
|
if len(parse_label) < 5:
|
|
@@ -83,18 +65,9 @@ class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
if len(watermark_box) == 0:
|
|
if len(watermark_box) == 0:
|
|
return False
|
|
return False
|
|
# 使用onnx进行推理
|
|
# 使用onnx进行推理
|
|
- session = onnxruntime.InferenceSession(model_file)
|
|
|
|
- ort_inputs = {session.get_inputs()[0].name: image_data,
|
|
|
|
- session.get_inputs()[1].name: np.array(1.0).astype('float64')}
|
|
|
|
- output = session.run(None, ort_inputs)
|
|
|
|
- roi_cls_locs, roi_scores, rois, _ = output
|
|
|
|
- # 处理模型预测输出
|
|
|
|
- num_classes = 20
|
|
|
|
- bbox_util = DecodeBox(num_classes)
|
|
|
|
- nms_iou = 0.3
|
|
|
|
- confidence = 0.5
|
|
|
|
- results = bbox_util.forward(roi_cls_locs, roi_scores, rois, image_shape, input_shape,
|
|
|
|
- nms_iou=nms_iou, confidence=confidence)
|
|
|
|
|
|
+ results = FasterRCNNInference(self.model_filename).predict(image_path)
|
|
|
|
+
|
|
|
|
+ # 检测模型是否存在黑盒水印
|
|
if results is not None:
|
|
if results is not None:
|
|
detect_result = detect_watermark(results, watermark_box)
|
|
detect_result = detect_watermark(results, watermark_box)
|
|
return detect_result
|
|
return detect_result
|