1
0

correction_captcha.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. """
  4. 人工在线验证脚本
  5. """
  6. import requests
  7. from io import BytesIO
  8. import time
  9. import matplotlib.pyplot as plt
  10. import json
  11. import numpy as np
  12. from PIL import Image
  13. import os
  14. from log_ware import LogWare
  15. logger = LogWare().get_logger()
  16. def correction(fail_path, pass_path, correction_times, remote_url):
  17. headers = {
  18. 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
  19. }
  20. fail_count = 0
  21. for index in range(correction_times):
  22. # 请求
  23. while True:
  24. try:
  25. response = requests.request("GET", remote_url, headers=headers, timeout=10)
  26. break
  27. except Exception as e:
  28. logger.debug(e)
  29. # 识别
  30. s = time.time()
  31. url = "http://127.0.0.1:6000/b"
  32. files = {'image_file': ('captcha.jpg', BytesIO(response.content), 'application')}
  33. r = requests.post(url=url, files=files)
  34. e = time.time()
  35. logger.debug("index:%d,时长:%s ms", index, int((e - s) * 1000))
  36. logger.debug("返回结果: %s", r.text)
  37. time.sleep(2)
  38. # 识别结果
  39. predict_text = json.loads(r.text)["value"]
  40. f = plt.figure()
  41. ax = f.add_subplot(111)
  42. ax.text(0.1, 0.9, "备注", ha='center', va='center', transform=ax.transAxes)
  43. # 图片字节流转为image array
  44. img = BytesIO(response.content)
  45. img = Image.open(img, mode="r")
  46. captcha_array = np.array(img)
  47. plt.imshow(captcha_array)
  48. # 预测图片
  49. logger.debug("预测: %s", predict_text)
  50. # 显示图片和预测结果
  51. plt.text(20, 2, 'predict:{}'.format(predict_text))
  52. plt.show()
  53. q = input("index:<{}> 正确按enter,错误输入真实值后会保存:".format(index))
  54. img_name = "{}_{}".format(q, str(time.time()).replace(".", ""))
  55. if q:
  56. path = os.path.join(fail_path, img_name)
  57. with open(path, "wb") as f:
  58. f.write(response.content)
  59. fail_count += 1
  60. else:
  61. path = os.path.join(pass_path, img_name)
  62. with open(path, "wb") as f:
  63. f.write(response.content)
  64. logger.debug("==============")
  65. rate = (correction_times - fail_count) / correction_times
  66. logger.debug("Pass Rate: %s", rate)
  67. def main():
  68. fail_path = "./sample/fail_sample/"
  69. pass_path = "./sample/pass_sample/"
  70. correction_times = 10
  71. remote_url = "https://www.xxxxxxx.com/getImg"
  72. correction(fail_path, pass_path, correction_times, remote_url)
  73. if __name__ == '__main__':
  74. main()