Draw safety alerts using the Mapbox Vision SDK for iOS
Familiarity with Xcode and Swift, and completion of the Mapbox Vision SDK for
iOS Install and configure
guide.
The Mapbox Vision SDK for iOS is a library for interpreting road scenes in real time directly on iOS devices using the device’s built-in camera. The Vision SDK detects many types of objects including cars, people, road signs, and more.
In this tutorial, you'll learn how to use the Vision Safety module from the Mapbox Vision SDK and apply it to detect overspeeding and potential collisions with cars.
In the example used in this tutorial, all custom alerts have predefined images.In your own application, you can use your own images.
Getting started
Here are the resources that you need before getting started:
- An application including Mapbox Vision SDK for iOS. Before starting this tutorial, go through the Install and configure steps in the Vision SDK for iOS documentation. This will walk you through how to install the Vision SDK and configure your application.
- Recorded session. This tutorial is based on a recorded driving session through a city and the replay capabilities of
MapboxVision
. You will useVisionReplayManager
for a recorded session.- Check our Testing and development guide to familiarize yourself with record and replay functionality.
- You can download the recorded session used in this tutorial below.
To configure your application with a prerecorded session:
- Unzip the contents to a folder on your local machine.
- Go your Xcode project
Info.plist
and setYES
forUIFileSharingEnabled
thus enabling file sharing through Finder. - Install the app to the device (
⌘
+R
). - Connect your device, choose it in Finder under
Locations
section, and selectFiles
tab. - Drag and drop the folder with the recorded session onto your app. Now the session is available in the
Documents
folder inside the app container. - In the code use
VisionReplayManager.create(recordPath:)
method to create an instance ofVisionReplayManager
by providing a path to a recorded session.
Configure Vision SDK lifecycle
In this tutorial, you'll use VisionReplayManager
to run a prerecorded session (which includes video and telemetry data) and find speed signs and cars in the video. The VisionReplayManager
class is the main object for registering for events from the Vision SDK and controlling its delivery. For production applications or testing in a live environment, use VisionManager
instead of VisionReplayManager
. See the Next steps section for details.
To set up the Vision SDK:
- Create a
VisionReplayManager
instance with a recorded session path.
- Register its
delegate
.
- Create an instance of
VisionSafetyManager
configured withVisionReplayManager
instance.
- Register its
delegate
to receive safety related events.
- Create
VisionPresentationViewController
and configure it withVisionReplayManager
to display camera frames.
- Start delivering events by calling
start
onVisionReplayManager
.
- Stop delivering events by calling
stop
onVisionReplayManager
.
Set up views to draw alerts
Overspeeding alert
To show an overspeeding alert you'll use a UIView
element named alertOverspeedingView
:
Collision detections
You will draw a collision state using a custom CollisionDetectionView
with a custom border color to show a bounding box around potential collision objects:
Having a custom class also helps to track detection views in UIView
's subviews.
If you need to remove all CollisionDetectionView
objects from superview, you can use the following code: