gen_label.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. import argparse
  3. import json
  4. def gen_rec_label(input_path, out_label):
  5. with open(out_label, "w") as out_file:
  6. with open(input_path, "r") as f:
  7. for line in f.readlines():
  8. tmp = line.strip("\n").replace(" ", "").split(",")
  9. img_path, label = tmp[0], tmp[1]
  10. label = label.replace('"', "")
  11. out_file.write(img_path + "\t" + label + "\n")
  12. def gen_det_label(root_path, input_dir, out_label):
  13. with open(out_label, "w") as out_file:
  14. for label_file in os.listdir(input_dir):
  15. img_path = os.path.join(root_path, label_file[3:-4] + ".jpg")
  16. label = []
  17. with open(
  18. os.path.join(input_dir, label_file), "r",
  19. encoding="utf-8-sig") as f:
  20. for line in f.readlines():
  21. tmp = line.strip("\n\r").replace("\xef\xbb\xbf",
  22. "").split(",")
  23. points = tmp[:8]
  24. s = []
  25. for i in range(0, len(points), 2):
  26. b = points[i:i + 2]
  27. b = [int(t) for t in b]
  28. s.append(b)
  29. result = {"transcription": tmp[8], "points": s}
  30. label.append(result)
  31. out_file.write(img_path + "\t" + json.dumps(
  32. label, ensure_ascii=False) + "\n")
  33. if __name__ == "__main__":
  34. parser = argparse.ArgumentParser()
  35. parser.add_argument(
  36. "--mode",
  37. type=str,
  38. default="rec",
  39. help="Generate rec_label or det_label, can be set rec or det", )
  40. parser.add_argument(
  41. "--root_path",
  42. type=str,
  43. default=".",
  44. help="The root directory of images.Only takes effect when mode=det ", )
  45. parser.add_argument(
  46. "--input_path",
  47. type=str,
  48. default=".",
  49. help="Input_label or input path to be converted", )
  50. parser.add_argument(
  51. "--output_label",
  52. type=str,
  53. default="out_label.txt",
  54. help="Output file name")
  55. args = parser.parse_args()
  56. if args.mode == "rec":
  57. print("Generate rec label")
  58. gen_rec_label(args.input_path, args.output_label)
  59. elif args.mode == "det":
  60. gen_det_label(args.root_path, args.input_path, args.output_label)