Safety alerts
Developers can create features to notify and alert drivers about road conditions and potential hazards with Mapbox Vision Safety, an add-on module that uses segmentation, detection, and classification information passed from the Vision SDK.
Monitor speed limits
Developers can monitor speed limits and other critical signage using sign classification and track the most recently observed speed limit. When the detected speed of the vehicle is more than the last observed speed limit, you can set programmable alerts.
There are two pieces to displaying speeding alerts: detecting speed limit signs and determining current vehicle speed.
Sign detection
Vision Safety uses the Mapbox Vision SDK's Detection
class to detect trafficSign
s in view and filters for SignType
s equal to any of the speedLimit*
types (including special speed limit signs like maximum speeds for trucks and minimum speed).
Vision Safety's SpeedLimitRange
class contains two numbers: a min
and a max
speed limit.
Vehicle speed
Use the VisionSafetyListener
to watch for speed limit signs and determine the max speed at the user's current location. The approach used below to determine max speed will look for speed limit signs, update the maxAllowedSpeed
variable, and continue to use that value as the max speed until another speed limit sign is detected.
@Override
public void onRoadRestrictionsUpdated(@NotNull RoadRestrictions roadRestrictions) {
maxAllowedSpeed = roadRestrictions.getSpeedLimits().getCar().getMax();
}
override fun onRoadRestrictionsUpdated(roadRestrictions: RoadRestrictions) {
maxAllowedSpeed = roadRestrictions.speedLimits.car.max
}
Then you can use the Vision SDK's VehicleLocation
class's speed
property to determine the speed at which the device is traveling. Then, introduce some logic to determine if the user's speed (mySpeed
) is above the speed limit (maxAllowedSpeed
), and display an alert if the user is speeding.
// current speed of the car
Float mySpeed = vehicleState.getSpeed();
// display toast with overspeed warning if the car's
// speed is greater than maximum allowed speed
if (mySpeed > maxAllowedSpeed && maxAllowedSpeed > 0) {
Toast.makeText(
MainActivity.this,
"Overspeeding! Current speed : " + mySpeed +
", allowed speed : " + maxAllowedSpeed,
Toast.LENGTH_LONG
).show();
}
// current speed of the car
val mySpeed = vehicleState.speed
// display toast with overspeed warning if the car's
// speed is greater than maximum allowed speed
if (mySpeed > maxAllowedSpeed && maxAllowedSpeed > 0) {
Toast.makeText(
this@MainActivity,
"Overspeeding! Current speed : $mySpeed, allowed speed : $maxAllowedSpeed",
Toast.LENGTH_LONG
).show()
}
See the full example code in the Speeding alerts example.
Alert drivers of pedestrians and cyclists
Vision Safety CollisionObject
class detects objects in the vehicle's path and provides an estimated time to impact and a CollisionDangerLevel
between 0 and 2. Collision detection can warn drivers when pedestrians or cyclists are in the vehicle’s path or a driver is closing too quickly on a lead vehicle.
Related resources
- Speeding alerts code example
- Vision Safety for Android reference documentation