Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/Vision/Vision Object Detection/Mask Detection
Required plugins for this example: MBS MacCF Plugin, MBS MacCG Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS AVFoundation Plugin, MBS MacFrameworks Plugin
Last modified Mon, 13th Apr 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacFrameworks/Vision/Vision Object Detection/Mask Detection
Download this example: Mask Detection.zip
Project "Mask Detection.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
Var faktor As Double = Min( g.height / Pic1.Height, g.Width / Pic1.Width)
// Calculate new size
Var w As Integer = Pic1.Width * faktor
Var h As Integer = Pic1.Height * faktor
Var x As Integer = (g.width - w)/2
Var 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
Var faktor As Double = Min( g.height / Pic2.Height, g.Width / Pic2.Width)
// Calculate new size
Var w As Integer = pic2.Width * faktor
Var h As Integer = pic2.Height * faktor
Var x As Integer = (g.width - w)/2
Var 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
Var faktor As Double = Min( g.height / Pic3.Height, g.Width / Pic3.Width)
// Calculate new size
Var w As Integer = pic3.Width * faktor
Var h As Integer = pic3.Height * faktor
Var x As Integer = (g.width - w)/2
Var 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
Var image As Picture = Picture.Open(obj.FolderItem)
performVisionRequest image
return
End If
If obj.PictureAvailable Then
Var image As Picture = obj.Picture
performVisionRequest image
Return
End If
Loop Until Not obj.NextItem
End EventHandler
EventHandler Sub Open()
Var f As FolderItem = FindFile("test.jpg")
If f.Exists Then
Var image As Picture = Picture.Open(f)
performVisionRequest image
End If
Me.AcceptFileDrop "" // all
me.AcceptPictureDrop
End EventHandler
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
Var parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
Var file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Sub GenerateForegroundInstanceMaskCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
If error <> Nil Then
MsgBox error.LocalizedDescription
End If
Var results() As VNObservationMBS = request.results
If results <> nil and results.Ubound >= 0 Then
Var v As Variant = results(0)
If v IsA VNInstanceMaskObservationMBS Then
Var r As VNInstanceMaskObservationMBS = v
Var error2 As NSErrorMBS
Var indexes As NSIndexSetMBS = r.allInstances
Var IndexValues() As Integer = indexes.Values
'Var image As CVPixelBufferMBS = r.generateMaskForInstances(indexes, error2)
Var error3 As NSErrorMBS
Var image2 As CVPixelBufferMBS = r.generateScaledMaskForImageForInstances(indexes, imageRequestHandler, error3)
If error2 <> Nil Then
MsgBox error2.LocalizedDescription
End If
If error3 <> Nil Then
MsgBox error3.LocalizedDescription
End If
If image2 <> Nil Then
'Var m As MemoryBlock = image.PixelData
Var c As NSImageMBS = image2.NSImage
pic2 = c.CopyPicture
canvas2.Invalidate
End If
Else
Break
End If
Else
Break
End If
End Sub
Sub GeneratePersonInstanceMaskCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
If error <> Nil Then
MsgBox error.LocalizedDescription
End If
Var results() As VNObservationMBS = request.results
If results <> nil and results.Ubound >= 0 Then
Var v As Variant = results(0)
If v IsA VNInstanceMaskObservationMBS Then
Var r As VNInstanceMaskObservationMBS = v
Var error2 As NSErrorMBS
Var indexes As NSIndexSetMBS = r.allInstances
Var IndexValues() As Integer = indexes.Values
'Var image As CVPixelBufferMBS = r.generateMaskForInstances(indexes, error2)
Var error3 As NSErrorMBS
Var image2 As CVPixelBufferMBS = r.generateScaledMaskForImageForInstances(indexes, imageRequestHandler, error3)
If error2 <> Nil Then
MsgBox error2.LocalizedDescription
End If
If image2 <> Nil Then
'Var m As MemoryBlock = image.PixelData // inspect bytes
Var c As NSImageMBS = image2.NSImage
pic3 = c.CopyPicture
canvas3.Invalidate
End If
Else
break
End If
Else
Break
End If
End Sub
Sub performVisionRequest(pic as Picture)
If pic = Nil Then
Break
Else
Self.pic1 = pic
canvas1.Invalidate
Var requests() As VNRequestMBS
requests.Append New VNGeneratePersonInstanceMaskRequestMBS(WeakAddressOf GeneratePersonInstanceMaskCompleted)
requests.Append New VNGenerateForegroundInstanceMaskRequestMBS(WeakAddressOf GenerateForegroundInstanceMaskCompleted)
// add more here like face detection or barcode detection
imageRequestHandler = VNImageRequestHandlerMBS.RequestWithPicture(pic)
// async
imageRequestHandler.performRequestsAsync(requests)
'
'// sync
'Var error As NSErrorMBS
'Var result As Boolean = imageRequestHandler.performRequests(requests, error)
End If
End Sub
Property imageRequestHandler As VNImageRequestHandlerMBS
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
Sign
End Sign
End Project
See also:
- /MacFrameworks/Vision/Vision Detection
- /MacFrameworks/Vision/Vision Object Detection/Saliency Image Requests
- /MacFrameworks/Vision/Vision Object Detection/Text Recognition
- /MacFrameworks/Vision/Vision Object Detection/Vision Object Detection
Download this example: Mask Detection.zip
The items on this page are in the following plugins: MBS MacFrameworks Plugin.