Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/Vision Object Detection/Saliency Image Requests
Required plugins for this example: MBS Main Plugin, MBS MacFrameworks Plugin, MBS MacCI Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS MacBase Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/Vision Object Detection/Saliency Image Requests
This example is the version from Sun, 17th Aug 2019.
Project "Saliency Image Requests.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
If VisionModuleMBS.available Then
// okay
Else
MsgBox "Please run as 64-bit MacOS app on MacOS 10.13 or newer."
end If
End EventHandler
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
MsgBox Join(error.Stack, EndOfLine)
End EventHandler
End Class
Class MainWindow Inherits Window
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
If pic1 <> Nil Then
Dim faktor As Double = Min( g.height / Pic1.Height, g.Width / Pic1.Width)
// Calculate new size
Dim w As Integer = Pic1.Width * faktor
Dim h As Integer = Pic1.Height * faktor
Dim x As Integer = (g.width - w)/2
Dim y As Integer = (g.height - h)/2
g.DrawPicture pic1, x, y, w, h, 0, 0, pic1.Width, pic1.Height
End If
End EventHandler
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
If pic2 <> Nil Then
Dim faktor As Double = Min( g.height / Pic2.Height, g.Width / Pic2.Width)
// Calculate new size
Dim w As Integer = pic2.Width * faktor
Dim h As Integer = pic2.Height * faktor
Dim x As Integer = (g.width - w)/2
Dim y As Integer = (g.height - h)/2
g.DrawPicture pic2, x, y, w, h, 0, 0, pic2.Width, pic2.Height
End If
End EventHandler
End Control
Control Canvas3 Inherits Canvas
ControlInstance Canvas3 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
If pic3 <> Nil Then
Dim faktor As Double = Min( g.height / Pic3.Height, g.Width / Pic3.Width)
// Calculate new size
Dim w As Integer = pic3.Width * faktor
Dim h As Integer = pic3.Height * faktor
Dim x As Integer = (g.width - w)/2
Dim y As Integer = (g.height - h)/2
g.DrawPicture pic3, x, y, w, h, 0, 0, pic3.Width, pic3.Height
End If
End EventHandler
End Control
EventHandler Sub DropObject(obj As DragItem, action As Integer)
Do
If obj.FolderItemAvailable Then
Dim image As Picture = Picture.Open(obj.FolderItem)
performVisionRequest image
return
End If
If obj.PictureAvailable Then
Dim image As Picture = obj.Picture
performVisionRequest image
Return
End If
Loop Until Not obj.NextItem
End EventHandler
EventHandler Sub Open()
Dim f As FolderItem = FindFile("card.jpg")
If f.Exists Then
Dim image As Picture = Picture.Open(f)
performVisionRequest image
End If
Me.AcceptFileDrop "" // all
me.AcceptPictureDrop
End EventHandler
Sub AttentionBasedSSaliencyImageCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
If error <> Nil Then
MsgBox error.LocalizedDescription
End If
Dim results() As VNObservationMBS = request.results
If results.Ubound >= 0 Then
Dim v As Variant = results(0)
If v IsA VNSaliencyImageObservationMBS Then
Dim r As VNSaliencyImageObservationMBS = v
Dim ci As CIImageMBS = r.CIImage
pic3 = ci.RenderPicture
canvas3.Invalidate
Else
break
End If
Else
Break
End If
End Sub
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
dim parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
dim file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Sub ObjectnessBasedSaliencyImageCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
If error <> Nil Then
MsgBox error.LocalizedDescription
End If
Dim results() As VNObservationMBS = request.results
If results.Ubound >= 0 Then
Dim v As Variant = results(0)
If v IsA VNSaliencyImageObservationMBS Then
Dim r As VNSaliencyImageObservationMBS = v
Dim ci As CIImageMBS = r.CIImage
pic2 = ci.RenderPicture
canvas2.Invalidate
Else
Break
End If
Else
Break
End If
End Sub
Sub performVisionRequest(pic as Picture)
Self.pic1 = pic
canvas1.Invalidate
Dim requests() As VNRequestMBS
requests.Append New VNGenerateObjectnessBasedSaliencyImageRequestMBS(WeakAddressOf ObjectnessBasedSaliencyImageCompleted)
requests.Append New VNGenerateAttentionBasedSaliencyImageRequestMBS (WeakAddressOf AttentionBasedSSaliencyImageCompleted)
Dim imageRequestHandler As VNImageRequestHandlerMBS = VNImageRequestHandlerMBS.RequestWithPicture(pic)
// sync
Dim error As NSErrorMBS
Dim result As Boolean = imageRequestHandler.performRequests(requests, error)
End Sub
Property pic1 As Picture
Property pic2 As Picture
Property pic3 As Picture
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS MacFrameworks Plugin.