This repository has been archived on 2025-06-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compLIB/lib/Vision.html
2021-01-21 21:51:10 +01:00

326 lines
No EOL
14 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vision &mdash; CompLib 0.0.2 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="prev" title="Servo" href="Servo.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> CompLib
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Robo4you Competition Library</a></li>
<li class="toctree-l1"><a class="reference internal" href="../other/usage.html">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="Api.html">Api</a></li>
<li class="toctree-l1"><a class="reference internal" href="Battery.html">Battery</a></li>
<li class="toctree-l1"><a class="reference internal" href="Buzzer.html">Buzzer</a></li>
<li class="toctree-l1"><a class="reference internal" href="IRSensor.html">Infrared Sensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="Motor.html">Motor</a></li>
<li class="toctree-l1"><a class="reference internal" href="Servo.html">Servo</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Vision</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#opencv-stream">Opencv Stream</a></li>
<li class="toctree-l2"><a class="reference internal" href="#examples">Examples</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#using-the-vision-module">Using the Vision Module</a></li>
<li class="toctree-l3"><a class="reference internal" href="#chessboard-detection">Chessboard Detection</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CompLib</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>Vision</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/lib/Vision.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="vision">
<span id="lib-vision"></span><h1>Vision<a class="headerlink" href="#vision" title="Permalink to this headline"></a></h1>
<p>This module provides an interface for grabbing an rtmp stream and using the images to do some processing in opencv.</p>
<p>How do I use this module?</p>
<ol class="arabic simple">
<li><p>Get frames from the raspberry pi camera</p></li>
<li><p> here comes your own processing </p></li>
<li><p>Publish the processed frames on an http server</p></li>
<li><p>You can view the http stream of your processed images in a web browser</p></li>
</ol>
<div class="section" id="opencv-stream">
<h2>Opencv Stream<a class="headerlink" href="#opencv-stream" title="Permalink to this headline"></a></h2>
<p>Because of the rtmp stream needing to buffer some frames and waiting for P-Frames, importing this module might take up
to 5 Seconds.</p>
<dl class="py class">
<dt id="compLib.Vision.__Streaming">
<em class="property">class </em><code class="sig-prename descclassname">compLib.Vision.</code><code class="sig-name descname">__Streaming</code><a class="headerlink" href="#compLib.Vision.__Streaming" title="Permalink to this definition"></a></dt>
<dd><p>Class that handles rtmp streaming for opencv.</p>
<p>DO NOT CREATE AN INSTANCE OF THIS CLASS YOURSELF!</p>
<p>This is automatically done when importing this module. Use Vision.Streaming which is
an instance of this class!</p>
<p>grab frames -&gt; do your own processing -&gt; publish frame -&gt; view on http server</p>
<dl class="py method">
<dt id="compLib.Vision.__Streaming.get_frame">
<code class="sig-name descname">get_frame</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#compLib.Vision.__Streaming.get_frame" title="Permalink to this definition"></a></dt>
<dd><p>Grab the newest frame from the rtmp stream.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>An opencv frame</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="compLib.Vision.__Streaming.publish_frame">
<code class="sig-name descname">publish_frame</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">image</span></em><span class="sig-paren">)</span><a class="headerlink" href="#compLib.Vision.__Streaming.publish_frame" title="Permalink to this definition"></a></dt>
<dd><p>Publish an opencv frame to the http webserver.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>image</strong> Opencv frame that will be published</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline"></a></h2>
<div class="section" id="using-the-vision-module">
<h3>Using the Vision Module<a class="headerlink" href="#using-the-vision-module" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">from</span> <span class="nn">compLib</span> <span class="kn">import</span> <span class="n">Vision</span>
<span class="c1"># get newest opencv frame from camera</span>
<span class="n">frame</span> <span class="o">=</span> <span class="n">Vision</span><span class="o">.</span><span class="n">Streaming</span><span class="o">.</span><span class="n">get_frame</span><span class="p">()</span>
<span class="c1"># do some processing with the frame.....</span>
<span class="c1"># publish frame to streaming server</span>
<span class="n">Vision</span><span class="o">.</span><span class="n">Streaming</span><span class="o">.</span><span class="n">publish_frame</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
</pre></div>
</div>
<p>Connect the raspberry pi to your internet and view the stream at: “<a class="reference external" href="http://your_raspi_ip:9898/">http://your_raspi_ip:9898/</a>”. 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.</p>
<p>The output on the website should show whatever your raspberry pi cam records:</p>
<a class="reference internal image-reference" href="../_images/opencv_http_stream.png"><img alt="Processed frames from opencv" src="../_images/opencv_http_stream.png" style="width: 680px;" /></a>
</div>
<div class="section" id="chessboard-detection">
<h3>Chessboard Detection<a class="headerlink" href="#chessboard-detection" title="Permalink to this headline"></a></h3>
<p>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.</p>
<p>For testing you can point it at this image:</p>
<a class="reference internal image-reference" href="../_images/chessboard.jpg"><img alt="Chessboard for opencv processing" src="../_images/chessboard.jpg" style="width: 680px;" /></a>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">from</span> <span class="nn">compLib</span> <span class="kn">import</span> <span class="n">Vision</span>
<span class="c1"># get newest opencv frame from camera</span>
<span class="n">frame</span> <span class="o">=</span> <span class="n">Vision</span><span class="o">.</span><span class="n">Streaming</span><span class="o">.</span><span class="n">get_frame</span><span class="p">()</span>
<span class="n">criteria</span> <span class="o">=</span> <span class="p">(</span><span class="n">cv2</span><span class="o">.</span><span class="n">TERM_CRITERIA_EPS</span> <span class="o">+</span> <span class="n">cv2</span><span class="o">.</span><span class="n">TERM_CRITERIA_MAX_ITER</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mf">0.001</span><span class="p">)</span>
<span class="c1"># convert image to grayscale image</span>
<span class="n">gray</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">cvtColor</span><span class="p">(</span><span class="n">frame</span><span class="p">,</span> <span class="n">cv2</span><span class="o">.</span><span class="n">COLOR_BGR2GRAY</span><span class="p">)</span>
<span class="c1"># find the chessboard corners</span>
<span class="n">ret</span><span class="p">,</span> <span class="n">corners</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">findChessboardCorners</span><span class="p">(</span><span class="n">gray</span><span class="p">,</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">9</span><span class="p">),</span> <span class="bp">None</span><span class="p">)</span>
<span class="c1"># draw detected chessboard position onto the image</span>
<span class="n">cv2</span><span class="o">.</span><span class="n">drawChessboardCorners</span><span class="p">(</span><span class="n">frame</span><span class="p">,</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">9</span><span class="p">),</span> <span class="n">corners</span><span class="p">,</span> <span class="n">ret</span><span class="p">)</span>
<span class="c1"># publish frame to streaming server</span>
<span class="n">Vision</span><span class="o">.</span><span class="n">Streaming</span><span class="o">.</span><span class="n">publish_frame</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
</pre></div>
</div>
<p>Connect the raspberry pi to your internet and view the stream at: “<a class="reference external" href="http://your_raspi_ip:9898/">http://your_raspi_ip:9898/</a>”.</p>
<p>The output image should look like this:</p>
<a class="reference internal image-reference" href="../_images/chessboard_detected.jpg"><img alt="Processed frames from opencv" src="../_images/chessboard_detected.jpg" style="width: 680px;" /></a>
<p>Here is a screenshot of the stream website while viewing the chessboard in this documentation.</p>
<a class="reference internal image-reference" href="../_images/opencv_processed.png"><img alt="Processed frames from opencv" src="../_images/opencv_processed.png" style="width: 680px;" /></a>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Servo.html" class="btn btn-neutral float-left" title="Servo" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&#169; Copyright 2021, robo4you.
</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>