trans_xfun_data.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. def transfer_xfun_data(json_path=None, output_file=None):
  16. with open(json_path, "r", encoding="utf-8") as fin:
  17. lines = fin.readlines()
  18. json_info = json.loads(lines[0])
  19. documents = json_info["documents"]
  20. with open(output_file, "w", encoding="utf-8") as fout:
  21. for idx, document in enumerate(documents):
  22. label_info = []
  23. img_info = document["img"]
  24. document = document["document"]
  25. image_path = img_info["fname"]
  26. for doc in document:
  27. x1, y1, x2, y2 = doc["box"]
  28. points = [[x1, y1], [x2, y1], [x2, y2], [x1, y2]]
  29. label_info.append(
  30. {
  31. "transcription": doc["text"],
  32. "label": doc["label"],
  33. "points": points,
  34. "id": doc["id"],
  35. "linking": doc["linking"],
  36. }
  37. )
  38. fout.write(
  39. image_path + "\t" + json.dumps(label_info, ensure_ascii=False) + "\n"
  40. )
  41. print("===ok====")
  42. def parser_args():
  43. import argparse
  44. parser = argparse.ArgumentParser(description="args for paddleserving")
  45. parser.add_argument(
  46. "--ori_gt_path", type=str, required=True, help="origin xfun gt path"
  47. )
  48. parser.add_argument("--output_path", type=str, required=True, help="path to save")
  49. args = parser.parse_args()
  50. return args
  51. args = parser_args()
  52. transfer_xfun_data(args.ori_gt_path, args.output_path)