11import logging
22import platform
33import sys
4+ import threading
45from collections .abc import Iterable , Sequence
56from pathlib import Path
67from typing import Optional , Type , TypedDict , cast
2526
2627
2728class NemotronOcrPrediction (TypedDict ):
28- """Exact prediction schema returned by `nemotron_ocr` 1.0.1 ."""
29+ """Exact prediction schema returned by `nemotron_ocr`."""
2930
3031 text : str
3132 confidence : float
@@ -70,6 +71,9 @@ def __init__(
7071 else None
7172 )
7273 self .reader = NemotronOCR (model_dir = model_dir )
74+ self ._reader_debug_lock = threading .Lock ()
75+ self ._active_reader_calls = 0
76+ self ._reader_call_seq = 0
7377
7478 @staticmethod
7579 def _fail_runtime (message : str ) -> None :
@@ -119,7 +123,7 @@ def _prediction_to_cell(
119123 image_height : int ,
120124 scale : int ,
121125 ) -> TextCell :
122- # `nemotron_ocr` 1.0.1 returns normalized `left/right` and an inverted
126+ # `nemotron_ocr` returns normalized `left/right` and an inverted
123127 # pair `lower/upper`, where `lower` is the top Y and `upper` is the
124128 # bottom Y in image coordinates.
125129 left = (prediction ["left" ] * image_width ) / scale + ocr_rect .l
@@ -160,8 +164,14 @@ def __call__(
160164 with TimeRecorder (conv_res , "ocr" ):
161165 ocr_rects = self .get_ocr_rects (page )
162166
167+ print (
168+ "[nemotron-debug] "
169+ f"page={ page .page_no } rect_count={ len (ocr_rects )} "
170+ f"rects={ [rect .as_tuple () for rect in ocr_rects ]} "
171+ )
172+
163173 all_ocr_cells = []
164- for ocr_rect in ocr_rects :
174+ for crop_index , ocr_rect in enumerate ( ocr_rects ) :
165175 if ocr_rect .area () == 0 :
166176 continue
167177
@@ -171,15 +181,69 @@ def __call__(
171181 image_width , image_height = high_res_image .size
172182 image_array = numpy .array (high_res_image )
173183
174- raw_predictions = cast (
175- Sequence [NemotronOcrPrediction ],
176- self .reader (
177- image_array ,
178- merge_level = self .options .merge_level ,
179- visualize = False ,
180- ),
184+ print (
185+ "[nemotron-debug] "
186+ f"page={ page .page_no } crop={ crop_index } "
187+ f"rect={ ocr_rect .as_tuple ()} "
188+ f"size={ image_width } x{ image_height } "
189+ f"shape={ image_array .shape } "
190+ f"dtype={ image_array .dtype } "
191+ f"contiguous={ image_array .flags .c_contiguous } "
192+ f"thread={ threading .current_thread ().name } :{ threading .get_ident ()} "
193+ f"model_id={ id (self )} reader_id={ id (self .reader )} "
194+ )
195+
196+ with self ._reader_debug_lock :
197+ self ._reader_call_seq += 1
198+ reader_call_id = self ._reader_call_seq
199+ self ._active_reader_calls += 1
200+ active_reader_calls = self ._active_reader_calls
201+
202+ print (
203+ "[nemotron-debug] "
204+ f"reader-enter call={ reader_call_id } "
205+ f"active={ active_reader_calls } "
206+ f"page={ page .page_no } crop={ crop_index } "
207+ f"thread={ threading .current_thread ().name } :{ threading .get_ident ()} "
208+ f"model_id={ id (self )} reader_id={ id (self .reader )} "
181209 )
182210
211+ try :
212+ raw_predictions = cast (
213+ Sequence [NemotronOcrPrediction ],
214+ self .reader (
215+ image_array ,
216+ merge_level = self .options .merge_level ,
217+ visualize = False ,
218+ ),
219+ )
220+ except Exception :
221+ print (
222+ "[nemotron-debug] "
223+ f"FAILED page={ page .page_no } crop={ crop_index } "
224+ f"rect={ ocr_rect .as_tuple ()} "
225+ f"size={ image_width } x{ image_height } "
226+ f"shape={ image_array .shape } "
227+ f"dtype={ image_array .dtype } "
228+ f"contiguous={ image_array .flags .c_contiguous } "
229+ f"reader_call={ reader_call_id } "
230+ f"thread={ threading .current_thread ().name } :{ threading .get_ident ()} "
231+ f"model_id={ id (self )} reader_id={ id (self .reader )} "
232+ )
233+ raise
234+ finally :
235+ with self ._reader_debug_lock :
236+ self ._active_reader_calls -= 1
237+ active_reader_calls = self ._active_reader_calls
238+ print (
239+ "[nemotron-debug] "
240+ f"reader-exit call={ reader_call_id } "
241+ f"active={ active_reader_calls } "
242+ f"page={ page .page_no } crop={ crop_index } "
243+ f"thread={ threading .current_thread ().name } :{ threading .get_ident ()} "
244+ f"model_id={ id (self )} reader_id={ id (self .reader )} "
245+ )
246+
183247 del high_res_image
184248 del image_array
185249
0 commit comments