Smart bank card number recognition system based on opencv+tensorflow+neural network - deep learning algorithm application (including python, model source code) + data set (2)


Insert image description here

Preface

This project is based on various bank card data sets obtained from the Internet, uses the functions of the OpenCV library for image processing, and conducts model training through neural networks. Finally, intelligent recognition and output of conventional bank card numbers are realized.

First, a diverse bank card dataset containing various types and designs of bank card images was obtained via the web. These image data will be used as training sets and test sets for training intelligent recognition models.

Secondly, using the functions of the OpenCV library, the project processes bank card images. Including image enhancement, edge detection, text positioning and other technologies to optimize images and improve the accuracy of card number extraction.

Next, model training is performed through the neural network. The neural network uses the deep learning framework TensorFlow to learn a large number of bank card images, enabling the model to understand and accurately identify the patterns and characteristics of different bank card numbers.

Finally, the trained neural network model can intelligently recognize and output regular bank card numbers. This makes the process of extracting card numbers from images more automated and efficient.

Overall, this project integrates data collection, image processing and deep learning technology to provide an advanced solution for the intelligent identification of bank card numbers. This has potential practical applications for automated bank card information extraction scenarios, such as financial services or identity verification systems.

overall design

This part includes the overall system structure diagram and system flow chart.

Overall system structure diagram

The overall structure of the system is shown in the figure.

Insert image description here

System flow chart

The system flow is shown in the figure.

Insert image description here

Operating environment

This part includes the Python environment, TensorFlow environment and OpenCV environment.

详见行客.

Module implementation

This project includes 4 modules: training set image processing, test image processing, model training and saving, and model testing. The functions and related codes of each module are introduced below.

1. Training set image processing

The data set download URL ishttp://www.cnsoftbei.com/plus/view.php?aid=348. The training set is 120 pictures of different sizes, composed of 4 bank card character units, selected from 1,000 manually processed bank card number screenshots. These data sets have been packaged and stored in a folder named images, as shown in the figure.

Insert image description here

1) Data loading

The relevant code is as follows:

def img_handle():
           for root, dirs, files in os.walk('./images'):
                for file in files:
                     img_path = root+'/'+file
                     img = cv2.imread(img_path, 0)
                     get_img(img, file)
          return data, label

2) Image processing

Read the picture as a grayscale image, perform Gaussian blur and binary processing on it. Draw a vertical projection histogram and scan column by column from left to right. The number of black pixels in this column will be the height of the corresponding histogram column. Scan the histogram from left to right by column. Mark the column whose histogram height is "from nothing to some" or "from something to nothing". The characters will be segmented based on these marks, and the individual characters will be assigned to each other. One to 16×16. After segmenting the characters, due to the small amount of data, the training set is enhanced by shifting and adding noise processing. The relevant code is as follows:

def get_img(img, file):
  blur = cv2.GaussianBlur(img, (3, 3), 0)  #高斯模糊
  ret, img = cv2.threshold(blur, 60, 255, cv2.THRESH_BINARY)  #二值化
    height, width = img.shape
    v = [0] * width
    z = [0] * height
    a = 0
    #垂直投影:统计并存储每一列的黑点数
    for x in range(0, width):
        for y in range(0, height):
            if img[y, x] == 255:
                continue
            else:
                a = a + 1
        v[x] = a
        a = 0
    #创建空白图片,绘制垂直投影图
    l = len(v)
    emptyImage = np.full((height, width), 255, dtype=np.uint8)
    for x in range(0, width):
        for y in range(0, v[x]):
            emptyImage[y, x] = 0
    #分割字符
    Position = []
    Wstart = 0
    Wend = 0
    W_Start = 0
    W_End = 0
    v[0], v[len(v) - 1] = 0, 0
    for j in range(len(v)):
        if v[j] > 0 and Wstart == 0:
            W_Start = j
            Wstart = 1
            Wend = 0
        if v[j] <= 0 and Wstart == 1:
            W_End = j
            Wstart = 0
            Wend = 1
        if Wend == 1:
            Position.append([W_Start, 0, W_End, height])
            Wend = 0
    i = 0
    for m in range(len(Position)):
        if Position[m][3]/(Position[m][2]-Position[m][0]) > 1 and Position[m][3]/(Position[m][2]-Position[m][0]) < 5:
            temp_img = img[Position[m][1]:Position[m][3], Position[m][0]:Position[m][2]]
            temp_img = cv2.resize(temp_img, (16, 16))       #大小归一化
#数据增强
            blur1 = cv2.GaussianBlur(temp_img, (1, 1), 0)  #高斯模糊
            blur2 = cv2.GaussianBlur(temp_img, (3, 3), 0)  #高斯模糊
            noise = sp_noise(temp_img, 0.01)                  #噪声
            h0, w0 = temp_img.shape
            temp_label = [0.0] * 10
            temp_data = []
            for hx in range(h0):
                for wx in range(w0):
                    temp_data.append(float(temp_img[hx, wx]))
            data.append(temp_data)
            temp_data = []
            for hx in range(h0):
                for wx in range(w0):
                    temp_data.append(float(blur1[hx, wx]))
            data.append(temp_data)
            temp_data = []
            for hx in range(h0):
                for wx in range(w0):
                    temp_data.append(float(blur2[hx, wx]))
            data.append(temp_data)
            temp_data = []
            for hx in range(h0):
                for wx in range(w0):
                    temp_data.append(float(noise[hx, wx]))
            data.append(temp_data)
            temp_data = []                                      #左移
            for hx in range(h0):
                for wx in range(w0):
                    if wx < w0-1:
                        temp_data.append(float(temp_img[hx, wx+1]))
                    else:
                        temp_data.append(255.0)
            data.append(temp_data)
            temp_data = []                                      #右移
            for hx in range(h0):
                for wx in range(w0):
                    if wx > 0:
                        temp_data.append(float(temp_img[hx, wx - 1]))
                    else:
                        temp_data.append(255.0)
            data.append(temp_data)
            temp_data = []                                      #上移
            for hx in range(h0):
                if hx < h0-1:
                    for wx in range(w0):
                        temp_data.append(float(temp_img[hx+1, wx]))
                else:
                    for wx in range(w0):
                        temp_data.append(255.0)
            data.append(temp_data)
            temp_data = []                                      #下移
            for hx in range(h0):
                if hx > 0:
                    for wx in range(w0):
                        temp_data.append(float(temp_img[hx-1, wx]))
                else:
                    for wx in range(w0):
                        temp_data.append(255.0)
            data.append(temp_data)
            temp_label[int(file[i])] = 1.0
            for j in range(8):
                label.append(temp_label)
            i += 1
#添加噪声
def sp_noise(image,prob):
    output = np.zeros(image.shape,np.uint8)
    thres = 1 - prob
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            rdn = random.random()
            if rdn < prob:
                output[i][j] = 0
            elif rdn > thres:
                output[i][j] = 255
            else:
                output[i][j] = image[i][j]
    return output

2. Test image processing

1) Image reading

The test images taken have been packaged and placed in the test_images folder. The loading code is as follows:

def image_process(file_path):
file_path = 'test_images/x.jpg'
#其中x.jpg为test_images文件夹中的任意图片

2) Image processing

Similar to the training set preprocessing, the image is directly read in as a grayscale image, and Gaussian blur, binarization, expansion corrosion processing and character segmentation are performed on it. Since the bank card image not only contains the card number, but also other graphics or characters, the card number needs to be positioned. In addition, no data augmentation processing is required here. The relevant code is as follows:

#图像处理
def image_process(file_path):
      img = cv2.imread(file_path, 0)
      blur = cv2.GaussianBlur(img, (3, 3), 0)     #高斯模糊
      ret, binary = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY) #二值化
      kernel = np.ones((1, 50), np.uint8)
      erosion = cv2.erode(binary, kernel)         #膨胀
      dilation = cv2.dilate(erosion, kernel)      #腐蚀
#通过边缘检测得到多条色块,再根据银行卡号分布的特征设定阈值,判定银行卡号的区域
      contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     sp = dilation.shape
     x, y, w, h = 0, 0, 0, 0
     for i in range(0, len(contours)):      
#矩形边框,返回值x、y是矩阵左上点的坐标,w、h是矩阵的宽和高
         x, y, w, h = cv2.boundingRect(contours[i])
         #计算矩形是否圈住银行卡号
         if h > sp[0]*0.05 and w > sp[1]*0.5 and y > sp[0]*0.2 and y < sp[0]*0.8 and w/h > 5:
             img = binary[y:y + h, x:x + w]
             break
     return num_split(img)
#得到卡号区域后,绘制垂直投影直方图,字符分割,再将单个字符归一化为16*16大小的图像
#图像分割
def num_split(img):
    height, width = img.shape
    v = [0] * width
    z = [0] * height
    a = 0 
    #垂直投影:统计并存储每一列的黑点数
    #逐行存储
    for x in range(0, width):
        for y in range(0, height):
            if img[y, x] == 255:
                continue
            else:
                a = a + 1
        v[x] = a
        a = 0
    #创建空白图片,绘制垂直投影图
    l = len(v)
    emptyImage = np.full((height, width), 255, dtype=np.uint8)
    for x in range(0, width):
        for y in range(0, v[x]):
            emptyImage[y, x] = 0
    #分割字符
    #将字符坐标及宽度存放到Position
    Position = []
    Wstart = 0
    Wend = 0
    W_Start = 0
    W_End = 0
    v[0], v[len(v)-1] = 0, 0
    for j in range(len(v)):
        if v[j] > 0 and Wstart == 0:
            W_Start = j
            Wstart = 1
            Wend = 0
        if v[j] <= 0 and Wstart == 1:
            W_End = j
            Wstart = 0
            Wend = 1
        if Wend == 1:
            Position.append([W_Start, 0, W_End, height])
            Wend = 0
    data = []
    #对单个字符处理 
    for m in range(len(Position)):
        temp_img = img[Position[m][1]:Position[m][3], Position[m][0]:Position[m][2]]
        #获得单个字符的宽度和高度
        h1, w1 = temp_img.shape
        if w1 > h1:
            return []
        temp_img = cv2.resize(temp_img, (16, 16))
        h0, w0 = temp_img.shape
        temp_data = []
        for hx in range(h0):
            for wx in range(w0):
                temp_data.append(float(temp_img[hx, wx]))
        data.append(temp_data)
    return data

Related other blogs

Smart bank card number identification system based on opencv+tensorflow+neural network - deep learning algorithm application (including python, model source code) + data set (1)

Smart bank card number identification system based on opencv+tensorflow+neural network - deep learning algorithm application (including python, model source code) + data set (3)

Project source code download

For details, please see my blog resource download page


Download other information

If you want to continue to understand the learning routes and knowledge systems related to artificial intelligence, you are welcome to read my other blog " Heavy | Complete Artificial Intelligence AI Learning - Basics Knowledge learning route, all materials can be downloaded directly from the network disk without following any routines
This blog refers to Github’s well-known open source platform, AI technology platform and experts in related fields : Datawhale, ApacheCN, AI Youdao and Dr. Huang Haiguang have about 100G of related information. I hope it can help all my friends.

Guess you like

Origin blog.csdn.net/qq_31136513/article/details/134391948