@@ -69,23 +69,103 @@ async function performOcrWithTesseract(file) {
6969
7070 const detections = [ ] ;
7171
72- // Process OCR words
72+ // Process OCR words - check different possible data structures
73+ console . log ( 'Mobile: Tesseract data structure:' , data ) ;
74+ console . log ( 'Mobile: Available data properties:' , Object . keys ( data ) ) ;
75+ console . log ( `Mobile: Image dimensions from Tesseract - width: ${ data . width } , height: ${ data . height } ` ) ;
76+
77+ // Check if dimensions are available in different locations
78+ const imageWidth = data . width || data . image ?. width || data . imageWidth ;
79+ const imageHeight = data . height || data . image ?. height || data . imageHeight ;
80+ console . log ( `Mobile: Resolved image dimensions - width: ${ imageWidth } , height: ${ imageHeight } ` ) ;
81+
7382 if ( data . words ) {
83+ console . log ( `Mobile: Processing ${ data . words . length } words from Tesseract` ) ;
7484 for ( const word of data . words ) {
7585 if ( ! word . text || ! word . text . trim ( ) ) continue ;
7686
87+ console . log ( 'Mobile: Word object structure:' , word ) ;
88+ console . log ( 'Mobile: Word bbox:' , word . bbox ) ;
89+
7790 const match = detectToken ( word . text ) ;
7891 if ( match ) {
7992 const { kind, reason } = match ;
8093
81- // Convert coordinates to normalized format
94+ // Try different possible bbox formats
95+ let bbox = null ;
96+
97+ if ( word . bbox && typeof word . bbox === 'object' ) {
98+ // Format 1: {x0, y0, x1, y1}
99+ if ( word . bbox . x0 !== undefined ) {
100+ bbox = {
101+ x0 : word . bbox . x0 ,
102+ y0 : word . bbox . y0 ,
103+ x1 : word . bbox . x1 ,
104+ y1 : word . bbox . y1
105+ } ;
106+ }
107+ // Format 2: {left, top, right, bottom}
108+ else if ( word . bbox . left !== undefined ) {
109+ bbox = {
110+ x0 : word . bbox . left ,
111+ y0 : word . bbox . top ,
112+ x1 : word . bbox . right ,
113+ y1 : word . bbox . bottom
114+ } ;
115+ }
116+ }
117+
118+ // Fallback: check if word has direct coordinate properties
119+ if ( ! bbox && word . x0 !== undefined ) {
120+ bbox = {
121+ x0 : word . x0 ,
122+ y0 : word . y0 ,
123+ x1 : word . x1 ,
124+ y1 : word . y1
125+ } ;
126+ }
127+
128+ // Another fallback: check common Tesseract formats
129+ if ( ! bbox && word . left !== undefined ) {
130+ bbox = {
131+ x0 : word . left ,
132+ y0 : word . top ,
133+ x1 : word . left + word . width ,
134+ y1 : word . top + word . height
135+ } ;
136+ }
137+
138+ console . log ( `Mobile: Extracted bbox for "${ word . text } ":` , bbox ) ;
139+
140+ if ( ! bbox || bbox . x0 === undefined || isNaN ( bbox . x0 ) ) {
141+ console . error ( `Mobile: Invalid bbox for word "${ word . text } "` , bbox ) ;
142+ continue ;
143+ }
144+
145+ // Convert to normalized format using resolved dimensions
146+ if ( ! imageWidth || ! imageHeight || isNaN ( imageWidth ) || isNaN ( imageHeight ) ) {
147+ console . error ( `Mobile: Invalid image dimensions for normalization: ${ imageWidth } x${ imageHeight } ` ) ;
148+ continue ;
149+ }
150+
82151 const box = {
83- x : word . bbox . x0 / data . width ,
84- y : word . bbox . y0 / data . height ,
85- width : ( word . bbox . x1 - word . bbox . x0 ) / data . width ,
86- height : ( word . bbox . y1 - word . bbox . y0 ) / data . height
152+ x : bbox . x0 / imageWidth ,
153+ y : bbox . y0 / imageHeight ,
154+ width : ( bbox . x1 - bbox . x0 ) / imageWidth ,
155+ height : ( bbox . y1 - bbox . y0 ) / imageHeight
87156 } ;
88157
158+ console . log ( `Mobile: Tesseract detected "${ word . text } " at bbox (${ bbox . x0 } , ${ bbox . y0 } , ${ bbox . x1 } , ${ bbox . y1 } ) image ${ imageWidth } x${ imageHeight } ` ) ;
159+ console . log ( `Mobile: Normalized to (${ box . x . toFixed ( 3 ) } , ${ box . y . toFixed ( 3 ) } ) size ${ box . width . toFixed ( 3 ) } x${ box . height . toFixed ( 3 ) } ` ) ;
160+
161+ // Validate normalized coordinates
162+ if ( isNaN ( box . x ) || isNaN ( box . y ) || isNaN ( box . width ) || isNaN ( box . height ) ) {
163+ console . error ( `Mobile: Normalization failed - got NaN values:` , box ) ;
164+ console . error ( `Mobile: bbox:` , bbox ) ;
165+ console . error ( `Mobile: dimensions:` , { imageWidth, imageHeight} ) ;
166+ continue ;
167+ }
168+
89169 detections . push ( {
90170 id : `${ kind } _${ detections . length } ` ,
91171 kind,
@@ -294,14 +374,26 @@ async function redactImageFile(file, detections, options = {}) {
294374
295375 // Draw redaction boxes
296376 ctx . fillStyle = 'black' ;
377+ console . log ( `Mobile: Canvas size: ${ canvas . width } x${ canvas . height } , Image size: ${ img . width } x${ img . height } ` ) ;
378+
297379 for ( const detection of detections ) {
298380 const x = detection . box . x * img . width ;
299381 const y = detection . box . y * img . height ;
300382 const width = detection . box . width * img . width ;
301383 const height = detection . box . height * img . height ;
302384
385+ console . log ( `Mobile: Drawing image redaction for "${ detection . text } ":` ) ;
386+ console . log ( `Mobile: Normalized box: (${ detection . box . x . toFixed ( 3 ) } , ${ detection . box . y . toFixed ( 3 ) } ) size ${ detection . box . width . toFixed ( 3 ) } x${ detection . box . height . toFixed ( 3 ) } ` ) ;
387+ console . log ( `Mobile: Canvas coords: (${ x . toFixed ( 2 ) } , ${ y . toFixed ( 2 ) } ) size ${ width . toFixed ( 2 ) } x${ height . toFixed ( 2 ) } ` ) ;
388+
389+ // Draw a thick red border for debugging (easier to see than black)
390+ ctx . strokeStyle = 'red' ;
391+ ctx . lineWidth = 3 ;
392+ ctx . strokeRect ( x , y , width , height ) ;
393+
394+ // Draw the black fill
303395 ctx . fillRect ( x , y , width , height ) ;
304- console . log ( `Mobile: Redacted ${ detection . kind } at (${ x } , ${ y } )` ) ;
396+ console . log ( `Mobile: Drew redaction box for ${ detection . kind } at (${ x . toFixed ( 2 ) } , ${ y . toFixed ( 2 ) } )` ) ;
305397 }
306398
307399 canvas . toBlob ( ( blob ) => {
0 commit comments