refactoring

This commit is contained in:
frankknoll
2022-06-18 17:39:39 +02:00
parent bc0a66d576
commit aadc5a488a

View File

@@ -254,7 +254,6 @@
" vocabulary=char_to_num.get_vocabulary(),\n", " vocabulary=char_to_num.get_vocabulary(),\n",
" mask_token=None, invert=True)\n", " mask_token=None, invert=True)\n",
"\n", "\n",
"\n",
"def encode_single_sample(img_path):\n", "def encode_single_sample(img_path):\n",
" # 1. Read image\n", " # 1. Read image\n",
" img = tf.io.read_file(img_path)\n", " img = tf.io.read_file(img_path)\n",
@@ -268,11 +267,13 @@
" # dimension to correspond to the width of the image.\n", " # dimension to correspond to the width of the image.\n",
" img = tf.transpose(img, perm=[1, 0, 2])\n", " img = tf.transpose(img, perm=[1, 0, 2])\n",
" # 7. Return a dict as our model is expecting two inputs\n", " # 7. Return a dict as our model is expecting two inputs\n",
" return asSingleSampleBatch(img)\n",
"\n",
"def asSingleSampleBatch(img):\n",
" array = keras.utils.img_to_array(img)\n", " array = keras.utils.img_to_array(img)\n",
" array = np.expand_dims(array, axis=0)\n", " array = np.expand_dims(array, axis=0)\n",
" return array\n", " return array\n",
"\n", "\n",
"\n",
"def decode_batch_predictions(pred):\n", "def decode_batch_predictions(pred):\n",
" input_len = np.ones(pred.shape[0]) * pred.shape[1]\n", " input_len = np.ones(pred.shape[0]) * pred.shape[1]\n",
" # Use greedy search. For complex tasks, you can use beam search\n", " # Use greedy search. For complex tasks, you can use beam search\n",
@@ -284,7 +285,6 @@
" output_text.append(res)\n", " output_text.append(res)\n",
" return output_text\n", " return output_text\n",
"\n", "\n",
"\n",
"def load_model():\n", "def load_model():\n",
" _model = keras.models.load_model('model')\n", " _model = keras.models.load_model('model')\n",
" model = keras.models.Model(\n", " model = keras.models.Model(\n",
@@ -292,13 +292,11 @@
" _model.get_layer(name=\"dense2\").output)\n", " _model.get_layer(name=\"dense2\").output)\n",
" return model\n", " return model\n",
"\n", "\n",
"\n",
"def getTextInCaptchaImage(captchaImageFile):\n", "def getTextInCaptchaImage(captchaImageFile):\n",
" batchImages = encode_single_sample(captchaImageFile)\n", " batchImages = encode_single_sample(captchaImageFile)\n",
" preds = model.predict(batchImages)\n", " preds = model.predict(batchImages)\n",
" return decode_batch_predictions(preds)[0]\n", " return decode_batch_predictions(preds)[0]\n",
"\n", "\n",
"\n",
"print(\"loading model...\")\n", "print(\"loading model...\")\n",
"model = load_model()\n", "model = load_model()\n",
"model.summary()" "model.summary()"