.. Copyright (c) 2019, The Linux Foundation. All rights reserved.
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials provided
    with the distribution.
  * Neither the name of The Linux Foundation nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.
  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

.. _basic_tut_5:

*******************************************************************
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 :ref:`basic_tut_1` and
            :ref:`basic_tut_4`. 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
====
.. literalinclude:: ../../../tests/test-preview-inference-overlay-snapshot.py
    :linenos:
    :lines: 28-
    :caption: test-preview-inference-overlay-snapshot.py

Walkthrough
===========
As you would have noticed most of the code is same as the :ref:`basic_tut_4`.
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
:ref:`test` section of the :ref:`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.