| 21 | |
| 22 | |
| 23 | class CNNPredictor(object): |
| 24 | __model = None |
| 25 | |
| 26 | # ensure predictor be initialized only once by using Singleton Pattern |
| 27 | def __new__(cls, *args, **kwargs): |
| 28 | if not hasattr(cls, '__instance'): |
| 29 | cls.__instance = super().__new__(cls) |
| 30 | cls.__model = load_model('app/models/convolutional.h5') |
| 31 | return cls.__instance |
| 32 | |
| 33 | @staticmethod |
| 34 | def predict(input_data): |
| 35 | assert CNNPredictor.__model, "Use 'CNNPredictor()' to initialize before using 'CNNPredictor.predict()'" |
| 36 | |
| 37 | x = input_data.reshape(1, 28, 28, 1) |
| 38 | return CNNPredictor.__model.predict(x).flatten().tolist() |