1
0

recognize_time_test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. """
  4. 使用自建的接口识别来自本地的验证码,测试识别某个文件耗时
  5. 需要配置参数:
  6. remote_url = "https://www.xxxxxxx.com/getImg" 验证码链接地址
  7. rec_times = 1 识别的次数
  8. """
  9. import datetime
  10. import requests
  11. from io import BytesIO
  12. import time
  13. import json
  14. import os
  15. from log_ware import LogWare
  16. logger = LogWare().get_logger()
  17. with open("conf/sample_config.json", "r") as f:
  18. sample_conf = json.load(f)
  19. # 配置参数
  20. env = sample_conf["env"] # 环境
  21. webserver_recognize_url = sample_conf['webserver_recognize_url'] # 识别服务器IP
  22. webserver_recognize_port = sample_conf['webserver_recognize_port'] # 识别服务器端口
  23. def recognize_captcha(index, test_path, save_path, image_suffix):
  24. image_file_name = 'captcha.{}'.format(image_suffix)
  25. with open(test_path, "rb") as f:
  26. content = f.read()
  27. # 识别
  28. s = time.time()
  29. url = "http://{}:{}/b".format(webserver_recognize_url, str(webserver_recognize_port))
  30. files = {'image_file': (image_file_name, BytesIO(content), 'application')}
  31. r = requests.post(url=url, files=files)
  32. e = time.time()
  33. # 测试参数
  34. result_dict = json.loads(r.text)["value"] # 响应
  35. predict_text = result_dict["value"] # 识别结果
  36. whole_time_for_work = int((e - s) * 1000)
  37. speed_time_by_rec = result_dict["speed_time(ms)"] # 模型识别耗时
  38. request_time_by_rec = whole_time_for_work - speed_time_by_rec # 请求耗时
  39. now_time = datetime.datetime.now().strftime('%Y-%m-%d@%H:%M:%S') # 当前时间
  40. # 记录日志
  41. log = "{},{},{},{},{},{}\n" \
  42. .format(index, predict_text, now_time, whole_time_for_work, speed_time_by_rec, request_time_by_rec)
  43. with open("./test.csv", "a+") as f:
  44. f.write(log)
  45. # 输出结果到控制台
  46. logger.debug("次数:%s, 结果:%s, 时刻:%s, 总耗时:%s ms, 识别:%s ms, 请求:%s ms",
  47. index, predict_text, now_time, whole_time_for_work, speed_time_by_rec, request_time_by_rec)
  48. if env.lower() == 'dev':
  49. # 保存文件
  50. img_name = "{}_{}.{}".format(predict_text, str(time.time()).replace(".", ""), image_suffix)
  51. path = os.path.join(save_path, img_name)
  52. with open(path, "wb") as f:
  53. f.write(content)
  54. def main():
  55. # 配置相关参数
  56. test_file = "sample/test/0001_15430304076164024.png" # 测试识别的图片路径
  57. save_path = sample_conf["local_image_dir"] # 保存的地址
  58. image_suffix = sample_conf["image_suffix"] # 文件后缀
  59. for i in range(20000):
  60. recognize_captcha(i, test_file, save_path, image_suffix)
  61. if __name__ == '__main__':
  62. main()