all_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import unittest
  2. import base64
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import requests
  6. # url = 'http://localhost:8080'
  7. url = 'http://192.168.199.249:38811'
  8. # header = {
  9. # 'Authorization': 'Bearer 9679c2b3-b90b-4029-a3c7-f347b4d242f7'
  10. # }
  11. # 带线框识别比较差 例:6.27_02.png
  12. def send_request(image_path, image_type='0'):
  13. with open(image_path, 'rb') as f:
  14. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  15. r = requests.post(f'{url}/ocr_system/regbook', json={'image': img_str, 'image_type': image_type})
  16. print(r.json())
  17. return r.json()
  18. @dataclass
  19. class ResultItem0:
  20. status: str
  21. orientation: int
  22. name: str
  23. id: str
  24. gender: str
  25. birthplace: str
  26. native_place: str
  27. blood_type: str
  28. religion: str
  29. @dataclass
  30. class ResultItem1:
  31. status: str
  32. orientation: int
  33. type: str
  34. address: str
  35. class TestSchoolCertOcr(unittest.TestCase):
  36. def _helper0(self, image_path, item: ResultItem0, image_type='0'):
  37. root = Path(__file__).parent
  38. image_path = str(root / image_path)
  39. r = send_request(image_path, image_type)
  40. self.assertEqual(item, ResultItem0(status=r['status'],
  41. orientation=r['result']['orientation'],
  42. name=r['result']['name']['text'],
  43. id=r['result']['id']['text'],
  44. gender=r['result']['gender']['text'],
  45. birthplace=r['result']['birthplace']['text'],
  46. native_place=r['result']['native_place']['text'],
  47. blood_type=r['result']['blood_type']['text'],
  48. religion=r['result']['religion']['text']
  49. ))
  50. def _helper1(self, image_path, item: ResultItem1, image_type='1'):
  51. root = Path(__file__).parent
  52. image_path = str(root / image_path)
  53. r = send_request(image_path, image_type)
  54. self.assertEqual(item, ResultItem1(status=r['status'],
  55. orientation=r['result']['orientation'],
  56. type=r['result']['type']['text'],
  57. address=r['result']['address']['text']
  58. ))
  59. # 0测试
  60. # 方向、专业识别错误
  61. def test_0_01(self):
  62. image_path = '../images/all/0/01.jpeg'
  63. self._helper0(image_path, ResultItem0(status='000',
  64. orientation=0,
  65. name='孙也桐',
  66. id='230125199603270066',
  67. gender='女',
  68. birthplace='黑龙江省宾县',
  69. native_place='黑龙江省宾县',
  70. blood_type='',
  71. religion=''))
  72. def test_0_02(self):
  73. image_path = '../images/all/0/02.jpg'
  74. self._helper0(image_path, ResultItem0(status='000',
  75. orientation=0,
  76. name='唐振宇',
  77. id='230826199211171418',
  78. gender='男',
  79. birthplace='黑龙江省佳木斯市桦川县',
  80. native_place='黑龙江省佳木斯市桦川县',
  81. blood_type='',
  82. religion=''))
  83. def test_0_03(self):
  84. image_path = '../images/all/0/03.jpg'
  85. self._helper0(image_path, ResultItem0(status='000',
  86. orientation=0,
  87. name='陈玉虎',
  88. id='622427199811114715',
  89. gender='男',
  90. birthplace='甘肃省临洮县',
  91. native_place='甘肃省临洮县',
  92. blood_type='不明',
  93. religion='无宗教信仰'))
  94. def test_0_04(self):
  95. image_path = '../images/all/0/04.jpg'
  96. self._helper0(image_path, ResultItem0(status='000',
  97. orientation=0,
  98. name='陈鑫',
  99. id='640382199605273716',
  100. gender='男',
  101. birthplace='宁夏回族自治区银川市灵武市',
  102. native_place='宁夏回族自治区银川市灵武市',
  103. blood_type='不明',
  104. religion='伊斯兰教'))
  105. def test_0_05_0(self):
  106. image_path = '../images/all/0/05_0.jpg'
  107. self._helper0(image_path, ResultItem0(status='000',
  108. orientation=0,
  109. name='马宁',
  110. id='64038219981016341x',
  111. gender='男',
  112. birthplace='宁夏灵武市无吴四队',
  113. native_place='宁夏',
  114. blood_type='B型',
  115. religion=''))
  116. def test_0_05_90(self):
  117. image_path = '../images/all/0/05_90.jpg'
  118. self._helper0(image_path, ResultItem0(status='000',
  119. orientation=1,
  120. name='马宁',
  121. id='64038219981016341x',
  122. gender='男',
  123. birthplace='宁夏灵武市无吴四队',
  124. native_place='宁夏',
  125. blood_type='B型',
  126. religion=''))
  127. def test_0_05_180(self):
  128. image_path = '../images/all/0/05_180.jpg'
  129. self._helper0(image_path, ResultItem0(status='000',
  130. orientation=2,
  131. name='马宁',
  132. id='64038219981016341x',
  133. gender='男',
  134. birthplace='宁夏灵武市无吴四队',
  135. native_place='宁夏',
  136. blood_type='B型',
  137. religion=''))
  138. def test_0_05_270(self):
  139. image_path = '../images/all/0/05_270.jpg'
  140. self._helper0(image_path, ResultItem0(status='000',
  141. orientation=3,
  142. name='马宁',
  143. id='64038219981016341x',
  144. gender='男',
  145. birthplace='宁夏灵武市无吴四队',
  146. native_place='宁夏',
  147. blood_type='B型',
  148. religion=''))
  149. def test_1_01_0(self):
  150. image_path = '../images/all/1/01_0.png'
  151. self._helper1(image_path, ResultItem1(status='000',
  152. orientation=0,
  153. type='居民家庭户口',
  154. address='江西省新余市渝水区胜利南路183号1栋1单元1402室'))
  155. def test_1_01_90(self):
  156. image_path = '../images/all/1/01_90.png'
  157. self._helper1(image_path, ResultItem1(status='000',
  158. orientation=1,
  159. type='居民家庭户口',
  160. address='江西省新余市渝水区胜利南路183号1栋1单元1402室'))
  161. def test_1_01_180(self):
  162. image_path = '../images/all/1/01_180.png'
  163. self._helper1(image_path, ResultItem1(status='000',
  164. orientation=2,
  165. type='居民家庭户口',
  166. address='江西省新余市渝水区胜利南路183号1栋1单元1402室'))
  167. def test_1_01_270(self):
  168. image_path = '../images/all/1/01_270.png'
  169. self._helper1(image_path, ResultItem1(status='000',
  170. orientation=3,
  171. type='居民家庭户口',
  172. address='江西省新余市渝水区胜利南路183号1栋1单元1402室'))
  173. def test_1_02(self):
  174. image_path = '../images/all/1/02.jpg'
  175. self._helper1(image_path, ResultItem1(status='000',
  176. orientation=0,
  177. type='非农业家庭户口',
  178. address='东岭东委9组东岭南街解困小区9栋1门201号'))
  179. def test_1_03(self):
  180. image_path = '../images/all/1/03.jpg'
  181. self._helper1(image_path, ResultItem1(status='000',
  182. orientation=0,
  183. type='居民户口',
  184. address='黑龙江省桦川县东河乡兴安村1组'))
  185. def test_1_04(self):
  186. image_path = '../images/all/1/04.jpg'
  187. self._helper1(image_path, ResultItem1(status='000',
  188. orientation=0,
  189. type='乡村家庭户',
  190. address='宁夏吴忠市利通区金积镇丁家湾子村18040号'))
  191. def test_1_05(self):
  192. image_path = '../images/all/1/05.jpg'
  193. self._helper1(image_path, ResultItem1(status='000',
  194. orientation=0,
  195. type='乡村家庭户',
  196. address='宁夏吴忠市利通区郭家桥乡吴桥村四队88号'))
  197. def test_1_mohu(self):
  198. image_path = '../images/all/1/mohu.jpg'
  199. self._helper1(image_path, ResultItem1(status='000',
  200. orientation=0,
  201. type='家庭户',
  202. address='甘肃省林洮县康家集乡大头山村羊角湾社'))
  203. def test_1_special(self):
  204. image_path = '../images/all/1/special.jpg'
  205. self._helper1(image_path, ResultItem1(status='000',
  206. orientation=0,
  207. type='居民家庭户',
  208. address='安徽省马鞍山市花山区湖北东路479号4栋405室'))
  209. if __name__ == '__main__':
  210. unittest.main()