generate_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import sys
  2. import re
  3. import json
  4. from itertools import chain
  5. from pathlib import Path
  6. import cv2
  7. # 配置
  8. img_type = 1
  9. data = '{"image": img_str, "image_type": image_type}'
  10. test_path = 'testing/clear_1_test.py'
  11. images_path = Path('images/clean/1')
  12. root = Path(__file__).parent
  13. img_paths = chain(*[Path(root / images_path).rglob(f'*.{ext}') for ext in ['jpg']])
  14. dict_orientation = {0: None, 1: cv2.ROTATE_90_CLOCKWISE, 2: cv2.ROTATE_180, 3: cv2.ROTATE_90_COUNTERCLOCKWISE}
  15. head = f"""
  16. import unittest
  17. import base64
  18. from dataclasses import dataclass
  19. from pathlib import Path
  20. import cv2
  21. import requests
  22. url = 'http://192.168.199.249:38811'
  23. def send_request(image_path, image_type, rotate=None):
  24. img = cv2.imread(str(image_path))
  25. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  26. if rotate is not None:
  27. img = cv2.rotate(img, rotate)
  28. _, im_arr = cv2.imencode('.jpg', img)
  29. img_str = base64.b64encode(im_arr).decode('utf-8')
  30. r = requests.post(url + '/ocr_system/regbook', json={data})
  31. print(r.json())
  32. return r.json()
  33. @dataclass
  34. class ResultItem:
  35. status: str
  36. orientation: int
  37. type: str
  38. address: str
  39. address_province: str
  40. address_city: str
  41. address_region: str
  42. address_detail: str
  43. name: str
  44. id: str
  45. gender: str
  46. birthplace: str
  47. birthplace_province: str
  48. birthplace_city: str
  49. birthplace_region: str
  50. native_place: str
  51. native_place_province: str
  52. native_place_city: str
  53. native_place_region: str
  54. blood_type: str
  55. religion: str
  56. """
  57. masterclass = f"""
  58. class TestRegBookOcr(unittest.TestCase):
  59. def _helper(self, image_path, item: ResultItem, rotate=None,image_type={img_type}):
  60. root = Path(__file__).parent
  61. image_path = str(root / image_path)
  62. r = send_request(image_path, image_type, rotate)
  63. self.assertEqual(item, ResultItem(status=r['status'],
  64. orientation=r['result']['orientation'],
  65. type=r['result']['type']['text'],
  66. address=r['result']['address']['text'],
  67. address_province=r['result']['address_province']['text'],
  68. address_city=r['result']['address_city']['text'],
  69. address_region=r['result']['address_region']['text'],
  70. address_detail=r['result']['address_detail']['text'],
  71. name=r['result']['name']['text'],
  72. id=r['result']['id']['text'],
  73. gender=r['result']['gender']['text'],
  74. birthplace=r['result']['birthplace']['text'],
  75. birthplace_province=r['result']['birthplace_province']['text'],
  76. birthplace_city=r['result']['birthplace_city']['text'],
  77. birthplace_region=r['result']['birthplace_region']['text'],
  78. native_place=r['result']['native_place']['text'],
  79. native_place_province=r['result']['native_place_province']['text'],
  80. native_place_city=r['result']['native_place_city']['text'],
  81. native_place_region=r['result']['native_place_region']['text'],
  82. blood_type=r['result']['blood_type']['text'],
  83. religion=r['result']['religion']['text']))
  84. """
  85. with open(root / test_path, 'w', encoding='utf-8') as f:
  86. f.write(head)
  87. f.write(masterclass)
  88. for path in img_paths:
  89. pic_name = path.stem.split("_img")[0]
  90. pic_path = str(path.parent).split("images")[-1] + '/' + path.name
  91. pic_json_name = path.stem + '.json'
  92. pic_json_path = path.parent / pic_json_name
  93. with pic_json_path.open('r') as json_f:
  94. json_input = json.load(json_f)
  95. del [json_input['img_type']]
  96. statsu_input = {'status': '000'}
  97. json_input = dict(statsu_input, **json_input)
  98. testfunc = f"""
  99. def case_{pic_name}_0(self, orientation=0):
  100. dict_orientation = {dict_orientation}
  101. image_path = '../images{pic_path}'
  102. json_input = {json_input}
  103. json_input['orientation'] = orientation
  104. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  105. def test_case_{pic_name}_0(self):
  106. self.case_{pic_name}_0(0)
  107. def test_case_{pic_name}_1(self):
  108. self.case_{pic_name}_0(1)
  109. def test_case_{pic_name}_2(self):
  110. self.case_{pic_name}_0(2)
  111. def test_case_{pic_name}_3(self):
  112. self.case_{pic_name}_0(3)
  113. """
  114. f.write(testfunc)
  115. print('ok')