DAY8:验证码辨识(一)

今天要来跟大家分享验证码辨识,因为公司也有用到,我就搭配一个网站,让大家来尝试看看,这个网站是"身分证国民身分证领补换资料查询作业",银行作业很常会用到,今天就以他的验证码来做练习吧!!
网站连结

  • 图片蒐集

要做图像辨识,我们第一件事情一定是先蒐集资料,爬取够多的图片,才能练出更准确的模型。小弟爬取图片的部分,是跟我们公司老大学的,主要先撷取网站页面,再定位验证码图片位置,把验证码再截图下来,再做人工标记。

这边我们主要用selenium去做操作,要用selenium开启chrome要先去这边下载chromedriver.exe。另外我们需要储存图片,我这边使用PIL的Image去做图片的开启和储存。我们这个图片爬取的执行都是在背景下执行,不开视窗。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image

chrome_options = Options()
chrome_options.add_argument("--headless")#不开视窗,在背景下处理
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.ris.gov.tw/apply-idCard/app/idcard/IDCardReissue/main")

先进去网页整页截图。

scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot('./cap_pic/fullpage.jpg')

这是我们全屏截图的图片。

现在我们要把他图形验证的图片给切下来。
先找到图形验证码的位置,然後抓取他的位置,指定好宽高,之後打开刚刚的全屏截图,在将我们设定的位置给取出来,转换成RGB去做储存。

element = driver.find_element_by_xpath('//*[@id="captchaImage_captcha-refresh"]')
left = element.location['x']
right = element.location['x'] + element.size['width']
top = element.location['y']
bottom = element.location['y'] + element.size['height']
img = Image.open('./cap_pic/fullpage.jpg')
img = img.crop((left, top, right, bottom))
img = img.convert("RGB")
img.save(f'C:/Users/Frank/PycharmProjects/practice/captcha_recognition/pic/{i}.jpg')

我们要抓很多张,这边假设我们抓100张图片,下面是完整程序码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image


for i in range(0,100):
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://www.ris.gov.tw/apply-idCard/app/idcard/IDCardReissue/main")
    ######
    scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
    scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
    driver.set_window_size(scroll_width, scroll_height)
    driver.save_screenshot('./cap_pic/fullpage.jpg')
    # 网页中图片验证码的位置
    element = driver.find_element_by_xpath('//*[@id="captchaImage_captcha-refresh"]')
    left = element.location['x']
    right = element.location['x'] + element.size['width']
    top = element.location['y']
    bottom = element.location['y'] + element.size['height']
    img = Image.open('./cap_pic/fullpage.jpg')
    img = img.crop((left, top, right, bottom))
    img = img.convert("RGB")
    img.save(f'./pic/{i}.jpg')
    print(f"已储存:{i}.jpg")

这样我们就把图片抓取下来啦,再来就是手动标签每张图片的label了。

因为如果在图片名称直接标签会有个问题,就是万一label是一样的,档名就会重复,那等於一种组合的字只能有一张图片,所以我会把它标记在CSV里面。

  • 今日小结

今天就先分享爬取图片的方法,因为这几天较忙,没有太多时间,所以建模部分,我就放在明天的部分再跟各位分享罗!!
我主要用pytorch的框架,会分享一个自己架CNN和使用预训练模型去训练的差别。
谢谢各位~明天见罗!!


<<:  Day 8 - 社交工程 101

>>:  [Day 08] 使用 fastAPI 部署 YOLOv4 (2/2) — 自行撰写 Client 进行互动

D26 将config等等隐密资讯另外放置 - yaml

将重要资讯放到yaml内 config.yaml(放在BASE_DIR) --- email: EM...

【Day 05】C 的资料型态(上)

今天一开始,我们先来讲讲基本的常识~~ 甚麽是位元、位元组? 位元(bit)可以保有两种资料(0 和...

<Day1> 前言

简短自我介绍 大家好!我是Marshal,目前还是一位在校大学生,就读资讯相关科系。 为什麽会想选这...

C语言和你 SAY HELLO!!

第二天 各位点进来的朋友,你们好阿 一样废话不多说直接上内容啦~~ ----------------...

【PHP Telegram Bot】Day13 - 基础(2):数学运算与乱数

$x = 3 + 2 * 8 - 2 ** 3; echo $x; // 11 在程序里最常做的事...