watermark_generate_controller.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. 数据集图片处理http接口
  3. """
  4. import os
  5. import shutil
  6. import time
  7. import zipfile
  8. from flask import Blueprint, request, jsonify
  9. from watermark_generate.exceptions import BusinessException
  10. from watermark_generate import logger
  11. from watermark_generate.tools import secret_label_func
  12. from watermark_generate.deals import yolox_pytorch_black_embed, yolox_pytorch_white_embed, \
  13. faster_rcnn_pytorch_black_embed, ssd_pytorch_black_embed, ssd_pytorch_white_embed, faster_rcnn_pytorch_white_embed, \
  14. classification_pytorch_white_embed, googlenet_pytorch_white_embed, classification_pytorch_black_embed
  15. generator = Blueprint('generator', __name__)
  16. # 允许的扩展名
  17. ALLOWED_EXTENSIONS = {'zip'}
  18. # 判断文件扩展名是否合法
  19. def allowed_file(filename):
  20. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  21. # 获取文件扩展名
  22. def get_file_extension(filename):
  23. return filename.rsplit('.', 1)[1].lower()
  24. @generator.route('/model/watermark/embed', methods=['POST'])
  25. def watermark_embed():
  26. """
  27. 上传模型代码压缩包文件路径,进行代码修改后,返回修改后的模型代码压缩包位置
  28. model_file: 模型代码压缩包文件绝对路径
  29. model_value: 模型名称
  30. model_type: 模型类型
  31. :return: 处理完成的模型代码压缩包绝对路径
  32. """
  33. data = request.json
  34. logger.info(f'watermark embed request: {data}')
  35. # 获取请求参数
  36. model_file = data.get('model_file')
  37. model_value = data.get('model_value')
  38. model_type = data.get('model_type')
  39. embed_type = data.get('embed_type')
  40. if embed_type is None or embed_type == '': # 通过传入参数控制嵌入方式,默认为黑盒水印嵌入
  41. embed_type = 'blackbox'
  42. if model_file is None:
  43. raise BusinessException(message='模型代码路径不可为空', code=-1)
  44. if model_value is None:
  45. raise BusinessException(message='模型值不可为空', code=-1)
  46. if model_type is None:
  47. raise BusinessException(message='模型类型不可为空', code=-1)
  48. file_path = os.path.dirname(model_file) # 获取文件路径
  49. file_name = os.path.basename(model_file) # 获取文件名
  50. if not allowed_file(file_name):
  51. raise BusinessException(message='模型文件必须是zip格式的压缩包', code=-1)
  52. if not os.path.exists(model_file):
  53. raise BusinessException(message='指定模型文件不存在', code=-1)
  54. extract_to_path = "./data/model_project"
  55. # 检查目标目录是否存在,如果不存在则创建
  56. os.makedirs(extract_to_path, exist_ok=True)
  57. # 解压模型文件代码
  58. logger.info(f"extract model project file to {extract_to_path}...")
  59. with zipfile.ZipFile(model_file, 'r') as zip_ref:
  60. zip_ref.extractall(extract_to_path)
  61. # 生成密码标签
  62. logger.info(f"generate secret label ...")
  63. ts = str(int(time.time()))
  64. secret_label, public_key = secret_label_func.generate_secret_label(ts)
  65. logger.debug(f"generate secret label: {secret_label} , public key: {public_key}")
  66. # 修改模型文件代码,并将public_key写入至文件保存至修改后的工程文件目录中
  67. logger.info(f"modify model project source, model_value: {model_value}, embed_type: {embed_type}")
  68. # TODO 添加其他模型工程代码处理
  69. if model_value == 'yolox' and embed_type == 'blackbox':
  70. yolox_pytorch_black_embed.modify_model_project(secret_label, extract_to_path, public_key)
  71. if model_value == 'yolox' and embed_type == 'whitebox':
  72. yolox_pytorch_white_embed.modify_model_project(secret_label, extract_to_path, public_key)
  73. if model_value == 'faster-rcnn' and embed_type == 'blackbox':
  74. faster_rcnn_pytorch_black_embed.modify_model_project(secret_label, extract_to_path, public_key)
  75. if model_value == 'faster-rcnn' and embed_type == 'whitebox':
  76. faster_rcnn_pytorch_white_embed.modify_model_project(secret_label, extract_to_path, public_key)
  77. if model_value == 'ssd' and embed_type == 'blackbox':
  78. ssd_pytorch_black_embed.modify_model_project(secret_label, extract_to_path, public_key)
  79. if model_value == 'ssd' and embed_type == 'whitebox':
  80. ssd_pytorch_white_embed.modify_model_project(secret_label, extract_to_path, public_key)
  81. if (model_value in ['alexnet', 'vggnet', 'resnet']) and embed_type == 'whitebox':
  82. classification_pytorch_white_embed.modify_model_project(secret_label, extract_to_path, public_key)
  83. if model_value == 'googlenet' and embed_type == 'whitebox':
  84. googlenet_pytorch_white_embed.modify_model_project(secret_label, extract_to_path, public_key)
  85. if (model_value in ['alexnet', 'vggnet', 'resnet', 'googlenet']) and embed_type == 'blackbox':
  86. classification_pytorch_black_embed.modify_model_project(secret_label, extract_to_path, public_key)
  87. # 压缩修改后的模型文件代码
  88. name, ext = os.path.splitext(file_name)
  89. zip_filename = f"{name}_embed{ext}"
  90. zip_filepath = os.path.join(file_path, zip_filename)
  91. logger.info(f"zip modified model project source to {zip_filepath}")
  92. with zipfile.ZipFile(zip_filepath, 'w', zipfile.ZIP_DEFLATED) as zipf:
  93. # 遍历指定目录,递归压缩所有文件和子目录
  94. for root, dirs, files in os.walk(extract_to_path):
  95. for file in files:
  96. # 获取文件的完整路径
  97. file_path = os.path.join(root, file)
  98. # 将文件添加到 ZIP 文件中,并去掉目录前缀
  99. arcname = os.path.relpath(file_path, extract_to_path)
  100. zipf.write(file_path, arcname)
  101. # 删除解压后的文件
  102. shutil.rmtree(extract_to_path)
  103. return jsonify({'model_file_new': zip_filepath, 'hash_flag': 0, 'license': public_key}), 200