test_816_cet.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import unittest
  2. import base64
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import cv2
  6. import requests
  7. url = 'http://192.168.199.249:18050'
  8. def send_request(image_path, rotate=None):
  9. img = cv2.imread(str(image_path))
  10. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  11. if rotate is not None:
  12. img = cv2.rotate(img, rotate)
  13. _, im_arr = cv2.imencode('.jpg', img)
  14. img_str = base64.b64encode(im_arr).decode('utf-8')
  15. r = requests.post(url + '/ocr_system/cet4', json={"image": img_str})
  16. print(r.json())
  17. return r.json()
  18. @dataclass
  19. class ResultItem:
  20. status: str
  21. orientation: int
  22. name: str
  23. id: str
  24. language: str
  25. level: str
  26. exam_time: str
  27. score: str
  28. class TestSchoolCertOcr(unittest.TestCase):
  29. def _helper(self, image_path, item: ResultItem, rotate=None):
  30. root = Path(__file__).parent
  31. image_path = str(root / image_path)
  32. r = send_request(image_path, rotate)
  33. self.assertEqual(item, ResultItem(status=r['status'],
  34. orientation=r['result']['orientation'],
  35. name=r['result']['name']['text'],
  36. id=r['result']['id']['text'],
  37. language=r['result']['language']['text'],
  38. level=r['result']['level']['text'],
  39. exam_time=r['result']['exam_time']['text'],
  40. score=r['result']['score']['text']))
  41. def case_02_0(self, orientation=0):
  42. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  43. image_path = '../images/cet4/02_img.jpg'
  44. json_input = {'status': '000', 'orientation': 0, 'name': '纪春', 'id': '230904199909090529', 'language': '英语', 'level': 'CET4', 'exam_time': '2018年6月', 'score': '472'}
  45. json_input['orientation'] = orientation
  46. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  47. def test_case_02_0(self):
  48. self.case_02_0(0)
  49. def test_case_02_1(self):
  50. self.case_02_0(1)
  51. def test_case_02_2(self):
  52. self.case_02_0(2)
  53. def test_case_02_3(self):
  54. self.case_02_0(3)
  55. def case_04_0(self, orientation=0):
  56. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  57. image_path = '../images/cet4/04_img.jpg'
  58. json_input = {'status': '000', 'orientation': 0, 'name': '林雄', 'id': '431121199807158036', 'language': '英语', 'level': 'CET4', 'exam_time': '2017年12月', 'score': '490'}
  59. json_input['orientation'] = orientation
  60. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  61. def test_case_04_0(self):
  62. self.case_04_0(0)
  63. def test_case_04_1(self):
  64. self.case_04_0(1)
  65. def test_case_04_2(self):
  66. self.case_04_0(2)
  67. def test_case_04_3(self):
  68. self.case_04_0(3)
  69. def case_07_0(self, orientation=0):
  70. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  71. image_path = '../images/cet4/07_img.jpg'
  72. json_input = {'status': '000', 'orientation': 0, 'name': '吴晓虎', 'id': '150426199902113039', 'language': '英语', 'level': 'CET4', 'exam_time': '2018年12月', 'score': '425'}
  73. json_input['orientation'] = orientation
  74. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  75. def test_case_07_0(self):
  76. self.case_07_0(0)
  77. def test_case_07_1(self):
  78. self.case_07_0(1)
  79. def test_case_07_2(self):
  80. self.case_07_0(2)
  81. def test_case_07_3(self):
  82. self.case_07_0(3)
  83. def case_05_0(self, orientation=0):
  84. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  85. image_path = '../images/cet4/05_img.jpg'
  86. json_input = {'status': '000', 'orientation': 0, 'name': '李然琦', 'id': '370502199709144023', 'language': '英语', 'level': 'CET4', 'exam_time': '2016年12月', 'score': '438'}
  87. json_input['orientation'] = orientation
  88. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  89. def test_case_05_0(self):
  90. self.case_05_0(0)
  91. def test_case_05_1(self):
  92. self.case_05_0(1)
  93. def test_case_05_2(self):
  94. self.case_05_0(2)
  95. def test_case_05_3(self):
  96. self.case_05_0(3)
  97. def case_10_0(self, orientation=0):
  98. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  99. image_path = '../images/cet4/10_img.jpg'
  100. json_input = {'status': '000', 'orientation': 0, 'name': '袁湘粤', 'id': '43052419961021177X', 'language': '英语', 'level': 'CET4', 'exam_time': '2020年12月', 'score': '448'}
  101. json_input['orientation'] = orientation
  102. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  103. def test_case_10_0(self):
  104. self.case_10_0(0)
  105. def test_case_10_1(self):
  106. self.case_10_0(1)
  107. def test_case_10_2(self):
  108. self.case_10_0(2)
  109. def test_case_10_3(self):
  110. self.case_10_0(3)
  111. def case_08_0(self, orientation=0):
  112. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  113. image_path = '../images/cet4/08_img.jpg'
  114. json_input = {'status': '000', 'orientation': 0, 'name': '张鑫', 'id': '140227199809282317', 'language': '英语', 'level': 'CET4', 'exam_time': '2021年6月', 'score': '445'}
  115. json_input['orientation'] = orientation
  116. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  117. def test_case_08_0(self):
  118. self.case_08_0(0)
  119. def test_case_08_1(self):
  120. self.case_08_0(1)
  121. def test_case_08_2(self):
  122. self.case_08_0(2)
  123. def test_case_08_3(self):
  124. self.case_08_0(3)
  125. def case_09_0(self, orientation=0):
  126. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  127. image_path = '../images/cet4/09_img.jpg'
  128. json_input = {'status': '000', 'orientation': 0, 'name': '张鹏远', 'id': '150203199812150615', 'language': '英语', 'level': 'CET4', 'exam_time': '2020年12月', 'score': '437'}
  129. json_input['orientation'] = orientation
  130. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  131. def test_case_09_0(self):
  132. self.case_09_0(0)
  133. def test_case_09_1(self):
  134. self.case_09_0(1)
  135. def test_case_09_2(self):
  136. self.case_09_0(2)
  137. def test_case_09_3(self):
  138. self.case_09_0(3)
  139. def case_03_0(self, orientation=0):
  140. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  141. image_path = '../images/cet4/03_img.jpg'
  142. json_input = {'status': '000', 'orientation': 0, 'name': '姚帆', 'id': '142702199903124229', 'language': '英语', 'level': 'CET4', 'exam_time': '2019年12月', 'score': '471'}
  143. json_input['orientation'] = orientation
  144. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  145. def test_case_03_0(self):
  146. self.case_03_0(0)
  147. def test_case_03_1(self):
  148. self.case_03_0(1)
  149. def test_case_03_2(self):
  150. self.case_03_0(2)
  151. def test_case_03_3(self):
  152. self.case_03_0(3)
  153. def case_01_0(self, orientation=0):
  154. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  155. image_path = '../images/cet4/01_img.jpg'
  156. json_input = {'status': '000', 'orientation': 0, 'name': '武文斌', 'id': '642226199704273215', 'language': '英语', 'level': 'CET4', 'exam_time': '2021年12月', 'score': '474'}
  157. json_input['orientation'] = orientation
  158. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  159. def test_case_01_0(self):
  160. self.case_01_0(0)
  161. def test_case_01_1(self):
  162. self.case_01_0(1)
  163. def test_case_01_2(self):
  164. self.case_01_0(2)
  165. def test_case_01_3(self):
  166. self.case_01_0(3)