From 1a8f704cce8354e3d7bbbc579c4519b1cf633c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=BA=AE=E5=9D=87?= Date: Thu, 23 Apr 2020 14:53:01 +0800 Subject: [PATCH] sort paper --- ctpn_detect_v1/demo.ipynb | 240 ++++++++++++-------------------------- 1 file changed, 75 insertions(+), 165 deletions(-) diff --git a/ctpn_detect_v1/demo.ipynb b/ctpn_detect_v1/demo.ipynb index 2d7d4e8..4b2fc12 100644 --- a/ctpn_detect_v1/demo.ipynb +++ b/ctpn_detect_v1/demo.ipynb @@ -2,125 +2,9 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/ctpn/model.py:30: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/VGGnet_test.py:17: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n", - "\n", - "Tensor(\"Placeholder:0\", shape=(?, ?, ?, 3), dtype=float32)\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:172: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:87: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:198: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n", - "\n", - "Tensor(\"conv5_3/conv5_3:0\", shape=(?, ?, ?, 512), dtype=float32)\n", - "Tensor(\"rpn_conv/3x3/rpn_conv/3x3:0\", shape=(?, ?, ?, 512), dtype=float32)\n", - "WARNING:tensorflow:\n", - "The TensorFlow contrib module will not be included in TensorFlow 2.0.\n", - "For more information, please see:\n", - " * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n", - " * https://github.com/tensorflow/addons\n", - " * https://github.com/tensorflow/io (for I/O related ops)\n", - "If you depend on functionality not listed there, please file an issue.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:101: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:105: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n", - "WARNING:tensorflow:From /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n", - "WARNING:tensorflow:From /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "Call initializer instance with the dtype argument instead of passing it to the constructor\n", - "WARNING:tensorflow:From /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "Call initializer instance with the dtype argument instead of passing it to the constructor\n", - "WARNING:tensorflow:Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AttributeError: module 'gast' has no attribute 'Num'\n", - "WARNING: Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AttributeError: module 'gast' has no attribute 'Num'\n", - "WARNING:tensorflow:Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AttributeError: module 'gast' has no attribute 'Num'\n", - "WARNING: Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AttributeError: module 'gast' has no attribute 'Num'\n", - "Tensor(\"lstm_o/Reshape_2:0\", shape=(?, ?, ?, 512), dtype=float32)\n", - "Tensor(\"lstm_o/Reshape_2:0\", shape=(?, ?, ?, 512), dtype=float32)\n", - "Tensor(\"rpn_cls_score/Reshape_1:0\", shape=(?, ?, ?, 20), dtype=float32)\n", - "Tensor(\"rpn_cls_prob:0\", shape=(?, ?, ?, ?), dtype=float32)\n", - "Tensor(\"Reshape_2:0\", shape=(?, ?, ?, 20), dtype=float32)\n", - "Tensor(\"rpn_bbox_pred/Reshape_1:0\", shape=(?, ?, ?, 40), dtype=float32)\n", - "Tensor(\"Placeholder_1:0\", shape=(?, 3), dtype=float32)\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/lib/networks/network.py:223: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "tf.py_func is deprecated in TF V2. Instead, there are two\n", - " options available in V2.\n", - " - tf.py_function takes a python function which manipulates tf eager\n", - " tensors instead of numpy arrays. It's easy to convert a tf eager tensor to\n", - " an ndarray (just call tensor.numpy()) but having access to eager tensors\n", - " means `tf.py_function`s can use accelerators such as GPUs as well as\n", - " being differentiable using a gradient tape.\n", - " - tf.numpy_function maintains the semantics of the deprecated tf.py_func\n", - " (it is not differentiable, and manipulates numpy arrays). It drops the\n", - " stateful argument making all functions stateful.\n", - " \n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/ctpn/model.py:33: The name tf.train.Saver is deprecated. Please use tf.compat.v1.train.Saver instead.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/ctpn/model.py:35: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.\n", - "\n", - "WARNING:tensorflow:From /Users/liuliangjun/PycharmProjects/vance/OCR/ctpn_detect_v1/ctpn/ctpn/model.py:38: The name tf.train.NewCheckpointReader is deprecated. Please use tf.compat.v1.train.NewCheckpointReader instead.\n", - "\n", - "Tensor_name is : rpn_cls_score/weights\n", - "Tensor_name is : rpn_cls_score/biases\n", - "Tensor_name is : rpn_bbox_pred/weights\n", - "Tensor_name is : lstm_o/weights\n", - "Tensor_name is : lstm_o/bidirectional_rnn/fw/lstm_cell/kernel\n", - "Tensor_name is : lstm_o/bidirectional_rnn/fw/lstm_cell/bias\n", - "Tensor_name is : lstm_o/bidirectional_rnn/bw/lstm_cell/kernel\n", - "Tensor_name is : rpn_conv/3x3/weights\n", - "Tensor_name is : lstm_o/bidirectional_rnn/bw/lstm_cell/bias\n", - "Tensor_name is : lstm_o/biases\n", - "Tensor_name is : conv5_3/biases\n", - "Tensor_name is : conv5_3/weights\n", - "Tensor_name is : conv5_2/weights\n", - "Tensor_name is : conv5_2/biases\n", - "Tensor_name is : conv5_1/biases\n", - "Tensor_name is : conv2_2/weights\n", - "Tensor_name is : conv1_2/biases\n", - "Tensor_name is : conv4_3/weights\n", - "Tensor_name is : conv4_2/biases\n", - "Tensor_name is : conv4_1/weights\n", - "Tensor_name is : conv4_1/biases\n", - "Tensor_name is : conv3_3/weights\n", - "Tensor_name is : rpn_bbox_pred/biases\n", - "Tensor_name is : conv1_1/weights\n", - "Tensor_name is : conv3_2/weights\n", - "Tensor_name is : conv3_3/biases\n", - "Tensor_name is : conv1_2/weights\n", - "Tensor_name is : conv3_1/weights\n", - "Tensor_name is : conv3_1/biases\n", - "Tensor_name is : conv2_2/biases\n", - "Tensor_name is : conv5_1/weights\n", - "Tensor_name is : conv4_3/biases\n", - "Tensor_name is : conv4_2/weights\n", - "Tensor_name is : conv1_1/biases\n", - "Tensor_name is : conv3_2/biases\n", - "Tensor_name is : rpn_conv/3x3/biases\n", - "Tensor_name is : conv2_1/biases\n", - "Tensor_name is : conv2_1/weights\n", - "WARNING:tensorflow:From /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "Use standard file APIs to check for files with this prefix.\n", - "INFO:tensorflow:Restoring parameters from ./ctpn/ctpn/retrain/ckpt/VGGnet_fast_rcnn_iter_50000.ckpt\n", - "load vggnet done\n" - ] - } - ], + "outputs": [], "source": [ "import time\n", "from glob import glob\n", @@ -133,55 +17,42 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "It takes time:26.09282612800598s\n", + "It takes time:25.37869167327881s\n", "---------------------------------------\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "I\n", - "\n", - "\n", - "Official Receipt\n", - "| Tremanction Code - 20190380007\n", - "\n", - "\n", - "_ Panent Code\n", - "\n", - "u\n", - "MEDICATION\n", - "" + "" ] }, - "execution_count": 2, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -208,38 +79,77 @@ "# 对版面进行排序(bubbleSort)\n", "# [x1,y1] [x2,y2]\n", "benchmark = 8 # 基准像素点,相差在这个范围内的文本域都认为是在同一水平线上\n", - "arr = sorted(result.values(), key=lambda x:x[0][1])\n", + "\n", + "arr = sorted(result.values(), key=lambda x:x[0][1]) # 按左上角点y排序\n", + "arr = [[*row, idx] for idx, row in enumerate(result.values())] # 每个文本域预先设定一个idx(idx理解为所在的行号)\n", + "\n", "_len = len(arr)\n", "for i in range(_len):\n", " for j in range(0, _len-i-1):\n", " if (arr[j+1][0][1] - arr[j][0][1]) <= benchmark and arr[j][0][0] > arr[j+1][0][0]:\n", - " # if (y2-y1)<= benchmark and x1 > x2 then change their position\n", + " # 如果 (y2-y1)<= benchmark and x1 > x2 则交换它们的位置\n", " arr[j], arr[j+1] = arr[j+1], arr[j]\n", + " if (arr[j+1][0][1] - arr[j][0][1]) <= benchmark:\n", + " # 如果 (y2-y1)<= benchmark 则后者继承前者的idx(也即认为他们在同一水平线上)\n", + " arr[j+1][-1] = arr[j][-1]\n", " \n", - "# print result\n", + " \n", + "# 将同一水平线的文本域合并\n", + "res = {}\n", "for v in arr:\n", - " print(v[1])\n", + " res.setdefault(v[-1], []).append(v[1])\n", + "\n", + "# 按行打印\n", + "for k, v in res.items():\n", + " print(k, v)\n", " \n", - "Image.fromarray(img)\n", - "\n" + "Image.fromarray(img)" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 ['ln rd']\n", + "1 ['']\n", + "3 ['Specialist in Gastroenterology and Hepatology', '4 Pedder St, Cen']\n", + "4 ['陋 督 及 肚 隧 科 耸 科 医 生 一']\n", + "5 ['_ Heng Kong']\n", + "7 ['MBBS(HK) MRCP(UK) FRCP(Edin) FHKCP ˇ', 'Tel : 2530 0633']\n", + "9 ['FHKAM(Medicine) MSc(Health and Hospital Mgt)(Birm)', 'Fax : 2530 0639']\n", + "10 ['Official Receipt']\n", + "11 ['Transaction Code : 201903180007', 'Date : 18 March, 2019']\n", + "13 ['Doctor _']\n", + "14 ['Patient Code']\n", + "16 ['Ttems', 'Amount']\n", + "18 ['MEDICATION', '~~ 170.00']\n", + "21 ['Total :', '~ 170.00', '~ Paid :', '170,00']\n", + "25 ['Date', 'AIC', 'Paid Voucher No.']\n", + "28 ['18 Mar, 2019', 'VISA / MASTER', '110.00 ,']\n", + "29 ['__ Diagnosis : Dyspepsia, Altered bowel habit']\n" + ] + } + ], + "source": [ + "# 包含 MEDICAL / CENTRE / hospital 等则认为这是医院名 一般在开始几行\n", + "# Building street road Room Hong Kong st\n", + "\n", + "# 按行打印\n", + "for k, v in res.items():\n", + " print(k, v)" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - " \n", - " " - ] + "source": [] }, { "cell_type": "code",