AnseMin commited on
Commit
c9c21c7
·
1 Parent(s): 0f5865d

missing run_ocr_2.0.py file

Browse files
Files changed (1) hide show
  1. src/parsers/got_ocr_parser.py +79 -3
src/parsers/got_ocr_parser.py CHANGED
@@ -26,6 +26,7 @@ class GotOcrParser(DocumentParser):
26
  # Path to the GOT-OCR repository
27
  _repo_path = None
28
  _weights_path = None
 
29
 
30
  @classmethod
31
  def get_name(cls) -> str:
@@ -78,10 +79,44 @@ class GotOcrParser(DocumentParser):
78
  logger.error(f"Missing dependency: {e}")
79
  return False
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  @classmethod
82
  def _setup_repository(cls) -> bool:
83
  """Set up the GOT-OCR2.0 repository if it's not already set up."""
84
- if cls._repo_path is not None and os.path.exists(cls._repo_path):
85
  return True
86
 
87
  try:
@@ -100,6 +135,14 @@ class GotOcrParser(DocumentParser):
100
 
101
  cls._repo_path = repo_dir
102
 
 
 
 
 
 
 
 
 
103
  # Set up the weights directory
104
  weights_dir = os.path.join(repo_dir, "GOT_weights")
105
  if not os.path.exists(weights_dir):
@@ -178,10 +221,17 @@ class GotOcrParser(DocumentParser):
178
  try:
179
  logger.info(f"Processing image with GOT-OCR: {file_path}")
180
 
 
 
 
 
 
 
 
181
  # Create the command for running the GOT-OCR script
182
  cmd = [
183
  sys.executable,
184
- os.path.join(self._repo_path, "GOT", "demo", "run_ocr_2.0.py"),
185
  "--model-name", self._weights_path,
186
  "--image-file", str(file_path),
187
  "--type", ocr_type
@@ -213,7 +263,18 @@ class GotOcrParser(DocumentParser):
213
  # If render was requested, find and return the path to the HTML file
214
  if render:
215
  # The rendered results are in /results/demo.html according to the README
216
- html_result_path = os.path.join(self._repo_path, "results", "demo.html")
 
 
 
 
 
 
 
 
 
 
 
217
  if os.path.exists(html_result_path):
218
  with open(html_result_path, 'r') as f:
219
  html_content = f.read()
@@ -233,6 +294,21 @@ class GotOcrParser(DocumentParser):
233
  except subprocess.CalledProcessError as e:
234
  logger.error(f"Error running GOT-OCR command: {str(e)}")
235
  logger.error(f"Stderr: {e.stderr}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  raise RuntimeError(f"Error processing document with GOT-OCR: {str(e)}")
237
 
238
  except Exception as e:
 
26
  # Path to the GOT-OCR repository
27
  _repo_path = None
28
  _weights_path = None
29
+ _demo_script_path = None
30
 
31
  @classmethod
32
  def get_name(cls) -> str:
 
79
  logger.error(f"Missing dependency: {e}")
80
  return False
81
 
82
+ @classmethod
83
+ def _find_demo_script(cls, base_dir):
84
+ """Find the run_ocr_2.0.py script by searching the repository.
85
+
86
+ Args:
87
+ base_dir: The base directory to start searching from
88
+
89
+ Returns:
90
+ Path to the script if found, None otherwise
91
+ """
92
+ logger.info(f"Searching for run_ocr_2.0.py in {base_dir}")
93
+ script_paths = []
94
+
95
+ # Walk through all directories and find all instances of run_ocr_2.0.py
96
+ for root, dirs, files in os.walk(base_dir):
97
+ if "run_ocr_2.0.py" in files:
98
+ script_path = os.path.join(root, "run_ocr_2.0.py")
99
+ script_paths.append(script_path)
100
+ logger.info(f"Found run_ocr_2.0.py at: {script_path}")
101
+
102
+ if not script_paths:
103
+ logger.error("Could not find run_ocr_2.0.py in the repository")
104
+ return None
105
+
106
+ # If there are multiple instances, try to find the one in demo folder
107
+ for path in script_paths:
108
+ if os.path.join("demo", "run_ocr_2.0.py") in path:
109
+ logger.info(f"Selected demo script at: {path}")
110
+ return path
111
+
112
+ # If no clear demo folder, just use the first one found
113
+ logger.info(f"Selected demo script at: {script_paths[0]}")
114
+ return script_paths[0]
115
+
116
  @classmethod
117
  def _setup_repository(cls) -> bool:
118
  """Set up the GOT-OCR2.0 repository if it's not already set up."""
119
+ if cls._repo_path is not None and os.path.exists(cls._repo_path) and cls._demo_script_path is not None:
120
  return True
121
 
122
  try:
 
135
 
136
  cls._repo_path = repo_dir
137
 
138
+ # Find the demo script
139
+ cls._demo_script_path = cls._find_demo_script(repo_dir)
140
+ if cls._demo_script_path is None:
141
+ logger.error("Could not find the run_ocr_2.0.py script in the cloned repository")
142
+ return False
143
+
144
+ logger.info(f"Using demo script: {cls._demo_script_path}")
145
+
146
  # Set up the weights directory
147
  weights_dir = os.path.join(repo_dir, "GOT_weights")
148
  if not os.path.exists(weights_dir):
 
221
  try:
222
  logger.info(f"Processing image with GOT-OCR: {file_path}")
223
 
224
+ # Check if demo script exists
225
+ if not self._demo_script_path or not os.path.exists(self._demo_script_path):
226
+ logger.warning("Demo script path not found, trying to locate it again")
227
+ self._demo_script_path = self._find_demo_script(self._repo_path)
228
+ if not self._demo_script_path:
229
+ raise RuntimeError("Could not find the run_ocr_2.0.py script in the repository")
230
+
231
  # Create the command for running the GOT-OCR script
232
  cmd = [
233
  sys.executable,
234
+ self._demo_script_path,
235
  "--model-name", self._weights_path,
236
  "--image-file", str(file_path),
237
  "--type", ocr_type
 
263
  # If render was requested, find and return the path to the HTML file
264
  if render:
265
  # The rendered results are in /results/demo.html according to the README
266
+ results_dir = os.path.join(os.path.dirname(self._demo_script_path), "..", "..", "results")
267
+ if not os.path.exists(results_dir):
268
+ # Try to find results directory
269
+ for root, dirs, files in os.walk(self._repo_path):
270
+ if "demo.html" in files:
271
+ html_result_path = os.path.join(root, "demo.html")
272
+ logger.info(f"Found rendered HTML at: {html_result_path}")
273
+ with open(html_result_path, 'r') as f:
274
+ html_content = f.read()
275
+ return html_content
276
+
277
+ html_result_path = os.path.join(results_dir, "demo.html")
278
  if os.path.exists(html_result_path):
279
  with open(html_result_path, 'r') as f:
280
  html_content = f.read()
 
294
  except subprocess.CalledProcessError as e:
295
  logger.error(f"Error running GOT-OCR command: {str(e)}")
296
  logger.error(f"Stderr: {e.stderr}")
297
+
298
+ # Print repository structure for debugging
299
+ logger.error("Repository structure for debugging:")
300
+ try:
301
+ subprocess.run(
302
+ ["find", self._repo_path, "-type", "f", "-name", "*.py"],
303
+ check=True,
304
+ capture_output=True,
305
+ text=True
306
+ )
307
+ structure_output = subprocess.getoutput(f"find {self._repo_path} -type f -name '*.py'")
308
+ logger.error(f"Python files in repository:\n{structure_output}")
309
+ except Exception as debug_e:
310
+ logger.error(f"Error getting repository structure: {debug_e}")
311
+
312
  raise RuntimeError(f"Error processing document with GOT-OCR: {str(e)}")
313
 
314
  except Exception as e: