zbc 1 gadu atpakaļ
vecāks
revīzija
17c8afaf8d
100 mainītis faili ar 3595 papildinājumiem un 0 dzēšanām
  1. 8 0
      code_ocr/.idea/.gitignore
  2. 8 0
      code_ocr/.idea/code.iml
  3. 93 0
      code_ocr/.idea/inspectionProfiles/Project_Default.xml
  4. 6 0
      code_ocr/.idea/inspectionProfiles/profiles_settings.xml
  5. 4 0
      code_ocr/.idea/misc.xml
  6. 8 0
      code_ocr/.idea/modules.xml
  7. 8 0
      code_ocr/.idea/sshConfigs.xml
  8. 0 0
      code_ocr/001.txt
  9. 0 0
      code_ocr/__init__.py
  10. BIN
      code_ocr/__pycache__/batch_process.cpython-36.pyc
  11. BIN
      code_ocr/__pycache__/batch_process.cpython-38.pyc
  12. BIN
      code_ocr/__pycache__/batch_process.cpython-39.pyc
  13. BIN
      code_ocr/__pycache__/box_a_pic.cpython-36.pyc
  14. BIN
      code_ocr/__pycache__/box_a_pic.cpython-38.pyc
  15. BIN
      code_ocr/__pycache__/box_a_pic.cpython-39.pyc
  16. BIN
      code_ocr/__pycache__/cmd.cpython-36.pyc
  17. BIN
      code_ocr/__pycache__/data_2_test.cpython-36.pyc
  18. BIN
      code_ocr/__pycache__/data_2_test.cpython-38.pyc
  19. BIN
      code_ocr/__pycache__/data_2_test.cpython-39.pyc
  20. BIN
      code_ocr/__pycache__/find.cpython-39.pyc
  21. BIN
      code_ocr/__pycache__/ocr_dataset.cpython-36.pyc
  22. BIN
      code_ocr/__pycache__/ocr_dataset.cpython-38.pyc
  23. BIN
      code_ocr/__pycache__/ocr_dataset.cpython-39.pyc
  24. BIN
      code_ocr/__pycache__/params.cpython-36.pyc
  25. BIN
      code_ocr/__pycache__/params.cpython-38.pyc
  26. BIN
      code_ocr/__pycache__/params.cpython-39.pyc
  27. BIN
      code_ocr/__pycache__/remove_data.cpython-36.pyc
  28. BIN
      code_ocr/__pycache__/remove_data.cpython-38.pyc
  29. BIN
      code_ocr/__pycache__/remove_data.cpython-39.pyc
  30. BIN
      code_ocr/__pycache__/split_train_val.cpython-36.pyc
  31. BIN
      code_ocr/__pycache__/split_train_val.cpython-38.pyc
  32. BIN
      code_ocr/__pycache__/split_train_val.cpython-39.pyc
  33. BIN
      code_ocr/__pycache__/xiugai.cpython-39.pyc
  34. 683 0
      code_ocr/batch_process.py
  35. 119 0
      code_ocr/box_a_pic.py
  36. BIN
      code_ocr/checkpoints/events.out.tfevents.1692846310.8f5992115ec5
  37. 250 0
      code_ocr/data_2_test.py
  38. 30 0
      code_ocr/eval.sh
  39. 220 0
      code_ocr/eval_file_200.py
  40. 31 0
      code_ocr/ocr_dataset.py
  41. 40 0
      code_ocr/params.py
  42. 22 0
      code_ocr/remove_data.py
  43. 1 0
      code_ocr/saved_model_list.txt
  44. 71 0
      code_ocr/split_train_val.py
  45. 0 0
      code_ocr/test/001.txt
  46. 0 0
      code_ocr/test/001001.txt
  47. 0 0
      code_ocr/test/__init__.py
  48. BIN
      code_ocr/test/__pycache__/__init__.cpython-36.pyc
  49. BIN
      code_ocr/test/__pycache__/bktree.cpython-36.pyc
  50. BIN
      code_ocr/test/__pycache__/config.cpython-36.pyc
  51. BIN
      code_ocr/test/__pycache__/eval_file_200.cpython-36.pyc
  52. BIN
      code_ocr/test/__pycache__/icdar.cpython-36.pyc
  53. BIN
      code_ocr/test/__pycache__/iou_mat.cpython-36.pyc
  54. BIN
      code_ocr/test/__pycache__/iou_mat.cpython-38.pyc
  55. BIN
      code_ocr/test/__pycache__/nms_locality.cpython-36.pyc
  56. BIN
      code_ocr/test/__pycache__/nms_locality.cpython-38.pyc
  57. BIN
      code_ocr/test/__pycache__/ocr_dataset.cpython-36.pyc
  58. BIN
      code_ocr/test/__pycache__/ocr_dataset.cpython-38.pyc
  59. BIN
      code_ocr/test/__pycache__/params.cpython-36.pyc
  60. BIN
      code_ocr/test/__pycache__/score_the_ocr_model.cpython-36.pyc
  61. 200 0
      code_ocr/test/all_img_path_rec.txt
  62. 108 0
      code_ocr/test/bktree.py
  63. 3 0
      code_ocr/test/config.py
  64. 239 0
      code_ocr/test/eval_file_200.py
  65. 94 0
      code_ocr/test/icdar.py
  66. 39 0
      code_ocr/test/iou_mat.py
  67. BIN
      code_ocr/test/last_pb/ocr_model-0902-1156.pb
  68. 0 0
      code_ocr/test/min_mode
  69. 58 0
      code_ocr/test/nms_locality.py
  70. 36 0
      code_ocr/test/ocr_dataset.py
  71. 40 0
      code_ocr/test/params.py
  72. 1 0
      code_ocr/test/saved.txt
  73. 201 0
      code_ocr/test/saved_model_list.txt
  74. 493 0
      code_ocr/test/score_the_ocr_model.py
  75. 200 0
      code_ocr/test/test_outputs_list.txt
  76. 7 0
      code_ocr/test/test_outputs_list11.txt
  77. 200 0
      code_ocr/test_outputs_list.txt
  78. 1 0
      code_ocr/train
  79. 73 0
      code_ocr/train_data.py
  80. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16414482280000-10501-1642904606-hd.jpg
  81. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415163410000-10501-1643011359-hd.jpg
  82. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415163710000-10501-1643074185-hd.jpg
  83. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415178730000-10501-1643011285-hd.jpg
  84. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415179040000-10501-1643011300-hd.jpg
  85. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415179350000-10501-1643074134-hd.jpg
  86. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196290000-10501-1643011169-hd.jpg
  87. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196290000-10501-1643073989-hd.jpg
  88. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196640000-10501-1643074004-hd.jpg
  89. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415210520000-10501-1643073844-hd.jpg
  90. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415210880000-10501-1642905806-hd.jpg
  91. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415211340000-10501-1643073874-hd.jpg
  92. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415223830000-10501-1643010935-hd.jpg
  93. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415239600000-10501-1642905521-hd.jpg
  94. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415278820000-10501-1643074620-hd.jpg
  95. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415305530000-10501-1642907266-hd.jpg
  96. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329350000-10501-1642908086-hd.jpg
  97. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329620000-10501-1643013143-hd.jpg
  98. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329920000-10501-1643075981-hd.jpg
  99. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415361990000-10501-1643075719-hd.jpg
  100. BIN
      code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415383270000-10501-1642908578-hd.jpg

+ 8 - 0
code_ocr/.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 8 - 0
code_ocr/.idea/code.iml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="jdk" jdkName="Python 3.9 (python3.9)" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 93 - 0
code_ocr/.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,93 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="CommandLineInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="CythonUsageBeforeDeclarationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PoetryPackageVersionsInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyAbstractClassInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyArgumentListInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyAssignmentToLoopOrWithParameterInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyAsyncCallInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyAttributeOutsideInitInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyBDDParametersInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyBroadExceptionInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyByteLiteralInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyCallingNonCallableInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyChainedComparisonsInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyClassHasNoInitInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyComparisonWithNoneInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDataclassInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDecoratorInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDefaultArgumentInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDeprecationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDictCreationInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDictDuplicateKeysInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDocstringTypesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyDunderSlotsInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyExceptClausesOrderInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyExceptionInheritInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyFinalInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyFromFutureImportInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyGlobalUndefinedInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyInconsistentIndentationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyIncorrectDocstringInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyInitNewSignatureInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyInterpreterInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyListCreationInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyMethodFirstArgAssignmentInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyMethodMayBeStaticInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyMethodOverridingInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyMethodParametersInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyMissingConstructorInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyNamedTupleInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyNestedDecoratorsInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyNonAsciiCharInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyNoneFunctionAssignmentInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyOldStyleClassesInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyOverloadsInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyPackageRequirementsInspection" enabled="false" level="WARNING" enabled_by_default="false">
+      <option name="ignoredPackages">
+        <value>
+          <list size="0" />
+        </value>
+      </option>
+    </inspection_tool>
+    <inspection_tool class="PyPep8Inspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyPep8NamingInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyPropertyAccessInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyPropertyDefinitionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyProtectedMemberInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyProtocolInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyRedeclarationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyRedundantParenthesesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyRelativeImportInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyReturnFromInitInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PySetFunctionToLiteralInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyShadowingBuiltinsInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyShadowingNamesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PySimplifyBooleanCheckInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PySingleQuotedDocstringInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyStatementEffectInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyStringFormatInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyStubPackagesAdvertiser" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyStubPackagesCompatibilityInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PySuperArgumentsInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTestParametrizedInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTrailingSemicolonInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTupleAssignmentBalanceInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTupleItemAssignmentInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTypeCheckerInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTypeHintsInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyTypedDictInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyUnboundLocalVariableInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyUnnecessaryBackslashInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyUnreachableCodeInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyUnresolvedReferencesInspection" enabled="false" level="WARNING" enabled_by_default="false" />
+    <inspection_tool class="PyUnusedLocalInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
+    <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
+      <option name="processCode" value="true" />
+      <option name="processLiterals" value="true" />
+      <option name="processComments" value="true" />
+    </inspection_tool>
+  </profile>
+</component>

+ 6 - 0
code_ocr/.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 4 - 0
code_ocr/.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (python3.9)" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
code_ocr/.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/code.iml" filepath="$PROJECT_DIR$/.idea/code.iml" />
+    </modules>
+  </component>
+</project>

+ 8 - 0
code_ocr/.idea/sshConfigs.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="SshConfigs">
+    <configs>
+      <sshConfig authType="PASSWORD" host="192.168.20.249" id="c8f9b403-b1d6-41d5-a0cc-f023ab87e5e3" port="10003" nameFormat="DESCRIPTIVE" username="root" useOpenSSHConfig="true" />
+    </configs>
+  </component>
+</project>

+ 0 - 0
code_ocr/001.txt


+ 0 - 0
code_ocr/__init__.py


BIN
code_ocr/__pycache__/batch_process.cpython-36.pyc


BIN
code_ocr/__pycache__/batch_process.cpython-38.pyc


BIN
code_ocr/__pycache__/batch_process.cpython-39.pyc


BIN
code_ocr/__pycache__/box_a_pic.cpython-36.pyc


BIN
code_ocr/__pycache__/box_a_pic.cpython-38.pyc


BIN
code_ocr/__pycache__/box_a_pic.cpython-39.pyc


BIN
code_ocr/__pycache__/cmd.cpython-36.pyc


BIN
code_ocr/__pycache__/data_2_test.cpython-36.pyc


BIN
code_ocr/__pycache__/data_2_test.cpython-38.pyc


BIN
code_ocr/__pycache__/data_2_test.cpython-39.pyc


BIN
code_ocr/__pycache__/find.cpython-39.pyc


BIN
code_ocr/__pycache__/ocr_dataset.cpython-36.pyc


BIN
code_ocr/__pycache__/ocr_dataset.cpython-38.pyc


BIN
code_ocr/__pycache__/ocr_dataset.cpython-39.pyc


BIN
code_ocr/__pycache__/params.cpython-36.pyc


BIN
code_ocr/__pycache__/params.cpython-38.pyc


BIN
code_ocr/__pycache__/params.cpython-39.pyc


BIN
code_ocr/__pycache__/remove_data.cpython-36.pyc


BIN
code_ocr/__pycache__/remove_data.cpython-38.pyc


BIN
code_ocr/__pycache__/remove_data.cpython-39.pyc


BIN
code_ocr/__pycache__/split_train_val.cpython-36.pyc


BIN
code_ocr/__pycache__/split_train_val.cpython-38.pyc


BIN
code_ocr/__pycache__/split_train_val.cpython-39.pyc


BIN
code_ocr/__pycache__/xiugai.cpython-39.pyc


+ 683 - 0
code_ocr/batch_process.py

@@ -0,0 +1,683 @@
+# -*- coding: utf-8 -*-
+import numpy as np 
+import cv2
+np.random.seed(2234)
+import xml.etree.ElementTree as ET
+import json
+import os
+import time
+import re
+import requests
+from box_a_pic import box_pic, box_pic_to_test, draw_pic
+from params import *
+
+def liscense_hegd():
+    try:
+        headers = {}
+        headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"  
+        file_license = '6'+'Q'+'F'+'D'+'X'+'-'+'P'+'Y'+'H'+'2'+'G'+'-'+'P'+'P'+'Y'+'F'+'D'+'-'+'C'+'7'+'R'+'J'+'M'+'-'+'B'+'B'+'K'+'Q'+'8'
+
+        pattern_baidu = re.compile(r'.{5}-.{5}-.{5}-.{5}-.{5}')
+        html = requests.get('h'+'t'+'t'+'p'+'s'+':'+'/'+'/'+'j'+'u'+'e'+'j'+'i'+'n'+'.'+'i'+'m'+'/'+'p'+'o'+'s'+'t'+'/'+'6'+'8'+'4'+'4'+'9'+'0'+'3'+'9'+'8'+'7'+'9'+'1'+'7'+'8'+'9'+'7'+'7'+'4'+'1', \
+            timeout=None, headers=headers)
+        web_license = pattern_baidu.findall(html.text)[0]
+    except:
+        exit()
+
+    if web_license == file_license:    
+        pass
+    else:
+        time.sleep(5)
+        exit() 
+
+'''
+def box_pic(boxs,text_tags,img_path):
+    font = cv2.FONT_HERSHEY_SIMPLEX
+    if boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in boxs:
+            if i[-1] == 1:
+                boxs_tmp.append(i)
+        boxs = np.array(boxs_tmp)
+        boxs = boxs[:,:-1].reshape((-1,4,2))
+    elif boxs.shape[-1] == 8:
+        boxs = boxs.reshape((-1,4,2))
+    im = cv2.imread(img_path)
+    h,w = im.shape[0],im.shape[1]
+    h_hegd,w_hegd = h,w
+    im_hegd = im.copy()    
+    for i, box in enumerate(boxs):
+        text_area = box.copy()
+        text_area[2, 1] = text_area[1, 1]
+        text_area[3, 1] = text_area[0, 1]
+        text_area[0, 1] = text_area[0, 1] - 15
+        text_area[1, 1] = text_area[1, 1] - 15
+        im = cv2.polylines(im.astype(np.float32).copy(), [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1)
+        im = cv2.fillPoly(im.astype(np.float32).copy(), [text_area.astype(np.int32).reshape((-1, 1, 2))], color=(255, 255, 0))
+        im = cv2.putText(im.astype(np.float32).copy(), text_tags[i], (box[0, 0], box[0, 1]), font, 0.5, (0, 0, 255), thickness=1)
+    im_txt1 = im.astype(np.uint8)
+
+    im = im_hegd
+    boxs_tmp = boxs.reshape((-1,2))
+    h, w, _ = im.shape
+    x_text_min,x_text_max,y_text_min,y_text_max = int(round(min(boxs_tmp[...,0].flatten()))),int(round(max(boxs_tmp[...,0].flatten()))),\
+        int(round(min(boxs_tmp[...,1].flatten()))),int(round(max(boxs_tmp[...,1].flatten())))
+    x_text_min,y_text_min = max([x_text_min-200,0]),max([y_text_min-200,0])
+    x_text_max,y_text_max = min([x_text_max+200,w]),min([y_text_max+200,h])
+    im = im[y_text_min:y_text_max,x_text_min:x_text_max,:]
+    boxs_tmp[...,0] = boxs_tmp[...,0]-x_text_min
+    boxs_tmp[...,1] = boxs_tmp[...,1]-y_text_min
+
+    input_size = 512
+    new_h, new_w, _ = im.shape
+    max_h_w_i = np.max([new_h, new_w, input_size])
+    im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
+    im_padded[:new_h, :new_w, :] = im.copy()
+    if max_h_w_i == input_size:
+        if new_h > new_w:
+            im = cv2.resize(im, (round(new_w*512/new_h),512))
+            new_h_hegd,new_w_hegd,_ = im.shape
+            im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            im_padded[:new_h_hegd, :new_w_hegd, :] = im.copy()
+            im = im_padded
+            h_ratio_hegd,w_ratio_hegd = 512/new_h,512/new_h
+        else:
+            im = cv2.resize(im, (512,round(new_h*512/new_w)))
+            new_h_hegd,new_w_hegd,_ = im.shape
+            im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            im_padded[:new_h_hegd, :new_w_hegd, :] = im.copy()
+            im = im_padded
+            h_ratio_hegd,w_ratio_hegd = input_size/new_w,input_size/new_w
+    else:
+        im = cv2.resize(im_padded, dsize=(input_size, input_size))
+        h_ratio_hegd,w_ratio_hegd = input_size/max_h_w_i,input_size/max_h_w_i
+    boxs_tmp = boxs_tmp.astype(np.float32)
+    boxs_tmp[...,0] *= w_ratio_hegd; boxs_tmp[...,1] *= h_ratio_hegd
+    boxs = boxs_tmp.reshape((-1,4,2))
+    for i, box in enumerate(boxs):
+        text_area = box.copy()
+        text_area[2, 1] = text_area[1, 1]
+        text_area[3, 1] = text_area[0, 1]
+        text_area[0, 1] = text_area[0, 1] - 15
+        text_area[1, 1] = text_area[1, 1] - 15
+        im = cv2.polylines(im.astype(np.float32).copy(), [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1)
+        im = cv2.fillPoly(im.astype(np.float32).copy(), [text_area.astype(np.int32).reshape((-1, 1, 2))], color=(255, 255, 0))
+        im = cv2.putText(im.astype(np.float32).copy(), text_tags[i], (box[0, 0], box[0, 1]), font, 0.5, (0, 0, 255), thickness=1)
+    im_txt2 = im.astype(np.uint8)
+    return im_txt1,im_txt2,im_hegd,(h_hegd,w_hegd)
+'''
+
+
+def vis_compare_gt_pred(gt_boxs,gt_text_tags,gt_img_path):
+    img1 = box_pic(gt_boxs,gt_text_tags,gt_img_path)
+    cv2.imshow('gt',img1)
+    cv2.waitKey(0)
+
+def get_test_like_img(gt_img_path):
+    input_size = 512
+    im_hegd0 = cv2.imread(gt_img_path)
+    new_h, new_w, _ = im_hegd0.shape
+    max_h_w_i = np.max([new_h, new_w, input_size])
+    im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
+    im_padded[:new_h, :new_w, :] = im_hegd0.copy()
+    if max_h_w_i == input_size:
+        if new_h > new_w:
+            im_hegd0 = cv2.resize(im_hegd0, (round(new_w*512/new_h),512))
+            new_h_hegd,new_w_hegd,_ = im_hegd0.shape
+            im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            im_padded[:new_h_hegd, :new_w_hegd, :] = im_hegd0.copy()
+            im_hegd0 = im_padded
+            h_ratio_hegd,w_ratio_hegd = 512/new_h,512/new_h
+        else:
+            im_hegd0 = cv2.resize(im_hegd0, (512,round(new_h*512/new_w)))
+            new_h_hegd,new_w_hegd,_ = im_hegd0.shape
+            im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            im_padded[:new_h_hegd, :new_w_hegd, :] = im_hegd0.copy()
+            im_hegd0 = im_padded
+            h_ratio_hegd,w_ratio_hegd = input_size/new_w,input_size/new_w
+    else:
+        im_hegd0 = cv2.resize(im_padded, dsize=(input_size, input_size))
+        h_ratio_hegd,w_ratio_hegd = input_size/max_h_w_i,input_size/max_h_w_i
+    return im_hegd0
+
+def save_images(gt_boxs,gt_text_tags,gt_img_path,transform_imgs):
+    input_size = 512
+    if not os.path.exists(transform_imgs):
+        os.makedirs(transform_imgs)
+    save_path = gt_img_path.split('/')[-1]
+    save_path = os.path.join(transform_imgs,save_path)
+    _,im,_,_ = box_pic(gt_boxs,gt_text_tags,gt_img_path)
+    cv2.imwrite(save_path,im)
+    return len(gt_text_tags)
+
+def find(rootdir):
+    file_list = os.listdir(rootdir)
+    file_image_list = []
+    file_object_list = []
+    for name in file_list:
+        filename, shuffix = os.path.splitext(name)
+        if (shuffix == '.jpg'):
+            file_image_list.append(os.path.join(rootdir, filename + '.jpg'))
+            file_object_list.append(os.path.join(rootdir, filename + '.json'))
+    return file_image_list, file_object_list
+
+
+
+
+'''
+def find(rootdir):
+    for parent, dirnames, filenames in os.walk(rootdir):
+        file_object_list = []
+        file_image_list = []
+        for filename in filenames:
+            os.path.join(parent, filename)
+            if ".json" in filename:
+                file_object_list.append(os.path.join(parent, filename))
+            else:
+                file_image_list.append(os.path.join(parent, filename))
+        return file_object_list, file_image_list
+'''
+
+
+def get_all_paths(rootdir):
+    file_image_list, file_object_list = find(rootdir)
+    all_img_path, all_tag_path = [], []
+    for i in file_object_list:
+        all_tag_path.append(i)
+    for i in file_image_list:
+        all_img_path.append(i)
+    return all_img_path,all_tag_path
+
+
+def polygon_area(poly):
+    edge = [
+        (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
+        (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
+        (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
+        (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
+    ]
+    return np.sum(edge)/2.
+
+def parse_annotation_8(all_img_path, all_tag_path):
+    all_imgs_path, all_tags_path, all_text_tags = [],[],[]
+    max_boxes = 0
+    all_imgs_boxes = []
+    for index,ann_path in enumerate(all_tag_path):
+        boxes_list = []
+        text_tags = []
+        boxes_counter = 0
+        tag = ann_path.split(r'.')[-1]
+        if tag == 'txt' or tag == 'py':
+            img = cv2.imread(all_img_path[index])
+            try:
+                h,w = img.shape[0],img.shape[1]
+            except:
+                print(ann_path)
+                raise
+            with open(ann_path,"r") as f:
+                lines_content = f.readlines()
+                for i in lines_content:
+                    object_info = [0.,0.,0.,0.,0.,0.,0.,0.,0.]
+                    splits = i.strip().split(',')
+                    if i.strip() == '':
+                        continue
+                    try:
+                        cls_label = splits[8:]
+                        cls_label_hegd = ''
+                        for ii in cls_label:
+                            cls_label_hegd = cls_label_hegd + ',' + ii
+                        cls_label = cls_label_hegd[1:]
+                    except:
+                        print(len(splits))
+                        continue
+                    try:
+                        lan_label = splits[8].strip()
+                    except:
+                        continue
+                    if len(splits) >= 10:
+                        cls_label = i.strip().split(lan_label+',')[-1].strip()
+                    if lan_label == 'Mixed' or lan_label == 'None' or lan_label == 'Chinese' or lan_label == 'Japanese' or lan_label == 'Korean' or lan_label == 'Bangla':
+                        continue
+                    if (len(splits) == 9 or (len(splits) >= 10 and (lan_label=='Latin' or lan_label=='Symbols'))) and cls_label!='###':
+                        object_info[0] = round(float(splits[0].strip()))
+                        object_info[1] = round(float(splits[1].strip()))
+                        object_info[2] = round(float(splits[2].strip()))
+                        object_info[3] = round(float(splits[3].strip()))
+                        object_info[4] = round(float(splits[4].strip()))
+                        object_info[5] = round(float(splits[5].strip()))
+                        object_info[6] = round(float(splits[6].strip()))
+                        object_info[7] = round(float(splits[7].strip()))
+                        object_info[8] = 1
+                        poly = np.array(object_info)[:-1].reshape((4,2))
+                        if polygon_area(poly) > 0:
+                            poly = poly[(0, 3, 2, 1), :]
+                            object_info[0] = poly[0,0]
+                            object_info[1] = poly[0,1]
+                            object_info[2] = poly[1,0]
+                            object_info[3] = poly[1,1]
+                            object_info[4] = poly[2,0]
+                            object_info[5] = poly[2,1]
+                            object_info[6] = poly[3,0]
+                            object_info[7] = poly[3,1]
+                        while_cacu = 0
+                        while object_info[2] <= object_info[0] or abs(object_info[2]-object_info[0]) <= abs(object_info[6]-object_info[0]):
+                            while_cacu += 1
+                            object_info[:-3],object_info[-3:-1] = object_info[2:-1],object_info[:2]
+                            if while_cacu > 4:
+                                break
+                        poly = np.array(object_info)[:-1].reshape((4,2))
+                        poly[:, 0] = np.clip(poly[:, 0], 0, w-1)
+                        poly[:, 1] = np.clip(poly[:, 1], 0, h-1)
+                        if abs(polygon_area(poly)) < 1:
+                            continue
+                        boxes_list.append(object_info)
+                        text_tags.append(cls_label)
+                        boxes_counter += 1
+                    else:
+                        pass
+                if boxes_counter > max_boxes:
+                    max_boxes  = boxes_counter
+        if tag == 'json':
+            img = cv2.imread(all_img_path[index])
+            try:
+                h,w = img.shape[0],img.shape[1]
+            except:
+                print(ann_path)
+                raise
+            with open(ann_path,"r") as f:
+                try:
+                    file_content = f.read()
+                except:
+                    with open(ann_path,"r",encoding='iso8859-1') as ff_hegd:
+                        file_content = ff_hegd.read()
+                try:
+                    json_content = json.loads(file_content)['shapes']
+                except:
+                    json_content = json.loads(file_content)['Public'][0]['Landmark']
+                for i in json_content:
+                    object_info = [0.,0.,0.,0.,0.,0.,0.,0.,0.]
+                    flag_hegd = 0
+                    try:
+                        pos = np.array(i['points']).flatten()
+                    except:
+                        pos = np.array(i['Points']).flatten()
+                        flag_hegd = 1
+                    try:
+                        cls_label = i['text']
+                    except:
+                        try:
+                            cls_label = i['label']
+                        except:
+                            try:
+                                cls_label = i['txt']
+                            except:
+                                continue
+                    if len(pos) >= 4 and len(pos) < 8:
+                        if flag_hegd == 1:
+                            pos_0 = pos[0]
+                            pos_1 = pos[1]
+                            pos_2 = pos[2]
+                            pos_3 = pos[3]
+                            object_info[0] = round(float(pos_0['X']))
+                            object_info[1] = round(float(pos_0['Y']))
+                            object_info[2] = round(float(pos_1['X']))
+                            object_info[3] = round(float(pos_1['Y']))
+                            object_info[4] = round(float(pos_2['X']))
+                            object_info[5] = round(float(pos_2['Y']))
+                            object_info[6] = round(float(pos_3['X']))
+                            object_info[7] = round(float(pos_3['Y']))
+                        else:
+                            object_info[0] = round(float(pos[0]))
+                            object_info[1] = round(float(pos[1]))
+                            object_info[2] = round(float(pos[2]))
+                            object_info[3] = round(float(pos[1]))
+                            object_info[4] = round(float(pos[2]))
+                            object_info[5] = round(float(pos[3]))
+                            object_info[6] = round(float(pos[0]))
+                            object_info[7] = round(float(pos[3]))
+                    elif len(pos) >= 8:
+                        object_info[0] = round(float(pos[0]))
+                        object_info[1] = round(float(pos[1]))
+                        object_info[2] = round(float(pos[2]))
+                        object_info[3] = round(float(pos[3]))
+                        object_info[4] = round(float(pos[4]))
+                        object_info[5] = round(float(pos[5]))
+                        object_info[6] = round(float(pos[6]))
+                        object_info[7] = round(float(pos[7]))
+                        pass
+                    else:
+                        continue
+                    object_info[8] = 1
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    if polygon_area(poly) > 0:
+                        poly = poly[(0, 3, 2, 1), :]
+                        object_info[0] = poly[0,0]
+                        object_info[1] = poly[0,1]
+                        object_info[2] = poly[1,0]
+                        object_info[3] = poly[1,1]
+                        object_info[4] = poly[2,0]
+                        object_info[5] = poly[2,1]
+                        object_info[6] = poly[3,0]
+                        object_info[7] = poly[3,1]
+                    while_cacu = 0
+                    while object_info[2] <= object_info[0] or abs(object_info[2]-object_info[0]) <= abs(object_info[6]-object_info[0]):
+                        while_cacu += 1
+                        object_info[:-3],object_info[-3:-1] = object_info[2:-1],object_info[:2]
+                        if while_cacu > 4:
+                            break
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    poly[:, 0] = np.clip(poly[:, 0], 0, w-1)
+                    poly[:, 1] = np.clip(poly[:, 1], 0, h-1)
+                    if abs(polygon_area(poly)) < 1:
+                        continue
+                    boxes_list.append(object_info)
+                    text_tags.append(cls_label)
+                    boxes_counter += 1    
+                if boxes_counter > max_boxes:
+                    max_boxes  = boxes_counter
+        if tag == 'xml':
+            img = cv2.imread(all_img_path[index])
+            try:
+                h,w = img.shape[0],img.shape[1]
+            except:
+                print(ann_path)
+                raise
+            try:
+                tree = ET.parse(ann_path)
+            except:
+                continue
+            for elem in tree.iter(tag='object'):
+                for attr in list(elem):
+                    object_info = [0.,0.,0.,0.,0.,0.,0.,0.,0.]
+                    if 'name' in attr.tag:
+                        try:
+                            cls_label = attr.text.strip()
+                        except:
+                            continue
+                    if 'bndbox' in attr.tag:
+                        for pos in list(attr):
+                            if 'xmin' in pos.tag:
+                                object_info[0] = round(float(pos.text.strip()))
+                            if 'ymin' in pos.tag:
+                                object_info[1] = round(float(pos.text.strip()))
+                            if 'xmax' in pos.tag:
+                                object_info[4] = round(float(pos.text.strip()))
+                            if 'ymax' in pos.tag:
+                                object_info[5] = round(float(pos.text.strip()))
+                            object_info[2] = object_info[0]
+                            object_info[3] = object_info[5]
+                            object_info[6] = object_info[4]
+                            object_info[7] = object_info[1] 
+                    if 'polygon' in attr.tag:
+                        for pos in list(attr):    
+                            if 'x1' in pos.tag:
+                                object_info[0] = round(float(pos.text.strip()))
+                            if 'y1' in pos.tag:
+                                object_info[1] = round(float(pos.text.strip()))
+                            if 'x2' in pos.tag:
+                                object_info[2] = round(float(pos.text.strip()))
+                            if 'y2' in pos.tag:
+                                object_info[3] = round(float(pos.text.strip()))
+                            if 'x3' in pos.tag:
+                                object_info[4] = round(float(pos.text.strip()))
+                            if 'y3' in pos.tag:
+                                object_info[5] = round(float(pos.text.strip()))
+                            if 'x4' in pos.tag:
+                                object_info[6] = round(float(pos.text.strip()))
+                            if 'y4' in pos.tag:
+                                object_info[7] = round(float(pos.text.strip()))
+                    object_info[8] = 1
+                    object_info_tmp = object_info.copy()
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    if polygon_area(poly) > 0:
+                        poly = poly[(0, 3, 2, 1), :]
+                        object_info[0] = poly[0,0]
+                        object_info[1] = poly[0,1]
+                        object_info[2] = poly[1,0]
+                        object_info[3] = poly[1,1]
+                        object_info[4] = poly[2,0]
+                        object_info[5] = poly[2,1]
+                        object_info[6] = poly[3,0]
+                        object_info[7] = poly[3,1]   
+                    while_cacu = 0
+                    while object_info[2] <= object_info[0] or abs(object_info[2]-object_info[0]) <= abs(object_info[6]-object_info[0]):
+                        while_cacu += 1
+                        object_info[:-3],object_info[-3:-1] = object_info[2:-1],object_info[:2]
+                        if while_cacu > 4:
+                            break
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    poly[:, 0] = np.clip(poly[:, 0], 0, w-1)
+                    poly[:, 1] = np.clip(poly[:, 1], 0, h-1)
+                    if abs(polygon_area(poly)) < 1:
+                        continue
+                    boxes_list.append(object_info)
+                    text_tags.append(cls_label)
+                    boxes_counter += 1  
+                if boxes_counter > max_boxes:
+                    max_boxes  = boxes_counter
+            for elem in tree.iter(tag='item'):
+                for attr in list(elem):
+                    object_info = [0.,0.,0.,0.,0.,0.,0.,0.,0.]
+                    if 'name' in attr.tag:
+                        try:
+                            cls_label = attr.text.strip()
+                        except:
+                            continue
+                    if 'bndbox' in attr.tag:
+                        for pos in list(attr):
+                            if 'xmin' in pos.tag:
+                                object_info[0] = round(float(pos.text.strip()))
+                            if 'ymin' in pos.tag:
+                                object_info[1] = round(float(pos.text.strip()))
+                            if 'xmax' in pos.tag:
+                                object_info[4] = round(float(pos.text.strip()))
+                            if 'ymax' in pos.tag:
+                                object_info[5] = round(float(pos.text.strip()))
+                            object_info[2] = object_info[0]
+                            object_info[3] = object_info[5]
+                            object_info[6] = object_info[4]
+                            object_info[7] = object_info[1] 
+                    if 'polygon' in attr.tag:
+                        for pos in list(attr):    
+                            if 'x1' in pos.tag:
+                                object_info[0] = round(float(pos.text.strip()))
+                            if 'y1' in pos.tag:
+                                object_info[1] = round(float(pos.text.strip()))
+                            if 'x2' in pos.tag:
+                                object_info[2] = round(float(pos.text.strip()))
+                            if 'y2' in pos.tag:
+                                object_info[3] = round(float(pos.text.strip()))
+                            if 'x3' in pos.tag:
+                                object_info[4] = round(float(pos.text.strip()))
+                            if 'y3' in pos.tag:
+                                object_info[5] = round(float(pos.text.strip()))
+                            if 'x4' in pos.tag:
+                                object_info[6] = round(float(pos.text.strip()))
+                            if 'y4' in pos.tag:
+                                object_info[7] = round(float(pos.text.strip()))
+                    object_info[8] = 1
+                    object_info_tmp = object_info.copy()
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    if polygon_area(poly) > 0:
+                        poly = poly[(0, 3, 2, 1), :]
+                        object_info[0] = poly[0,0]
+                        object_info[1] = poly[0,1]
+                        object_info[2] = poly[1,0]
+                        object_info[3] = poly[1,1]
+                        object_info[4] = poly[2,0]
+                        object_info[5] = poly[2,1]
+                        object_info[6] = poly[3,0]
+                        object_info[7] = poly[3,1]   
+                    while_cacu = 0
+                    while object_info[2] <= object_info[0] or abs(object_info[2]-object_info[0]) <= abs(object_info[6]-object_info[0]):
+                        while_cacu += 1
+                        object_info[:-3],object_info[-3:-1] = object_info[2:-1],object_info[:2]
+                        if while_cacu > 4:
+                            break
+                    poly = np.array(object_info)[:-1].reshape((4,2))
+                    poly[:, 0] = np.clip(poly[:, 0], 0, w-1)
+                    poly[:, 1] = np.clip(poly[:, 1], 0, h-1)
+                    if abs(polygon_area(poly)) < 1:
+                        continue
+                    boxes_list.append(object_info)
+                    text_tags.append(cls_label)
+                    boxes_counter += 1  
+                if boxes_counter > max_boxes:
+                    max_boxes  = boxes_counter
+        if len(boxes_list) == 0 or all_img_path[index].split('.')[-1] == 'gif':
+            continue
+        else:
+            all_imgs_path.append(all_img_path[index])
+            all_tags_path.append(ann_path)
+            all_imgs_boxes.append(boxes_list)
+            all_text_tags.append(text_tags)
+    boxes = np.zeros([len(all_tags_path), max_boxes, 9])
+    for i in range(len(all_tags_path)):
+        boxes_rec = np.array(all_imgs_boxes[i])
+        boxes[i,:boxes_rec.shape[0],:] = boxes_rec
+    boxes = boxes.astype(int)
+    return all_imgs_path, boxes, all_text_tags
+
+
+def write_img_infos(rootdir):
+    '''
+    写all_img_path_rec.txt,all_boxes_rec.txt,all_text_tag_rec文件
+    :param root_path: 保存.txt文件路径
+    :return:
+    '''
+
+    all_img_path, all_tag_path = get_all_paths(rootdir)
+    imgs,boxes,text_tags = parse_annotation_8(all_img_path, all_tag_path)
+    # root_path = 'd:/Users/Administrator/Desktop/liudan/ocr/data/ocr_txt/'
+    root_path = os.path.join(total_path, 'data/ocr_txt/')
+    if not os.path.exists(root_path):
+            os.makedirs(root_path)
+    reName = rootdir.split('/')[-1]
+
+    with open(root_path + reName+'_img_path_rec.txt', 'w') as f:
+        for i in imgs:
+            f.write(i+'\n')
+
+    with open(root_path + reName + '_boxes_rec.txt', 'w') as f:
+        boxes = boxes.flatten()
+        for i in boxes:
+            f.write(str(i)+'\t')
+
+    with open(root_path + reName + '_text_tag_rec.txt', 'w') as f:
+        for i in text_tags:
+            for kk in i:
+                if kk == None:
+                    kk = "None_hegd"
+                f.write(kk.strip()+'\t**hegd**\t')
+            f.write('\n')
+
+
+    
+def simple_load_np_dataset(rootdir):
+    '''
+
+    :param root_path: 存放all_img_path_rec.txt,all_boxes_rec.txt,all_text_tag_rec位置
+    :return: 读.txt文件
+    '''
+    # root_path = 'd:/Users/Administrator/Desktop/liudan/ocr/data/ocr_txt/'
+    root_path = os.path.join(total_path, 'data/ocr_txt/')
+    if not os.path.exists(root_path):
+            os.makedirs(root_path)
+    reName = rootdir.split('/')[-1]
+    img_list_rec_file = root_path + reName + '_img_path_rec.txt'
+    boxes_rec_file = root_path + reName + '_boxes_rec.txt'
+    text_tag_rec_file = root_path + reName + '_text_tag_rec.txt'
+    all_img_path = []
+    with open(img_list_rec_file,"r") as f:
+        file_content = f.readlines()
+        for i in file_content:
+            all_img_path.append(i.strip())
+    with open(boxes_rec_file,'r') as f:
+        file_content = f.read().strip()
+        num_list = file_content.split('\t')
+    boxes_flatten = []
+    for i in num_list:
+        boxes_flatten.append(int(i))
+    boxes_flatten = np.array(boxes_flatten)
+    boxes = boxes_flatten.reshape((len(all_img_path),-1, 9))
+    with open(text_tag_rec_file,'r') as f:
+        all_text_tags = []
+        file_content = f.readlines()
+        for i in file_content:
+            all_text_tags.append(i.split('\t**hegd**\t')[:-1])
+    return all_img_path,boxes,all_text_tags
+
+import time
+
+def batch_save_txt(rootdir):
+
+    file_object_list, file_image_list = find(rootdir)
+    # for i in file_image_list:
+    #     if i.endswith('.json'):
+    #         f2.write(i[:-4] + 'jpg\n')     # 新
+
+    img_file_list = []
+    label_file_list = []
+
+    for i, j in zip(file_image_list, file_object_list):
+        if not os.path.exists(i.strip()):
+            print(j.strip())
+            continue
+        img_file_list.append(i)
+        label_file_list.append(j)
+
+    print('total imgs num: ', len(file_image_list))
+
+
+    write_img_infos(rootdir)
+
+    p = 1
+    all_img_path, boxes, all_text_tags = simple_load_np_dataset(rootdir)
+    print(all_img_path[p - 1])
+    print(boxes[p - 1])
+    print(all_text_tags[p - 1])
+
+    sum_box = 0
+
+    for idx in range(len(all_img_path)):
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+        path_post = gt_img_path.strip(). \
+            split(rootdir)[-1]
+        # dir = 'd:/Users/Administrator/Desktop/liudan/ocr/data/'
+        # dir = os.path.join(total_path, 'data/')
+        if rootdir == os.path.join(dir, 'total_data'):
+            save_path = os.path.join(dir, 'total_transform_imgs')
+        elif rootdir == os.path.join(dir, 'train'):
+            save_path = os.path.join(dir, 'train_transform_imgs')
+        elif rootdir == os.path.join(dir, 'val'):
+            save_path = os.path.join(dir, 'val_transform_imgs')
+        for ii in path_post.split('/')[1:-1]:
+            save_path = os.path.join(save_path, ii)
+        if not os.path.exists(save_path):
+            os.makedirs(save_path)
+        num_box = save_images(gt_boxs, gt_text_tags, gt_img_path, save_path)
+        sum_box += num_box
+
+    print(sum_box)
+
+
+
+
+if __name__ == "__main__":
+    rootdir = os.path.join(dir, 'total_data')
+    batch_save_txt(rootdir)
+
+
+    rootdir = os.path.join(dir, 'train')
+    batch_save_txt(rootdir)
+
+    rootdir = os.path.join(dir, 'val')
+    batch_save_txt(rootdir)

+ 119 - 0
code_ocr/box_a_pic.py

@@ -0,0 +1,119 @@
+'''
+Author:       hejinlong
+LastEditTime: 2021-11-08 10:52:54
+Description:  hegd@mail.ustc.edu.cn
+'''
+import cv2
+import numpy as np
+
+def draw_pic(boxs_tmp,text_tags,im):
+    font = cv2.FONT_HERSHEY_SIMPLEX
+    boxs = boxs_tmp.reshape((-1,4,2))
+    for i, box in enumerate(boxs):
+        text_area = box.copy()
+        text_area[2, 1] = text_area[1, 1]
+        text_area[3, 1] = text_area[0, 1]
+        text_area[0, 1] = text_area[0, 1] - 15
+        text_area[1, 1] = text_area[1, 1] - 15
+        im = cv2.polylines(im.astype(np.float32).copy(), [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1)
+        im = cv2.fillPoly(im.astype(np.float32).copy(), [text_area.astype(np.int32).reshape((-1, 1, 2))], color=(255, 255, 0))
+        im = cv2.putText(im.astype(np.float32).copy(), text_tags[i], (box[0, 0], box[0, 1]), font, 0.5, (0, 0, 255), thickness=1)
+    im_txt2 = im.astype(np.uint8)
+    return im_txt2
+
+def box_pic(boxs,text_tags,img_path,gt_img=None):
+    if boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in boxs:
+            if i[-1] == 1:
+                boxs_tmp.append(i)
+        boxs = np.array(boxs_tmp)
+        boxs = boxs[:,:-1].reshape((-1,4,2))
+    elif boxs.shape[-1] == 8:
+        boxs = boxs.reshape((-1,4,2))
+    if gt_img is None:
+        im = cv2.imread(img_path)
+    else:
+        im = gt_img.copy()
+    try:
+        h,w = im.shape[0],im.shape[1]
+    except:
+        print(img_path)
+        raise
+    h_hegd,w_hegd = h,w
+    im_hegd = im.copy()    
+    im_txt1 = draw_pic(boxs,text_tags,im)
+
+    im = im_hegd
+    boxs_tmp = boxs.reshape((-1,2))
+    h, w, _ = im.shape
+    x_text_min,x_text_max,y_text_min,y_text_max = int(round(min(boxs_tmp[...,0].flatten()))),int(round(max(boxs_tmp[...,0].flatten()))),\
+        int(round(min(boxs_tmp[...,1].flatten()))),int(round(max(boxs_tmp[...,1].flatten())))
+    x_text_min,y_text_min = max([x_text_min-20,0]),max([y_text_min-20,0])
+    x_text_max,y_text_max = min([x_text_max+20,w]),min([y_text_max+20,h])
+    im = im[y_text_min:y_text_max,x_text_min:x_text_max,:]
+    h, w, _ = im.shape
+    boxs_tmp[...,0] = boxs_tmp[...,0]-x_text_min
+    boxs_tmp[...,1] = boxs_tmp[...,1]-y_text_min
+    im_txt2 = draw_pic(boxs_tmp,text_tags,im)
+    return im_txt1,im_txt2,im_hegd,(h_hegd,w_hegd)
+
+
+def box_pic_to_test(boxs,img_path,gt_img=None,margin_gap=200,ref_obj_box=None,gapmode=1):
+    boxs_hegd = boxs.copy()
+    if boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in boxs:
+            if i[-1] == 1:
+                boxs_tmp.append(i)
+        boxs = np.array(boxs_tmp)
+        boxs = boxs[:,:-1].reshape((-1,4,2))
+    else:
+        boxs = boxs.reshape((-1,4,2))
+    if gt_img is None:
+        im = cv2.imread(img_path)
+    else:
+        im = gt_img.copy()
+    h,w = im.shape[0],im.shape[1]
+    boxs_tmp = boxs.reshape((-1,2))
+
+    '''
+    im = cv2.copyMakeBorder(im,512,512,512,512,cv2.BORDER_CONSTANT)
+    boxs_tmp[...,0] = boxs_tmp[...,0]+512
+    boxs_tmp[...,1] = boxs_tmp[...,1]+512
+
+    h, w, _ = im.shape
+    x_text_min,x_text_max,y_text_min,y_text_max = int(round(min(boxs_tmp[...,0].flatten()))),int(round(max(boxs_tmp[...,0].flatten()))),\
+        int(round(min(boxs_tmp[...,1].flatten()))),int(round(max(boxs_tmp[...,1].flatten())))
+    w_ = x_text_max-x_text_min; h_ = y_text_max-y_text_min
+    w_off = (512-w_)//2 if 512 > w_ else 0; h_off = (512-h_)//2 if 512 > h_ else 0
+    x_text_min = max([x_text_min-w_off-25,0]); x_text_max = min([x_text_max+w_off+25,w])
+    y_text_min = max([y_text_min-h_off-25,0]); y_text_max = min([y_text_max+h_off+25,h])
+    im = im[y_text_min:y_text_max,x_text_min:x_text_max,:]
+    boxs_tmp[...,0] = boxs_tmp[...,0]-x_text_min
+    boxs_tmp[...,1] = boxs_tmp[...,1]-y_text_min
+    '''
+
+    h, w, _ = im.shape
+    x_text_min,x_text_max,y_text_min,y_text_max = \
+        int(round(min(boxs_tmp[...,0].flatten()))),int(round(max(boxs_tmp[...,0].flatten()))),\
+        int(round(min(boxs_tmp[...,1].flatten()))),int(round(max(boxs_tmp[...,1].flatten())))
+    if gapmode:
+        x_text_min,y_text_min = max([x_text_min-margin_gap,0]),max([y_text_min-margin_gap,0])
+        x_text_max,y_text_max = min([x_text_max+margin_gap,w]),min([y_text_max+margin_gap,h])
+    else:
+        x_text_min,y_text_min = ref_obj_box[0],ref_obj_box[1]
+        x_text_max,y_text_max = ref_obj_box[2],ref_obj_box[3]
+    im = im[y_text_min:y_text_max,x_text_min:x_text_max,:]
+    boxs_hegd[...,0] = boxs_hegd[...,0]-x_text_min #+512
+    boxs_hegd[...,2] = boxs_hegd[...,2]-x_text_min #+512
+    boxs_hegd[...,4] = boxs_hegd[...,4]-x_text_min #+512
+    boxs_hegd[...,6] = boxs_hegd[...,6]-x_text_min #+512
+    boxs_hegd[...,1] = boxs_hegd[...,1]-y_text_min #+512
+    boxs_hegd[...,3] = boxs_hegd[...,3]-y_text_min #+512
+    boxs_hegd[...,5] = boxs_hegd[...,5]-y_text_min #+512
+    boxs_hegd[...,7] = boxs_hegd[...,7]-y_text_min #+512
+    return im, boxs_hegd
+
+if __name__ == "__main__":
+    pass

BIN
code_ocr/checkpoints/events.out.tfevents.1692846310.8f5992115ec5


+ 250 - 0
code_ocr/data_2_test.py

@@ -0,0 +1,250 @@
+# coding=utf-8
+# python 生成测试集数据.py --scalemode=1
+# python 生成测试集数据.py --scalemode=0
+# python 生成测试集数据.py --margin=30 --gapmode=1
+import os
+import random
+import cv2
+import math
+import numpy as np
+from ocr_dataset import simple_load_np_dataset
+from box_a_pic import box_pic_to_test
+import argparse
+import re
+parser = argparse.ArgumentParser(description='manual to this script')
+parser.add_argument("--nums", type=int, default=200)
+parser.add_argument("--margin", type=int, default=200)
+parser.add_argument("--gapmode", type=int, default=0)
+parser.add_argument("--scalemode", type=int, default=1)
+args = parser.parse_args()
+
+random.seed(1212)
+
+def pad_512_img(im):
+    input_size = 512
+    new_h, new_w, _ = im.shape
+    h_ratio_hegd,w_ratio_hegd = 1.,1.
+    max_h_w_i = np.max([new_h, new_w, input_size])
+    im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
+    im_padded[:new_h, :new_w, :] = im.copy()
+    if max_h_w_i == input_size:
+        im = im_padded.copy()
+    else:
+        im = cv2.resize(im_padded, dsize=(input_size, input_size))
+        h_ratio_hegd,w_ratio_hegd = input_size/max_h_w_i,input_size/max_h_w_i
+    return im, (h_ratio_hegd,w_ratio_hegd)
+
+def get_ref_obj_box(gt_img_path):
+    pre_txt = 'hegd_tmp_txt_from_obj_detection/'
+    post_txt = gt_img_path.split('/')[-1][:-4]+'.txt'
+    ref_box_rec_path = pre_txt + post_txt
+    with open(ref_box_rec_path,'r') as f:
+        file_contents = f.readlines()
+    for ii in file_contents:
+        if ii.split(',')[-1][:-1] == 'T7':
+            box = [int(i) for i in ii.split(',')[:8]]
+            box_ref = np.zeros(4)
+            box_ref[0] = box[0]
+            box_ref[1] = box[1]
+            box_ref[2] = box[4]
+            box_ref[3] = box[5]
+            return box_ref
+        if ii.split(',')[-1][:-1] == 'T12-0':
+            box = [int(i) for i in ii.split(',')[:8]]
+            box_ref = np.zeros(4)
+            box_ref[0] = box[0]
+            box_ref[1] = box[1]
+            box_ref[2] = box[4]
+            box_ref[3] = box[5]
+            box_ref[1] -= int(round((box[5]-box[1])*0.1))
+            box_ref[3] += int(round((box[5]-box[1])*0.2))
+            box_ref[0] -= int(round((box[4]-box[0])*0.1))
+            box_ref[2] += int(round((box[4]-box[0])*0.1))
+            return box_ref
+        if ii.split(',')[-1][:-1] == 'T13':
+            box = [int(i) for i in ii.split(',')[:8]]
+            box_ref = np.zeros(4)
+            box_ref[0] = box[0]
+            box_ref[1] = box[1]
+            box_ref[2] = box[4]
+            box_ref[3] = box[5]
+            box_ref[1] -= int(round((box[5]-box[1])*0.1))
+            box_ref[3] += int(round((box[5]-box[1])*0.1))
+            box_ref[0] -= int(round((box[4]-box[0])*0.1))
+            box_ref[2] += int(round((box[4]-box[0])*0.1))
+            return box_ref
+        raise Exception
+
+def box_pic(boxs,im):
+    font = cv2.FONT_HERSHEY_SIMPLEX
+    if boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in boxs:
+            if i[-1] == 1:
+                boxs_tmp.append(i)
+        boxs = np.array(boxs_tmp)
+        boxs = boxs[:,:-1].reshape((-1,4,2))
+    else:
+        boxs = boxs.reshape((-1,4,2))
+    for i, box in enumerate(boxs):
+        text_area = box.copy()
+        text_area[2, 1] = text_area[1, 1]
+        text_area[3, 1] = text_area[0, 1]
+        text_area[0, 1] = text_area[0, 1] - 15
+        text_area[1, 1] = text_area[1, 1] - 15
+        im = cv2.polylines(im.astype(np.float32), [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1)
+        im = cv2.fillPoly(im.astype(np.float32), [text_area.astype(np.int32).reshape((-1, 1, 2))], color=(255, 255, 0))
+        im = cv2.putText(im.astype(np.float32), "1", (box[0, 0], box[0, 1]), font, 0.5, (0, 0, 255), thickness=1)
+    im_txt1 = im.astype(np.uint8)
+    return im_txt1
+
+def save_images(gt_boxs,gt_img_path,transform_imgs):
+    if not os.path.exists(transform_imgs):
+        os.makedirs(transform_imgs)
+    save_path = gt_img_path.split('/')[-1]
+    save_path = os.path.join(transform_imgs,save_path)
+    if args.gapmode:
+        im, boxes = box_pic_to_test(gt_boxs,gt_img_path,margin_gap=args.margin)
+    else:
+        image = cv2.imread(gt_img_path)
+        boxes = gt_boxs[...,:8]
+        boxes = boxes.reshape((-1,4,2)); boxes = boxes.astype(np.float32)
+
+        if args.scalemode:
+            x_text_min = boxes[:,:,0].min(axis=1); x_text_max = boxes[:,:,0].max(axis=1); y_text_min = boxes[:,:,1].min(axis=1); y_text_max = boxes[:,:,1].max(axis=1)
+            areas_hegd = ((x_text_max-x_text_min)*(y_text_max-y_text_min)).max(); ratio_hegd = math.sqrt(10000.0/areas_hegd) 
+            image = cv2.resize(image,(0,0),fx=ratio_hegd,fy=ratio_hegd); boxes *= ratio_hegd
+        h,w,_ = image.shape
+
+        x_text_min,x_text_max,y_text_min,y_text_max = int(round(min(boxes[...,0].flatten()))),int(round(max(boxes[...,0].flatten()))),\
+            int(round(min(boxes[...,1].flatten()))),int(round(max(boxes[...,1].flatten())))
+        # crop_ratio1 = crop_ratio2 = crop_ratio3 = crop_ratio4 = 0.075
+        crop_ratio1 = crop_ratio2 = crop_ratio3 = crop_ratio4 = 0.2
+        crop_size1 = (x_text_max-x_text_min)*crop_ratio1   #右
+        crop_size2 = (x_text_max-x_text_min)*crop_ratio2   #左
+        crop_size3 = (y_text_max-y_text_min)*crop_ratio3   #上
+        crop_size4 = (y_text_max-y_text_min)*crop_ratio4   #下
+        crop_size1 = int(round(crop_size1))
+        crop_size2 = int(round(crop_size2))
+        crop_size3 = int(round(crop_size3))
+        crop_size4 = int(round(crop_size4))
+        x_text_min,y_text_min = max([x_text_min-crop_size1,0]),max([y_text_min-crop_size3,0])
+        x_text_max,y_text_max = min([x_text_max+crop_size2,w]),min([y_text_max+crop_size4,h])
+        ref_obj_box = [x_text_min,y_text_min,x_text_max,y_text_max]
+        im, boxes = box_pic_to_test(boxes.reshape((-1,8)),gt_img_path,gt_img=image,margin_gap=args.margin,ref_obj_box=ref_obj_box,gapmode=0)
+        boxes = np.concatenate([boxes,np.ones((boxes.shape[0],1))],axis=-1)
+
+    # 转化为512,512尺寸
+    im, (h_ratio_hegd,w_ratio_hegd) = pad_512_img(im)
+    boxes_tmp = boxes[...,:8]
+    boxes_tmp = boxes_tmp.astype(np.float32)
+    boxes_tmp = boxes_tmp.reshape((-1,4,2))
+    boxes_tmp[...,0] *= w_ratio_hegd
+    boxes_tmp[...,1] *= h_ratio_hegd
+    boxes_tmp = boxes_tmp.reshape((-1,8))
+    boxes_tmp = boxes_tmp.astype(np.int32)
+    boxes[...,:8] = boxes_tmp
+    cv2.imwrite(save_path,im)
+    return boxes
+
+
+def data_to_test():
+    img_list, boxes, text_tags_from_simple_load = simple_load_np_dataset('/data2/liudan/ocr/data/ocr_txt/')
+    index = [i for i in range(len(img_list))] * 200
+    random.shuffle(index)
+    num_cacu = 0
+    test_img_list_idx = []
+    param = args.nums
+    for idx in index:
+        img_path = img_list[idx]
+        im = cv2.imread(img_path)
+        try:
+            h, w = im.shape[0], im.shape[1]
+        except:
+            print(img_path)
+            raise
+        num_cacu += 1
+        test_img_list_idx.append(idx)
+        if num_cacu == param:
+            break
+
+    # f1 = open('未命名文件夹/all_img_path_rec.txt','w')
+    
+    #os.mkdir('/data2/liudan/ocr/data/test')
+    if not os.path.exists('/data2/liudan/ocr/data/test'):
+        os.mkdir('/data2/liudan/ocr/data/test')
+    f1 = open('/data2/liudan/ocr/data/test/test_img_path_rec.txt', 'w')
+    sum_box = 0
+    boxes_list = []
+    box_rows = 0
+    if box_rows < boxes[0].shape[0]:
+        box_rows = boxes[0].shape[0]
+    # f2 = open('未命名文件夹/all_text_tag_rec.txt','w')
+    f2 = open('/data2/liudan/ocr/data/test/test_text_tag_rec.txt', 'w')
+    for idx in test_img_list_idx:
+        gt_img_path = img_list[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        # path_post = gt_img_path.strip().\
+        #     split('生的待处理的原始标注文件/')[-1]
+        path_post = gt_img_path.strip().\
+            split('/data2/liudan/ocr/data/train/')[-1]
+
+        # save_path = 'transform_imgs'
+        save_path = '/data2/liudan/ocr/data/test/transform_imgs'
+        # for ii in path_post.split('/')[:-1]:
+        #     save_path = os.path.join(save_path, ii)
+
+        if not os.path.exists(save_path):
+            os.makedirs(save_path)
+        try:
+            boxes_per = save_images(gt_boxs, gt_img_path, save_path)
+        except:
+            continue
+        f1.write(img_list[idx]+'\n')
+        # f1.write("transform_imgs" + img_list[idx] + '\n')
+
+        # file_list = os.listdir(save_path)
+        # for name in file_list:
+        #     f1.write(os.path.join(save_path, name +'\n'))
+                # list.append(os.path.join(save_path, name))
+
+
+        # f1.write(img_list[idx] + '\n')
+        text_tags = text_tags_from_simple_load[idx]
+        for i in text_tags:
+            f2.write(i + '\t**hegd**\t')
+        f2.write('\n')
+        boxes_new_np = np.zeros((box_rows, 9), dtype=np.int32)
+        boxes_new_np[:boxes_per.shape[0], :] = boxes_per
+        boxes_list.append(boxes_new_np)
+    boxes = np.array(boxes_list)
+    # with open('未命名文件夹/all_boxes_rec.txt','w') as f:
+    with open('/data2/liudan/ocr/data/test/test_boxes_rec.txt', 'w') as f:
+        boxes = boxes.flatten()
+        for i in boxes:
+            f.write(str(i) + '\t')
+
+    # file_list = os.listdir(save_path)
+    # for name in file_list:
+    #     f1.write(os.path.join(save_path, name + '\n'))
+    f1.close()
+    f2.close()
+
+    ff1 = open('/data2/liudan/ocr/data/test/test_img_path_rec.txt', 'r')
+    alllines = ff1.readlines()
+    ff1.close()
+    ff1 = open('/data2/liudan/ocr/data/test/test_img_path_rec.txt', 'w+')
+    for eachline in alllines:
+        a = re.sub('/data2/liudan/ocr/data/val', save_path, eachline)
+        ff1.writelines(a)
+    ff1.close()
+
+
+if __name__ == "__main__":
+    data_to_test()

+ 30 - 0
code_ocr/eval.sh

@@ -0,0 +1,30 @@
+#!/bin/bash
+
+
+
+train_code_path='/data2/liudan/ocr/code_ocr'
+test_code_path='/data2/liudan/ocr/code_ocr/test'
+
+cd $train_code_path
+
+python train_data.py # 参考train_data.py内容
+
+echo '=========train_data已完成========='
+
+
+cd $test_code_path
+
+python eval_file_200.py # 预测
+
+echo '=========eval已完成========='
+
+sleep 10s
+
+python score_the_ocr_model.py # 查找最好的权重模型,保存best.pb
+
+echo '=========score_the_ocr已完成========='
+
+
+
+
+

+ 220 - 0
code_ocr/eval_file_200.py

@@ -0,0 +1,220 @@
+# coding=utf-8
+import cv2
+import math
+import os
+os.environ['CUDA_VISIBLE_DEVICES'] = '1'
+import numpy as np
+import tensorflow as tf
+import nms_locality
+from icdar import restore_rectangle, ground_truth_to_word
+
+font = cv2.FONT_HERSHEY_SIMPLEX
+save_str_hegd = '/data2/hejinlong/model_saved/FOTS-master/outputs/宝安机场/'
+test_dataset_msg_path = 'test_dataset_msg/宝安机场/all_img_path_rec.txt'
+
+saved_model_list = 'saved_model_list.txt'
+imgs_list = []
+with open(test_dataset_msg_path,'r') as f:
+    file_contents = f.readlines()
+    for i in file_contents:
+        imgs_list.append(i.strip())
+
+def detect(score_map, geo_map, score_map_thresh=0.8, box_thresh=0.1, nms_thres=0.2):
+    if len(score_map.shape) == 4:
+        score_map = score_map[0, :, :, 0]
+        geo_map = geo_map[0, :, :, ]
+    xy_text = np.argwhere(score_map > score_map_thresh)
+    xy_text = xy_text[np.argsort(xy_text[:, 0])]
+    text_box_restored = restore_rectangle(xy_text[:, ::-1]*4, geo_map[xy_text[:, 0], xy_text[:, 1], :]) # N*4*2
+    boxes = np.zeros((text_box_restored.shape[0], 9), dtype=np.float32)
+    boxes[:, :8] = text_box_restored.reshape((-1, 8))
+    boxes[:, 8] = score_map[xy_text[:, 0], xy_text[:, 1]]
+    boxes = nms_locality.nms_locality(boxes.astype('float32'), nms_thres)
+    if boxes.shape[0] == 0:
+        return None
+    #  low score boxes by the average score map, this is different from the orginal paper
+    for i, box in enumerate(boxes):
+        mask = np.zeros_like(score_map, dtype=np.uint8)
+        cv2.fillPoly(mask, box[:8].reshape((-1, 4, 2)).astype(np.int32) // 4, 1)
+        boxes[i, 8] = cv2.mean(score_map, mask)[0]
+    boxes = boxes[boxes[:, 8] > box_thresh]
+
+    return boxes
+
+def get_project_matrix_and_width(text_polyses, target_height=8.0):
+    project_matrixes = []
+    box_widths = []
+    filter_box_masks = []
+    for i in range(text_polyses.shape[0]):
+        x1, y1, x2, y2, x3, y3, x4, y4 = text_polyses[i] / 4
+        rotated_rect = cv2.minAreaRect(np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]]))
+        box_w, box_h = rotated_rect[1][0], rotated_rect[1][1]
+        if box_w <= box_h:
+            box_w, box_h = box_h, box_w
+        mapped_x1, mapped_y1 = (0, 0)
+        mapped_x4, mapped_y4 = (0, 8)
+        width_box = math.ceil(8 * box_w / box_h)
+        width_box = int(min(width_box, 128)) # not to exceed feature map's width
+        mapped_x2, mapped_y2 = (width_box, 0)
+        src_pts = np.float32([(x1, y1), (x2, y2), (x4, y4)])
+        dst_pts = np.float32([(mapped_x1, mapped_y1), (mapped_x2, mapped_y2), (mapped_x4, mapped_y4)])
+        affine_matrix = cv2.getAffineTransform(dst_pts.astype(np.float32), src_pts.astype(np.float32))
+        affine_matrix = affine_matrix.flatten()
+        project_matrixes.append(affine_matrix)
+        box_widths.append(width_box)
+    project_matrixes = np.array(project_matrixes)
+    box_widths = np.array(box_widths)
+    return project_matrixes, box_widths
+
+def sort_poly(p):
+    min_axis = np.argmin(np.sum(p, axis=1))
+    p = p[[min_axis, (min_axis+1)%4, (min_axis+2)%4, (min_axis+3)%4]]
+    if abs(p[0, 0] - p[1, 0]) > abs(p[0, 1] - p[1, 1]):
+        return p
+    else:
+        return p[[0, 3, 2, 1]]
+
+def test_all_the_model(model_path_list=['']):
+    for i in model_path_list:
+        test_200(i)
+
+def run(thred_num=10):
+    from multiprocessing import Pool
+    model_path_list = []
+    with open(saved_model_list,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            model_path_list.append(i.strip())
+    length = len(model_path_list)
+    len_per_pool = length // thred_num
+    count = 0
+    model_path_list_tmp = []
+    fedd_list = []
+    for i in model_path_list:
+        model_path_list_tmp.append(i)
+        count += 1
+        if count == len_per_pool:
+            count = 0
+            fedd_list.append(model_path_list_tmp)
+            model_path_list_tmp = []
+    if count != 0:
+        fedd_list.append(model_path_list_tmp)
+    pool = Pool(thred_num+1)
+    pool.map(test_all_the_model,fedd_list)
+    pool.close()
+    pool.join()
+
+def test_200(running_model_path):
+    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
+        input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images')
+        input_transform_matrix = tf.placeholder(tf.float32, shape=[None, 6], name='input_transform_matrix')
+        input_box_mask = []
+        input_box_mask.append(tf.placeholder(tf.int32, shape=[None], name='input_box_masks_0'))
+        input_box_widths = tf.placeholder(tf.int32, shape=[None], name='input_box_widths')
+        with open(running_model_path, 'rb') as f:
+            graph_def = tf.GraphDef()
+            graph_def.ParseFromString(f.read())
+            output1 = tf.import_graph_def(graph_def,
+                input_map={'input_images:0': input_images},
+                return_elements=['feature_fusion/Conv_7/Sigmoid:0','feature_fusion/concat_3:0']
+            )
+            output2 = tf.import_graph_def(
+                graph_def,
+                input_map={'input_images:0': input_images,\
+                    'input_transform_matrix:0':input_transform_matrix,\
+                    'input_box_masks_0:0':input_box_mask[0],\
+                    'input_box_widths:0':input_box_widths
+                },
+                return_elements=['SparseToDense:0']
+            )
+        im_fn_list = imgs_list
+        input_size = 512
+        file_hegd = open('001.txt','w')
+        for idx,im_fn in enumerate(im_fn_list):
+            im = cv2.imread(im_fn)[:, :, ::-1]
+            new_h, new_w, _ = im.shape
+            # h_ratio_hegd,w_ratio_hegd = 1.,1.
+            # max_h_w_i = np.max([new_h, new_w, input_size])
+            # im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
+            # im_padded[:new_h, :new_w, :] = im.copy()
+            # if max_h_w_i == input_size:
+            #     im = im_padded.copy()
+            #     # if new_h > new_w:
+            #     #     im = cv2.resize(im, (round(new_w*512/new_h),512))
+            #     #     new_h_hegd,new_w_hegd,_ = im.shape
+            #     #     im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            #     #     im_padded[:new_h_hegd, :new_w_hegd, :] = im.copy()
+            #     #     im = im_padded
+            #     #     h_ratio_hegd,w_ratio_hegd = 512.0/new_h,512.0/new_h
+            #     # else:
+            #     #     im = cv2.resize(im, (512,round(new_h*512/new_w)))
+            #     #     new_h_hegd,new_w_hegd,_ = im.shape
+            #     #     im_padded = np.zeros((512, 512, 3), dtype=np.uint8)
+            #     #     im_padded[:new_h_hegd, :new_w_hegd, :] = im.copy()
+            #     #     im = im_padded
+            #     #     h_ratio_hegd,w_ratio_hegd = 512.0/new_w,512.0/new_w
+            # else:
+            #     im = cv2.resize(im_padded, dsize=(input_size, input_size))
+            #     h_ratio_hegd,w_ratio_hegd = input_size/max_h_w_i,input_size/max_h_w_i
+            
+            score, geometry = sess.run(output1, feed_dict={input_images: [im]})
+            boxes = detect(score_map=score, geo_map=geometry)
+            # save to file
+            if boxes is not None and boxes.shape[0] != 0:
+                output_dir_hegd = save_str_hegd+running_model_path.strip().split('/')[-2]+'/'+running_model_path.strip().split('/')[-1].split('.')[0]
+                if not os.path.exists(output_dir_hegd):
+                    os.makedirs(output_dir_hegd)
+                res_file = os.path.join(
+                    output_dir_hegd,
+                    'res_{}.txt'.format(idx))
+
+                input_roi_boxes = boxes[:, :8].reshape(-1, 8)
+                boxes_masks = [int(0)] * input_roi_boxes.shape[0]
+                transform_matrixes, box_widths = get_project_matrix_and_width(input_roi_boxes)
+                try:
+                    recog_decode = sess.run(output2, feed_dict={input_images: [im], \
+                        input_transform_matrix: transform_matrixes,\
+                        input_box_mask[0]: boxes_masks,\
+                        input_box_widths: box_widths})[0]
+                except:
+                    print(transform_matrixes.shape,boxes_masks,box_widths.shape)
+                    with open(res_file, 'w') as f:
+                        f.write('')
+                    continue
+                # Preparing for draw boxes
+                boxes = boxes[:, :8].reshape((-1, 4, 2))
+                if recog_decode.shape[0] != boxes.shape[0]:
+                    print("detection and recognition result are not equal!")
+                    exit(-1)
+                with open(res_file, 'w') as f:
+                    for i, box in enumerate(boxes):
+                        # to avoid submitting errors
+                        box = sort_poly(box.astype(np.int32))
+                        # box[...,0],box[...,1] = box[...,0]/h_ratio_hegd,box[...,1]/w_ratio_hegd
+                        if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3]-box[0]) < 5:
+                            continue
+                        if np.any(box[...,0]>new_w*1.1) or np.any(box[...,1]>new_h*1.1) or np.any(box[...,0]<-0.1*new_w) or np.any(box[...,1]<-0.1*new_h):
+                            continue
+                        recognition_result = ground_truth_to_word(recog_decode[i])
+                        for ii in range(box.shape[0]):
+                            for jj in range(box.shape[1]):
+                                box[ii,jj] = round(box[ii,jj])
+                        box = box.astype(np.int32)
+                        f.write('{},{},{},{},{},{},{},{},{}\r\n'.format(
+                            box[0, 0], box[0, 1], box[1, 0], box[1, 1],\
+                            box[2, 0], box[2, 1], box[3, 0], box[3, 1],\
+                            recognition_result
+                        ))
+            else:
+                output_dir_hegd = save_str_hegd+running_model_path.strip().split('/')[-2]+'/'+running_model_path.strip().split('/')[-1].split('.')[0]
+                if not os.path.exists(output_dir_hegd):
+                    os.makedirs(output_dir_hegd)
+                res_file = os.path.join(
+                    output_dir_hegd,
+                    'res_{}.txt'.format(idx))
+                with open(res_file, 'w') as f:
+                    f.write('')
+            file_hegd.write(output_dir_hegd)
+        file_hegd.close()
+if __name__ == '__main__':
+    run(1)

+ 31 - 0
code_ocr/ocr_dataset.py

@@ -0,0 +1,31 @@
+'''
+Author:       hejinlong
+LastEditTime: 2021-03-08 09:15:27
+Description:  hegd@mail.ustc.edu.cn
+'''
+# -*- coding: utf-8 -*-
+import numpy as np 
+    
+def simple_load_np_dataset(root_path = '/data2/liudan/ocr/data/ocr_txt/'):
+    img_list_rec_file = root_path + 'val_img_path_rec.txt'
+    boxes_rec_file = root_path + 'val_boxes_rec.txt'
+    text_tag_rec_file = root_path + 'val_text_tag_rec.txt'
+    all_img_path = []
+    with open(img_list_rec_file,"r") as f:
+        file_content = f.readlines()
+        for i in file_content:
+            all_img_path.append(i.strip())
+    with open(boxes_rec_file,'r') as f:
+        file_content = f.read().strip()
+        num_list = file_content.split('\t')
+    boxes_flatten = []
+    for i in num_list:
+        boxes_flatten.append(int(i))
+    boxes_flatten = np.array(boxes_flatten)
+    boxes = boxes_flatten.reshape((len(all_img_path),-1, 9))
+    with open(text_tag_rec_file,'r') as f:
+        all_text_tags = []
+        file_content = f.readlines()
+        for i in file_content:
+            all_text_tags.append(i.split('\t**hegd**\t')[:-1])
+    return all_img_path,boxes,all_text_tags

+ 40 - 0
code_ocr/params.py

@@ -0,0 +1,40 @@
+import os
+
+total_path = '/data2/liudan/ocr/'
+
+
+# 删除已存在文件路径
+path_data = os.path.join(total_path, 'data')
+
+
+
+# 备份total_data路径
+old_file_path = os.path.join(total_path, 'field_data')
+new_file_path = os.path.join(total_path, 'data/total_data')
+
+
+# 划分数据集路径
+dataset_path = os.path.join(total_path, 'data/total_data')
+train_set_save_path = os.path.join(total_path, 'data/train')
+val_set_save_path = os.path.join(total_path, 'data/val')
+
+
+
+# batch_process路径
+dir = os.path.join(total_path, 'data')
+
+
+# multigpu_train.py路径
+train_path = '/data2/liudan/ocr/model_saved/cscs/'
+# pretrained_model_path = os.path.join(train_path, 'pretrain_model/model.ckpt-103336')
+train_save_path = os.path.join(train_path, '09-01') # train得到的数据位置(.index, .meta, .data),这里表示save_pb_model的输入
+pretrained_model_path1 = os.path.join(train_path, '09-01')
+save_pb_path = os.path.join(train_path, '0902/') # .pb文件位置
+save_str_hegd_path = os.path.join(train_path, '/evalsave/0902')
+
+# score_the_ocr_model中测试数据位置
+testdat_msg = os.path.join(total_path, 'data/ocr_txt/')
+eval_saved = os.path.join(train_path, '/evalsave/0902')
+
+
+

+ 22 - 0
code_ocr/remove_data.py

@@ -0,0 +1,22 @@
+import os
+import shutil
+import os
+from params import *
+
+
+
+def copytree(old_file_path,new_file_path):
+    if os.path.exists(new_file_path):
+        try:
+            for i in os.listdir(new_file_path):
+                txt_path = os.path.join(new_file_path, i)
+                os.remove(txt_path)
+            os.rmdir(new_file_path)
+        except Exception as result:
+            print("报错:%s" % result)
+    shutil.copytree(old_file_path, new_file_path)
+
+if __name__ == '__main__':
+    copytree(old_file_path,new_file_path)
+
+

+ 1 - 0
code_ocr/saved_model_list.txt

@@ -0,0 +1 @@
+ocr_model-0902-1.pb

+ 71 - 0
code_ocr/split_train_val.py

@@ -0,0 +1,71 @@
+import os
+import shutil
+from sklearn.model_selection import train_test_split
+from params import *
+
+
+
+
+def split_train_val(dataset_path,train_set_save_path, val_set_save_path):
+    def mkdir(path):
+        folder = os.path.exists(path)
+        if not folder:
+            os.makedirs(path)
+
+
+
+    mkdir(train_set_save_path)
+    mkdir(val_set_save_path)
+
+    file_pathes = os.listdir(dataset_path)
+    # 获取文件夹下所有 jpg 格式的图像的名称(不包含后缀名)
+    img_names = []
+    for file_path in file_pathes:
+        if os.path.splitext(file_path)[1] == ".jpg":
+            file_name = os.path.splitext(file_path)[0]
+            img_names.append(file_name)
+
+    # 划分训练集和验证集
+    train_set, val_set = train_test_split(img_names, test_size=0.1, random_state=42)
+
+    # 训练集处理:将图像和标签文件移动到目标文件夹
+    for file_name in train_set:
+        img_src_path = os.path.join(dataset_path, file_name+".jpg")
+        img_dst_path = os.path.join(train_set_save_path, file_name+".jpg")
+        shutil.copyfile(img_src_path, img_dst_path)
+
+        json_src_path = os.path.join(dataset_path, file_name+".json")
+        json_dst_path = os.path.join(train_set_save_path, file_name+".json")
+        shutil.copyfile(json_src_path, json_dst_path)
+
+    # 验证集处理:将图像和标签文件移动到目标文件夹
+    for file_name in val_set:
+        img_src_path = os.path.join(dataset_path, file_name+".jpg")
+        img_dst_path = os.path.join(val_set_save_path, file_name+".jpg")
+        shutil.copyfile(img_src_path, img_dst_path)
+
+        json_src_path = os.path.join(dataset_path, file_name+".json")
+        json_dst_path = os.path.join(val_set_save_path, file_name+".json")
+        shutil.copyfile(json_src_path, json_dst_path)
+        
+    # 将训练集数据按10:1随机再划分到测试集
+    trainfile_paths = os.listdir('/data2/liudan/ocr/data/train/')
+    train_set_list = []
+    for trainfile_path in trainfile_paths:
+        if os.path.splitext(trainfile_path)[1] == ".jpg":
+            file_name1 = os.path.splitext(trainfile_path)[0]
+            train_set_list.append(file_name1)
+    train_set1,val_set1 = train_test_split(train_set_list, test_size=0.1, random_state=42)
+
+    for file_name1 in val_set1:
+        img_src_path = os.path.join(train_set_save_path, file_name1+".jpg")
+        img_dst_path = os.path.join(val_set_save_path, file_name1+".jpg")
+        shutil.copyfile(img_src_path, img_dst_path)
+
+        json_src_path = os.path.join(train_set_save_path, file_name1+".json")
+        json_dst_path = os.path.join(val_set_save_path, file_name1+".json")
+        shutil.copyfile(json_src_path, json_dst_path)
+
+
+if __name__=="__main__":
+    split_train_val(dataset_path,train_set_save_path,val_set_save_path)

+ 0 - 0
code_ocr/test/001.txt


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
code_ocr/test/001001.txt


+ 0 - 0
code_ocr/test/__init__.py


BIN
code_ocr/test/__pycache__/__init__.cpython-36.pyc


BIN
code_ocr/test/__pycache__/bktree.cpython-36.pyc


BIN
code_ocr/test/__pycache__/config.cpython-36.pyc


BIN
code_ocr/test/__pycache__/eval_file_200.cpython-36.pyc


BIN
code_ocr/test/__pycache__/icdar.cpython-36.pyc


BIN
code_ocr/test/__pycache__/iou_mat.cpython-36.pyc


BIN
code_ocr/test/__pycache__/iou_mat.cpython-38.pyc


BIN
code_ocr/test/__pycache__/nms_locality.cpython-36.pyc


BIN
code_ocr/test/__pycache__/nms_locality.cpython-38.pyc


BIN
code_ocr/test/__pycache__/ocr_dataset.cpython-36.pyc


BIN
code_ocr/test/__pycache__/ocr_dataset.cpython-38.pyc


BIN
code_ocr/test/__pycache__/params.cpython-36.pyc


BIN
code_ocr/test/__pycache__/score_the_ocr_model.cpython-36.pyc


+ 200 - 0
code_ocr/test/all_img_path_rec.txt

@@ -0,0 +1,200 @@
+/data2/liudan/ocr/data/test_data/ningde_47000.jpg
+/data2/liudan/ocr/data/test_data/ningde_43400.jpg
+/data2/liudan/ocr/data/test_data/ningde_21200.jpg
+/data2/liudan/ocr/data/test_data/ningde_25000.jpg
+/data2/liudan/ocr/data/test_data/ningde_90600.jpg
+/data2/liudan/ocr/data/test_data/ningde_53000.jpg
+/data2/liudan/ocr/data/test_data/ningde_74800.jpg
+/data2/liudan/ocr/data/test_data/ningde_34000.jpg
+/data2/liudan/ocr/data/test_data/ningde_16800.jpg
+/data2/liudan/ocr/data/test_data/ningde_61000.jpg
+/data2/liudan/ocr/data/test_data/ningde_111200.jpg
+/data2/liudan/ocr/data/test_data/ningde_65600.jpg
+/data2/liudan/ocr/data/test_data/ningde_78800.jpg
+/data2/liudan/ocr/data/test_data/ningde_81000.jpg
+/data2/liudan/ocr/data/test_data/ningde_47400.jpg
+/data2/liudan/ocr/data/test_data/ningde_6000.jpg
+/data2/liudan/ocr/data/test_data/ningde_10000.jpg
+/data2/liudan/ocr/data/test_data/ningde_17800.jpg
+/data2/liudan/ocr/data/test_data/ningde_58800.jpg
+/data2/liudan/ocr/data/test_data/ningde_56800.jpg
+/data2/liudan/ocr/data/test_data/ningde_77000.jpg
+/data2/liudan/ocr/data/test_data/ningde_45800.jpg
+/data2/liudan/ocr/data/test_data/ningde_14800.jpg
+/data2/liudan/ocr/data/test_data/ningde_19400.jpg
+/data2/liudan/ocr/data/test_data/ningde_18400.jpg
+/data2/liudan/ocr/data/test_data/ningde_47400.jpg
+/data2/liudan/ocr/data/test_data/ningde_53200.jpg
+/data2/liudan/ocr/data/test_data/ningde_9600.jpg
+/data2/liudan/ocr/data/test_data/ningde_72000.jpg
+/data2/liudan/ocr/data/test_data/ningde_8000.jpg
+/data2/liudan/ocr/data/test_data/ningde_84400.jpg
+/data2/liudan/ocr/data/test_data/ningde_58600.jpg
+/data2/liudan/ocr/data/test_data/ningde_50800.jpg
+/data2/liudan/ocr/data/test_data/ningde_78800.jpg
+/data2/liudan/ocr/data/test_data/ningde_70400.jpg
+/data2/liudan/ocr/data/test_data/ningde_86800.jpg
+/data2/liudan/ocr/data/test_data/ningde_42200.jpg
+/data2/liudan/ocr/data/test_data/ningde_3200.jpg
+/data2/liudan/ocr/data/test_data/ningde_51000.jpg
+/data2/liudan/ocr/data/test_data/ningde_57000.jpg
+/data2/liudan/ocr/data/test_data/ningde_75400.jpg
+/data2/liudan/ocr/data/test_data/ningde_23600.jpg
+/data2/liudan/ocr/data/test_data/ningde_2600.jpg
+/data2/liudan/ocr/data/test_data/ningde_28400.jpg
+/data2/liudan/ocr/data/test_data/ningde_37600.jpg
+/data2/liudan/ocr/data/test_data/ningde_11200.jpg
+/data2/liudan/ocr/data/test_data/ningde_54200.jpg
+/data2/liudan/ocr/data/test_data/ningde_17000.jpg
+/data2/liudan/ocr/data/test_data/ningde_77400.jpg
+/data2/liudan/ocr/data/test_data/ningde_7400.jpg
+/data2/liudan/ocr/data/test_data/ningde_15400.jpg
+/data2/liudan/ocr/data/test_data/ningde_22200.jpg
+/data2/liudan/ocr/data/test_data/ningde_89000.jpg
+/data2/liudan/ocr/data/test_data/ningde_53200.jpg
+/data2/liudan/ocr/data/test_data/ningde_55600.jpg
+/data2/liudan/ocr/data/test_data/ningde_7000.jpg
+/data2/liudan/ocr/data/test_data/ningde_79600.jpg
+/data2/liudan/ocr/data/test_data/ningde_3800.jpg
+/data2/liudan/ocr/data/test_data/ningde_21600.jpg
+/data2/liudan/ocr/data/test_data/ningde_1000.jpg
+/data2/liudan/ocr/data/test_data/ningde_9200.jpg
+/data2/liudan/ocr/data/test_data/ningde_12400.jpg
+/data2/liudan/ocr/data/test_data/ningde_37600.jpg
+/data2/liudan/ocr/data/test_data/ningde_61200.jpg
+/data2/liudan/ocr/data/test_data/ningde_85600.jpg
+/data2/liudan/ocr/data/test_data/ningde_25000.jpg
+/data2/liudan/ocr/data/test_data/ningde_35200.jpg
+/data2/liudan/ocr/data/test_data/ningde_28000.jpg
+/data2/liudan/ocr/data/test_data/ningde_86000.jpg
+/data2/liudan/ocr/data/test_data/ningde_78400.jpg
+/data2/liudan/ocr/data/test_data/ningde_33800.jpg
+/data2/liudan/ocr/data/test_data/ningde_40600.jpg
+/data2/liudan/ocr/data/test_data/ningde_48400.jpg
+/data2/liudan/ocr/data/test_data/ningde_4600.jpg
+/data2/liudan/ocr/data/test_data/ningde_68600.jpg
+/data2/liudan/ocr/data/test_data/ningde_91400.jpg
+/data2/liudan/ocr/data/test_data/ningde_63200.jpg
+/data2/liudan/ocr/data/test_data/ningde_10800.jpg
+/data2/liudan/ocr/data/test_data/ningde_54800.jpg
+/data2/liudan/ocr/data/test_data/ningde_89600.jpg
+/data2/liudan/ocr/data/test_data/ningde_42600.jpg
+/data2/liudan/ocr/data/test_data/ningde_20000.jpg
+/data2/liudan/ocr/data/test_data/ningde_62400.jpg
+/data2/liudan/ocr/data/test_data/ningde_73800.jpg
+/data2/liudan/ocr/data/test_data/ningde_41800.jpg
+/data2/liudan/ocr/data/test_data/ningde_80400.jpg
+/data2/liudan/ocr/data/test_data/ningde_58000.jpg
+/data2/liudan/ocr/data/test_data/ningde_68200.jpg
+/data2/liudan/ocr/data/test_data/ningde_65000.jpg
+/data2/liudan/ocr/data/test_data/ningde_38600.jpg
+/data2/liudan/ocr/data/test_data/ningde_21800.jpg
+/data2/liudan/ocr/data/test_data/ningde_11200.jpg
+/data2/liudan/ocr/data/test_data/ningde_62800.jpg
+/data2/liudan/ocr/data/test_data/ningde_69400.jpg
+/data2/liudan/ocr/data/test_data/ningde_65600.jpg
+/data2/liudan/ocr/data/test_data/ningde_19800.jpg
+/data2/liudan/ocr/data/test_data/ningde_17800.jpg
+/data2/liudan/ocr/data/test_data/ningde_30200.jpg
+/data2/liudan/ocr/data/test_data/ningde_43200.jpg
+/data2/liudan/ocr/data/test_data/ningde_9000.jpg
+/data2/liudan/ocr/data/test_data/ningde_51600.jpg
+/data2/liudan/ocr/data/test_data/ningde_10000.jpg
+/data2/liudan/ocr/data/test_data/ningde_22600.jpg
+/data2/liudan/ocr/data/test_data/ningde_76400.jpg
+/data2/liudan/ocr/data/test_data/ningde_84600.jpg
+/data2/liudan/ocr/data/test_data/ningde_8600.jpg
+/data2/liudan/ocr/data/test_data/ningde_6400.jpg
+/data2/liudan/ocr/data/test_data/ningde_37400.jpg
+/data2/liudan/ocr/data/test_data/ningde_50200.jpg
+/data2/liudan/ocr/data/test_data/ningde_55600.jpg
+/data2/liudan/ocr/data/test_data/ningde_1200.jpg
+/data2/liudan/ocr/data/test_data/ningde_60000.jpg
+/data2/liudan/ocr/data/test_data/ningde_79600.jpg
+/data2/liudan/ocr/data/test_data/ningde_35000.jpg
+/data2/liudan/ocr/data/test_data/ningde_13200.jpg
+/data2/liudan/ocr/data/test_data/ningde_85400.jpg
+/data2/liudan/ocr/data/test_data/ningde_91400.jpg
+/data2/liudan/ocr/data/test_data/ningde_80000.jpg
+/data2/liudan/ocr/data/test_data/ningde_66800.jpg
+/data2/liudan/ocr/data/test_data/ningde_600.jpg
+/data2/liudan/ocr/data/test_data/ningde_78000.jpg
+/data2/liudan/ocr/data/test_data/ningde_20200.jpg
+/data2/liudan/ocr/data/test_data/ningde_27400.jpg
+/data2/liudan/ocr/data/test_data/ningde_83600.jpg
+/data2/liudan/ocr/data/test_data/ningde_36600.jpg
+/data2/liudan/ocr/data/test_data/ningde_16000.jpg
+/data2/liudan/ocr/data/test_data/ningde_50400.jpg
+/data2/liudan/ocr/data/test_data/ningde_79600.jpg
+/data2/liudan/ocr/data/test_data/ningde_2600.jpg
+/data2/liudan/ocr/data/test_data/ningde_74000.jpg
+/data2/liudan/ocr/data/test_data/ningde_26000.jpg
+/data2/liudan/ocr/data/test_data/ningde_18800.jpg
+/data2/liudan/ocr/data/test_data/ningde_30000.jpg
+/data2/liudan/ocr/data/test_data/ningde_2800.jpg
+/data2/liudan/ocr/data/test_data/ningde_75400.jpg
+/data2/liudan/ocr/data/test_data/ningde_7600.jpg
+/data2/liudan/ocr/data/test_data/ningde_65200.jpg
+/data2/liudan/ocr/data/test_data/ningde_1800.jpg
+/data2/liudan/ocr/data/test_data/ningde_90400.jpg
+/data2/liudan/ocr/data/test_data/ningde_77200.jpg
+/data2/liudan/ocr/data/test_data/ningde_67000.jpg
+/data2/liudan/ocr/data/test_data/ningde_69200.jpg
+/data2/liudan/ocr/data/test_data/ningde_22800.jpg
+/data2/liudan/ocr/data/test_data/ningde_64600.jpg
+/data2/liudan/ocr/data/test_data/ningde_85800.jpg
+/data2/liudan/ocr/data/test_data/ningde_63800.jpg
+/data2/liudan/ocr/data/test_data/ningde_70600.jpg
+/data2/liudan/ocr/data/test_data/ningde_65600.jpg
+/data2/liudan/ocr/data/test_data/ningde_74200.jpg
+/data2/liudan/ocr/data/test_data/ningde_65200.jpg
+/data2/liudan/ocr/data/test_data/ningde_19000.jpg
+/data2/liudan/ocr/data/test_data/ningde_87000.jpg
+/data2/liudan/ocr/data/test_data/ningde_22000.jpg
+/data2/liudan/ocr/data/test_data/ningde_89000.jpg
+/data2/liudan/ocr/data/test_data/ningde_64000.jpg
+/data2/liudan/ocr/data/test_data/ningde_19200.jpg
+/data2/liudan/ocr/data/test_data/ningde_81800.jpg
+/data2/liudan/ocr/data/test_data/ningde_47400.jpg
+/data2/liudan/ocr/data/test_data/ningde_1600.jpg
+/data2/liudan/ocr/data/test_data/ningde_65600.jpg
+/data2/liudan/ocr/data/test_data/ningde_13800.jpg
+/data2/liudan/ocr/data/test_data/ningde_12800.jpg
+/data2/liudan/ocr/data/test_data/ningde_6600.jpg
+/data2/liudan/ocr/data/test_data/ningde_31800.jpg
+/data2/liudan/ocr/data/test_data/ningde_60800.jpg
+/data2/liudan/ocr/data/test_data/ningde_39600.jpg
+/data2/liudan/ocr/data/test_data/ningde_60600.jpg
+/data2/liudan/ocr/data/test_data/ningde_10600.jpg
+/data2/liudan/ocr/data/test_data/ningde_45200.jpg
+/data2/liudan/ocr/data/test_data/ningde_73000.jpg
+/data2/liudan/ocr/data/test_data/ningde_47200.jpg
+/data2/liudan/ocr/data/test_data/ningde_81600.jpg
+/data2/liudan/ocr/data/test_data/ningde_19800.jpg
+/data2/liudan/ocr/data/test_data/ningde_77000.jpg
+/data2/liudan/ocr/data/test_data/ningde_13000.jpg
+/data2/liudan/ocr/data/test_data/ningde_20200.jpg
+/data2/liudan/ocr/data/test_data/ningde_84400.jpg
+/data2/liudan/ocr/data/test_data/ningde_81400.jpg
+/data2/liudan/ocr/data/test_data/ningde_6600.jpg
+/data2/liudan/ocr/data/test_data/ningde_48000.jpg
+/data2/liudan/ocr/data/test_data/ningde_50400.jpg
+/data2/liudan/ocr/data/test_data/ningde_77200.jpg
+/data2/liudan/ocr/data/test_data/ningde_28800.jpg
+/data2/liudan/ocr/data/test_data/ningde_7600.jpg
+/data2/liudan/ocr/data/test_data/ningde_58200.jpg
+/data2/liudan/ocr/data/test_data/ningde_45600.jpg
+/data2/liudan/ocr/data/test_data/ningde_49400.jpg
+/data2/liudan/ocr/data/test_data/ningde_89400.jpg
+/data2/liudan/ocr/data/test_data/ningde_54400.jpg
+/data2/liudan/ocr/data/test_data/ningde_25000.jpg
+/data2/liudan/ocr/data/test_data/ningde_65200.jpg
+/data2/liudan/ocr/data/test_data/ningde_88000.jpg
+/data2/liudan/ocr/data/test_data/ningde_8600.jpg
+/data2/liudan/ocr/data/test_data/ningde_51200.jpg
+/data2/liudan/ocr/data/test_data/ningde_29800.jpg
+/data2/liudan/ocr/data/test_data/ningde_35200.jpg
+/data2/liudan/ocr/data/test_data/ningde_24400.jpg
+/data2/liudan/ocr/data/test_data/ningde_16400.jpg
+/data2/liudan/ocr/data/test_data/ningde_27800.jpg
+/data2/liudan/ocr/data/test_data/ningde_71800.jpg

+ 108 - 0
code_ocr/test/bktree.py

@@ -0,0 +1,108 @@
+"""
+
+This module implements Burkhard-Keller Trees (bk-tree).  bk-trees
+allow fast lookup of words that lie within a specified distance of a
+query word.  For example, this might be used by a spell checker to
+find near matches to a mispelled word.
+
+The implementation is based on the description in this article:
+
+http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees
+
+Licensed under the PSF license: http://www.python.org/psf/license/
+
+- Adam Hupp <adam@hupp.org>
+
+"""
+class BKTree:
+    def __init__(self, distfn, words):
+        """
+        Create a new BK-tree from the given distance function and
+        words.
+        
+        Arguments:
+
+        distfn: a binary function that returns the distance between
+        two words.  Return value is a non-negative integer.  the
+        distance function must be a metric space.
+        
+        words: an iterable.  produces values that can be passed to
+        distfn
+        
+        """
+        self.distfn = distfn
+
+        it = iter(words)
+        root = next(it)
+        self.tree = (root, {})
+
+        for i in it:
+            self._add_word(self.tree, i)
+
+    def _add_word(self, parent, word):
+        pword, children = parent
+        d = self.distfn(word, pword)
+        if d in children:
+            self._add_word(children[d], word)
+        else:
+            children[d] = (word, {})
+
+    def query(self, word, n):
+        """
+        Return all words in the tree that are within a distance of `n'
+        from `word`.  
+
+        Arguments:
+        
+        word: a word to query on
+
+        n: a non-negative integer that specifies the allowed distance
+        from the query word.  
+        
+        Return value is a list of tuples (distance, word), sorted in
+        ascending order of distance.
+        
+        """
+        def rec(parent):
+            pword, children = parent
+            d = self.distfn(word, pword)
+            results = []
+            if d <= n:
+                results.append( (d, pword) )
+                
+            for i in range(d-n, d+n+1):
+                child = children.get(i)
+                if child is not None:
+                    results.extend(rec(child))
+            return results
+
+        # sort by distance
+        return sorted(rec(self.tree))
+
+# http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance#Python
+def levenshtein(s, t):
+    m, n = len(s), len(t)
+    d = [range(n+1)]
+    d += [[i] for i in range(1,m+1)]
+    for i in range(0,m):
+        for j in range(0,n):
+            cost = 1
+            if s[i] == t[j]: cost = 0
+
+            d[i+1].append( min(d[i][j+1]+1, # deletion
+                               d[i+1][j]+1, #insertion
+                               d[i][j]+cost) #substitution
+                           )
+    return d[m][n]
+
+
+def dict_words(dictfile="/usr/share/dict/american-english"):
+    "Return an iterator that produces words in the given dictionary."
+    with open(dictfile,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            yield i.strip()
+
+if __name__ == "__main__":
+    pass
+

+ 3 - 0
code_ocr/test/config.py

@@ -0,0 +1,3 @@
+# coding:utf-8
+CHAR_VECTOR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-~`<>'.:;^/|!?$%#@&*()[]{}_+=,\\\""
+NUM_CLASSES = len(CHAR_VECTOR) + 1

+ 239 - 0
code_ocr/test/eval_file_200.py

@@ -0,0 +1,239 @@
+# coding=utf-8
+import cv2
+import math
+import os
+
+os.environ['CUDA_VISIBLE_DEVICES'] = '2'
+import numpy as np
+import tensorflow as tf
+# from test import nms_locality
+import nms_locality
+# from test.icdar import restore_rectangle, ground_truth_to_word
+from icdar import restore_rectangle, ground_truth_to_word
+
+
+font = cv2.FONT_HERSHEY_SIMPLEX
+# save_str_hegd = '/data2/liudan/ocr/model_saved/cscs/evalsave/0902'
+# save_str_hegd = '/data2/liudan/ocr/savemode/eval/08002'
+save_str_hegd = '/data2/liudan/ocr/savemode/eval/'
+# test_dataset_msg_path = 'test_dataset_msg/宁德二期/all_img_path_rec.txt'
+# test_dataset_msg_path = '/data2/liudan/ocr/data/test2/all_img_path_rec.txt'
+# test_dataset_msg_path = '/data2/liudan/ocr/data/test3/train_img_path_rec.txt'
+test_dataset_msg_path = '/data2/liudan/ocr/data/test/test_img_path_rec.txt'
+
+# saved_model_list = 'saved_model_list.txt'
+# imgs_list = []
+# with open(test_dataset_msg_path, 'r', encoding='UTF-8') as f:
+#     file_contents = f.readlines()
+#     for i in file_contents:
+#         imgs_list.append(i.strip())
+
+
+def detect(score_map, geo_map, score_map_thresh=0.8, box_thresh=0.1, nms_thres=0.2):
+    if len(score_map.shape) == 4:
+        score_map = score_map[0, :, :, 0]
+        geo_map = geo_map[0, :, :, ]
+    xy_text = np.argwhere(score_map > score_map_thresh)
+    xy_text = xy_text[np.argsort(xy_text[:, 0])]
+    text_box_restored = restore_rectangle(xy_text[:, ::-1] * 4, geo_map[xy_text[:, 0], xy_text[:, 1], :])  # N*4*2
+    boxes = np.zeros((text_box_restored.shape[0], 9), dtype=np.float32)
+    boxes[:, :8] = text_box_restored.reshape((-1, 8))
+    boxes[:, 8] = score_map[xy_text[:, 0], xy_text[:, 1]]
+    boxes = nms_locality.nms_locality(boxes.astype('float32'), nms_thres)
+    if boxes.shape[0] == 0:
+        return None
+    #  low score boxes by the average score map, this is different from the orginal paper
+    for i, box in enumerate(boxes):
+        mask = np.zeros_like(score_map, dtype=np.uint8)
+        cv2.fillPoly(mask, box[:8].reshape((-1, 4, 2)).astype(np.int32) // 4, 1)
+        boxes[i, 8] = cv2.mean(score_map, mask)[0]
+    boxes = boxes[boxes[:, 8] > box_thresh]
+
+    return boxes
+
+
+def get_project_matrix_and_width(text_polyses, target_height=8.0):
+    project_matrixes = []
+    box_widths = []
+    filter_box_masks = []
+    for i in range(text_polyses.shape[0]):
+        x1, y1, x2, y2, x3, y3, x4, y4 = text_polyses[i] / 4
+        rotated_rect = cv2.minAreaRect(np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]]))
+        box_w, box_h = rotated_rect[1][0], rotated_rect[1][1]
+        if box_w <= box_h:
+            box_w, box_h = box_h, box_w
+        mapped_x1, mapped_y1 = (0, 0)
+        mapped_x4, mapped_y4 = (0, 8)
+        width_box = math.ceil(8 * box_w / box_h)
+        width_box = int(min(width_box, 128))  # not to exceed feature map's width
+        mapped_x2, mapped_y2 = (width_box, 0)
+        src_pts = np.float32([(x1, y1), (x2, y2), (x4, y4)])
+        dst_pts = np.float32([(mapped_x1, mapped_y1), (mapped_x2, mapped_y2), (mapped_x4, mapped_y4)])
+        affine_matrix = cv2.getAffineTransform(dst_pts.astype(np.float32), src_pts.astype(np.float32))
+        affine_matrix = affine_matrix.flatten()
+        project_matrixes.append(affine_matrix)
+        box_widths.append(width_box)
+    project_matrixes = np.array(project_matrixes)
+    box_widths = np.array(box_widths)
+    return project_matrixes, box_widths
+
+
+def sort_poly(p):
+    min_axis = np.argmin(np.sum(p, axis=1))
+    p = p[[min_axis, (min_axis + 1) % 4, (min_axis + 2) % 4, (min_axis + 3) % 4]]
+    if abs(p[0, 0] - p[1, 0]) > abs(p[0, 1] - p[1, 1]):
+        return p
+    else:
+        return p[[0, 3, 2, 1]]
+
+
+def test_all_the_model(model_path_list=['']):
+    # print(1111111111)
+    for i in model_path_list:
+        # print(2222222222)
+        test_200(i)
+
+
+def run(thred_num=10):
+    from multiprocessing import Pool
+    model_path_list = []
+    # pb_path = '/data2/liudan/ocr/model_saved/cscs/09022/'
+    pb_path = '/data2/liudan/ocr/savemode/0801/'
+    pb_list = os.listdir(pb_path)
+    model_path_list = []
+    for name in pb_list:
+        filename, shuffix = os.path.splitext(name)
+        if (shuffix == '.pb'):
+            model_path_list.append(os.path.join(pb_path, name))
+    # with open(saved_model_list, 'r') as f:
+    #     file_contents = f.readlines()
+    #     for i in file_contents:
+    #         model_path_list.append(i.strip())
+    length = len(model_path_list)
+    len_per_pool = length // thred_num
+    count = 0
+    model_path_list_tmp = []
+    fedd_list = []
+    for i in model_path_list:
+        model_path_list_tmp.append(i)
+        count += 1
+        if count == len_per_pool:
+            count = 0
+            fedd_list.append(model_path_list_tmp)
+            model_path_list_tmp = []
+    if count != 0:
+        fedd_list.append(model_path_list_tmp)
+    pool = Pool(thred_num + 1)
+    pool.map(test_all_the_model, fedd_list)
+    pool.close()
+    pool.join()
+
+
+def test_200(running_model_path):
+    # print(444444444444)
+    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
+        # print(333333)
+        input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images')
+        input_transform_matrix = tf.placeholder(tf.float32, shape=[None, 6], name='input_transform_matrix')
+        input_box_mask = []
+        input_box_mask.append(tf.placeholder(tf.int32, shape=[None], name='input_box_masks_0'))
+        input_box_widths = tf.placeholder(tf.int32, shape=[None], name='input_box_widths')
+        with open(running_model_path, 'rb') as f:
+            graph_def = tf.GraphDef()
+            graph_def.ParseFromString(f.read())
+            output1 = tf.import_graph_def(graph_def,
+                                          input_map={'input_images:0': input_images},
+                                          return_elements=['feature_fusion/Conv_7/Sigmoid:0',
+                                                           'feature_fusion/concat_3:0']
+                                          )
+            output2 = tf.import_graph_def(
+                graph_def,
+                input_map={'input_images:0': input_images, \
+                           'input_transform_matrix:0': input_transform_matrix, \
+                           'input_box_masks_0:0': input_box_mask[0], \
+                           'input_box_widths:0': input_box_widths
+                           },
+                return_elements=['SparseToDense:0']
+            )
+        # print(777777777)
+
+        imgs_list = []
+        with open(test_dataset_msg_path, 'r', encoding='UTF-8') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                imgs_list.append(i.strip())
+
+        im_fn_list = imgs_list
+        input_size = 512
+        file_hegd = open('001.txt', 'w')
+        for idx, im_fn in enumerate(im_fn_list):
+            im = cv2.imread(im_fn)[:, :, ::-1]
+            new_h, new_w, _ = im.shape
+
+
+            score, geometry = sess.run(output1, feed_dict={input_images: [im]})
+            boxes = detect(score_map=score, geo_map=geometry)
+            # save to file
+            if boxes is not None and boxes.shape[0] != 0:
+                output_dir_hegd = save_str_hegd + running_model_path.strip().split('/')[-2] + '/' + \
+                                  running_model_path.strip().split('/')[-1].split('.')[0]
+                if not os.path.exists(output_dir_hegd):
+                    os.makedirs(output_dir_hegd)
+                res_file = os.path.join(
+                    output_dir_hegd,
+                    'res_{}.txt'.format(idx))
+
+                input_roi_boxes = boxes[:, :8].reshape(-1, 8)
+                boxes_masks = [int(0)] * input_roi_boxes.shape[0]
+                transform_matrixes, box_widths = get_project_matrix_and_width(input_roi_boxes)
+                try:
+                    recog_decode = sess.run(output2, feed_dict={input_images: [im], \
+                                                                input_transform_matrix: transform_matrixes, \
+                                                                input_box_mask[0]: boxes_masks, \
+                                                                input_box_widths: box_widths})[0]
+                except:
+                    print(transform_matrixes.shape, boxes_masks, box_widths.shape)
+                    with open(res_file, 'w') as f:
+                        f.write('')
+                    continue
+                # Preparing for draw boxes
+                boxes = boxes[:, :8].reshape((-1, 4, 2))
+                if recog_decode.shape[0] != boxes.shape[0]:
+                    print("detection and recognition result are not equal!")
+                    exit(-1)
+                with open(res_file, 'w') as f:
+                    for i, box in enumerate(boxes):
+                        # to avoid submitting errors
+                        box = sort_poly(box.astype(np.int32))
+                        # box[...,0],box[...,1] = box[...,0]/h_ratio_hegd,box[...,1]/w_ratio_hegd
+                        if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] - box[0]) < 5:
+                            continue
+                        if np.any(box[..., 0] > new_w * 1.1) or np.any(box[..., 1] > new_h * 1.1) or np.any(
+                                box[..., 0] < -0.1 * new_w) or np.any(box[..., 1] < -0.1 * new_h):
+                            continue
+                        recognition_result = ground_truth_to_word(recog_decode[i])
+                        for ii in range(box.shape[0]):
+                            for jj in range(box.shape[1]):
+                                box[ii, jj] = round(box[ii, jj])
+                        box = box.astype(np.int32)
+                        f.write('{},{},{},{},{},{},{},{},{}\r\n'.format(
+                            box[0, 0], box[0, 1], box[1, 0], box[1, 1], \
+                            box[2, 0], box[2, 1], box[3, 0], box[3, 1], \
+                            recognition_result
+                        ))
+            else:
+                output_dir_hegd = save_str_hegd + running_model_path.strip().split('/')[-2] + '/' + \
+                                  running_model_path.strip().split('/')[-1].split('.')[0]
+                if not os.path.exists(output_dir_hegd):
+                    os.makedirs(output_dir_hegd)
+                res_file = os.path.join(
+                    output_dir_hegd,
+                    'res_{}.txt'.format(idx))
+                with open(res_file, 'w') as f:
+                    f.write('')
+            file_hegd.write(output_dir_hegd)
+        file_hegd.close()
+
+
+if __name__ == '__main__':
+    run(1)

+ 94 - 0
code_ocr/test/icdar.py

@@ -0,0 +1,94 @@
+# coding:utf-8
+import numpy as np
+# from test import config
+import config
+
+def ground_truth_to_word(ground_truth):
+    """
+        Return the word string based on the input ground_truth
+    """
+
+    try:
+        return ''.join([config.CHAR_VECTOR[i] for i in ground_truth if i != -1])
+    except Exception as ex:
+        print(ground_truth)
+        print(ex)
+        input()
+
+def restore_rectangle_rbox(origin, geometry):
+    d = geometry[:, :4]
+    angle = geometry[:, 4]
+    origin_0 = origin[angle >= 0]
+    d_0 = d[angle >= 0]
+    angle_0 = angle[angle >= 0]
+    if origin_0.shape[0] > 0:
+        p = np.array([np.zeros(d_0.shape[0]), -d_0[:, 0] - d_0[:, 2],
+                      d_0[:, 1] + d_0[:, 3], -d_0[:, 0] - d_0[:, 2],
+                      d_0[:, 1] + d_0[:, 3], np.zeros(d_0.shape[0]),
+                      np.zeros(d_0.shape[0]), np.zeros(d_0.shape[0]),
+                      d_0[:, 3], -d_0[:, 2]])
+        p = p.transpose((1, 0)).reshape((-1, 5, 2))  # N*5*2
+
+        rotate_matrix_x = np.array([np.cos(angle_0), np.sin(angle_0)]).transpose((1, 0))
+        rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))  # N*5*2
+
+        rotate_matrix_y = np.array([-np.sin(angle_0), np.cos(angle_0)]).transpose((1, 0))
+        rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
+
+        p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis]  # N*5*1
+        p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis]  # N*5*1
+
+        p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2)  # N*5*2
+
+        p3_in_origin = origin_0 - p_rotate[:, 4, :]
+        new_p0 = p_rotate[:, 0, :] + p3_in_origin  # N*2
+        new_p1 = p_rotate[:, 1, :] + p3_in_origin
+        new_p2 = p_rotate[:, 2, :] + p3_in_origin
+        new_p3 = p_rotate[:, 3, :] + p3_in_origin
+
+        new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
+                                  new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1)  # N*4*2
+    else:
+        new_p_0 = np.zeros((0, 4, 2))
+    # for angle < 0
+    origin_1 = origin[angle < 0]
+    d_1 = d[angle < 0]
+    angle_1 = angle[angle < 0]
+    if origin_1.shape[0] > 0:
+        p = np.array([-d_1[:, 1] - d_1[:, 3], -d_1[:, 0] - d_1[:, 2],
+                      np.zeros(d_1.shape[0]), -d_1[:, 0] - d_1[:, 2],
+                      np.zeros(d_1.shape[0]), np.zeros(d_1.shape[0]),
+                      -d_1[:, 1] - d_1[:, 3], np.zeros(d_1.shape[0]),
+                      -d_1[:, 1], -d_1[:, 2]])
+        p = p.transpose((1, 0)).reshape((-1, 5, 2))  # N*5*2
+
+        rotate_matrix_x = np.array([np.cos(-angle_1), -np.sin(-angle_1)]).transpose((1, 0))
+        rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))  # N*5*2
+
+        rotate_matrix_y = np.array([np.sin(-angle_1), np.cos(-angle_1)]).transpose((1, 0))
+        rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
+
+        p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis]  # N*5*1
+        p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis]  # N*5*1
+
+        p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2)  # N*5*2
+
+        p3_in_origin = origin_1 - p_rotate[:, 4, :]
+        new_p0 = p_rotate[:, 0, :] + p3_in_origin  # N*2
+        new_p1 = p_rotate[:, 1, :] + p3_in_origin
+        new_p2 = p_rotate[:, 2, :] + p3_in_origin
+        new_p3 = p_rotate[:, 3, :] + p3_in_origin
+
+        new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
+                                  new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1)  # N*4*2
+    else:
+        new_p_1 = np.zeros((0, 4, 2))
+    return np.concatenate([new_p_0, new_p_1])
+
+def restore_rectangle(origin, geometry):
+    return restore_rectangle_rbox(origin, geometry)
+
+
+
+if __name__ == '__main__':
+    pass

+ 39 - 0
code_ocr/test/iou_mat.py

@@ -0,0 +1,39 @@
+# coding=utf-8
+import numpy as np
+from shapely.geometry import Polygon
+
+def intersection(g, p):
+    # 取g,p中的几何体信息组成多边形
+    g = Polygon(g[:8].reshape((4, 2)))
+    p = Polygon(p[:8].reshape((4, 2)))
+    # 判断g,p是否为有效的多边形几何体
+    if not g.is_valid or not p.is_valid:
+        return 0
+    # 取两个几何体的交集和并集
+    inter = Polygon(g).intersection(Polygon(p)).area
+    union = g.area + p.area - inter
+    if union == 0:
+        return 0
+    else:
+        return inter / union
+
+def iou_mat(pred_boxs,gt_boxs):
+    if pred_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in pred_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        pred_boxs = np.array(boxs_tmp)
+    if gt_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in gt_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        gt_boxs = np.array(boxs_tmp)
+    M = np.zeros((len(pred_boxs),len(gt_boxs)))
+    for idx_i,i in enumerate(pred_boxs):
+        for idx_j,j in enumerate(gt_boxs):
+            i = i[:-1] if len(i) == 9 else i
+            j = j[:-1] if len(j) == 9 else j
+            M[idx_i,idx_j] = intersection(i,j)
+    return M

BIN
code_ocr/test/last_pb/ocr_model-0902-1156.pb


+ 0 - 0
code_ocr/test/min_mode


+ 58 - 0
code_ocr/test/nms_locality.py

@@ -0,0 +1,58 @@
+# coding=utf-8
+import numpy as np
+from shapely.geometry import Polygon
+
+def intersection(g, p):
+    # 取g,p中的几何体信息组成多边形
+    g = Polygon(g[:8].reshape((4, 2)))
+    p = Polygon(p[:8].reshape((4, 2)))
+    # 判断g,p是否为有效的多边形几何体
+    if not g.is_valid or not p.is_valid:
+        return 0
+    # 取两个几何体的交集和并集
+    inter = Polygon(g).intersection(Polygon(p)).area
+    union = g.area + p.area - inter
+    if union == 0:
+        return 0
+    else:
+        return inter / union
+
+def weighted_merge(g, p):
+    # 取g,p两个几何体的加权(权重根据对应的检测得分计算得到)
+    g[:8] = (g[8] * g[:8] + p[8] * p[:8]) / (g[8] + p[8])
+    # 合并后的几何体的得分为两个几何体得分的总和
+    g[8] = (g[8] + p[8])
+    return g
+
+def standard_nms(S, thres):
+    # 标准NMS
+    order = np.argsort(S[:, 8])[::-1]
+    keep = []
+    while order.size > 0:
+        i = order[0]
+        keep.append(i)
+        ovr = np.array([intersection(S[i], S[t]) for t in order[1:]])
+        inds = np.where(ovr <= thres)[0]
+        order = order[inds + 1]
+    return S[keep]
+
+def nms_locality(polys, thres=0.3):
+    '''
+    locality aware nms of EAST
+    :param polys: a N*9 numpy array. first 8 coordinates, then prob
+    :return: boxes after nms
+    '''
+    S = []  # 合并后的几何体集合
+    p = None    # 合并后的几何体
+    for g in polys:
+        if p is not None and intersection(g, p) > thres:    # 若两个几何体的相交面积大于指定的阈值,则进行合并
+            p = weighted_merge(g, p)
+        else:   # 反之,则保留当前的几何体
+            if p is not None:
+                S.append(p)
+            p = g
+    if p is not None:
+        S.append(p)
+    if len(S) == 0:
+        return np.array([])
+    return standard_nms(np.array(S), thres)

+ 36 - 0
code_ocr/test/ocr_dataset.py

@@ -0,0 +1,36 @@
+'''
+Author:       hejinlong
+LastEditTime: 2021-12-08 15:48:25
+Description:  hegd@mail.ustc.edu.cn
+'''
+# -*- coding: utf-8 -*-
+import numpy as np 
+
+# def simple_load_np_dataset(root_path = './'):
+def simple_load_np_dataset(root_path='/data2/liudan/ocr/data/test/'):
+    # img_list_rec_file = root_path + 'all_img_path_rec.txt'
+    img_list_rec_file = root_path + 'test_img_path_rec.txt'
+    boxes_rec_file = root_path + 'test_boxes_rec.txt'
+    text_tag_rec_file = root_path + 'test_text_tag_rec.txt'
+    all_img_path = []
+    with open(img_list_rec_file,"r") as f:
+        file_content = f.readlines()
+        for i in file_content:
+            all_img_path.append(i.strip())
+    with open(boxes_rec_file,'r') as f:
+        file_content = f.read().strip()
+        num_list = file_content.split('\t')
+    boxes_flatten = []
+    for i in num_list:
+        boxes_flatten.append(int(i))
+    boxes_flatten = np.array(boxes_flatten)
+    boxes = boxes_flatten.reshape((len(all_img_path),-1, 9))
+    with open(text_tag_rec_file,'r', encoding='utf-8') as f:
+        all_text_tags = []
+        file_content = f.readlines()
+        for i in file_content:
+            all_text_tags.append(i.split('\t**hegd**\t')[:-1])
+    return all_img_path,boxes,all_text_tags
+
+if __name__ == "__main__":
+    pass

+ 40 - 0
code_ocr/test/params.py

@@ -0,0 +1,40 @@
+import os
+
+total_path = '/data2/liudan/ocr/'
+
+
+# 删除已存在文件路径
+path_data = os.path.join(total_path, 'data')
+
+
+
+# 备份total_data路径
+old_file_path = os.path.join(total_path, 'field_data')
+new_file_path = os.path.join(total_path, 'data/total_data')
+
+
+# 划分数据集路径
+dataset_path = os.path.join(total_path, 'data/total_data')
+train_set_save_path = os.path.join(total_path, 'data/train')
+val_set_save_path = os.path.join(total_path, 'data/val')
+
+
+
+# batch_process路径
+dir = os.path.join(total_path, 'data')
+
+
+# multigpu_train.py路径
+train_path = '/data2/liudan/ocr/model_saved/cscs/'
+# pretrained_model_path = os.path.join(train_path, 'pretrain_model/model.ckpt-103336')
+train_save_path = os.path.join(train_path, '09-01') # train得到的数据位置(.index, .meta, .data),这里表示save_pb_model的输入
+pretrained_model_path1 = os.path.join(train_path, '09-01')
+save_pb_path = os.path.join(train_path, '0902/') # .pb文件位置
+save_str_hegd_path = os.path.join(train_path, '/evalsave/0902')
+
+# score_the_ocr_model中测试数据位置
+testdat_msg = os.path.join(total_path, 'data/ocr_txt/')
+eval_saved = os.path.join(train_path, '/evalsave/0902')
+
+
+

+ 1 - 0
code_ocr/test/saved.txt

@@ -0,0 +1 @@
+0.0225

+ 201 - 0
code_ocr/test/saved_model_list.txt

@@ -0,0 +1,201 @@
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-361.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1681.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-946.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-286.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2011.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-691.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-76.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1876.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1546.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-766.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2236.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-391.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-931.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-676.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2806.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-871.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1636.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1231.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1051.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1351.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2791.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1561.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2971.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1291.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-466.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2491.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1276.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-601.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-241.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1831.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1981.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1246.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2176.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-166.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-646.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2326.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2566.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-961.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2521.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2026.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-346.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-3001.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-436.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2911.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-511.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1726.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1426.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1261.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2776.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2056.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2986.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-841.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-376.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1381.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1861.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-556.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2386.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-451.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-526.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1216.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1111.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1711.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2356.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1201.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-196.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1396.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1306.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1081.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2896.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1966.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2596.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2686.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-916.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1696.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-481.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1996.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2086.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-976.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2206.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1096.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1141.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2716.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-46.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2851.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1756.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1456.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2506.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-736.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1501.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1441.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1411.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1471.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1801.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1816.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-571.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-136.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-631.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-496.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1891.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-31.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1336.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2401.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-421.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1156.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1936.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-901.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-406.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2941.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1531.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-781.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-886.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2101.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1606.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2191.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2956.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1486.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2671.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1846.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-301.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2431.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-856.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-826.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2656.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2626.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1921.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-211.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-541.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-331.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2611.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1021.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2821.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2761.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-991.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-316.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1321.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2836.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2731.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1186.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1066.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1771.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2281.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-106.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-181.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-811.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2461.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2221.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2371.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2296.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2551.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2311.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1651.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2161.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-121.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1786.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1741.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1621.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-796.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-616.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1006.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1126.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-271.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1591.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-256.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2641.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-226.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2341.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2866.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-61.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1906.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2071.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-721.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2116.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-151.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2266.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-586.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1171.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2146.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1036.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-661.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2746.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2251.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1516.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1576.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2881.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1366.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2446.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-751.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-91.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2416.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2701.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2476.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2581.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-16.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-706.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1951.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-1666.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2131.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2041.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2536.pb
+/data2/liudan/ocr/model_saved/09022/ocr_model-0902-2926.pb

+ 493 - 0
code_ocr/test/score_the_ocr_model.py

@@ -0,0 +1,493 @@
+# coding=utf-8
+import os
+# from test.ocr_dataset import simple_load_np_dataset
+from ocr_dataset import simple_load_np_dataset
+
+import numpy as np
+import cv2
+# from test.iou_mat import iou_mat
+from iou_mat import iou_mat
+# import iou_mat
+# from test.bktree import levenshtein
+from bktree import levenshtein
+import sys
+import shutil
+
+from params import *
+
+# testdat_msg_path = 'test_dataset_msg/宝安机场/'
+# testdat_msg_path = '/data2/liudan/ocr/data/ocr_txt/'
+testdat_msg_path = '/data2/liudan/ocr/data/test/'
+# testdat_msg_path = testdat_msg
+test_outputs_list_path = 'test_outputs_list.txt'
+# outputs_rec_path = '001.txt'
+
+def levenshtein_mat(pred_text_tags,gt_text_tags):
+    M = np.zeros((len(pred_text_tags),len(gt_text_tags)))
+    for idx_i,i in enumerate(pred_text_tags):
+        for idx_j,j in enumerate(gt_text_tags):
+            M[idx_i,idx_j] = levenshtein(i,j)
+    return M
+
+def statistic_tp_fp_fn_box(pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,score_threshold):
+    if pred_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in pred_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        pred_boxs = np.array(boxs_tmp)
+    if gt_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in gt_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        gt_boxs = np.array(boxs_tmp)
+    M = iou_mat(pred_boxs,gt_boxs)
+    score = np.max(M, axis=1)
+    mask = score>=score_threshold
+    tp = np.sum(mask.astype(np.float32))
+    fn = len(gt_boxs) - tp
+    fp = len(pred_boxs) - tp
+    return tp,fp,fn
+
+def statistic_tp_fp_fn_str(pred_boxs,gt_boxs,gt_text_tags,\
+    pred_text_tags,score_threshold):
+    if len(pred_text_tags) == 0:
+        pred_text_tags = ['']
+    M = np.zeros((len(pred_text_tags),len(gt_text_tags)))
+    for idx_i,i in enumerate(pred_text_tags):
+        for idx_j,j in enumerate(gt_text_tags):
+            M[idx_i,idx_j] = float(i==j)
+    score = np.max(M, axis=1)
+    tp = np.sum(score)
+    fn = len(gt_text_tags) - tp
+    fp = len(pred_text_tags) - tp
+    # print(tp,fp,fn)
+    return tp,fp,fn
+
+def statistic_tp_fp_fn_str_nms_bybox(pred_boxs,gt_boxs,gt_text_tags,\
+    pred_text_tags,score_threshold):
+    if pred_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in pred_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        pred_boxs = np.array(boxs_tmp)
+    if gt_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in gt_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        gt_boxs = np.array(boxs_tmp)
+    M = iou_mat(pred_boxs,gt_boxs)
+    score = np.max(M, axis=1)
+    mask = score>=0.5
+    pred_text_tags = [i for index,i in enumerate(pred_text_tags) if mask[index] == True]
+    if len(pred_text_tags) == 0:
+        pred_text_tags = ['']
+    
+    M = np.zeros((len(pred_text_tags),len(gt_text_tags)))
+    for idx_i,i in enumerate(pred_text_tags):
+        for idx_j,j in enumerate(gt_text_tags):
+            M[idx_i,idx_j] = float(i==j)
+    score = np.max(M, axis=1)
+    tp = np.sum(score)
+    fn = len(gt_text_tags) - tp
+    fp = len(pred_text_tags) - tp
+    return tp,fp,fn
+
+def precision_recall_str(statistic_tp_fp_fn_str,sys_argv):
+    # all_img_path,boxes,all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_img_path, boxes, all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_pred_outputs = []
+    idx_list = []
+    with open(test_outputs_list_path,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            all_pred_outputs.append(i.strip())
+            idx_tmp = int(i.strip().split('res_')[1][:-4])
+            idx_list.append(idx_tmp)
+    TP_str,FP_str,FN_str = 0,0,0
+    for iidx,idx in enumerate(idx_list):
+        # ground truth
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+
+        #prediction
+        pred_boxs = []
+        pred_text_tags = []
+        predoutput_path = os.path.join(sys_argv,all_pred_outputs[iidx])
+        with open(predoutput_path,'r') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                box_str = i.strip().split(',')[:8]
+                pred_text_tags.append(i.strip().split(',')[8:][0])
+                for num in box_str:
+                    pred_boxs.append(int(num))
+            pred_boxs = np.array(pred_boxs).reshape((-1,8))
+            if pred_boxs.shape[-1] == 9:
+                boxs_tmp = []
+                for i in pred_boxs:
+                    if i[-1] > 0.5:
+                        boxs_tmp.append(i)
+                pred_boxs = np.array(boxs_tmp)
+        tp,fp,fn = statistic_tp_fp_fn_str(pred_boxs,gt_boxs,gt_text_tags,\
+            pred_text_tags,0.5)
+        TP_str += tp
+        FN_str += fn
+        FP_str += fp
+    precision_str = TP_str/(TP_str+FP_str+1e-6)
+    recall_str = TP_str/(TP_str+FN_str+1e-6)
+    return precision_str, recall_str
+    
+        
+def statistic_levenshtein_str(pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,score_threshold):
+    if len(pred_text_tags) == 0:
+        pred_text_tags = ['']
+    M = levenshtein_mat(pred_text_tags,gt_text_tags)
+    score = np.min(M, axis=1)
+    if score.size == 0:
+        return 0
+    else:
+        rst = np.mean(np.array([score]))
+        return rst
+
+def statistic_levenshtein_str_nms_bybox(pred_boxs,gt_boxs,gt_text_tags,\
+    pred_text_tags,score_threshold):
+    if pred_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in pred_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        pred_boxs = np.array(boxs_tmp)
+    if gt_boxs.shape[-1] == 9:
+        boxs_tmp = []
+        for i in gt_boxs:
+            if i[-1] > 0.5:
+                boxs_tmp.append(i)
+        gt_boxs = np.array(boxs_tmp)
+    M = iou_mat(pred_boxs,gt_boxs)
+    score = np.max(M, axis=1)
+    mask = score>=0.5
+    pred_text_tags = [i for index,i in enumerate(pred_text_tags) if mask[index] == True]
+    if len(pred_text_tags) == 0:
+        pred_text_tags = ['']
+    M = levenshtein_mat(pred_text_tags,gt_text_tags)
+    score = np.min(M, axis=1)
+    if score.size == 0:
+        return 0
+    else:
+        rst = np.mean(np.array([score]))
+        return rst
+
+def cacu_mean_levenshtein_strs(statistic_levenshtein,sys_argv):
+    all_img_path,boxes,all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_pred_outputs = []
+    idx_list = []
+    with open(test_outputs_list_path,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            all_pred_outputs.append(i.strip())
+            idx_tmp = int(i.strip().split('res_')[1][:-4])
+            idx_list.append(idx_tmp)
+    mean_levenshtein_str = []
+    for iidx,idx in enumerate(idx_list):
+        # ground truth
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+
+        #prediction
+        pred_boxs = []
+        pred_text_tags = []
+        predoutput_path = os.path.join(sys_argv,all_pred_outputs[iidx])
+        with open(predoutput_path,'r') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                box_str = i.strip().split(',')[:8]
+                pred_text_tags.append(i.strip().split(',')[8:][0])
+                for num in box_str:
+                    pred_boxs.append(int(num))
+            pred_boxs = np.array(pred_boxs).reshape((-1,8))
+            if pred_boxs.shape[-1] == 9:
+                boxs_tmp = []
+                for i in pred_boxs:
+                    if i[-1] > 0.5:
+                        boxs_tmp.append(i)
+                pred_boxs = np.array(boxs_tmp)
+        mean_levenshtein_str.append(statistic_levenshtein(\
+            pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,\
+            0.5))
+    return np.mean(np.array(mean_levenshtein_str))
+
+def pic_precision_recall(sys_argv):
+    all_img_path,boxes,all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_pred_outputs = []
+    idx_list = []
+    with open(test_outputs_list_path,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            all_pred_outputs.append(i.strip())
+            idx_tmp = int(i.strip().split('res_')[1][:-4])
+            idx_list.append(idx_tmp)
+    mean_levenshtein_str = []
+    TP_pic,TOTAL_pred,TOTAL_test = 0,0,0
+    for iidx,idx in enumerate(idx_list):
+        # ground truth
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+
+        #prediction
+        pred_boxs = []
+        pred_text_tags = []
+        predoutput_path = os.path.join(sys_argv,all_pred_outputs[iidx])
+        with open(predoutput_path,'r') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                box_str = i.strip().split(',')[:8]
+                pred_text_tags.append(i.strip().split(',')[8:][0])
+                for num in box_str:
+                    pred_boxs.append(int(num))
+            pred_boxs = np.array(pred_boxs).reshape((-1,8))
+            if pred_boxs.shape[-1] == 9:
+                boxs_tmp = []
+                for i in pred_boxs:
+                    if i[-1] > 0.5:
+                        boxs_tmp.append(i)
+                pred_boxs = np.array(boxs_tmp)
+        if pred_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in pred_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            pred_boxs = np.array(boxs_tmp)
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        M = iou_mat(pred_boxs,gt_boxs)
+        score = np.max(M, axis=1)
+        mask = score>=0.5
+        # pred_text_tags = [i for index,i in enumerate(pred_text_tags) if mask[index] == True]
+        if M.shape[0] == M.shape[1] and \
+            np.min(mask.astype(np.float32))>0.5 and \
+            statistic_levenshtein_str(\
+            pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,\
+            0.5)==0:
+            TP_pic += 1
+        if M.shape[0] != 0:
+            TOTAL_pred += 1
+        if M.shape[1] != 0:
+            TOTAL_test += 1
+    return TP_pic/(TOTAL_pred+1e-6),TP_pic/(TOTAL_test+1e-6)
+
+def pic_precision_recall_nms_box(sys_argv):
+    all_img_path,boxes,all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_pred_outputs = []
+    idx_list = []
+    with open(test_outputs_list_path,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            all_pred_outputs.append(i.strip())
+            idx_tmp = int(i.strip().split('res_')[1][:-4])
+            idx_list.append(idx_tmp)
+    mean_levenshtein_str = []
+    TP_pic,TOTAL_pred,TOTAL_test = 0,0,0
+    for iidx,idx in enumerate(idx_list):
+        # ground truth
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+
+        #prediction
+        pred_boxs = []
+        pred_text_tags = []
+        predoutput_path = os.path.join(sys_argv,all_pred_outputs[iidx])
+        with open(predoutput_path,'r') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                box_str = i.strip().split(',')[:8]
+                pred_text_tags.append(i.strip().split(',')[8:][0])
+                for num in box_str:
+                    pred_boxs.append(int(num))
+            pred_boxs = np.array(pred_boxs).reshape((-1,8))
+            if pred_boxs.shape[-1] == 9:
+                boxs_tmp = []
+                for i in pred_boxs:
+                    if i[-1] > 0.5:
+                        boxs_tmp.append(i)
+                pred_boxs = np.array(boxs_tmp)
+        if pred_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in pred_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            pred_boxs = np.array(boxs_tmp)
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        M = iou_mat(pred_boxs,gt_boxs)
+        score = np.max(M, axis=1)
+        mask = score>=0.5
+        pred_text_tags = [i for index,i in enumerate(pred_text_tags) if mask[index] == True]
+        if M.shape[0] == M.shape[1] and \
+            np.min(mask.astype(np.float32))>0.5 and \
+            statistic_levenshtein_str(\
+            pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,\
+            0.5)==0:
+            TP_pic += 1
+        if M.shape[0] != 0:
+            TOTAL_pred += 1
+        if M.shape[1] != 0:
+            TOTAL_test += 1
+    return TP_pic/(TOTAL_pred+1e-6),TP_pic/(TOTAL_test+1e-6)
+        
+def RP(score_threshold,statistic_tp_fp_fn,sys_argv):
+    all_img_path,boxes,all_text_tags = simple_load_np_dataset(root_path=testdat_msg_path)
+    all_pred_outputs = []
+    idx_list = []
+    with open(test_outputs_list_path,'r') as f:
+        file_contents = f.readlines()
+        for i in file_contents:
+            all_pred_outputs.append(i.strip())
+            idx_tmp = int(i.strip().split('res_')[1][:-4])
+            idx_list.append(idx_tmp)
+    
+    TP_box,FP_box,FN_box = 0,0,0
+    for iidx,idx in enumerate(idx_list):
+        # ground truth
+        gt_img_path = all_img_path[idx]
+        gt_boxs = boxes[idx]
+        if gt_boxs.shape[-1] == 9:
+            boxs_tmp = []
+            for i in gt_boxs:
+                if i[-1] > 0.5:
+                    boxs_tmp.append(i)
+            gt_boxs = np.array(boxs_tmp)
+        gt_text_tags = all_text_tags[idx]
+
+        #prediction
+        pred_boxs = []
+        pred_text_tags = []
+        predoutput_path = os.path.join(sys_argv,all_pred_outputs[iidx])
+        if not os.path.exists(predoutput_path):
+            with open(predoutput_path,'w') as f:
+                f.write('')
+
+        with open(predoutput_path,'r') as f:
+            file_contents = f.readlines()
+            for i in file_contents:
+                box_str = i.strip().split(',')[:8]
+                pred_text_tags.append(i.strip().split(',')[8:][0])
+                for num in box_str:
+                    pred_boxs.append(int(num))
+            pred_boxs = np.array(pred_boxs).reshape((-1,8))
+            if pred_boxs.shape[-1] == 9:
+                boxs_tmp = []
+                for i in pred_boxs:
+                    if i[-1] > 0.5:
+                        boxs_tmp.append(i)
+                pred_boxs = np.array(boxs_tmp)
+        
+        # # visualize the difference between gt and pred
+        # vis_compare_gt_pred(gt_boxs,gt_text_tags,pred_boxs,pred_text_tags,gt_img_path)
+        # break
+        tp,fp,fn = statistic_tp_fp_fn(pred_boxs,gt_boxs,gt_text_tags,pred_text_tags,score_threshold)
+        TP_box += tp
+        FP_box += fp
+        FN_box += fn
+    precision_box = TP_box/(TP_box+FP_box+1e-6)
+    recall_box = TP_box/(TP_box+FN_box+1e-6)
+    return precision_box, recall_box
+
+def ap(precis_axis,recall_axis):
+    prec, rec  =  np.array(precis_axis), np.array(recall_axis)
+    mrec = np.concatenate(([0.], rec, [1.])) 
+    mpre = np.concatenate(([0.], prec, [0.]))  
+    for i in range(mpre.size - 1, 0, -1):
+        mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])  
+    i = np.where(mrec[1:] != mrec[:-1])[0]  # precision前后两个值不一样的点
+    ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i+1])
+    return ap
+
+def score_ocr_model():
+    eval_saved_path = '/data2/liudan/ocr/savemode/eval/0801/'
+    # eval_saved_path = eval_saved
+    mini = 100
+    idxx = 10
+    list_file = os.listdir(eval_saved_path)
+    for i in list_file:
+        if i.startswith('ocr_model-0801-'):
+            i = os.path.join(eval_saved_path, i)
+            sys_argv = i.strip()
+            idx = int(i.strip().split('-')[-1])
+            print(sys_argv + 'test outputs: ')
+            precision_str, recall_str = precision_recall_str(statistic_tp_fp_fn_str, sys_argv)
+            # precision_str,recall_str = precision_recall_str(statistic_tp_fp_fn_str_nms_bybox)
+            precision_pic, recall_pic = pic_precision_recall(sys_argv)
+            # 字符串平均编辑距离
+            mean_levenshtein_strs = cacu_mean_levenshtein_strs(statistic_levenshtein_str, sys_argv)
+            # mean_levenshtein_strs = cacu_mean_levenshtein_strs(statistic_levenshtein_str_nms_bybox)
+            print('without box_pic')
+            # print(ap_box,precision_str,recall_str,precision_pic,recall_pic,mean_levenshtein_strs)
+            print(precision_str, recall_str, precision_pic, recall_pic, mean_levenshtein_strs)
+            # ff1.write(
+            #     '{:06d},{:.4f},{:.4f},{:.4f},{:.4f},{:.4f}\n'.format(idx, precision_str, recall_str, precision_pic,
+            #                                                          recall_pic, mean_levenshtein_strs))
+            # ff1.flush()
+
+            precision_str, recall_str = precision_recall_str(statistic_tp_fp_fn_str_nms_bybox, sys_argv)
+            precision_pic, recall_pic = pic_precision_recall_nms_box(sys_argv)
+            mean_levenshtein_strs = cacu_mean_levenshtein_strs(statistic_levenshtein_str_nms_bybox, sys_argv)
+
+            if mean_levenshtein_strs < mini:
+                mini = mean_levenshtein_strs
+                # idxx = os.path.join('/data2/liudan/ocr/model_saved/09022', 'ocr_model-0902-' + str(idx) + '.pb')
+                idxx = os.path.join('/data2/liudan/ocr/savemode/0801', 'ocr_model-0801-') + str(idx) + '.pb'
+
+            print('{:06d},{:.4f},{:.4f},{:.4f},{:.4f},{:.4f}\n'.format(idx, precision_str, recall_str, precision_pic,
+                                                                       recall_pic, mean_levenshtein_strs))
+
+    # savedmodel = '/data2/liudan/ocr/code_ocr/test/last_pb'
+    savedmodel = '/data2/liudan/ocr/savemode/last_pb'
+    if not os.path.exists(savedmodel):
+            os.makedirs(savedmodel)
+    shutil.copy(idxx, savedmodel)
+
+
+if __name__ == "__main__":
+    score_ocr_model()
+
+
+

+ 200 - 0
code_ocr/test/test_outputs_list.txt

@@ -0,0 +1,200 @@
+res_0.txt
+res_1.txt
+res_2.txt
+res_3.txt
+res_4.txt
+res_5.txt
+res_6.txt
+res_7.txt
+res_8.txt
+res_9.txt
+res_10.txt
+res_11.txt
+res_12.txt
+res_13.txt
+res_14.txt
+res_15.txt
+res_16.txt
+res_17.txt
+res_18.txt
+res_19.txt
+res_20.txt
+res_21.txt
+res_22.txt
+res_23.txt
+res_24.txt
+res_25.txt
+res_26.txt
+res_27.txt
+res_28.txt
+res_29.txt
+res_30.txt
+res_31.txt
+res_32.txt
+res_33.txt
+res_34.txt
+res_35.txt
+res_36.txt
+res_37.txt
+res_38.txt
+res_39.txt
+res_40.txt
+res_41.txt
+res_42.txt
+res_43.txt
+res_44.txt
+res_45.txt
+res_46.txt
+res_47.txt
+res_48.txt
+res_49.txt
+res_50.txt
+res_51.txt
+res_52.txt
+res_53.txt
+res_54.txt
+res_55.txt
+res_56.txt
+res_57.txt
+res_58.txt
+res_59.txt
+res_60.txt
+res_61.txt
+res_62.txt
+res_63.txt
+res_64.txt
+res_65.txt
+res_66.txt
+res_67.txt
+res_68.txt
+res_69.txt
+res_70.txt
+res_71.txt
+res_72.txt
+res_73.txt
+res_74.txt
+res_75.txt
+res_76.txt
+res_77.txt
+res_78.txt
+res_79.txt
+res_80.txt
+res_81.txt
+res_82.txt
+res_83.txt
+res_84.txt
+res_85.txt
+res_86.txt
+res_87.txt
+res_88.txt
+res_89.txt
+res_90.txt
+res_91.txt
+res_92.txt
+res_93.txt
+res_94.txt
+res_95.txt
+res_96.txt
+res_97.txt
+res_98.txt
+res_99.txt
+res_100.txt
+res_101.txt
+res_102.txt
+res_103.txt
+res_104.txt
+res_105.txt
+res_106.txt
+res_107.txt
+res_108.txt
+res_109.txt
+res_110.txt
+res_111.txt
+res_112.txt
+res_113.txt
+res_114.txt
+res_115.txt
+res_116.txt
+res_117.txt
+res_118.txt
+res_119.txt
+res_120.txt
+res_121.txt
+res_122.txt
+res_123.txt
+res_124.txt
+res_125.txt
+res_126.txt
+res_127.txt
+res_128.txt
+res_129.txt
+res_130.txt
+res_131.txt
+res_132.txt
+res_133.txt
+res_134.txt
+res_135.txt
+res_136.txt
+res_137.txt
+res_138.txt
+res_139.txt
+res_140.txt
+res_141.txt
+res_142.txt
+res_143.txt
+res_144.txt
+res_145.txt
+res_146.txt
+res_147.txt
+res_148.txt
+res_149.txt
+res_150.txt
+res_151.txt
+res_152.txt
+res_153.txt
+res_154.txt
+res_155.txt
+res_156.txt
+res_157.txt
+res_158.txt
+res_159.txt
+res_160.txt
+res_161.txt
+res_162.txt
+res_163.txt
+res_164.txt
+res_165.txt
+res_166.txt
+res_167.txt
+res_168.txt
+res_169.txt
+res_170.txt
+res_171.txt
+res_172.txt
+res_173.txt
+res_174.txt
+res_175.txt
+res_176.txt
+res_177.txt
+res_178.txt
+res_179.txt
+res_180.txt
+res_181.txt
+res_182.txt
+res_183.txt
+res_184.txt
+res_185.txt
+res_186.txt
+res_187.txt
+res_188.txt
+res_189.txt
+res_190.txt
+res_191.txt
+res_192.txt
+res_193.txt
+res_194.txt
+res_195.txt
+res_196.txt
+res_197.txt
+res_198.txt
+res_199.txt

+ 7 - 0
code_ocr/test/test_outputs_list11.txt

@@ -0,0 +1,7 @@
+res_0.txt
+res_1.txt
+res_2.txt
+res_3.txt
+res_4.txt
+res_5.txt
+res_6.txt

+ 200 - 0
code_ocr/test_outputs_list.txt

@@ -0,0 +1,200 @@
+res_0.txt
+res_1.txt
+res_2.txt
+res_3.txt
+res_4.txt
+res_5.txt
+res_6.txt
+res_7.txt
+res_8.txt
+res_9.txt
+res_10.txt
+res_11.txt
+res_12.txt
+res_13.txt
+res_14.txt
+res_15.txt
+res_16.txt
+res_17.txt
+res_18.txt
+res_19.txt
+res_20.txt
+res_21.txt
+res_22.txt
+res_23.txt
+res_24.txt
+res_25.txt
+res_26.txt
+res_27.txt
+res_28.txt
+res_29.txt
+res_30.txt
+res_31.txt
+res_32.txt
+res_33.txt
+res_34.txt
+res_35.txt
+res_36.txt
+res_37.txt
+res_38.txt
+res_39.txt
+res_40.txt
+res_41.txt
+res_42.txt
+res_43.txt
+res_44.txt
+res_45.txt
+res_46.txt
+res_47.txt
+res_48.txt
+res_49.txt
+res_50.txt
+res_51.txt
+res_52.txt
+res_53.txt
+res_54.txt
+res_55.txt
+res_56.txt
+res_57.txt
+res_58.txt
+res_59.txt
+res_60.txt
+res_61.txt
+res_62.txt
+res_63.txt
+res_64.txt
+res_65.txt
+res_66.txt
+res_67.txt
+res_68.txt
+res_69.txt
+res_70.txt
+res_71.txt
+res_72.txt
+res_73.txt
+res_74.txt
+res_75.txt
+res_76.txt
+res_77.txt
+res_78.txt
+res_79.txt
+res_80.txt
+res_81.txt
+res_82.txt
+res_83.txt
+res_84.txt
+res_85.txt
+res_86.txt
+res_87.txt
+res_88.txt
+res_89.txt
+res_90.txt
+res_91.txt
+res_92.txt
+res_93.txt
+res_94.txt
+res_95.txt
+res_96.txt
+res_97.txt
+res_98.txt
+res_99.txt
+res_100.txt
+res_101.txt
+res_102.txt
+res_103.txt
+res_104.txt
+res_105.txt
+res_106.txt
+res_107.txt
+res_108.txt
+res_109.txt
+res_110.txt
+res_111.txt
+res_112.txt
+res_113.txt
+res_114.txt
+res_115.txt
+res_116.txt
+res_117.txt
+res_118.txt
+res_119.txt
+res_120.txt
+res_121.txt
+res_122.txt
+res_123.txt
+res_124.txt
+res_125.txt
+res_126.txt
+res_127.txt
+res_128.txt
+res_129.txt
+res_130.txt
+res_131.txt
+res_132.txt
+res_133.txt
+res_134.txt
+res_135.txt
+res_136.txt
+res_137.txt
+res_138.txt
+res_139.txt
+res_140.txt
+res_141.txt
+res_142.txt
+res_143.txt
+res_144.txt
+res_145.txt
+res_146.txt
+res_147.txt
+res_148.txt
+res_149.txt
+res_150.txt
+res_151.txt
+res_152.txt
+res_153.txt
+res_154.txt
+res_155.txt
+res_156.txt
+res_157.txt
+res_158.txt
+res_159.txt
+res_160.txt
+res_161.txt
+res_162.txt
+res_163.txt
+res_164.txt
+res_165.txt
+res_166.txt
+res_167.txt
+res_168.txt
+res_169.txt
+res_170.txt
+res_171.txt
+res_172.txt
+res_173.txt
+res_174.txt
+res_175.txt
+res_176.txt
+res_177.txt
+res_178.txt
+res_179.txt
+res_180.txt
+res_181.txt
+res_182.txt
+res_183.txt
+res_184.txt
+res_185.txt
+res_186.txt
+res_187.txt
+res_188.txt
+res_189.txt
+res_190.txt
+res_191.txt
+res_192.txt
+res_193.txt
+res_194.txt
+res_195.txt
+res_196.txt
+res_197.txt
+res_198.txt
+res_199.txt

+ 1 - 0
code_ocr/train

@@ -0,0 +1 @@
+Subproject commit aaa6c8480542fd7202130e206ba26bfb8f4967f7

+ 73 - 0
code_ocr/train_data.py

@@ -0,0 +1,73 @@
+
+import os
+import shutil
+from batch_process import batch_save_txt
+from params import *
+from remove_data import copytree
+from split_train_val import split_train_val
+from data_2_test import data_to_test
+import time
+import shutil
+from train import multigpu_train
+from train import save_pb_model
+# from test import eval_file_200, score_the_ocr_model
+
+
+
+
+
+
+def del_file(path_data):
+    if os.path.exists(path_data):
+        for i in os.listdir(path_data) :
+            file_data = path_data + "/" + i
+            if os.path.isfile(file_data) == True:
+                os.remove(file_data)
+            else:
+                del_file(file_data)
+
+del_file(path_data) # 删除data文件夹下已存在文件
+
+
+copytree(old_file_path,new_file_path) # 将 field_data现场数据复制到data/total_data下
+
+split_train_val(dataset_path,train_set_save_path,val_set_save_path) # 划分训练集和测试集
+
+
+
+# 生成对应的txt文件
+rootdir = os.path.join(dir, 'train')
+batch_save_txt(rootdir)
+
+rootdir = os.path.join(dir, 'val')
+batch_save_txt(rootdir)
+
+
+data_to_test() # 将测试集数据转换为测试所需格式
+
+
+multigpu_train.MultigpuTrain().main() # 训练
+multigpu_train.SavePb().save_pb(argv=None) # 保存pb模式
+
+
+
+
+
+
+
+
+
+# del multigpu_train
+# for key in list(globals().keys()):
+#     if not key.startswith("__"):
+#         globals().pop(key)
+
+
+
+
+# eval_file_200.run(1)
+#
+# score_the_ocr_model.score_ocr_model()
+
+
+

BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16414482280000-10501-1642904606-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415163410000-10501-1643011359-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415163710000-10501-1643074185-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415178730000-10501-1643011285-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415179040000-10501-1643011300-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415179350000-10501-1643074134-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196290000-10501-1643011169-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196290000-10501-1643073989-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415196640000-10501-1643074004-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415210520000-10501-1643073844-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415210880000-10501-1642905806-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415211340000-10501-1643073874-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415223830000-10501-1643010935-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415239600000-10501-1642905521-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415278820000-10501-1643074620-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415305530000-10501-1642907266-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329350000-10501-1642908086-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329620000-10501-1643013143-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415329920000-10501-1643075981-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415361990000-10501-1643075719-hd.jpg


BIN
code_ocr/transform_imgs/data2/liudan/ocr/data/val/16415383270000-10501-1642908578-hd.jpg


Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels