-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImgMain.py
63 lines (50 loc) · 1.92 KB
/
ImgMain.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
# -*- coding: utf-8 -*-
import cv2
from Convert import Convert
from CharMap import charMap
import numpy as np
def cutting_img(im,im_position,xoffset = 1,yoffset = 1):
# 识别出的字符个数
im_number = len(im_position[1])
if(im_number>=4): im_number = 4;
imgArr = []
# 切割字符
for i in range(im_number):
im_start_X = im_position[1][i][0] - xoffset
im_end_X = im_position[1][i][1] + xoffset
im_start_Y = im_position[2][i][0] - yoffset
im_end_Y = im_position[2][i][1] + yoffset
cropped = im[im_start_Y:im_end_Y, im_start_X:im_end_X]
imgArr.append(cropped)
# cv2.imwrite(str(i)+"v.jpg",cropped) # 查看切割效果
return im_number,imgArr
def get_code(code_request):
cvt = Convert()
req = code_request
# 注意有些教务加装了所谓云防护,没有请求头会拦截,导致获取不了验证码图片,报错可以打印req.content看看
img = cvt.run(req.content)
# cv2.imwrite("v.jpg",img) # 查看验证码
# 切割的位置
im_position = ([7, 7, 7, 7], [[5, 12], [15, 22], [25, 32], [34, 41]], [[4, 15], [4, 15], [4, 15], [4, 15]])
cutting_img_num,imgArr = cutting_img(img,im_position,1,1)
# 识别验证码
result=""
for i in range(cutting_img_num):
try:
template = imgArr[i]
tempResult=""
matchingDegree=0.0
for char in charMap:
img = np.asarray(charMap[char],dtype = np.uint8)
res = cv2.matchTemplate(img,template,3) #img原图 template模板 用模板匹配原图
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if(max_val>matchingDegree):
tempResult=char
matchingDegree=max_val
result += tempResult
matchingDegree=0.0
except Exception as err:
raise Exception
# print("ERROR "+ str(err))
pass
return result