generate_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = 0
  9. data = '{"image": img_str, "image_type": image_type}'
  10. test_path = 'testing/id727_0_test.py'
  11. images_path = Path('images/test_img/7.27/0')
  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:18080'
  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/idcard', json={data})
  31. print(r.json())
  32. return r.json()
  33. @dataclass
  34. class ResultItem:
  35. status: str
  36. orientation: int
  37. name: str
  38. id: str
  39. ethnicity: str
  40. gender: str
  41. birthday: str
  42. address: str
  43. address_province: str
  44. address_city: str
  45. address_region: str
  46. address_detail: str
  47. expire_date:str
  48. """
  49. masterclass = f"""
  50. class TestSchoolCertOcr(unittest.TestCase):
  51. def _helper(self, image_path, item: ResultItem, rotate=None,image_type={img_type}):
  52. root = Path(__file__).parent
  53. image_path = str(root / image_path)
  54. r = send_request(image_path, image_type, rotate)
  55. self.assertEqual(item, ResultItem(status=r['status'],
  56. orientation=r['result']['orientation'],
  57. name=r['result']['name']['text'],
  58. id=r['result']['id']['text'],
  59. ethnicity=r['result']['ethnicity']['text'],
  60. gender=r['result']['gender']['text'],
  61. birthday=r['result']['birthday']['text'],
  62. address=r['result']['address']['text'],
  63. address_province=r['result']['address_province']['text'],
  64. address_city=r['result']['address_city']['text'],
  65. address_region=r['result']['address_region']['text'],
  66. address_detail=r['result']['address_detail']['text'],
  67. expire_date=r['result']['expire_date']['text']))
  68. """
  69. with open(root / test_path, 'w', encoding='utf-8') as f:
  70. f.write(head)
  71. f.write(masterclass)
  72. for path in img_paths:
  73. pic_name = path.stem.split("_img")[0]
  74. pic_path = str(path.parent).split("images")[-1] + '/' + path.name
  75. pic_json_name = path.stem + '.json'
  76. pic_json_path = path.parent / pic_json_name
  77. with pic_json_path.open('r') as json_f:
  78. json_input = json.load(json_f)
  79. del [json_input['card_type']]
  80. statsu_input = {'status': '000'}
  81. json_input = dict(statsu_input, **json_input)
  82. testfunc = f"""
  83. def case_{pic_name}_0(self, orientation=0):
  84. dict_orientation = {dict_orientation}
  85. image_path = '../images{pic_path}'
  86. json_input = {json_input}
  87. json_input['orientation'] = orientation
  88. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  89. def test_case_{pic_name}_0(self):
  90. self.case_{pic_name}_0(0)
  91. def test_case_{pic_name}_1(self):
  92. self.case_{pic_name}_0(1)
  93. def test_case_{pic_name}_2(self):
  94. self.case_{pic_name}_0(2)
  95. def test_case_{pic_name}_3(self):
  96. self.case_{pic_name}_0(3)
  97. """
  98. f.write(testfunc)
  99. print('ok')