You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

postprocess.py 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  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. # less 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. # ============================================================================
  15. """post process for 310 inference"""
  16. import os
  17. import argparse
  18. import numpy as np
  19. from src.metric import CRNNAccuracy
  20. from src.config import config1 as config
  21. parser = argparse.ArgumentParser(description="yolov3_darknet53 inference")
  22. parser.add_argument("--ann_file", type=str, required=True, help="ann file.")
  23. parser.add_argument("--result_path", type=str, required=True, help="image file path.")
  24. parser.add_argument("--dataset", type=str, default="ic03", choices=['ic03', 'ic13', 'svt', 'iiit5k'])
  25. args = parser.parse_args()
  26. def read_annotation(ann_file):
  27. file = open(ann_file)
  28. ann = {}
  29. for line in file.readlines():
  30. img_info = line.rsplit("/")[-1].split(",")
  31. img_path = img_info[0].split('/')[-1]
  32. ann[img_path] = img_info[1].strip()
  33. return ann
  34. def read_IC13_annotation(ann_file):
  35. file = open(ann_file)
  36. ann = {}
  37. for line in file.readlines():
  38. img_info = line.split(",")
  39. img_path = img_info[0].split('/')[-1]
  40. ann[img_path] = img_info[1].strip().replace('\"', '')
  41. return ann
  42. def read_svt_annotation(ann_file):
  43. file = open(ann_file)
  44. ann = {}
  45. for line in file.readlines():
  46. img_info = line.split(",")
  47. img_path = img_info[0].split('/')[-1]
  48. ann[img_path] = img_info[1].strip()
  49. return ann
  50. def get_eval_result(result_path, ann_file):
  51. metrics = CRNNAccuracy(config)
  52. if args.dataset == "ic03" or args.dataset == "iiit5k":
  53. ann = read_annotation(args.ann_file)
  54. elif args.dataset == "ic13":
  55. ann = read_IC13_annotation(args.ann_file)
  56. elif args.dataset == "svt":
  57. ann = read_svt_annotation(args.ann_file)
  58. for img_name, label in ann.items():
  59. result_file = os.path.join(result_path, img_name[:-4] + "_0.bin")
  60. pred_y = np.fromfile(result_file, dtype=np.float32).reshape(config.num_step, -1, config.class_num)
  61. metrics.update(pred_y, [label])
  62. print("result CRNNAccuracy is: ", metrics.eval())
  63. metrics.clear()
  64. if __name__ == '__main__':
  65. get_eval_result(args.result_path, args.ann_file)