『工程项目实践』银行卡识别(CTPN+CRNN)
生活随笔
收集整理的這篇文章主要介紹了
『工程项目实践』银行卡识别(CTPN+CRNN)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
銀行卡識別
- 前言
- 一、數據預處理
- 1.1 數據準備
- 1.2 數據增強
- 二、訓練(CRNN)
- 三、需要修改的內容
- 3.1 數據增強
- 3.2 訓練
- 四、CRNN 結構說明
- 4.1 CNN
- 4.2 BiLSTM
- 4.3 CTC
- 五、卡號檢測
- 六、BIN碼校驗
- 參考鏈接
前言
- 對各種銀行卡進行卡號識別,CTPN 進行文字定位,CRNN 進行文字識別以及 Web 實現銀行卡號碼識別
- 我們國內現在目前用的卡類標準為 ISO/IEC7810:2003 的 ID-1 標準,卡片尺寸是 85.60mm*53.98mm,通過計算長寬比可以得到為 0.623.
- 先思考一下我們的大致思路: 因為我們最終是要完成卡面信息的識別,我們可以分為兩個關鍵流程:文本檢測,文本識別
一、數據預處理
1.1 數據準備
- bankcard_pre 每張圖片大小為 120 * 46,圖片命名格式為 0000a_0.png
- 每個原始訓練圖片包括 4 個字符,待后來進行隨機 4~6 個隨機拼接形成類似銀行卡號的長度。
- 訓練集與驗證集的比例是 9:1
1.2 數據增強
- 先對圖像做數據增強,然后再做圖像拼接。
- 隨機縮放、平移、顏色變換、模糊處理、噪聲,最后進行連接,增強倍數 96 倍
- noise: 隨機在圖像中添加 500 個白色點像素;(2次)
- resize: 在一定范圍內非同比例縮放;(4次)
- colormap: 隨機進行顏色變換;(1次)
- blur: 分別有高斯模糊、均值模糊、中值模糊、雙邊濾波;(4次)
- place_img: 上下浮動 10%~20%, 左右浮動 1.5%~2%;(4次)
代碼實現:
import os import numpy as np from os import getcwd import cv2 import PIL.Image import random import copy import multiprocessingPATH = 'train_img/'def rand(a=0, b=1):return np.random.rand() * (b - a) + adef rand_resize(img, img_name, count, annotation, jitter=.3):w, h, _ = img.shapenew_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(1 - jitter, 1 + jitter)scale = rand(.25, 2)if new_ar < 1:nh = int(scale * h)nw = int(nh * new_ar)else:nw = int(scale * w)nh = int(nw / new_ar)new_name = img_name[:6] + str(count) + '.png'count = count + 1annotation[PATH + new_name] = annotation[PATH + img_name]new_img = cv2.resize(img, (nh, nw), cv2.INTER_CUBIC)cv2.imwrite(PATH + new_name, new_img)return countdef place_img(img, img_name, count, annotation):H, W, C = img.shapeoffestH = int(rand(int(H * 0.1), int(H * 0.2)))offestW = int(rand(int(W * 0.015), int(W * 0.02)))dst = np.zeros((H, W, C), np.uint8)dst1 = np.zeros((H, W, C), np.uint8)dst2 = np.zeros((H, W, C), np.uint8)dst3 = np.zeros((H, W, C), np.uint8)for i in range(H - offestH):for j in range(W - offestW):dst[i + offestH, j + offestW] = img[i, j]dst1[i, j] = img[i + offestH, j + offestW]dst2[i + offestH, j] = img[i, j + offestW]dst3[i, j + offestW] = img[i + offestH, j]new_name = img_name[:6] + str(count) + '.png'count += 1new_name1 = img_name[:6] + str(count) + '.png'count += 1new_name2 = img_name[:6] + str(count) + '.png'count += 1new_name3 = img_name[:6] + str(count) + '.png'count += 1cv2.imwrite(PATH + new_name, dst)cv2.imwrite(PATH + new_name1, dst1)cv2.imwrite(PATH + new_name2, dst2)cv2.imwrite(PATH + new_name3, dst3)annotation[PATH + new_name] = annotation[PATH + img_name]annotation[PATH + new_name1] = annotation[PATH + img_name]annotation[PATH + new_name2] = annotation[PATH + img_name]annotation[PATH + new_name3] = annotation[PATH + img_name]return countdef colormap(img, img_name, count, annotation):rand_b = rand() + 1rand_g = rand() + 1rand_r = rand() + 1H, W, C = img.shapenew_name = img_name[:6] + str(count) + '.png'count += 1dst = np.zeros((H, W, C), np.uint8)for i in range(H):for j in range(W):(b, g, r) = img[i, j]b = int(b * rand_b)g = int(g * rand_g)r = int(r * rand_r)if b > 255:b = 255if g > 255:g = 255if r > 255:r = 255dst[i][j] = (b, g, r)annotation[PATH + new_name] = annotation[PATH + img_name]cv2.imwrite(PATH + new_name, dst)return countdef blur(img, img_name, count, annotation):img_GaussianBlur = cv2.GaussianBlur(img, (5, 5), 0)img_Mean = cv2.blur(img, (5, 5))img_Median = cv2.medianBlur(img, 3)img_Bilater = cv2.bilateralFilter(img, 5, 100, 100)new_name = img_name[:6] + str(count) + '.png'count += 1new_name1 = img_name[:6] + str(count) + '.png'count += 1new_name2 = img_name[:6] + str(count) + '.png'count += 1new_name3 = img_name[:6] + str(count) + '.png'count += 1annotation[PATH + new_name] = annotation[PATH + img_name]annotation[PATH + new_name1] = annotation[PATH + img_name]annotation[PATH + new_name2] = annotation[PATH + img_name]annotation[PATH + new_name3] = annotation[PATH + img_name]cv2.imwrite(PATH + new_name, img_GaussianBlur)cv2.imwrite(PATH + new_name1, img_Mean)cv2.imwrite(PATH + new_name2, img_Median)cv2.imwrite(PATH + new_name3, img_Bilater)return countdef noise(img, img_name, count, annotation):H, W, C = img.shapenoise_img = np.zeros((H, W, C), np.uint8)for i in range(H):for j in range(W):noise_img[i, j] = img[i, j]for i in range(500):x = np.random.randint(H)y = np.random.randint(W)noise_img[x, y, :] = 255new_name = img_name[:6] + str(count) + '.png'count += 1annotation[PATH + new_name] = annotation[PATH + img_name]cv2.imwrite(PATH + new_name, noise_img)return countdef concat(img, img_name, count, annotation, img_list):# img = cv2.imread(PATH+img_name)H, W, C = img.shapenum = int(rand(4, 6))imgs = random.sample(img_list, num)dst = np.zeros((H, W * (num + 1), C), np.uint8)for h in range(H):for w in range(W):dst[h, w] = img[h, w]new_name = img_name[:6] + str(count) + '.png'count += 1boxes = copy.copy(annotation[PATH + img_name])for i, image_name in enumerate(imgs):image = cv2.imread(PATH + image_name)for h in range(H):for w in range(W):dst[h, W * (i + 1) + w] = image[h, w]for label in annotation[PATH + image_name]:boxes.append(label)cv2.imwrite(PATH + new_name, dst)annotation[PATH + new_name] = boxescount = noise(dst, new_name, count, annotation) # 1count = noise(dst, new_name, count, annotation)for i in range(4):count = rand_resize(dst, new_name, count, annotation) # 4count = colormap(dst, new_name, count, annotation) # 1count = blur(dst, new_name, count, annotation) # 4count = place_img(dst, new_name, count, annotation) # 4return countdef main(img, img_name, annotation):count = 1for i in range(5):count = concat(img, img_name, count, annotation, img_list) # 1print(img_name + "增強完成")if __name__ == '__main__':trainval_percent = 0.2train_percent = 0.8img_list = os.listdir(PATH)manager = multiprocessing.Manager()annotation = manager.dict()for img_name in img_list:boxes = []a = np.linspace(0, 120, 5, dtype=np.int)for i in range(len(a) - 1):if img_name[i] == '_':continueboxes.append(img_name[i])annotation[PATH + img_name] = boxes''' 多進程處理數據,windows下測試出錯 '''pool = multiprocessing.Pool(10)for img_name in img_list:img = cv2.imread(PATH + img_name, -1)pool.apply_async(main, (img, img_name, annotation,))pool.close()pool.join()''' 非多進程處理數據 '''# for img_name in img_list:# img = cv2.imread(PATH + img_name,-1)# main(img,img_name,annotation)train_file = open('train.txt', 'w')val_file = open('val.txt', 'w')val_split = 0.1rand_index = list(range(len(annotation)))random.shuffle(rand_index)val_index = rand_index[0: int(0.1 * len(annotation))]for i, names in enumerate(annotation.keys()):if i in val_index:val_file.write(names)for label in annotation[names]:val_file.write(" " + str(label))val_file.write('\n')else:train_file.write(names)for label in annotation[names]:train_file.write(" " + str(label))train_file.write('\n')train_file.close()val_file.close()二、訓練(CRNN)
- step 1: 傳入訓練的圖像均為 灰度化圖像,并且 shape: (32,256)
- step 2: 預測的 label 長度:img_w / 4(即每 4 個像素分隔有一個 pred,最后采用 ctc_loss 對齊)
- step 3: 訓練指令如下
三、需要修改的內容
3.1 數據增強
- PATH 的路徑
- train_file 的路徑
- val_file 的路徑
3.2 訓練
- weight_save_path 的路徑
- train_txt_path 的路徑
- val_txt_path 的路徑
四、CRNN 結構說明
- 具體實現細節可參考我另一篇博客:CRNN
- 博客地址:https://blog.csdn.net/libo1004/article/details/111595054
4.1 CNN
CNN: 采用 CNN(類似于 VGG 網絡)進行特征提取,提取到的特征以序列方式輸出。
# Map2Sequence part x = Permute((2, 3, 1), name='permute')(conv_otput) # 64*512*1 rnn_input = TimeDistributed(Flatten(), name='for_flatten_by_time')(x) # 64*5124.2 BiLSTM
BLSTM: 特征輸入到 BLSTM,輸出每個序列代表的值(這個值是一個序列,代表可能出現的值),對輸出進行 softmax 操作,計算每個可能出現值得概率。
y = Bidirectional(LSTM(256, kernel_initializer=initializer, return_sequences=True), merge_mode='sum', name='LSTM_1')(rnn_input) # 64*512 y = BatchNormalization(name='BN_8')(y) y = Bidirectional(LSTM(256, kernel_initializer=initializer, return_sequences=True), name='LSTM_2')(y) # 64*512 y_pred = Dense(num_classes, activation='softmax', name='y_pred')(y)4.3 CTC
CTC: 相當于一個 loss,一個計算概率到實際輸出的概率。
五、卡號檢測
判斷銀行卡(儲蓄卡,信用卡)的卡號的合法性用到了 Luhn 算法
算法流程如下:
因為最終的結果會對10取余來判斷是否能夠整除10,所以又叫做模10算法。
算法代碼:
def luhn_checksum(card_number):def digits_of(n):return [int(d) for d in str(n)]digits = digits_of(card_number)odd_digits = digits[-1::-2]even_digits = digits[-2::-2]checksum = 0checksum += sum(odd_digits)for d in even_digits:return checksum % 10def is_luhn_valid(card_number):return luhn_checksum(card_number) == 0六、BIN碼校驗
銀行卡號一般是13-19位組成,國內一般是 16,19 位,其中 16 位為信用卡,19 位為儲蓄卡,通常情況下都是由 “卡BIN+發卡行自定位+校驗位” 這三部分構成,
銀行卡的前 6 位用來識別發卡銀行或者發卡機構的,稱為發卡行識別碼,簡稱為卡BIN。拿出錢包里的卡,會發現如果是只帶有銀聯標注的卡,十有八九都是以62開頭的,但是也有例外。
這里邊包含一個坑:你知道Bin碼的規則,但是你不知道國內銀行的BIN碼,網上的也大都不全,只能以后慢慢人工擴充。
我這邊整理了一份,但是也不全,大概包含有 1300 個 BIN 號,以后再慢慢整理
只需要將合法的卡號前6位切片出來進行查詢就好了。
參考鏈接
總結
以上是生活随笔為你收集整理的『工程项目实践』银行卡识别(CTPN+CRNN)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jgp图片不支持透明, 需要保存为 pn
- 下一篇: 2021-05-26wms系统的出库单价