Skip to content

Commit 27d112b

Browse files
committed
remove overlapped boxes
1 parent 3d44656 commit 27d112b

4 files changed

Lines changed: 126 additions & 5 deletions

File tree

googleOCR/Utils.bas

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
B4J=true
2+
Group=Default Group
3+
ModulesStructureVersion=1
4+
Type=StaticCode
5+
Version=7.8
6+
@EndOfDesignText@
7+
'Static code module
8+
Sub Process_Globals
9+
Private fx As JFX
10+
End Sub
11+
12+
13+
Sub OverlappingPercent(boxGeometry1 As Map,boxGeometry2 As Map) As Double
14+
'Log("boxGeometry1"&boxGeometry1)
15+
'Log("boxGeometry2"&boxGeometry2)
16+
Dim X1,Y1,W1,H1 As Int
17+
X1=boxGeometry1.Get("X")
18+
Y1=boxGeometry1.Get("Y")
19+
W1=boxGeometry1.Get("width")
20+
H1=boxGeometry1.Get("height")
21+
Dim X2,Y2,W2,H2 As Int
22+
X2=boxGeometry2.Get("X")
23+
Y2=boxGeometry2.Get("Y")
24+
W2=boxGeometry2.Get("width")
25+
H2=boxGeometry2.Get("height")
26+
Dim theSmallerX,theBiggerX,theSmallerXWidth,theBiggerXWidth As Int
27+
If theSmallerOneIndex(X1,X2)=0 Then
28+
theSmallerX=X1
29+
theBiggerX=X2
30+
theSmallerXWidth=W1
31+
theBiggerXWidth=W2
32+
Else
33+
theSmallerX=X2
34+
theBiggerX=X1
35+
theSmallerXWidth=W2
36+
theBiggerXWidth=W1
37+
End If
38+
Dim theSmallerY,theBiggerY,theSmallerYHeight,theBiggerYHeight As Int
39+
If theSmallerOneIndex(Y1,Y2)=0 Then
40+
theSmallerY=Y1
41+
theBiggerY=Y2
42+
theSmallerYHeight=H1
43+
theBiggerYHeight=H2
44+
Else
45+
theSmallerY=Y2
46+
theBiggerY=Y1
47+
theSmallerYHeight=H2
48+
theBiggerYHeight=H1
49+
End If
50+
51+
If theSmallerX+theSmallerXWidth>=theBiggerX And theSmallerY+theSmallerYHeight>=theBiggerY Then
52+
'overlapping
53+
Dim overlappingArea As Double = (theSmallerOne(X1+W1,X2+W2)-theBiggerX)*(theSmallerOne(Y1+H1,Y2+H2)-theBiggerY)
54+
Dim area1,area2 As Double
55+
area1=W1*H1
56+
area2=W2*H2
57+
Dim theSmallArea As Int=theSmallerOne(area1,area2)
58+
'Log("overlappingArea:"&overlappingArea)
59+
'Log("theSmallArea:"&theSmallArea)
60+
'Log("overlapping percent:")
61+
'Log(overlappingArea/theSmallArea)
62+
Return overlappingArea/theSmallArea
63+
Else
64+
Return 0
65+
End If
66+
End Sub
67+
68+
Sub theSmallerOneIndex(X1 As Int,X2 As Int) As Int
69+
If X1<X2 Then
70+
Return 0
71+
Else
72+
Return 1
73+
End If
74+
End Sub
75+
76+
Sub theSmallerOne(X1 As Int,X2 As Int) As Int
77+
If X1<X2 Then
78+
Return X1
79+
Else
80+
Return X2
81+
End If
82+
End Sub

googleOCR/googleOCR.b4j

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ Library7=javaobject
1414
Library8=jokhttputils2
1515
Library9=jstringutils
1616
Module1=googleOCRPlugin
17+
Module2=Utils
1718
NumberOfFiles=0
1819
NumberOfLibraries=12
19-
NumberOfModules=1
20+
NumberOfModules=2
2021
Version=7.8
2122
@EndOfDesignText@
2223
#Region Project Attributes
@@ -36,7 +37,7 @@ Sub AppStart (Form1 As Form, Args() As String)
3637
MainForm.Show
3738
Dim t As googleOCRPlugin
3839
t.Initialize
39-
wait for (t.GetTextWithLocation(fx.LoadImage(File.DirApp,"1.jpg"),"zh")) Complete (done As Object)
40+
wait for (t.GetTextWithLocation(fx.LoadImage(File.DirApp,"18_094.jpg"),"ja")) Complete (done As Object)
4041
Log(done)
4142
End Sub
4243

googleOCR/googleOCR.b4j.meta

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
ModuleBookmarks0=
22
ModuleBookmarks1=
3+
ModuleBookmarks2=
34
ModuleBreakpoints0=
45
ModuleBreakpoints1=
6+
ModuleBreakpoints2=
57
ModuleClosedNodes0=
68
ModuleClosedNodes1=
7-
NavigationStack=googleOCRPlugin,Initialize,7,0,googleOCRPlugin,GetNiceName,14,0,googleOCRPlugin,getLangs,39,5,googleOCRPlugin,Run,23,0,Main,AppStart,17,6,googleOCRPlugin,Paragraph2Box,332,6,googleOCRPlugin,ocr,252,6,Main,Process_Globals,5,0,googleOCRPlugin,Class_Globals,0,0
9+
ModuleClosedNodes2=
10+
NavigationStack=googleOCRPlugin,saveImgToDiskWithSizeCheck,345,0,Main,AppStart,18,6,Utils,Process_Globals,4,0,Utils,theSmallerOne,75,0,Utils,OverlappingPercent,6,0,googleOCRPlugin,Class_Globals,0,0,googleOCRPlugin,Paragraph2Box,336,6,googleOCRPlugin,GetArea,352,6,googleOCRPlugin,RemoveOverlapped,344,6,googleOCRPlugin,ocr,204,6
811
SelectedBuild=0
9-
VisibleModules=1
12+
VisibleModules=1,2

googleOCR/googleOCRPlugin.bas

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ Sub ocr(img As B4XBitmap,lang As String) As ResumableSub
233233
features.Initialize
234234
Dim feature As Map
235235
feature.Initialize
236-
feature.Put("type","TEXT_DETECTION")
236+
'feature.Put("type","TEXT_DETECTION")
237+
feature.Put("type","DOCUMENT_TEXT_DETECTION")
237238
features.Add(feature)
238239
request.Put("image",imageMap)
239240
request.Put("features",features)
@@ -280,6 +281,7 @@ Sub ocr(img As B4XBitmap,lang As String) As ResumableSub
280281
Log(LastException)
281282
End Try
282283
End If
284+
RemoveOverlapped(boxes)
283285
Return boxes
284286
End Sub
285287

@@ -339,6 +341,39 @@ Sub Paragraph2Box(paragraph As Map) As Map
339341
Return box
340342
End Sub
341343

344+
Sub RemoveOverlapped(boxes As List)
345+
Dim new As List
346+
new.Initialize
347+
For i=0 To boxes.Size-1
348+
Dim shouldRemove As Boolean=False
349+
Dim box1 As Map=boxes.Get(i)
350+
Dim geometry1 As Map
351+
geometry1=box1.Get("geometry")
352+
For j=0 To boxes.Size-1
353+
Dim box2 As Map=boxes.Get(j)
354+
Dim geometry2 As Map
355+
geometry2=box2.Get("geometry")
356+
If Utils.OverlappingPercent(geometry1,geometry2)>0.5 Then
357+
If GetArea(geometry1)<GetArea(geometry2) Then
358+
shouldRemove=True
359+
End If
360+
End If
361+
Next
362+
If shouldRemove=False Then
363+
new.Add(box1)
364+
End If
365+
Next
366+
boxes.Clear
367+
boxes.Addall(new)
368+
End Sub
369+
370+
Sub GetArea(boxGeometry As Map) As Int
371+
Dim width,height As Int
372+
width=boxGeometry.Get("width")
373+
height=boxGeometry.Get("height")
374+
Return width*height
375+
End Sub
376+
342377
Sub getMap(key As String,parentmap As Map) As Map
343378
Return parentmap.Get(key)
344379
End Sub

0 commit comments

Comments
 (0)