parse_qrcode_label_file.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. def parse_labels(file_path):
  3. categories = {}
  4. with open(file_path, 'r') as file:
  5. for line in file:
  6. parts = line.strip().split()
  7. file_path = parts[0]
  8. category = int(float(parts[-1]))
  9. if category not in categories:
  10. categories[category] = []
  11. categories[category].append(file_path)
  12. return categories
  13. def load_watermark_info(watermark_txt, image_path):
  14. """
  15. 从标签文件中加载指定图片二维码嵌入坐标及所属类别
  16. :param watermark_txt: 标签文件
  17. :param image_path: 图片路径
  18. :return: [x1, y1, x2, y2, cls]
  19. """
  20. with open(watermark_txt, 'r') as f:
  21. for line in f.readlines():
  22. parts = line.strip().split()
  23. filename = parts[0]
  24. filename = os.path.basename(filename)
  25. if filename == os.path.basename(image_path):
  26. x_center, y_center, w, h = map(float, parts[1:5])
  27. cls = int(float(parts[5])) # 转换类别为整数
  28. return [x_center, y_center, w, h, cls]
  29. return []