You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

recognizer.py 5.4 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #coding=utf-8
  2. from keras.models import Sequential
  3. from keras.layers import Dense, Dropout, Activation, Flatten
  4. from keras.layers import Convolution2D, MaxPooling2D
  5. from keras.optimizers import SGD
  6. from keras import backend as K
  7. K.set_image_dim_ordering('tf')
  8. import cv2
  9. import numpy as np
  10. index = {u"京": 0, u"沪": 1, u"津": 2, u"渝": 3, u"冀": 4, u"晋": 5, u"蒙": 6, u"辽": 7, u"吉": 8, u"黑": 9, u"苏": 10, u"浙": 11, u"皖": 12,
  11. u"闽": 13, u"赣": 14, u"鲁": 15, u"豫": 16, u"鄂": 17, u"湘": 18, u"粤": 19, u"桂": 20, u"琼": 21, u"川": 22, u"贵": 23, u"云": 24,
  12. u"藏": 25, u"陕": 26, u"甘": 27, u"青": 28, u"宁": 29, u"新": 30, u"0": 31, u"1": 32, u"2": 33, u"3": 34, u"4": 35, u"5": 36,
  13. u"6": 37, u"7": 38, u"8": 39, u"9": 40, u"A": 41, u"B": 42, u"C": 43, u"D": 44, u"E": 45, u"F": 46, u"G": 47, u"H": 48,
  14. u"J": 49, u"K": 50, u"L": 51, u"M": 52, u"N": 53, u"P": 54, u"Q": 55, u"R": 56, u"S": 57, u"T": 58, u"U": 59, u"V": 60,
  15. u"W": 61, u"X": 62, u"Y": 63, u"Z": 64,u"港":65,u"学":66 ,u"O":67 ,u"使":68,u"警":69,u"澳":70,u"挂":71};
  16. chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
  17. "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
  18. "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P",
  19. "Q", "R", "S", "T", "U", "V", "W", "X",
  20. "Y", "Z","港","学","O","使","警","澳","挂" ];
  21. def Getmodel_tensorflow(nb_classes):
  22. # nb_classes = len(charset)
  23. img_rows, img_cols = 23, 23
  24. # number of convolutional filters to use
  25. nb_filters = 32
  26. # size of pooling area for max pooling
  27. nb_pool = 2
  28. # convolution kernel size
  29. nb_conv = 3
  30. # x = np.load('x.npy')
  31. # y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
  32. # weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
  33. # weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
  34. model = Sequential()
  35. model.add(Convolution2D(32, 5, 5,
  36. border_mode='valid',
  37. input_shape=(img_rows, img_cols,1)))
  38. model.add(Activation('relu'))
  39. model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  40. model.add(Dropout(0.25))
  41. model.add(Convolution2D(32, 3, 3))
  42. model.add(Activation('relu'))
  43. model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  44. model.add(Dropout(0.25))
  45. model.add(Convolution2D(512, 3, 3))
  46. # model.add(Activation('relu'))
  47. # model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  48. # model.add(Dropout(0.25))
  49. model.add(Flatten())
  50. model.add(Dense(512))
  51. model.add(Activation('relu'))
  52. model.add(Dropout(0.5))
  53. model.add(Dense(nb_classes))
  54. model.add(Activation('softmax'))
  55. model.compile(loss='categorical_crossentropy',
  56. optimizer='adam',
  57. metrics=['accuracy'])
  58. return model
  59. def Getmodel_ch(nb_classes):
  60. # nb_classes = len(charset)
  61. img_rows, img_cols = 23, 23
  62. # number of convolutional filters to use
  63. nb_filters = 32
  64. # size of pooling area for max pooling
  65. nb_pool = 2
  66. # convolution kernel size
  67. nb_conv = 3
  68. # x = np.load('x.npy')
  69. # y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
  70. # weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
  71. # weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
  72. model = Sequential()
  73. model.add(Convolution2D(32, 5, 5,
  74. border_mode='valid',
  75. input_shape=(img_rows, img_cols,1)))
  76. model.add(Activation('relu'))
  77. model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  78. model.add(Dropout(0.25))
  79. model.add(Convolution2D(32, 3, 3))
  80. model.add(Activation('relu'))
  81. model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  82. model.add(Dropout(0.25))
  83. model.add(Convolution2D(512, 3, 3))
  84. # model.add(Activation('relu'))
  85. # model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
  86. # model.add(Dropout(0.25))
  87. model.add(Flatten())
  88. model.add(Dense(756))
  89. model.add(Activation('relu'))
  90. model.add(Dropout(0.5))
  91. model.add(Dense(nb_classes))
  92. model.add(Activation('softmax'))
  93. model.compile(loss='categorical_crossentropy',
  94. optimizer='adam',
  95. metrics=['accuracy'])
  96. return model
  97. model = Getmodel_tensorflow(65)
  98. #构建网络
  99. model_ch = Getmodel_ch(31)
  100. model_ch.load_weights("./model/char_chi_sim.h5")
  101. model.load_weights("./model/char_rec.h5")
  102. def SimplePredict(image,pos):
  103. image = cv2.resize(image, (23, 23))
  104. image = cv2.equalizeHist(image)
  105. image = image.astype(np.float) / 255
  106. image -= image.mean()
  107. image = np.expand_dims(image, 3)
  108. if pos!=0:
  109. res = np.array(model.predict(np.array([image]))[0])
  110. else:
  111. res = np.array(model_ch.predict(np.array([image]))[0])
  112. zero_add = 0 ;
  113. if pos==0:
  114. res = res[:31]
  115. elif pos==1:
  116. res = res[31+10:65]
  117. zero_add = 31+10
  118. else:
  119. res = res[31:]
  120. zero_add = 31
  121. max_id = res.argmax()
  122. return res.max(),chars[max_id+zero_add],max_id+zero_add

高性能开源中文车牌识别框架