12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- """
- 使用自建的接口识别来自本地的验证码
- 需要配置参数:
- remote_url = "https://www.xxxxxxx.com/getImg" 验证码链接地址
- rec_times = 1 识别的次数
- """
- import datetime
- import requests
- from io import BytesIO
- import time
- import json
- import os
- from log_ware import LogWare
- logger = LogWare().get_logger()
- with open("conf/sample_config.json", "r") as f:
- sample_conf = json.load(f)
- # 配置参数
- env = sample_conf["env"] # 环境
- local_save_path = sample_conf["local_image_dir"] # 本地识别后的保存路径
- local_origin_image_dir = sample_conf["local_origin_image_dir"] # 本地待识别图片的路径
- image_suffix = sample_conf["image_suffix"] # 文件后缀
- webserver_recognize_url = sample_conf['webserver_recognize_url'] # 识别服务器IP
- webserver_recognize_port = sample_conf['webserver_recognize_port'] # 识别服务器端口
- def recognize_captcha(test_path, save_path, image_suffix):
- image_file_name = 'captcha.{}'.format(image_suffix)
- with open(test_path, "rb") as f:
- content = f.read()
- # 识别
- s = time.time()
- url = "http://{}:{}/b".format(webserver_recognize_url, str(webserver_recognize_port))
- files = {'image_file': (image_file_name, BytesIO(content), 'application')}
- r = requests.post(url=url, files=files)
- e = time.time()
- # 识别结果
- logger.debug("本地图片,调用本地识别服务,接口响应: %s", r.text)
- predict_text = json.loads(r.text)["value"]
- now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- logger.debug("【%s】 耗时:%s ms 预测结果:%s", now_time, int((e - s) * 1000), predict_text)
- if env.lower() == 'dev':
- # 保存文件
- img_name = "{}_{}.{}".format(predict_text, str(time.time()).replace(".", ""), image_suffix)
- path = os.path.join(save_path, img_name)
- with open(path, "wb") as f:
- f.write(content)
- logger.debug("============== local recognized end ==============")
- def main():
- files = os.listdir(local_origin_image_dir)
- if len(files) <= 0:
- logger.debug("%s,%s", local_origin_image_dir, "没有任何文件")
- logger.debug("...没有任何文件")
- return
- for file in files:
- test_path = "{}{}".format(local_origin_image_dir, file)
- logger.debug("test_path: %s", test_path)
- recognize_captcha(test_path, local_save_path, image_suffix)
- if __name__ == '__main__':
- main()
|