CV2识别图片A,B,C,D
程序员文章站
2023-11-06 23:11:46
这个模型是经过简单训练,只能识别四个英文字母:ABCD,是我为了提取答案才训练的,因为科大,百度,以及腾讯的识别不是很准,所以自己训练自己使用,希望能够给你带来方便。下面是训练好的model,如多你感觉图片的类型相似,可直接使用。 1、图片形式,尽量png 2、模型 如果需要可以加我qq:18519 ......
这个模型是经过简单训练,只能识别四个英文字母: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')