.. _lib_vision:

Vision
*******

This module provides an interface for grabbing an rtmp stream and using the images to do some processing in opencv.

How do I use this module?

1. Get frames from the raspberry pi camera
2. -- here comes your own processing --
3. Publish the processed frames on an http server
4. You can view the http stream of your processed images in a web browser

Opencv Stream
==============

Because of the rtmp stream needing to buffer some frames and waiting for P-Frames, importing this module might take up
to 5 Seconds.

.. autoclass:: compLib.Vision.__Streaming
   :members:

Examples
=========

Using the Vision Module
-------------------------

.. code-block:: python

    import cv2
    from compLib import Vision

    while True:
        # get newest opencv frame from camera
        frame = Vision.Streaming.get_frame()

        # do some processing with the frame.....

        # publish frame to streaming server
        Vision.Streaming.publish_frame(frame)

Connect the raspberry pi to your internet and view the stream at: "http://your_raspi_ip:9898/". This should display
your raspberry pi camera. Note: the stream will lag a little bit BUT the processing of the image will be done in
realtime.

The output on the website should show whatever your raspberry pi cam records:

.. image:: images/opencv_http_stream.png
  :width: 680
  :alt: Processed frames from opencv


Chessboard Detection
------------------------------------------

In this example we process the captured stream of images and want to detect chessboards. Run this example and
point your raspberry pi camera to a chessboard and it should be detected.

For testing you can point it at this image:

.. image:: images/chessboard.jpg
  :width: 680
  :alt: Chessboard for opencv processing

.. code-block:: python

    import cv2
    from compLib import Vision

    while True:
        # get newest opencv frame from camera
        frame = Vision.Streaming.get_frame()

        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

        # convert image to grayscale image
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # find the chessboard corners
        ret, corners = cv2.findChessboardCorners(gray, (6, 9), None)

        # draw detected chessboard position onto the image
        cv2.drawChessboardCorners(frame,  (6, 9), corners, ret)

        # publish frame to streaming server
        Vision.Streaming.publish_frame(frame)

Connect the raspberry pi to your internet and view the stream at: "http://your_raspi_ip:9898/".

The output image should look like this:

.. image:: images/chessboard_detected.jpg
  :width: 680
  :alt: Processed frames from opencv

Here is a screenshot of the stream website while viewing the chessboard in this documentation.

.. image:: images/opencv_processed.png
  :width: 680
  :alt: Processed frames from opencv


