parse_qrcode_label_file.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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, image_path):
  14. """
  15. 从标签文件中加载指定图片二维码嵌入坐标及所属类别
  16. :param watermark_txt: 标签文件
  17. :param img_width: 图像宽度
  18. :param img_height: 图像高度
  19. :param image_path: 图片路径
  20. :return: [x1, y1, x2, y2, cls]
  21. """
  22. with open(watermark_txt, 'r') as f:
  23. for line in f.readlines():
  24. parts = line.strip().split()
  25. filename = parts[0]
  26. filename = os.path.basename(filename)
  27. if filename == os.path.basename(image_path):
  28. x_center, y_center, w, h = map(float, parts[1:5])
  29. cls = int(float(parts[5])) # 转换类别为整数
  30. # 计算绝对坐标
  31. x1 = (x_center - w / 2) * img_width
  32. y1 = (y_center - h / 2) * img_height
  33. x2 = (x_center + w / 2) * img_width
  34. y2 = (y_center + h / 2) * img_height
  35. return [x1, y1, x2, y2, cls]
  36. return []