-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
304 lines (246 loc) · 8.49 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/python3
import glob
import csv
import string
import os
import time
import cv2
import requests
import hashlib
from reg_predict_one import predict_img
def test_pred():
pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\webCrawler\bojindai_screen\labeled\*.png')
# pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\any-captcha-jj\output\bojindai_test_fake\*.jpg')
total_cnt = len(pics)
print('test count: ', total_cnt)
true_cnt = 0
for pic in pics:
pred = predict_img(pic)
# real = pic[-41:-37]
real = pic[-8:-4]
print('real-predict: ', real, pred)
if pred == real:
true_cnt += 1
print('Accuracy: ', true_cnt/total_cnt)
def label_bojindai():
pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\webCrawler\bojindai_screen\*.png')
# pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\any-captcha-jj\output\bojindai_test_fake\*.jpg')
total_cnt = len(pics)
chs = string.digits + string.ascii_lowercase
print('possible characters: ', chs)
print('test count: ', total_cnt)
true_cnt = 0
for pic in pics:
true_cnt += 1
pred = predict_img(pic)
print('predict%s: ' % true_cnt, pred)
if len(pred) == 4:
if pred[0] in chs and pred[1] in chs and pred[2] in chs and pred[3] in chs:
os.rename(pic, pic.replace('xxxx_', pred+'_'))
# label_bojindai()
def phone_split():
with open('./phone_list/data.txt', 'r') as f:
s = f.read()
data = s.split('\n')
xinye_phone = []
with open('./phone_list/30642_for_feng.csv', 'rU') as xy:
reader = csv.reader(xy)
for row in reader:
xinye_phone.append(row[0])
print(xinye_phone[1])
# distinct
phone_list0 = list(set(xinye_phone))
phone_list = []
for p in phone_list0:
if len(p) == 11:
phone_list.append(p)
print(len(phone_list))
# split_cnt = 0
# data = []
# for j, no in enumerate(phone_list):
# data.append(no)
# if j == 3832 * (split_cnt+1)-1 or j == len(phone_list)-1:
# # print(type(data[0]))
# with open('./phone_list/data{}.csv'.format(split_cnt), 'w', newline='') as dt:
# writer = csv.writer(dt)
# for row in data:
# writer.writerow([row])
#
# split_cnt += 1
# data = []
def result_combine():
result = [['phone_no', '博金贷', '拓道金服', '爱投金融']]
for i in range(10):
with open('./result/result_data{}.csv'.format(i), 'rU') as fr:
reader = csv.reader(fr)
next(reader)
for v in reader:
result.append(v)
print('Total number of phone: ', len(result))
with open('./result/result_data_0801.csv', 'w', newline='') as rs:
writer = csv.writer(rs)
for row in result:
writer.writerow(row)
def label_pic():
pics = glob.glob(r'C:\Bee\Python\Projects\registry_check\aitouzi_pic\batch\*.png')
print('Number of pictures: ', len(pics))
for i, pic in enumerate(pics):
img = cv2.imread(pic)
cv2.imshow('原始', img)
cv2.waitKey(10)
lab_value = input('Please label%s: ' % i)
os.rename(pic, pic.replace(pic[-41:-36], lab_value + '_'))
# break
# label_pic()
def auto_label():
from aitouzi_api import CNNPredictionATZ
cnnm_atz = CNNPredictionATZ()
pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\webCrawler\aitouzi_unlab\*.png')
print('Number of pictures: ', len(pics))
for i, pic in enumerate(pics):
img = cv2.imread(pic)
pred = cnnm_atz.make_prediction(pic)
print(i, pred)
cv2.imshow('原始', img)
cv2.waitKey(10)
lab_value = input('Please label%s: ' % i)
if lab_value == '0':
os.rename(pic, pic.replace('\\_', '\\' + pred + '_')) # true prediction
else:
os.rename(pic, pic.replace('\\_', '\\' + lab_value + '_')) # input label
# auto_label()
def cnn_label():
from aitouzi_api import CNNPredictionATZ
cnnm_atz = CNNPredictionATZ()
pics = glob.glob(r'C:\Bee\Python\Projects\registry_check\aitouzi_pic\*.png')
print('Number of pictures: ', len(pics))
for i, pic in enumerate(pics):
img = cv2.imread(pic)
pred = cnnm_atz.make_prediction(pic)
print(i, pred)
os.rename(pic, pic.replace('xxxx_', pred + '_')) # true prediction
# cnn_label()
def add_md5():
pics = glob.glob(r'C:\Bee\Python\Projects\ComputerVision\webCrawler\bojindai_screen\labeled_test\*.png')
for pic in pics:
cont = pic[-8:-4]
t = str(time.time())
md = hashlib.md5()
md.update(t.encode('utf-8'))
pic_name = md.hexdigest()
os.rename(pic, pic.replace(cont, cont+'_'+pic_name))
def find_invalid_characters():
characters = string.digits + string.ascii_lowercase
pic_dir = os.listdir(r'C:\Bee\Python\Projects\registry_check\aitouzi_pic')
for pic in pic_dir:
if pic[4] != '_':
print(pic)
continue
for lt in pic[:4]:
if lt not in characters:
print(pic)
break
def remove_same():
characters = string.digits + string.ascii_lowercase
pic_dir = os.listdir(r'C:\Bee\Python\Projects\registry_check\aitouzi_pic')
exist_pics = set()
for pic in pic_dir:
if pic[:4] in exist_pics:
os.remove(r'C:\Bee\Python\Projects\registry_check\aitouzi_pic\\' + pic)
else:
exist_pics.add(pic[:4])
from multiprocessing import Pool
import multiprocessing
# s = 0
def multiply(x):
# global s
s = x[0] * x[1]
time.sleep(2)
return s
def multiply1(x):
# global s
s = x * y
time.sleep(2)
return s
def multi_process():
"""Must be run in if __name__ == "__main__"."""
a = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
a1 = [(1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,)]
t0 = time.clock()
# result = []
pool = Pool(8)
result = pool.starmap(func=multiply1, iterable=a1)
pool.close()
pool.join()
print(result)
t1 = time.clock()
print(t1-t0)
def multi_process1():
"""Must be run in if __name__ == "__main__"."""
a = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
a1 = [(1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,)]
t0 = time.clock()
# result = []
cores = multiprocessing.cpu_count()
print('cores cnt: ', cores)
pool = multiprocessing.Pool(processes=cores)
# method 1: map
print(pool.map(multiply, a)) # prints [0, 1, 4, 9, 16]
# # method 2: imap
# for y in pool.imap(multiply, a):
# print(y) # 0, 1, 4, 9, 16, respectively
t1 = time.clock()
print(t1-t0)
if __name__ == "__main_":
multi_process1()
pass
def get_proxy():
return requests.get("http://127.0.0.1:5010/get/").content
def delete_proxy(proxy):
requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy))
# your spider code
def getHtml():
# ....
retry_count = 5
proxy = get_proxy()
print('proxy', proxy)
while retry_count > 0:
try:
html = requests.get('https://blog.csdn.net/zpf_07/article/details/78030249', proxies={"http": "http://{}".format(proxy)})
# 使用代理访问
return html
except Exception:
retry_count -= 1
# 出错5次, 删除代理池中代理
delete_proxy(proxy)
return None
# getHtml()
def check_ip():
try:
requests.get('http://wenshu.court.gov.cn/', proxies={"http": "183.129.207.86:11632"})
except:
print('connection failed')
else:
print('success')
# check_ip()
class C(object):
def __init__(self, x):
self._x = x
# @property
def showx(self):
return self._x
def cross_combine():
aitouzi, rest = {}, {}
for i in range(8):
with open(r'C:\Bee\Python\Projects\registry_check\result\result_data{}.csv'.format(i), 'rU') as fr:
reader = csv.reader(fr)
for row in reader:
aitouzi[row[0]] = row[1:]
with open(r'C:\Bee\Python\Projects\registry_check\result\result_30642_for_feng.csv', 'rU') as fr:
reader = csv.reader(fr)
for row in reader:
aitouzi[row[0]].append(row[1])
with open(r'C:\Bee\Python\Projects\registry_check\result\result_30642_0819.csv', 'w', newline='') as fw:
writer = csv.writer(fw)
for k, v in aitouzi.items():
writer.writerow([k] + v)