Skip to content

Commit 651066c

Browse files
chapter10
1 parent 4249eab commit 651066c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Chapter10/CaptchaGenerator.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from claptcha import Claptcha
2+
import os
3+
import numpy as np
4+
import cv2
5+
import fire
6+
from elapsedtimer import ElapsedTimer
7+
8+
def generate_captcha(outdir,font,num_captchas=20000):
9+
alphabets = 'abcdefghijklmnopqrstuvwxyz'
10+
alphabets = alphabets.upper()
11+
try:
12+
os.mkdir(outdir)
13+
except:
14+
'Directory already present,writing captchas to the same'
15+
#rint(char_num_ind)
16+
# select one alphabet if indicator 1 else number
17+
for i in range(num_captchas):
18+
char_num_ind = list(np.random.randint(0,2,4))
19+
text = ''
20+
for ind in char_num_ind:
21+
if ind == 1:
22+
loc = np.random.randint(0,26,1)
23+
text = text + alphabets[np.random.randint(0,26,1)[0]]
24+
else:
25+
text = text + str(np.random.randint(0,10,1)[0])
26+
c = Claptcha(text,font)
27+
text,image = c.image
28+
image.save(outdir + text + '.png')
29+
30+
def main_process(outdir_train,num_captchas_train,
31+
outdir_val,num_captchas_val,
32+
outdir_test,num_captchas_test,
33+
font):
34+
35+
generate_captcha(outdir_train,font,num_captchas_train)
36+
generate_captcha(outdir_val,font,num_captchas_val)
37+
generate_captcha(outdir_test,font,num_captchas_test)
38+
39+
40+
if __name__ == '__main__':
41+
with ElapsedTimer('main_process'):
42+
fire.Fire(main_process)
43+
44+
45+

0 commit comments

Comments
 (0)