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.

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #coding=utf-8
  2. from keras import backend as K
  3. from keras.models import load_model
  4. from keras.layers import *
  5. import numpy as np
  6. import random
  7. import string
  8. import cv2
  9. import e2emodel as model
  10. chars = [u"京", u"沪", u"津", u"渝", u"冀", u"晋", u"蒙", u"辽", u"吉", u"黑", u"苏", u"浙", u"皖", u"闽", u"赣", u"鲁", u"豫", u"鄂", u"湘", u"粤", u"桂",
  11. u"琼", u"川", u"贵", u"云", u"藏", u"陕", u"甘", u"青", u"宁", u"新", u"0", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"A",
  12. u"B", u"C", u"D", u"E", u"F", u"G", u"H", u"J", u"K", u"L", u"M", u"N", u"P", u"Q", u"R", u"S", u"T", u"U", u"V", u"W", u"X",
  13. u"Y", u"Z",u"港",u"学",u"使",u"警",u"澳",u"挂",u"军",u"北",u"南",u"广",u"沈",u"兰",u"成",u"济",u"海",u"民",u"航",u"空"
  14. ];
  15. pred_model = model.construct_model("./model/ocr_plate_all_w_rnn_2.h5",)
  16. import time
  17. def fastdecode(y_pred):
  18. results = ""
  19. confidence = 0.0
  20. table_pred = y_pred.reshape(-1, len(chars)+1)
  21. res = table_pred.argmax(axis=1)
  22. for i,one in enumerate(res):
  23. if one<len(chars) and (i==0 or (one!=res[i-1])):
  24. results+= chars[one]
  25. confidence+=table_pred[i][one]
  26. confidence/= len(results)
  27. return results,confidence
  28. def recognizeOne(src):
  29. # x_tempx= cv2.imread(src)
  30. x_tempx = src
  31. # x_tempx = cv2.bitwise_not(x_tempx)
  32. x_temp = cv2.resize(x_tempx,( 160,40))
  33. x_temp = x_temp.transpose(1, 0, 2)
  34. t0 = time.time()
  35. y_pred = pred_model.predict(np.array([x_temp]))
  36. y_pred = y_pred[:,2:,:]
  37. # plt.imshow(y_pred.reshape(16,66))
  38. # plt.show()
  39. #
  40. # cv2.imshow("x_temp",x_tempx)
  41. # cv2.waitKey(0)
  42. return fastdecode(y_pred)
  43. #
  44. #
  45. # import os
  46. #
  47. # path = "/Users/yujinke/PycharmProjects/HyperLPR_Python_web/cache/finemapping"
  48. # for filename in os.listdir(path):
  49. # if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".bmp"):
  50. # x = os.path.join(path,filename)
  51. # recognizeOne(x)
  52. # # print time.time() - t0
  53. #
  54. # # cv2.imshow("x",x)
  55. # # cv2.waitKey()