new.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import operator
  2. from pathlib import Path
  3. from typing import List
  4. import numpy as np
  5. from mdutils.mdutils import MdUtils
  6. import cv2
  7. import requests
  8. from dataclasses import dataclass
  9. import json
  10. import time
  11. import base64
  12. from itertools import chain
  13. class MarkdownTable(object):
  14. def __init__(self, name):
  15. self.name = name
  16. self.mdFile = MdUtils(file_name=time.strftime("%m-%d", time.localtime()) + name)
  17. self.field_table = ['字段', '正确率']
  18. self.true_table = ['图片', '识别结果']
  19. self.false_table = ['图片', '识别结果']
  20. def add_field_table(self, fields: List):
  21. self.field_table.extend(fields)
  22. def add_true_table(self, image_and_field: List):
  23. self.true_table.extend(image_and_field)
  24. def add_false_table(self, image_and_field: List):
  25. self.false_table.extend(image_and_field)
  26. @dataclass
  27. class Image:
  28. path: Path
  29. rotate: int
  30. @property
  31. def fn(self):
  32. return self.path.stem
  33. @property
  34. def json_path(self):
  35. return self.path.parent / f'{self.path.stem}.json'
  36. def get_base64(self, rotate=0):
  37. return 'dsf'
  38. class DataSet(object):
  39. def __init__(self, image_path, rotate=False):
  40. self.image_path = image_path
  41. self.image_list = []
  42. for p in chain(*[Path(self.image_path).rglob('*.jpg')]):
  43. self.image_list.append(Image(p, 0))
  44. self.attrs = ['orientation','name','id','language', 'level', 'exam_time', 'score']
  45. self.tp = {k: 0 for k in self.attrs}
  46. # self.field_rate = {
  47. # 'orientation': self.count,
  48. # 'name': self.count,
  49. # 'id': self.count,
  50. # 'language': self.count,
  51. # 'level': self.count,
  52. # 'exam_time': self.count,
  53. # 'score': self.count,
  54. # }
  55. # self.del_field = del_field
  56. def _evaluate_one(self, image: Image):
  57. @property
  58. def count(self):
  59. return len(list(chain(*[self.image_paths.rglob('*.jpg')]))) * 4 if self.is_rotate else 1
  60. @property
  61. def images(self):
  62. images_path_list = list(chain(*[Path(self.image_paths).rglob('*.jpg')]))
  63. images_path_dict = {path.name: [str(path), str(path.parent / f'{path.stem}.json')] for path in images_path_list}
  64. return {i: images_path_dict[i] for i in sorted(images_path_dict)}
  65. def revise_field_rate(self, field):
  66. self.field_rate[field] = self.field_rate[field] - 1
  67. @property
  68. def field_rate_2_list(self):
  69. table = []
  70. for k, v in self.field_rate.items():
  71. table.extend((k, "{:.2f}%".format(v / self.count * 100)))
  72. return table
  73. def image_2_base64(self, image):
  74. dire = self.image_paths.parent / (".ro_dire")
  75. if not dire.exists(): dire.mkdir()
  76. image_path = Path(self.images[image][0])
  77. if self.is_rotate:
  78. img = cv2.imread(str(image_path))
  79. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  80. base64_imgs = []
  81. for rotate in {cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_180, cv2.ROTATE_90_COUNTERCLOCKWISE}:
  82. img_rotated = cv2.rotate(img, rotate)
  83. img_rotated = cv2.cvtColor(img_rotated, cv2.COLOR_BGR2RGB)
  84. img_rotated_path = dire / f"{image_path.stem}_{str(rotate + 1)}.jpg"
  85. cv2.imread(str(img_rotated_path), img_rotated)
  86. with img_rotated_path.open('rb') as f:
  87. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  88. base64_imgs.extend(img_str)
  89. return base64_imgs
  90. else:
  91. with image_path.open('rb') as f:
  92. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  93. return [img_str]
  94. def res_2_dict(self, r):
  95. if r['status'] == '000':
  96. r = r['result']
  97. if r:
  98. del r['confidence']
  99. if self.del_field is not None: del r[self.del_field]
  100. return {k: v['text'] if isinstance(v, dict) else v for k, v in r.items()}
  101. elif r['status'] == '101':
  102. return r['msg']
  103. def json_2_dict(self, image):
  104. json_path = Path(self.images[image][1])
  105. with json_path.open('r') as f:
  106. json_dict = json.load(f)
  107. if self.del_field is not None: del json_dict[self.del_field]
  108. return json_dict
  109. def compare_dict(self, MT: MarkdownTable, res_dict, json_dict, image_path):
  110. image_mark = MT.mdFile.new_inline_image(text='', path=image_path)
  111. if operator.eq(res_dict, json_dict):
  112. MT.add_true_table([image_mark, json_dict])
  113. elif type(res_dict) == dict:
  114. err_str = ""
  115. for key in res_dict:
  116. if res_dict[key] != res_dict[key]:
  117. err_str = f"{err_str}正确:{res_dict[key]}<br>返回:{res_dict[key]}<br>"
  118. self.revise_field_rate(key)
  119. MT.add_false_table(([image_mark, err_str]))
  120. elif type(res_dict) == str:
  121. MT.add_false_table([image_mark, res_dict])