Motion Detection

Is there anything moving in front of my object? Where exactly?

Getting Started

Using Angus python SDK:

# -*- coding: utf-8 -*-
import angus.client
from pprint import pprint

conn = angus.client.connect()
service ='motion_detection', version=1)


for i in range(200):
    job = service.process({'image': open('./photo-{}.jpg'.format(i), 'rb')})



The API takes a stream of 2d still images as input, of format jpg or png, without constraints on resolution.

Note however that the bigger the resolution, the longer the API will take to process and give a result.

The function process() takes a dictionary as input formatted as follows:

{'image' : file}
  • image: a python File Object as returned for example by open() or a StringIO buffer.


Events will be pushed to your client following that format:


  "input_size" : [480, 640],
  "nb_targets": 1
                "mean_position" : [34, 54],
                "mean_velocity" : [5, 10],
                "confidence" : 45
  • input_size : width and height of the input image in pixels.
  • mean_position : [pt.x, pt.y] where pt is the center of gravity of the moving pixels.
  • mean_velocity : [v.x, v.y] where v is the average velocity of the moving pixels.
  • confidence : in [0,1] measures how significant the motion is (a function of the number of keypoints moving in the same direction).

Code Sample

requirements: opencv2, opencv2 python bindings

This code sample retrieves the stream of a web cam and display in a GUI the result of the motion_detection service.

# -*- coding: utf-8 -*-
import StringIO
import cv2
import numpy as np
import angus.client

def main(stream_index):
    camera = cv2.VideoCapture(stream_index)
    camera.set(, 640)
    camera.set(, 480)
    camera.set(, 10)

    if not camera.isOpened():
        print("Cannot open stream of index {}".format(stream_index))

    print("Input stream is of resolution: {} x {}".format(camera.get(3), camera.get(4)))

    conn = angus.client.connect()
    service ="motion_detection", 1)


    while camera.isOpened():
        ret, frame =
        if not ret:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ret, buff = cv2.imencode(".jpg", gray, [cv2.IMWRITE_JPEG_QUALITY, 80])
        buff = StringIO.StringIO(np.array(buff).tostring())

        job = service.process({"image": buff})
        res = job.result

        for target in res['targets']:
            x, y = map(int, target['mean_position'])
            vx, vy = map(int, target['mean_velocity'])

  , (x, y), 5, (255,255,255))
            cv2.line(frame, (x, y), (x + vx, y + vy), (255,255,255))

        cv2.imshow('original', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):



if __name__ == '__main__':
    ### Web cam index might be different from 0 on your setup.
    ### To grab a given video file instead of the host computer cam, try:
    ### main("/path/to/myvideo.avi")