trans_xfun_data.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "transcription": doc["text"],
  31. "label": doc["label"],
  32. "points": points,
  33. "id": doc["id"],
  34. "linking": doc["linking"]
  35. })
  36. fout.write(image_path + "\t" + json.dumps(
  37. label_info, ensure_ascii=False) + "\n")
  38. print("===ok====")
  39. def parser_args():
  40. import argparse
  41. parser = argparse.ArgumentParser(description="args for paddleserving")
  42. parser.add_argument(
  43. "--ori_gt_path", type=str, required=True, help='origin xfun gt path')
  44. parser.add_argument(
  45. "--output_path", type=str, required=True, help='path to save')
  46. args = parser.parse_args()
  47. return args
  48. args = parser_args()
  49. transfer_xfun_data(args.ori_gt_path, args.output_path)