Skip to content

Commit

Permalink
Update Cap Recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
ter-s committed Nov 20, 2021
1 parent d6a2b02 commit 53e464d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea/
/__pycache__/
39 changes: 39 additions & 0 deletions cap_denoise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from io import BytesIO
from PIL import Image

def dn(cap):
img = Image.open(BytesIO(cap))
_STEPS_LAYER_1 = ((1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1))
STEPS8 = _STEPS_LAYER_1
_PX_WHITE=(250,250,250)
def _denoise(img, steps, threshold, repeat):
for _ in range(repeat):
for j in range(img.width):
for i in range(img.height):
px = img.getpixel((j,i))
if px == _PX_WHITE: # 自身白
continue
count = 0
if px[0]<px[1]+px[2]:
count=2
for x, y in steps:
j2 = j + y
i2 = i + x
if 0 <= j2 < img.width and 0 <= i2 < img.height: # 边界内
if img.getpixel((j2,i2)) == _PX_WHITE: # 周围白
count += 1
else: # 边界外全部视为黑
count += 1
if count >= threshold:
img.putpixel((j,i), _PX_WHITE)

return img

def denoise8(img, steps=STEPS8, threshold=7, repeat=2):
""" 考虑外一周的降噪 """
return _denoise(img, steps, threshold, repeat)

buf = BytesIO()
denoise8(img).save(buf, format='PNG')
byte_im = buf.getvalue()
return byte_im
11 changes: 7 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
from Crypto.PublicKey import RSA
from ddddocr import DdddOcr

from cap_denoise import dn

org_id = '172442' # "北京市海淀团区委"

username = os.environ["USERNAME"]
password = os.environ["PASSWORD"]


if not (username and password):
raise Exception("请设置Secret: USERNAME和PASSWORD")

# or check string type
try:
org_id_input = os.environ["ORGID"]
if org_id_input:
org_id=int(org_id_input)
org_id = int(org_id_input)
except:
...


def encrypt(t):
public_key = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD5uIDebA2qU746e/NVPiQSBA0Q3J8/G23zfrwMz4qoip1vuKaVZykuMtsAkCJFZhEcmuaOVl8nAor7cz/KZe8ZCNInbXp2kUQNjJiOPwEhkGiVvxvU5V5vCK4mzGZhhawF5cI/pw2GJDSKbXK05YHXVtOAmg17zB1iJf+ie28TbwIDAQAB\n-----END PUBLIC KEY-----"
rsa_key = RSA.importKey(public_key)
Expand All @@ -51,7 +53,8 @@ def encrypt(t):

cap = bjySession.get(url=cap_url)
ocr = DdddOcr()
cap_text = ocr.classification(cap.content)
cap = dn(cap.content)
cap_text = ocr.classification(cap)
print(f'Captcha OCR: {cap_text}')
_csrf_mobile = bjySession.cookies.get_dict()['_csrf_mobile']
# TODO: 有时间看一下这个算法
Expand All @@ -64,7 +67,7 @@ def encrypt(t):
'Login[password]': login_password,
'Login[verifyCode]': cap_text
})
if login_r.text=='8':
if login_r.text == '8':
print('Login: 验证码错误')
print(f'Login: [{login_r.status_code}]{login_r.text}')
r = json.loads(bjySession.get("https://m.bjyouth.net/dxx/index").text)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

建议配置运行频率一周2次(默认为3天一次),没有成功会出错,默认配置下Github会向邮箱推送,所以没有推送功能


更新:优化了验证码识别和验证码失败的反馈


# How to use
Expand Down
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ddddocr
requests
pycryptodome
ddddocr~=1.0.6
requests~=2.26.0
pycryptodome
Pillow~=8.4.0

0 comments on commit 53e464d

Please sign in to comment.