CV2识别图片A,B,C,D

这个模型是经过简单训练,只能识别四个英文字母:ABCD,是我为了提取答案才训练的,因为科大,百度,以及腾讯的识别不是很准,所以自己训练自己使用,希望能够给你带来方便。下面是训练好的model,如多你感觉图片的类型相似,可直接使用。

1、图片形式,尽量png

2、模型

如果需要可以加我qq:1851999452

 

3、

  1 import os
  2 import numpy as np
  4 import cv2
  5 import matplotlib.pyplot as plt
  6 import tensorflow as tf
  7 import keras
  8 from keras.models import Model,load_model
  9 from keras.layers import Conv2D,Dense,Activation,Input,MaxPooling2D,Flatten
 10 from sklearn.model_selection import train_test_split
 11 # file_dir = '/home/youxue/BorD'
 12 # B = []
 13 # A =[]
 14 # C =[]
 15 # label_A=[]
 16 # label_C=[]
 17 # label_B = []
 18 # D = []
 19 # label_D = []
 20 # for file in os.listdir(file_dir):
 21 #     if file == 'A':
 22 #         for img in os.listdir(os.path.join(file_dir, file)):
 23 #             A.append(file_dir + '/' + file + '/' + img)
 24 #             label_A.append(0)
 25 #     elif file == 'B':
 26 #         for img in os.listdir(os.path.join(file_dir, file)):
 27 #             B.append(file_dir + '/' + file + '/' + img)
 28 #             label_B.append(1)
 29 #     elif file == 'C':
 30 #         for img in os.listdir(os.path.join(file_dir, file)):
 31 #             C.append(file_dir + '/' + file + '/' + img)
 32 #             label_C.append(2)
 33 #     else:
 34 #         for img in os.listdir(os.path.join(file_dir, file)):
 35 #             D.append(file_dir + '/' + file + '/' + img)
 36 #             label_D.append(3)
 37 #
 38 # print('There are %d B\n There are %d D' % (len(B), len(D)))
 39 #
 40 # image_list = np.hstack((A,B,C,D))
 41 # label_list = np.hstack((label_A,label_B,label_C, label_D))
 42 # temp = np.array([image_list, label_list])
 43 # temp = temp.transpose()
 44 #
 45 #
 46 # np.random.shuffle(temp)
 47 # png_list=[]
 48 # image_list = list(temp[:, 0])
 49 # label_list = list(temp[:, 1])
 50 # label_list = [int(i) for i in label_list]
 51 # for png in image_list:
 52 #     png_list.append(cv2.imread(png))
 53 # print(png_list[100].shape)
 54 # png = np.array(png_list)
 55 # label = np.array(label_list)
 56 # x_train,x_test,y_train,y_test=train_test_split(png,label,test_size=0.1)
 57 #
 58 # # plt.imshow(x_train[11])
 59 # # plt.show()
 60 #
 61 # x_train = x_train/255.
 62 # x_test = x_test/255.
 63 #
 64 # y_train = np.eye(4)[y_train.reshape(-1)]
 65 # y_test = np.eye(4)[y_test.reshape(-1)]
 66 # print(x_train.shape,y_train.shape)
 67 
 68 # input_shape= (24,810,3)
 69 # input_data = Input(input_shape)
 70 # layer1 = Conv2D(32,(3,3),padding='same',activation='relu')(input_data)
 71 # layer2 = MaxPooling2D(pool_size=(2,2))(layer1)
 72 # layer3 = Conv2D(32,(3,3),padding='same',activation='relu')(layer2)
 73 # layer4 = MaxPooling2D(pool_size=(2,2))(layer3)
 74 # layer5 = Conv2D(32,(3,3),padding='same',activation='relu')(layer4)
 75 # layer6 = Flatten()(layer5)
 76 # layer7 = Dense(4)(layer6)
 77 # pred= Activation('softmax')(layer7)
 78 # model= Model(input=input_data,output=pred)
 79 # model.summary()
 80 # model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
 81 # model.fit(x_train,y_train,batch_size=32,epochs=20,validation_data=(x_test,y_test))
 82 # model.save('ABCD.model')
 83 model=load_model('ABCD.model')
 84 #这里填写自己的存放路径
 85 with open('id.txt','a+') as f:
 86     #这里填写你需要识别图片的路径
 87     path=''
 88     for png in os.listdir(path):
 89         pn= cv2.imread(os.path.join(path,png))
 90         ids_1 = png.split('answer')[0]
 91         print(type(ids_1))
 92     #png2= cv2.imread('/home/youxue/4907988answer.png')
 93         # plt.imshow(pn)
 94         # plt.show()
 95         img=[]
 96         img.append(pn)
 97     #img.append(png2)
 98         img=np.array(img)
 99         img = img/255.
100 
101     # print(img.shape)
102         pre=model.predict(img)
103         for c in pre:
104             temp= max(c[0],c[1],c[2],c[3])
105             if temp == c[0]:
106                 ABCD = 'A'
107             elif temp == c[1]:
108                 ABCD = 'B'
109             elif temp == c[2]:
110                 ABCD = 'C'
111             else:
112                 ABCD = 'D'
113        
114         f.write(ids_1 + '--' + ABCD + '\n')

 

你可能感兴趣的