forked from nickliqian/cnn_captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize_time_test.py
70 lines (55 loc) · 2.26 KB
/
recognize_time_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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
def recognize_captcha(index, 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://127.0.0.1:6000/b"
files = {'image_file': (image_file_name, BytesIO(content), 'application')}
r = requests.post(url=url, files=files)
e = time.time()
# 测试参数
result_dict = json.loads(r.text)["value"] # 响应
predict_text = result_dict["value"] # 识别结果
whole_time_for_work = int((e - s) * 1000)
speed_time_by_rec = result_dict["speed_time(ms)"] # 模型识别耗时
request_time_by_rec = whole_time_for_work - speed_time_by_rec # 请求耗时
now_time = datetime.datetime.now().strftime('%Y-%m-%d@%H:%M:%S') # 当前时间
# 记录日志
log = "{},{},{},{},{},{}\n"\
.format(index, predict_text, now_time, whole_time_for_work, speed_time_by_rec, request_time_by_rec)
with open("./test.csv", "a+") as f:
f.write(log)
# 输出结果到控制台
print("次数:{},结果:{},时刻:{},总耗时:{}ms,识别:{}ms,请求:{}ms"
.format(index, predict_text, now_time, whole_time_for_work, speed_time_by_rec, request_time_by_rec))
# 保存文件
# 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)
def main():
with open("conf/sample_config.json", "r") as f:
sample_conf = json.load(f)
# 配置相关参数
test_file = "sample/test/0001_15430304076164024.png" # 测试识别的图片路径
save_path = sample_conf["local_image_dir"] # 保存的地址
image_suffix = sample_conf["image_suffix"] # 文件后缀
for i in range(20000):
recognize_captcha(i, test_file, save_path, image_suffix)
if __name__ == '__main__':
main()