inference_yolo_puzzle.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import os
  2. import cv2
  3. from PIL import Image
  4. from keras.layers import Lambda, Input
  5. from keras.models import Model
  6. import numpy as np
  7. from click_captcha.model import tiny_yolo_body
  8. from click_captcha.post_process import yolo_eval, letterbox_image
  9. from click_captcha.utils import get_classes, get_colors, draw_boxes, get_anchors, pil_resize, np2pil
  10. def detect_puzzle(image_path, model):
  11. image_np = cv2.imread(image_path)
  12. # create image input
  13. h, w = image_np.shape[:2]
  14. best_h, best_w = 160, 256
  15. image_resize = pil_resize(image_np, best_h, best_w)
  16. image_pil = np2pil(image_resize)
  17. image_resize = cv2.cvtColor(image_resize, cv2.COLOR_BGR2GRAY)
  18. # cv2.imshow("image_resize1", image_resize)
  19. image_resize = 255. - image_resize
  20. image_resize = np.uint8(image_resize)
  21. # cv2.imshow("image_resize2", image_resize)
  22. image_resize = image_resize / 255.
  23. image_resize = np.expand_dims(image_resize, 0)
  24. image_resize = np.expand_dims(image_resize, -1)
  25. print("image_resize.shape", image_resize.shape)
  26. # create image shape input
  27. image_shape = np.array([best_h, best_w])
  28. image_shape = np.expand_dims(image_shape, 0)
  29. # inference data
  30. out_boxes, out_scores, out_classes = model.predict([image_resize, image_shape])
  31. print("image_size", image_shape)
  32. print("out_boxes", out_boxes)
  33. print("out_scores", out_scores)
  34. print("out_classes", out_classes)
  35. out_boxes = out_boxes.astype(np.int32)
  36. out_classes = out_classes.astype(np.int32)
  37. # draw
  38. # image_np = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
  39. class_names = get_classes("yolo_data/my_classes_puzzle.txt")
  40. colors = get_colors(len(class_names))
  41. image_resize, out_boxes = draw_boxes(image_pil, out_boxes, out_classes, out_scores, class_names, colors)
  42. image_np_result = cv2.cvtColor(np.array(image_resize), cv2.COLOR_RGB2BGR)
  43. cv2.imshow("result", image_np_result)
  44. cv2.waitKey(0)
  45. return image_np, out_boxes, out_classes
  46. def get_tiny_inference_model(anchors, num_classes, weights_path='models/tiny_yolo_weights.h5'):
  47. """create the inference model, for Tiny YOLOv3"""
  48. image_input = Input(shape=(None, None, 1))
  49. image_shape = Input(shape=(2,), dtype='int64', name='image_shape')
  50. num_anchors = len(anchors)
  51. model_body = tiny_yolo_body(image_input, num_anchors//2, num_classes)
  52. print('Create Tiny YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
  53. model_body.load_weights(weights_path)
  54. print('Load weights {}.'.format(weights_path))
  55. boxes, scores, classes = Lambda(yolo_eval,
  56. name='yolo_eval',
  57. arguments={'anchors': anchors,
  58. 'num_classes': num_classes}
  59. )([model_body.output, image_shape])
  60. # boxes, scores, classes = yolo_eval([model_body.output, image_shape], anchors, num_classes)
  61. model = Model([model_body.input, image_shape], [boxes, scores, classes])
  62. model.summary(120)
  63. return model
  64. if __name__ == '__main__':
  65. _dir = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
  66. model_path = _dir + "/models/puzzle_yolo_loss_4.15.h5"
  67. anchors = get_anchors(_dir + "/yolo_data/my_anchors_puzzle.txt")
  68. class_names = get_classes(_dir + "/yolo_data/my_classes_puzzle.txt")
  69. colors = get_colors(len(class_names))
  70. yolo_model = get_tiny_inference_model(anchors, len(class_names), weights_path=model_path)
  71. yolo_model.load_weights(model_path)
  72. image_path = "../data/test/yolo_15.jpg"
  73. # image_path = "../data/detect2/23552.jpg"
  74. detect_puzzle(image_path, yolo_model)