Basic tutorial 1: Live preview

So you bought the camera, what next? This tutorial will walk you through steps that are required to get preview working both over RTSP as well as via HDMI. We will also see how to configure preview using the configure_preview API.

Code

test-preview.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
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(camera_client.preview_url)

        print("Running for 60 seconds")
        time.sleep(60)

        camera_client.set_preview_state("off")

        print("Changing preview resolution to 4K and encode type to AVC")
        print(camera_client.configure_preview(
            resolution="4K", encode="AVC/H.264"))

        camera_client.set_preview_state("on")
        print("Running for 60 seconds")
        time.sleep(60)
        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

Don’t get scared. Let’s break down this code and see what these lines of code do.

import argparse
import sys
import time

The above lines will allow us to use the mentioned standard python packages. argparse is used for parsing the command line arguments. sys for printing the python version below and time is used for adding sleep. This is pretty basic stuff.

from sdk.camera import CameraClient

This is where we import the camera module of the SDK as CameraClient. We have now got the power to use all the SDK APIs.

Next is the main function. This is where actual work happens.

def main(protocol=None):
print("\nPython %s\n" % sys.version)

The first thing that will happen when this script runs is it will print the python version through which we are running this script.

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')

The above code tells argparse that our script supports –ip, –username, and –password as command line arguments. We also tell argparse that what their default values will be just in case someone forgets to add them. All these arguments are required by the connect method of the camera module sdk.camera module of the SDK.

args = parser.parse_args()
ip_addr = args.ip
username = args.username
password = args.password

Above lines are capturing the arguments passed in the command line into the respective variable. If no arguments were specified for the parameters then it will assign the default value for the param which is specified in add_argument.

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

This is where we login to the camera whose ip_address and other params we have specified as command line arguments. And get a handle to the CameraClient which can be used for controlling the camera or requesting the camera.

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))

Next, we just print some information that can be used for configuring the preview, for instance, resolution, codec type, bitrate, and framerate. This is just to see what parameters are supported. And also to show how you can access them in your code.

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

The above line will configure preview resolution to 1080p(resolution=”1080P”) and enable HDMI output(display_out=1). For the full set of options for configure_preview please check Module Index. Now that we have setup preview lets start preview and get some frames out.

camera_client.set_preview_state("on")
print(camera_client.preview_url)

If the request was successful you should now be seeing live preview on HDMI. To check the live stream. The preview_url can be used to get the RTSP url as illustrated below. You can play/open stream using any player that supports RTSP client or write your own function to parse this using python packages. We use VLC media player.

print("Running for 60 seconds")
time.sleep(60)
camera_client.set_preview_state("off")

The preview will keep running till you tell it to stop. The above lines will ensure that the preview runs for 60 seconds. We then stop it using the set_preview_state API.

print("Changing preview resolution to 4K and encode type to AVC")
print(camera_client.configure_preview(
    resolution="4K", encode="AVC/H.264"))

The above lines illustrate how you can use the configure_preview API to change the preview resolution(resolution=”4K”) and codec type(encode=”AVC/H.264”). Notice that we are not setting the display_out here. This is because once the display_out is enabled it will persist until you stop it by setting it to 0 i.e. display_out=0 using configure_preview API. Also, the other parameters will retain the previously set value or the default value if not specified earlier. As before since we have configured the preview lets turn it on using the set_preview_state API.

camera_client.set_preview_state("on")

Again, the preview will keep running till you tell it to stop. For this test will keep it running for 60 seconds(time.sleep) and then stop it using the set_preview_state API.

print("Running for 60 seconds")
time.sleep(60)
camera_client.set_preview_state("off")

Once you are done using the camera you can disconnect from the camera using the logout API.

camera_client.logout()

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.