-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSegmentAnythingMaskGen.b4j
More file actions
116 lines (107 loc) · 2.91 KB
/
SegmentAnythingMaskGen.b4j
File metadata and controls
116 lines (107 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
AppType=JavaFX
Build1=Default,org.xulihang.imagetrans
Group=Default Group
Library1=javaobject
Library10=xmlbuilder
Library11=threading
Library2=jbitmapcreator
Library3=jcore
Library4=jfx
Library5=jokhttputils2
Library6=json
Library7=jstringutils
Library8=jxmlsax
Library9=jxui
Module1=cv2
Module2=cvMat
Module3=jSegmentAnything
Module4=SegmentAnythingMaskGenPlugin
NumberOfFiles=0
NumberOfLibraries=11
NumberOfModules=4
Version=8.9
@EndOfDesignText@
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
#AdditionalJar: segmentanything4j
#AdditionalJar: opencv-490
#AdditionalJar: onnxruntime-1.20.0-nodll
Sub Process_Globals
Private fx As JFX
Public MainForm As Form
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
cv2.Initialize
loadOpenCV
loadOnnxruntime
Dim n As SegmentAnythingMaskGenPlugin
n.Initialize
Dim boxes As List
boxes.Initialize
Dim box As Map
box.Initialize
Dim geometry As Map
geometry.Initialize
geometry.Put("X",210)
geometry.Put("Y",200)
geometry.Put("width",140)
geometry.Put("height",300)
box.Put("geometry",geometry)
Dim box2 As Map
box2.Initialize
Dim geometry2 As Map
geometry2.Initialize
geometry2.Put("X",380)
geometry2.Put("Y",230)
geometry2.Put("width",100)
geometry2.Put("height",190)
box2.Put("geometry",geometry2)
boxes.Add(box)
'boxes.Add(box2)
Dim img As Image=fx.LoadImage(File.DirApp,"1.jpg")
wait for (n.genMask(img,boxes)) complete (result As B4XBitmap)
Dim out As OutputStream
out=File.OpenOutput(File.DirApp,"mask.png",False)
result.WriteToStream(out,100,"PNG")
out.Close
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub loadOpenCV
Dim System As JavaObject
System.InitializeStatic("java.lang.System")
System.RunMethod("loadLibrary",Array(cv2.NATIVE_LIBRARY_NAME))
End Sub
'windows, mac or linux
Sub DetectOS As String
Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
If os.Contains("win") Then
Return "windows"
Else If os.Contains("mac") Then
Return "mac"
Else
Return "linux"
End If
End Sub
Sub loadOnnxruntime
Dim System As JavaObject
System.InitializeStatic("java.lang.System")
Dim os As String = DetectOS
If os = "windows" Then
System.RunMethod("load",Array(File.Combine(File.DirApp,"onnxruntime.dll")))
System.RunMethod("load",Array(File.Combine(File.DirApp,"onnxruntime4j_jni.dll")))
else if os = "mac" Then
System.RunMethod("load",Array(File.Combine(File.DirApp,"libonnxruntime.dylib")))
System.RunMethod("load",Array(File.Combine(File.DirApp,"libonnxruntime4j_jni.dylib")))
Else
System.RunMethod("load",Array(File.Combine(File.DirApp,"libonnxruntime.so")))
System.RunMethod("load",Array(File.Combine(File.DirApp,"libonnxruntime4j_jni.so")))
End If
End Sub