Basic tutorial 3: Live preview with snapshot

This tutorial will walk you through steps that are required to get preview working both over RTSP as well as via HDMI and take snapshot when the preview is running.

Warning

We assume you have already checked Basic tutorial 1: Live preview. If not, please go through it before proceeding as this tutorial only explains the additional code i.e. capturing snapshot.

Code

test-preview-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
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('supported resolutions: ' + str(camera_client.resolutions))
        print('supported encodetype: ' + str(camera_client.encodetype))
        print('supported bitrates: ' + str(camera_client.bitrates))
        print('supported framerates: ' + str(camera_client.framerates))
        print(camera_client.configure_preview(resolution="1080P", display_out=1))

        camera_client.set_preview_state("on")

        print("Running for 30 seconds")
        time.sleep(30)
        print("Taking snapshot")
        if not camera_client.captureimage():
            print("captureimage failed")
        print("Running for 30 seconds")
        time.sleep(30)

        camera_client.set_preview_state("off")
        camera_client.logout()


if __name__ == '__main__':
    main()

Note

This script is present in the tests/ directory of the SDK.

Walkthrough

As you would have noticed most of the code is same as the Basic tutorial 1: Live preview. So in this walkthrough we will just see how the snapshot is being captured using the captureimage API.

You can capture image anytime after obtaining the handle to the CameraClient and starting the preview using the captureimage API. In the above mentioned example preview is started and after 30 seconds of preview a snapshot is taken.

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

Note

The captured snapshot is stored on the device as well in the directory from where you have triggered this script.

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.