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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # 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. # ============================================================================
  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. """
  52. Calculate accuracy according to the annotation file and result file.
  53. """
  54. metrics = CRNNAccuracy(config)
  55. if args.dataset == "ic03" or args.dataset == "iiit5k":
  56. ann = read_annotation(ann_file)
  57. elif args.dataset == "ic13":
  58. ann = read_ic13_annotation(ann_file)
  59. elif args.dataset == "svt":
  60. ann = read_svt_annotation(ann_file)
  61. for img_name, label in ann.items():
  62. result_file = os.path.join(result_path, img_name[:-4] + "_0.bin")
  63. pred_y = np.fromfile(result_file, dtype=np.float32).reshape(config.num_step, -1, config.class_num)
  64. metrics.update(pred_y, [label])
  65. print("result CRNNAccuracy is: ", metrics.eval())
  66. metrics.clear()
  67. if __name__ == '__main__':
  68. get_eval_result(args.result_path, args.ann_file)