Basic tutorial 5: Live preview with analytics, overlay and snapshot

This tutorial will walk you through steps that are required to start analytics and see the results i.e. get preview, analytics and overlay working together. Also, how to take an action based on analytics result, in this example we will be taking a snapshot but you can take any action based on the result.

Warning

We assume that the correct analytics engine is installed and for ML based engine you have installed required model and related files. If you are not clear on what is required, please get in touch with QTI’s support team. We also assume that you have already checked Basic tutorial 1: Live preview and Basic tutorial 4: Live preview with analytics and overlay. If not, please go through it before proceeding as this tutorial only explains the additional code i.e. taking an snapshot based on analytics result.

Code

test-preview-inference-overlay-snapshot.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import argparse
import sys
import time

from sdk.camera import CameraClient

def main(protocol=None):
    print("\nPython %s\n" % sys.version)
    parser = argparse.ArgumentParser()
    parser.add_argument('--ip', help='ip address of the camera', default='127.0.0.1')
    parser.add_argument('--username', help='username of the camera', default='admin')
    parser.add_argument('--password', help='password of the camera', default='admin')
    args = parser.parse_args()
    ip_addr = args.ip
    username = args.username
    password = args.password

    with CameraClient.connect(ip_address=ip_addr, username=username, password=password) as camera_client:

        print(camera_client.configure_preview(resolution="1080P", display_out=1))

        camera_client.set_preview_state("on")

        print(camera_client.preview_url)

        if not camera_client.captureimage():
            print("captureimage failed")

        camera_client.set_analytics_state("on")

        print(camera_client.vam_url)

        camera_client.configure_overlay("inference")

        camera_client.set_overlay_state("on")

        try:
            with camera_client.get_inferences() as results:
                print_inferences(results, camera_client)
        except:
            print("Stopping")

def print_inferences(results=None, camera_client=None):
    print("")
    for result in results:
        if result is not None and result.objects is not None and len(result.objects):
            timestamp = result.timestamp
            if timestamp:
                print("timestamp={}".format(timestamp))
            else:
                print("timestamp= " + "None")
            for object in result.objects:
                id = object.id
                print("id={}".format(id))
                label = object.label
                print("label={}".format(label))
                confidence = object.confidence
                print("confidence={}".format(confidence))
                if int(confidence) >= 90:
                    if not camera_client.captureimage():
                        print("captureimage failed for {}".format(label))
                x = object.position.x
                y = object.position.y
                w = object.position.width
                h = object.position.height
                print("Position(x,y,w,h)=({},{},{},{})".format(x, y, w, h))
                print("")
        else:
            print("No results")

if __name__ == '__main__':
    main()

Walkthrough

As you would have noticed most of the code is same as the Basic tutorial 4: Live preview with analytics and overlay. Which means we now have things in place for starting preview both live and on HDMI. We can start the analytics using the set_analytics_state API. And are getting results using the get_inferences API. Based on this result you can take any action. In this example we capture an image if the confidence level of the object is greater than 90. Below is location in the print_inferences method where we are doing this

if int(confidence) >= 90:
    if not camera_client.captureimage():
        print("captureimage failed for {}".format(label))

It’s time to test

Once you have the application/script ready. You can test it by following the steps mentioned in Test section of the Getting started page.

Warning

This example script will not exit until there is an exception. If you wish to stop it you can do so by pressing Ctrl+C key on your keyboard.