refactoring
This commit is contained in:
@@ -7,6 +7,7 @@ from captcha.DatasetFactory import DatasetFactory
|
||||
import numpy as np
|
||||
from tensorflow import keras
|
||||
|
||||
|
||||
class CaptchaReader:
|
||||
|
||||
def __init__(self, modelFilepath, captchaShape):
|
||||
@@ -14,19 +15,17 @@ class CaptchaReader:
|
||||
self.captchaShape = captchaShape
|
||||
|
||||
def getTextInCaptchaImage(self, captchaImageFile):
|
||||
# FK-TODO: refactor
|
||||
modelDAO = ModelDAO(inColab = False)
|
||||
model = modelDAO.loadModel(self.modelFilepath)
|
||||
prediction_model = ModelFactory.createPredictionModel(model)
|
||||
charNumConverter = CharNumConverter(CaptchaGenerator.characters)
|
||||
datasetFactory = DatasetFactory(self.captchaShape,charNumConverter.char_to_num, batch_size = 64)
|
||||
batchImages = self._asSingleSampleBatch(datasetFactory._encode_single_sample(captchaImageFile, 'dummy')['image'])
|
||||
preds = prediction_model.predict(batchImages)
|
||||
predictionsDecoder = PredictionsDecoder(CaptchaGenerator.captchaLength, charNumConverter.num_to_char)
|
||||
pred_texts = predictionsDecoder.decode_batch_predictions(preds)
|
||||
return pred_texts[0]
|
||||
return self._getTextsInCaptchaImage(self._getCaptchaImage(captchaImageFile))[0]
|
||||
|
||||
def _getCaptchaImage(self, captchaImageFile):
|
||||
return self._asSingleSampleBatch(DatasetFactory.encodeImage(captchaImageFile, self.captchaShape))
|
||||
|
||||
def _asSingleSampleBatch(self, img):
|
||||
array = keras.utils.img_to_array(img)
|
||||
array = np.expand_dims(array, axis=0)
|
||||
return array
|
||||
return np.expand_dims(keras.utils.img_to_array(img), axis=0)
|
||||
|
||||
def _getTextsInCaptchaImage(self, captchaImage):
|
||||
preds = self._createPredictionModel().predict(captchaImage)
|
||||
return PredictionsDecoder(CaptchaGenerator.captchaLength, CharNumConverter(CaptchaGenerator.characters).num_to_char).decode_batch_predictions(preds)
|
||||
|
||||
def _createPredictionModel(self):
|
||||
return ModelFactory.createPredictionModel(ModelDAO(inColab=False).loadModel(self.modelFilepath))
|
||||
|
||||
Reference in New Issue
Block a user