-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.py
42 lines (39 loc) · 1.44 KB
/
a.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
import requests
import numpy
import PIL
import os
import shutil
import cv2
from PIL import Image
index = 0
path = os.getcwd()
os.chdir(path + '/' + 'data_unpro')
number = 0
for index in range(500):
with open('kaptcha.jpg', 'wb') as file:
res = requests.get('https://ap.ceec.edu.tw/RegInfo/Account/CaptchaImage', verify = True)
file.write(res.content)
image = cv2.imread('kaptcha.jpg')
open_cv_image = numpy.array(image)
nc = cv2.fastNlMeansDenoisingColored(open_cv_image, None, 13, 13, 7, 21)
imgray = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(imgray, 180, 255, 0, cv2.THRESH_BINARY)
#plt.imshow(thresh)
#plt.show()
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in contours], key = lambda x: x[1])
ary = []
for (c, _) in cnts:
(x, y, w, h) = cv2.boundingRect(c)
if w > 10: print((x, y, w, h))
if x < 70 and w >= 6 and w < 15 and h >= 10 and x > 0:
ary.append((x, y, w, h))
print("This")
print((x, y, w, h))
for id, (x, y, w, h) in enumerate(ary):
roi = imgray[y:y + h, x:x + w]
thresh = roi.copy()
with open('{}.jpg'.format(number), 'wb') as f:
cv2.imwrite('{}.jpg'.format(number), thresh)
number = number + 1
print(number)