inference_char.py 932 B

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import cv2
  3. import numpy as np
  4. from click_captcha.model import mobile_net, cnn_net
  5. from click_captcha.utils import pil_resize
  6. image_shape = (40, 40, 3)
  7. weights_path = "./models/char_f1_0.93.h5"
  8. project_dir = os.path.dirname(os.path.abspath(__file__)) + "/../"
  9. def recognize(image_path):
  10. model = cnn_net(input_shape=image_shape)
  11. model.load_weights(weights_path)
  12. img = cv2.imread(image_path)
  13. img = pil_resize(img, image_shape[0], image_shape[1])
  14. cv2.imshow("img", img)
  15. cv2.waitKey(0)
  16. img = img / 255.
  17. X = np.expand_dims(img, 0)
  18. pred = model.predict(X)
  19. index = int(np.argmax(pred))
  20. with open(project_dir + "data/chinese_5710.txt") as f:
  21. char_str = f.read()
  22. char = char_str[index]
  23. print("recognize chinese", char)
  24. return char
  25. if __name__ == "__main__":
  26. _path = "../data/test/char_6.jpg"
  27. # _path = "../data/click/2019_73_1.jpg"
  28. recognize(_path)