Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/Vision/Vision Detection
Required plugins for this example: MBS MacCF Plugin, MBS MacCG Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS MacCI Plugin, MBS AVFoundation Plugin, MBS MacFrameworks Plugin
Last modified Fri, 3rd Jul 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacFrameworks/Vision/Vision Detection
Download this example: Vision Detection.zip
Project "Vision Detection.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control ImageCanvas Inherits DesktopCanvas
ControlInstance ImageCanvas Inherits DesktopCanvas
EventHandler Sub Paint(g As Graphics, areas() As Rect)
If pic <> Nil Then
Var faktor As Double = Min( g.Height / Pic.Height, g.Width / Pic.Width)
// Calculate new size
Var w As Integer = Pic.Width * faktor
Var h As Integer = Pic.Height * faktor
var x as integer = (g.Width - w)/2
Var y As Integer = (g.Height - h)/2
// draw picture in the new size
g.DrawPicture Pic, x, y, w, h, 0, 0, Pic.Width, Pic.Height
If overlay <> Nil Then
g.DrawPicture overlay, x, y, w, h, 0, 0, overlay.Width, overlay.Height
End If
if point <> nil then
g.PenWidth = 2
g.PenHeight = 2
g.ForeColor = &cFF0000
g.DrawOval x + point.x * w - 4, y + (1-point.y) * h - 4, 8, 8
End If
if Rect <> nil then
g.PenWidth = 2
g.PenHeight = 2
g.ForeColor = &cFF0000
g.DrawRectangle x + rect.Origin.x * w - 4, y + (1-rect.Origin.y-rect.size.Height) * h - 4, rect.Size.Width * w, rect.size.Height * h
End If
If region <> Nil Then
g.PenWidth = 2
g.PenHeight = 2
g.ForeColor = &cFF0000
Var ImageSize As CGSizeMBS = CGSizeMBS.Make(1, 1)
Var Points() As CGPointMBS = region.pointsInImageOfSize(ImageSize)
var coords(0) as integer
For Each p As CGPointMBS In Points
coords.Add x + p.x * w
coords.Add y + (1-p.y) * h
'g.DrawOval x + p.x * w - 4, y + (1-p.y) * h - 4, 8, 8
Next
g.DrawPolygon coords
end if
End If
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
EventHandler Sub RowExpanded(row As Integer)
Var v As Variant = Me.RowTagAt(row)
if v isa VNRequestMBS then
var r as VNRequestMBS = v
Var results() As VNObservationMBS = r.results
For Each result As VNObservationMBS In results
If result.Confidence < 0.5 Then
// ignore low confidence
else
List.AddFolder result.ClassName, result
end if
Next
return
End If
If v IsA VNFaceLandmarks2DMBS Then
Var o As VNFaceLandmarks2DMBS = v
Var fo As VNFaceObservationMBS = list.CellTagAt(row, 0)
// these points are relative to
List.AddFolder "faceContour", o.faceContour, fo
List.AddFolder "innerLips", o.innerLips, fo
List.AddFolder "leftEye", o.leftEye, fo
List.AddFolder "leftEyebrow", o.leftEyebrow, fo
List.AddFolder "leftPupil", o.leftPupil, fo
List.AddFolder "medianLine", o.medianLine, fo
List.AddFolder "nose", o.nose, fo
List.AddFolder "noseCrest", o.noseCrest, fo
List.AddFolder "outerLips", o.outerLips, fo
List.AddFolder "rightEye", o.rightEye, fo
List.AddFolder "rightEyebrow", o.rightEyebrow, fo
List.AddFolder "rightPupil", o.rightPupil, fo
Return
ElseIf v IsA VNFaceLandmarkRegion2DMBS Then
Var o As VNFaceLandmarkRegion2DMBS = v
Var pointCount As Integer = o.pointCount
Var fo As VNFaceObservationMBS = list.CellTagAt(row, 0)
List.AddRow "pointCount", o.pointCount.ToString
var ImageSize as CGSizeMBS = CGSizeMBS.Make(1, 1)
Var Points() As CGPointMBS = o.pointsInImageOfSize(ImageSize)
For Each p As CGPointMBS In Points
List.AddRow "point", p.X.ToString+"/"+p.Y.ToString, p
Next
Return
End If
If v IsA VNObservationMBS Then
Var r As VNObservationMBS = v
List.AddRow "ClassName", r.className
'List.AddRow "Description", r.description
List.AddRow "Confidence", r.Confidence.ToString
List.AddRow "UUID", r.UUID
If v IsA VNFeaturePrintObservationMBS Then
Var o As VNFeaturePrintObservationMBS = v
List.AddRow "elementCount", o.elementCount.ToString
List.AddRow "elementType", o.elementType.ToString
elseIf v IsA VNInstanceMaskObservationMBS Then
Var o As VNInstanceMaskObservationMBS = v
List.AddRow "allInstances", o.allInstances.count.ToString
// render all the layers together to one pixel buffer
Var error As NSErrorMBS
Var image As CVPixelBufferMBS = o.generateScaledMaskForImageForInstances(o.allInstances, imageRequestHandler, error)
// and show in window
Var c As NSImageMBS = image.NSImage
Var p As Picture = c.CopyPicture
List.AddRow "picture"
list.RowTagAt(List.LastAddedRowIndex) = p
ElseIf v IsA VNPixelBufferObservationMBS Then
Var o As VNPixelBufferObservationMBS = v
List.AddRow "featureName", o.featureName
#if mbs.HasMacCIPlugin then
Var c As CIImageMBS = o.CIImage
If c <> Nil Then
var p as Picture = c.RenderPicture(pic.Width, pic.Height)
List.AddRow "picture"
list.RowTagAt(List.LastAddedRowIndex) = p
End If
#EndIf
elseIf v IsA VNClassificationObservationMBS Then
Var o As VNClassificationObservationMBS = v
List.AddRow "Identifier", o.identifier
elseIf v IsA VNHorizonObservationMBS Then
Var o As VNHorizonObservationMBS = v
List.AddRow "Angle", o.angle.ToString
elseIf v IsA VNDetectedObjectObservationMBS Then
Var o As VNDetectedObjectObservationMBS = v
Var boundingBox As CGRectMBS = o.boundingBox
List.AddRow "boundingBox", _
boundingBox.Left.ToString+"/"+_
boundingBox.Top.ToString+" "+_
boundingBox.Width.ToString+"x"+_
boundingBox.Height.ToString, boundingBox
If v IsA VNFaceObservationMBS Then
Var fo As VNFaceObservationMBS = v
// all values could be empty if uknown.
List.AddRow "faceCaptureQuality", fo.faceCaptureQuality
List.AddRow "pitch", fo.pitch
List.AddRow "roll", fo.roll
List.AddRow "yaw", fo.yaw
List.AddFolder "landmarks", fo.landmarks, fo
ElseIf v IsA VNRecognizedObjectObservationMBS Then
Var ro As VNRecognizedObjectObservationMBS = v
Var labels() as VNClassificationObservationMBS = ro.labels
'List.AddRow "label", labels(0).identifier
For Each label As VNClassificationObservationMBS In labels
List.AddFolder "label", label
next
ElseIf v IsA VNRectangleObservationMBS Then
Var ro As VNRectangleObservationMBS = v
List.AddRow "topLeft", ro.topLeft.X.ToString+"/"+ro.topLeft.Y.ToString, ro.topLeft
List.AddRow "topRight", ro.topRight.X.ToString+"/"+ro.topRight.Y.ToString, ro.topRight
List.AddRow "bottomLeft", ro.bottomLeft.X.ToString+"/"+ro.bottomLeft.Y.ToString, ro.bottomLeft
List.AddRow "bottomRight", ro.bottomRight.X.ToString+"/"+ro.bottomRight.Y.ToString, ro.bottomRight
If v IsA VNBarcodeObservationMBS Then
Var bo As VNBarcodeObservationMBS = v
List.AddRow "payloadStringValue", bo.payloadStringValue
List.AddRow "symbology", bo.symbology
List.AddRow "supplementalPayloadString", bo.supplementalPayloadString
elseIf v IsA VNRecognizedTextObservationMBS Then
Var ot As VNRecognizedTextObservationMBS = v
List.AddRow "Text", ot.String
elseIf v IsA VNTextObservationMBS Then
Var oo As VNTextObservationMBS = v
var characterBoxes() as VNRectangleObservationMBS = oo.characterBoxes
ElseIf v IsA VNRectangleObservationMBS Then
// done
Else
Break
End If
ElseIf v IsA VNDetectedObjectObservationMBS Then
// done
Else
Break
End If
ElseIf v IsA VNObservationMBS Then
// done
Else
Break
End If
Else
Break
End If
End EventHandler
EventHandler Sub SelectionChanged()
If overlay <> Nil or point <> nil or rect <> nil and region <> nil Then
// remove it
overlay = Nil
point = Nil
rect = Nil
region = nil
ImageCanvas.Refresh
End If
Var row As Integer = Me.SelectedRowIndex
If row >=0 and row < Me.RowCount Then
Var v As Variant = Me.RowTagAt(row)
If v IsA Picture Then
overlay = v
ImageCanvas.Refresh
elseif v IsA CGRectMBS Then
rect = v
ImageCanvas.Refresh
ElseIf v IsA CGPointMBS Then
point = v
ImageCanvas.Refresh
ElseIf v IsA VNFaceLandmarkRegion2DMBS then
region = v
ImageCanvas.Refresh
end if
End If
End EventHandler
End Control
EventHandler Sub DropObject(obj As DragItem, action As DragItem.Types)
If obj.PictureAvailable Then
RunPic obj.Picture
ElseIf obj.FolderItemAvailable Then
RunFile obj.FolderItem
end if
End EventHandler
EventHandler Sub Opening()
Me.AcceptPictureDrop
Me.AcceptFileDrop FileTypeGroup1.All
Var file As FolderItem = SpecialFolder.Desktop.Child("IMG_4282.jpeg")
If file <> Nil Then
End If
End EventHandler
Sub RequestCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
if request <> nil then
List.AddFolder request.ClassName, request
End If
End Sub
Sub Run()
region = Nil
rect = Nil
point = Nil
overlay = Nil
ImageCanvas.Refresh
list.RemoveAllRows
Var requests() As VNRequestMBS
requests.Append New VNClassifyImageRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNDetectBarcodesRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNDetectDocumentSegmentationRequestMBS(WeakAddressOf RequestCompleted)
requests.append New VNDetectFaceCaptureQualityRequestMBS(WeakAddressOf RequestCompleted)
requests.append New VNDetectFaceLandmarksRequestMBS(WeakAddressOf RequestCompleted)
requests.append New VNDetectFaceRectanglesRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNDetectHorizonRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNDetectHumanRectanglesRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNDetectRectanglesRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNGenerateForegroundInstanceMaskRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNGeneratePersonInstanceMaskRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNRecognizeAnimalsRequestMBS(WeakAddressOf RequestCompleted)
requests.Append New VNRecognizeTextRequestMBS(WeakAddressOf RequestCompleted)
if file <> nil then
imageRequestHandler = VNImageRequestHandlerMBS.RequestWithFile(file)
ElseIf pic <> Nil Then
imageRequestHandler = VNImageRequestHandlerMBS.RequestWithPicture(pic)
Else
return
End If
imageRequestHandler.performRequestsAsync(requests, nil)
End Sub
Sub RunFile(file as FolderItem)
self.file = file
Self.pic = Picture.Open(file)
ImageCanvas.Refresh
Run
End Sub
Sub RunPic(pic as Picture)
self.file = nil
self.pic = pic
ImageCanvas.Refresh
Run
End Sub
Property file As FolderItem
Property imageRequestHandler As VNImageRequestHandlerMBS
Property overlay As Picture
Property pic As Picture
Property point As CGPointMBS
Property rect As CGRectMBS
Property region As VNFaceLandmarkRegion2DMBS
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"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
FileTypeGroup1
Filetype Png
Filetype Jpeg
End FileTypeGroup1
Module Module1
Sub AddFolder(extends List as DesktopListbox, r as string, o as variant)
If o = Nil Then
list.AddRow r, "nil"
else
list.AddExpandableRow r
list.RowTagAt(list.LastAddedRowIndex) = o
End If
End Sub
Sub AddFolder(extends List as DesktopListbox, r as string, o as variant, parent as Variant)
If o = Nil Then
list.AddRow r, "nil"
else
list.AddExpandableRow r
list.RowTagAt(list.LastAddedRowIndex) = o
List.CellTagAt(List.LastAddedRowIndex, 0) = parent
End If
End Sub
Sub AddRow(extends List as DesktopListbox, r as string, s as string, o as variant)
list.AddRow r, s
list.RowTagAt(list.LastAddedRowIndex) = o
End Sub
End Module
End Project
See also:
- /MacFrameworks/Vision/Vision Object Detection/Mask 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: Vision Detection.zip
The items on this page are in the following plugins: MBS MacFrameworks Plugin.