Vue Camera Gestures - add AI powered gesture controls to your app in 1 line of HTML
Tags: vue, machinelearning
Published: 2019-10-19 10:52:28

---
title: Vue Camera Gestures - add AI powered gesture controls to your app in 1 line of HTML
published: true
description: Let users control your Vue app using AI, their camera, and gestures of their choice in just 1 line of HTML!
tags: vue,machinelearning
cover_image: https://thepracticaldev.s3.amazonaws.com/i/hrq99rregwf2x280id9a.png
---
Consider a basic Vue application. There's a ball, and clicking "left" or "right" buttons make it move left or right.
![Red ball in a box with "Left" and "Right" buttons underneath. Clicking the buttons moves the ball in that direction.](https://thepracticaldev.s3.amazonaws.com/i/a5kmk2boveynpwxyxd83.gif)
We'll suppose that you have a component called `MovingBall` that deals with displaying the ball and moving it, and we are using that component as follows:
```html
<template>
  <div>
    <moving-ball :direction="direction"></moving-ball>
    <button @click="left">Left</button>
    <button @click="right">Right</button>
  </div>
</template>

<script>
import MovingBall from './MovingBall'
export default {
  components: {
    MovingBall
  },
  data: function () {
    return {
      direction: null
    }
  },
  methods: {
    left () {
      this.direction = 'left'
    },
    right () {
      this.direction = 'right'
    },
  }
}
</script>
```
Controlling the ball using buttons is a bit boring. What if you could instead control it by gesturing a direction to your device's camera?

If you think that would take a lot of code to achieve, think again! I've just released a library, [Vue Camera Gestures](https://vue.cameragestures.com), that will let you do that in just one line in your component's template. We just need to install the library,
```
npm install vue-camera-gestures
```
import and register the component,
```js
import CameraGestures from 'vue-camera-gestures'
export default {
  components: {
    CameraGestures
  },
```
and put the following line into our template, under the buttons.
```html
<camera-gestures @left="left" @right="right"></camera-gestures>
```
And that's it! When the user opens the page, they'll be prompted to train a left and a right gesture, verify them, and then they can control the ball using the trained gestures.
![In a Chrome window, a camera feed is displayed with instructions to maintain a neutral position, and perform "left" and "right" gestures. The user does so. After this the user can use the trained gestures to control the movement of a ball.](https://thepracticaldev.s3.amazonaws.com/i/e39wuudhosk0f47wyn3n.gif)

Note that the gestures don't have to involve pointing - the user can do basically anything they're comfortable with. Under the hood, the [transfer learning](https://www.tensorflow.org/hub/tutorials/image_retraining) technique is used to retrain an [existing image recognition library](https://github.com/tensorflow/tfjs-models/tree/master/mobilenet). We then use a [KNN classifier](https://github.com/tensorflow/tfjs-models/tree/master/knn-classifier) algorithm which, after training, takes a frame of video, and finds the 10 nearest frames from the training phase that match. We assume that the user is performing the gesture that has the most matching cases.

But you don't need to know any of that - you can just use the component, and have all the AI complexity abstracted away. The `camera-gestures` component's events are completely configurable, and you can have as many of them as you like. So if you had a blogging app, you could do something like
```js
<camera-gestures
  @newPost="new"
  @savePost="save"
  @enterBoldText="bold"
  @undo="undo"
  @redo="redo"
></camera-gestures>
```
and the user will be prompted to train the five gestures, and can then use them to control the blogging app. After training, the specified events will be fired whenever the user performs the associated gesture.

Give it a go! Easily make your app more accessible/fun! Find a demo and full documentation at [vue.cameragestures.com](https://vue.cameragestures.com) and explore the source code [here](https://github.com/danielelkington/vue-camera-gestures).