1+ using OpenCvSharp ;
2+ using Sdcb . PaddleOCR . Models ;
3+ using Sdcb . PaddleOCR . Models . Local ;
4+ using System . Runtime . InteropServices ;
5+ using Xunit ;
6+ using Xunit . Abstractions ;
7+
8+ namespace Sdcb . PaddleOCR . Tests ;
9+
10+ public class SingleCharTest ( ITestOutputHelper testOutputHelper )
11+ {
12+ [ Fact ]
13+ public void SingleCharRecognitionTest ( )
14+ {
15+ testOutputHelper . WriteLine (
16+ $ "Running SingleChar test on { RuntimeInformation . OSDescription } ({ RuntimeInformation . OSArchitecture } )") ;
17+
18+ FullOcrModel model = LocalFullModels . ChineseV5 ;
19+ byte [ ] sampleImageData = File . ReadAllBytes ( @"./samples/vsext.png" ) ;
20+
21+ using PaddleOcrAll all = new ( model )
22+ {
23+ AllowRotateDetection = true ,
24+ Enable180Classification = false ,
25+ } ;
26+
27+ using Mat src = Cv2 . ImDecode ( sampleImageData , ImreadModes . Color ) ;
28+ PaddleOcrResult result = all . Run ( src ) ;
29+ testOutputHelper . WriteLine ( "Detected all texts: \n " + result . Text ) ;
30+
31+ Assert . NotEmpty ( result . Regions ) ;
32+
33+ foreach ( PaddleOcrResultRegion region in result . Regions )
34+ {
35+ testOutputHelper . WriteLine ( $ "Text: { region . Text } , Score: { region . Score } ") ;
36+ testOutputHelper . WriteLine ( $ "SingleChars count: { region . SingleChars . Count } ") ;
37+
38+ // Verify single characters exist
39+ Assert . NotEmpty ( region . SingleChars ) ;
40+
41+ // Verify indices are properly set and sequential
42+ for ( int i = 0 ; i < region . SingleChars . Count ; i ++ )
43+ {
44+ OcrRecognizerResultSingleChar singleChar = region . SingleChars [ i ] ;
45+ Assert . Equal ( i , singleChar . Index ) ;
46+ Assert . NotNull ( singleChar . Character ) ;
47+ Assert . True ( singleChar . Score > 0 , $ "Character '{ singleChar . Character } ' should have a positive score") ;
48+
49+ testOutputHelper . WriteLine (
50+ $ " Char[{ singleChar . Index } ]: '{ singleChar . Character } ', Score: { singleChar . Score : F3} ") ;
51+ }
52+
53+ // Verify the concatenated single characters match the full text
54+ string reconstructedText = string . Join ( "" , region . SingleChars . Select ( c => c . Character ) ) ;
55+ Assert . Equal ( region . Text , reconstructedText ) ;
56+ }
57+ }
58+ }
0 commit comments