当前位置:网站首页>GIF验证码分析
GIF验证码分析
2022-06-23 07:00:00 【考古学家lx(李玺)】
GIF验证码 和 普通验证码 的区别是图片上的文字是时隐时现的,如果按帧数查看,每帧都会缺失文字。
GIF验证码图片示例:

所以现在有如下思路:
- 方案一:对图片多次截图,然后把所有图片的数组合并覆盖到一张图中,再识别得到完整图片。
- 方案二:对图片多次抽帧,然后对每张图片单独识别,每个位置出现字符频率最高的则为正确结果。
方案一代码:
from PIL import Image
import numpy as np
import cv2
path = r"C:\Users\lixi\Desktop\p1.gif"
image =Image.open(path)
shapes = []
for i in range(1,4):
image.seek(i)
image.save(f'image/{
i}.png')
shapes.append(np.array(image))
result = np.subtract(shapes[0], shapes[2])
result2 = np.subtract(shapes[0], shapes[1])
result3 = np.subtract(shapes[1], shapes[2])
cv2.imwrite("image/result.png", shapes[0] + result+result2+result3)
合并后的图片:
方案二代码:(代码转自 https://juejin.cn/post/6855483334512869389)
# -*- coding: utf-8 -*-
# @Software: PyCharm
import requests
import time
import json
from PIL import Image
from io import BytesIO
from collections import Counter
def get_max_char(str):
''' 获取频率最高字符 :param str: :return: '''
count = Counter(str)
count_list = list(count.values())
max_value = max(count_list)
max_list = []
for k, v in count.items():
if v == max_value:
max_list.append(k)
return max_list[0]
def recogition(yzm_data):
''' 验证码识别 :param yzm_data: :return: '''
resp = requests.post('http://127.0.0.1:8080', data=yzm_data)
return resp.text
def img_to_text(yzmdatas):
''' 图片转字符 :param length: :return: '''
yzm1 = ""
yzm2 = ""
yzm3 = ""
yzm4 = ""
for data in yzmdatas:
text = recogition(data)
json_obj = json.loads(text)
yzm_text = json_obj.get("code","")
#本文中的验证码长度为4 实际测试中只要长度大于等于4的都可以统计进去,不影响识别准确率
if len(yzm_text) == 4:
l_yzm = list(yzm_text)
yzm1 = yzm1 + l_yzm[0]
yzm2 = yzm2 + l_yzm[1]
yzm3 = yzm3 + l_yzm[2]
yzm4 = yzm4 + l_yzm[3]
yzm1 = get_max_char(yzm1)
yzm2 = get_max_char(yzm2)
yzm3 = get_max_char(yzm3)
yzm4 = get_max_char(yzm4)
return yzm1+yzm2+yzm3+yzm4
def download():
''' 下载验证码 :return: '''
#验证码地址
url = 'http://credit.customs.gov.cn/ccppserver/verifyCode/creator'
resp = requests.get(url)
data = resp.content
return data
def gif_to_png(length,image):
''' gif抽帧 :param length: :param image: :return: '''
try:
yzm_list = []
for i in range(1, length):
image.seek(i)
stream = BytesIO()
image.save(stream, 'PNG')
s = stream.getvalue()
yzm_list.append(s)
return yzm_list
except Exception as e:
print(e)
return None
def handle_yzm(length):
''' 处理验证码 :return: '''
gif = download()
start = time.time()
if gif:
data = BytesIO(gif)
image = Image.open(data)
png_list = gif_to_png(length, image)
if png_list:
yzm_text = img_to_text(png_list)
with open("./Gif_IMG/{}_{}.gif".format(yzm_text, str(time.time())),"wb") as fw:
fw.write(gif)
end = time.time()
print("抽帧length:{}-花费时间:{}".format(length, end - start))
def run():
#抽帧长度:具体抽帧多少可以依据实际的gif识别准确率来调整。
#抽帧越少识别率可能会低,但是识别所需的时间会减少。23帧准确率98%,时间1s; 6帧准确率85%,时间0.5s左右
#在识别速度和精度之间找一个平衡点即可
length = 10
#识别图片个数
num = 20
for i in range(num):
handle_yzm(length)
if __name__ == '__main__':
run()
方案一比较简单粗暴,但是会把其他干扰元素也合并到一块,所以合并后还需要其他处理方法。
方案二代码转别人的,效果会好一点,先识别再合成,但是效率不高,需要自己改。
两个方案的代码在使用时都需要优化和调整,大家根据自己的需求选择对应的方案。
边栏推荐
- HCIP之路MPLS
- [cloud computing event] vocational skill competition -- container development example pig rapid development framework
- Spock constraint - call frequency / target / method parameters
- Flannel 工作原理
- csrf攻击在laravel中如何解决
- Solutions to abnormal network connection of Xiaoai speakers
- Nacos适配oracle11g-修改Nacos源码
- User mode and kernel mode
- Product axure9 (English version), prototype design background dynamic secondary menu display content
- MySQL Niuke brush questions
猜你喜欢

vs在连接SQL时出现的问题myconn.OPen();无法运行

在kubernetes中部署kubersphere

How to tag and label naming before the project release

How to quickly and gracefully download large files from Google cloud disk (II)

干货来了|《PaaS》合辑抢先看~

Data types in tensorflow

JS to determine the added and decreased elements of two arrays

Online text filter less than specified length tool

用户态和内核态

SimpleDateFormat 线程安全问题
随机推荐
20BN-Jester完整数据集下载
【星球精选】如何高效构建 Roam 与 theBrain 间细粒度双向链接?
The Sandbox 与《足球小将》达成合作,将流行的足球漫画及动画带入元宇宙
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
[AI practice] xgbgressor model accelerates training and uses GPU to train xgbgressor in seconds
Several characteristics of MySQL database
【AI实战】XGBRegressor模型加速训练,使用GPU秒级训练XGBRegressor
char和varchar区别
[* * * array * * *]
How to solve CSRF attack in laravel
Heuristic search strategy
Data types in tensorflow
Detailed explanation of redis persistence, master-slave and sentry architecture
RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
Spock sub piling
Yan's DP analysis
Left multiply right multiply matrix problem
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
Redis设置密码
传智教育 | 多人协作开发出现代码冲突,如何合并代码?