inference_equation_denoise.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import re
  3. from glob import glob
  4. import cv2
  5. import numpy as np
  6. from click_captcha.model import u_net_denoise
  7. from click_captcha.pre_process import add_contrast
  8. from click_captcha.utils import pil_resize
  9. image_shape = (32, 192, 1)
  10. weights_path = "./models/e153-loss53.97-denoise.h5"
  11. project_dir = os.path.dirname(os.path.abspath(__file__)) + "/../"
  12. model = u_net_denoise(input_shape=image_shape, class_num=image_shape[2])
  13. model.load_weights(weights_path)
  14. def denoise(image_np):
  15. X = []
  16. # img = cv2.imread(image_path)
  17. img = pil_resize(image_np, image_shape[0], image_shape[1])
  18. # cv2.imshow("img", img)
  19. # img = add_contrast(img)
  20. # cv2.imshow("contrast", img)
  21. # cv2.waitKey(0)
  22. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  23. img = np.expand_dims(img, axis=-1)
  24. img = img / 255.
  25. X.append(img)
  26. X = np.array(X)
  27. pred = model.predict(X)
  28. # print(pred.shape)
  29. pred = np.uint8(pred[0]*255.)
  30. # pred = cv2.adaptiveThreshold(pred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,
  31. # 11, 20)
  32. #
  33. # cv2.imshow("pred", pred)
  34. # cv2.waitKey(0)
  35. return pred
  36. if __name__ == "__main__":
  37. # _path = "../data/test/char_9.jpg"
  38. _paths = glob("../data/equation/*")
  39. # _paths = glob("../data/test/FileInfo1021/*")
  40. for _path in _paths:
  41. denoise(cv2.imread(_path))