Version: 1.8.1

Getting Started - Swift

A VayAnalyser configures the analysis session with the desired exercise and then communicates with the API by sending images. In return, events are called containing specific event data. Each analyser has listeners implemented that are called on their respective event. Visit the VayAnalyser and VayListener sections for more details.

A short guide

Make sure you have imported the package, especially if your IDE does not add imports automatically.

import SwiftClient

When you create an analyser instance, which also sets up the connection to the server, you can pass an instance of the VayListener protocol with implementations for each event. Note that the callbacks for the events have a default implementation, which allows you to only implement the callbacks that are needed. An example of the implementation for the listener can look as follows:

class AnalyserVayListener : VayListener {
    private(set) var totalRepetitions = 0

    // If you want to visualize the keypoints, do it from here
    func onPose(_ event: VayPoseEvent) {
        // Get the points for each body point type
        let points = event.pose
        // For example the left knee
        let pointLeftKnee = points[VayBodyPointType.leftKnee]

    // Metric values can be analysed here.
    func onMetricValues(_ event: VayMetricValuesEvent) {
        // Get the metric values
        let metricValues = event.metricValues

    // Feedbacks are received here.
    func onFeedback(_ event: VayFeedbackEvent) {
        // Get a list of feedbacks
        let feedbacks = event.feedbacks

    // Any logic that evaluates an activity can go here. An activity can either
    // be an attempt or a repetition
    func onActivity(_ event: VayActivityEvent) {
        let activity = event.activity
        if let repetition = activity as? VayRepetition {
            // Count total repetitions
            totalRepetitions += 1
            // Get the duration of the repetition
            let duration = repetition.duration
            // Get all feedbacks that occurred during this repetition
            let feedbacks = repetition.feedbacks
        else if let attempt = activity as? VayAttempt {
            // Get all feedbacks that occurred during this attempt
            let feedbacks = attempt.feedbacks

    // Any logic that evaluates repetitions can go here
    // Note that the same information is provided here as in the
    // VayActivityEvent if the activity is a repetition
    func onRepetition(_ event: VayRepetitionEvent) {
        // Count total repetitions
        totalRepetitions += 1
        // Get the duration of the repetition
        let duration = event.repetition.duration
        // Get all feedbacks that occurred during this repetition
        let feedbacks = event.repetition.feedbacks

    // Analyse the occurred error here
    func onError(_ event: VayErrorEvent) {
        // Get the error
        let error = event.error
        // Display the error reason
        print("Error reason: \(error)")

    // When the session is configured and ready, the exercise including all
    // metrics is received
    func onReady(_ event: VayReadyEvent) {
        // Get the exercise name
        let exerciseName =
        // Get the exercise key
        let exerciseKey = event.exercise.key
        // Get the metrics
        let metrics = event.exercise.metrics

    // Information that the analyser has been stopped. This is the place where
    // reconnection or disposing could be implemented.
    func onStop() {

    // When the session state changes e.g. from POSITIONING to EXERCISING the
    // new state can be stored here
    func onSessionStateChanged(_ event: VaySessionStateChangedEvent) {
        // Get the session state
        let sessionState = event.sessionState

    // Here the state of the ENVIRONMENT and the LATENCY can be analysed
    func onSessionQualityChanged(_ event: VaySessionQualityChangedEvent) {
        // Get the session quality
        let sessionQuality = event.sessionQuality

Once the callbacks are defined, you can pass the instance of AnalyserVayListener together with the server uri, which will automatically try to set up a connection. The session is configured with the passed exerciseKey if the provided apiKey is correct. An optional userExternalId may be passed.

let url = "rpcs://<hostname>:<port>" // url that will be provided
let apiKey = "API-KEY" // api key for authentication
let exerciseKey = 1 // key of the exercise to analyse
let userExternalId = "USER-ID" // optionally pass your user ID
let analyserVayListener = AnalyserVayListener()
let analyser: VayAnalyser?
do {
    analyser = try AnalyserFactory.createStreamingAnalyser(url: url, apiKey: apiKey, exerciseKey: exerciseKey, listener: listener, userExternalId: userExternalId)
} catch VayErrorType.invalidInput(let reason) {
    analyser = nil
    // handle invalid input error
} catch {
    analyser = nil
    // handle error related to the failed analyser

Once the analyser is created, images can be submitted for analysis at any time.

CGImage cgImage = ...
// example for a portrait mode image
let scaledImage = UIImage(cgImage: cgImage)
    .scaleToHeight(newHeight: 640)
let compressedImage = scaledImage
    .jpegData(compressionQuality: 0.6)

let input = AnalyserFactory.createInput(for: compressedImage)
analyser.enqueue(input: input)

Don't forget to stop the analyser, once you're done.


results matching ""

    No results matching ""