parse_qrcode_label_file.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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, img_width, img_height):
  14. watermark_boxes = {}
  15. with open(watermark_txt, 'r') as f:
  16. for line in f.readlines():
  17. parts = line.strip().split()
  18. filename = parts[0]
  19. filename = os.path.basename(filename)
  20. x_center, y_center, w, h = map(float, parts[1:5])
  21. cls = int(float(parts[5])) # 转换类别为整数
  22. # 计算绝对坐标
  23. x1 = (x_center - w / 2) * img_width
  24. y1 = (y_center - h / 2) * img_height
  25. x2 = (x_center + w / 2) * img_width
  26. y2 = (y_center + h / 2) * img_height
  27. if filename not in watermark_boxes:
  28. watermark_boxes[filename] = []
  29. watermark_boxes[filename].append([x1, y1, x2, y2, cls])
  30. return watermark_boxes