File size: 5,163 Bytes
17e2002
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
<h4><strong>Introduction</strong></h4><p><br></p><p>We're about to create a <strong>Computer Vision (CV) API </strong>and<strong> Web App.</strong> But first, why would we want to do this? </p><ul><li><p>Ever wanted to share your Computer Vision App on the web to show a friend using their PC&nbsp;or Phone?</p></li><li><p>Want to sell or commercialize your python computer vision code, but not sure how to distribute Python?</p></li></ul><p>Well if you want to make your CV code available to the world you need to make it accessible from the web.&nbsp;There are three main ways you can do this:</p><p>1. Create an <strong>API</strong>, which allows any other application (whether it's an iOS, Android or within any other software) to access it via the API&nbsp;protocol. APIs are Application Programming Interfaces that facilitate access to complex external applications via a simplified interface. In our example, we'll be using the RESTful&nbsp;API protocol which allows us to run our Python CV via the web.</p><p>2. Create a <strong>Web App</strong>, this is basically the same as an API, except we'll be allowing users to access or run our CV app via any web browser.</p><p>3. Run the CV code natively on another platform. This is the more difficult option is it requires you to re-code your Python CV code into another language. This is far more difficult and requires you learning an entirely new language e.g. Java if making an Android App. But the local access reduces the need to send data over the web and can perform real-time operations. This method won't be shown in this tutorial.</p><h4></h4><p><br></p><h4><strong>Introducing Flask </strong></h4><p><br></p><p>Before we begin installing Flask and setting up everything on AWS, let me first explain what exactly is <strong>Flask,</strong> and why we need it.</p><figure><img height="138" src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_22-32-59-b6e6be615ec9b18d3f348b500492faca.JPG" width="340"></figure><p><strong>Flask </strong>is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. This allows us to easily build simple APIs without much effort. </p><p><strong>Step 1: Installing Flask</strong></p><p>If using your VM, activate your environment (so that we can use OpenCV and Keras).</p><p><code>source activate cv</code> or whatever your environment is named </p><p> <code>pip install flask</code></p><p>That's it, Flask is ready to go!</p><p>Let's run a simple Flask test.</p><p><strong>Step 2:&nbsp;</strong>Installing a code editor since we won't be using Ipython Notebooks. I recommend Sublime, but VSCode, Atom,&nbsp;Spyder, and PyCharm are all great!</p><p>If using Windows or Mac, visit the Sublime website to get the download link - <a href="https://www.sublimetext.com/download" rel="noopener noreferrer" target="_blank">https://www.sublimetext.com/download</a></p><p>Ubuntu/VM users can bring up the Ubuntu Software app, search for Sublime and install it from there.</p><figure><img src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_22-53-13-6613cddd9daf61ecf9d07c4d56e2c38c.JPG"></figure><p>Open up your chosen Text/Code Editor and copy the code below into it and save it in an easy to find directory, e.g. </p><p><code>/home/deeplearningcv/MyFlaskProjects</code></p><figure><img src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_23-00-53-bfd6feec89e093ff03e58bc52e8d3e94.JPG"></figure><p>This is code for your first simple Flask app, the 'hello world' app. </p><p><strong>Also, see Resources to download all code required for this section</strong></p><pre class="prettyprint linenums">from flask import Flask
app = Flask(__name__)
 
@app.route("/")
def hello():
    return "Hello World!"
 
if __name__ == "__main__":
    app.run()</pre><p>Once your code is saved in the path, use Terminal or Command Prompt and navigate to the folder where you saved your helloworld.py file.</p><figure><img src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_23-26-30-dbbc9629d30892671866580522faea9f.JPG"></figure><p>Upon executing this code, you'll see the following: </p><p>This means your server is running successfully and you can now go to your web browser and view your hello world project!</p><figure><img src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_23-32-38-e41640a083fa6900739c4eddf0143da9.JPG"></figure><p>You should see this if you enter <code>127.0.0.1:5000 </code>in your browser. This means you Flask server is pushing/serving the text "Hello World!" to your localhost on Port 5000. </p><figure><img src="https://udemy-images.s3.amazonaws.com:443/redactor/raw/2019-04-22_23-34-14-2a757fe288b275bff67d55a208fef895.JPG"></figure><p>Remember to press <strong>CTRL+C t</strong>o exit (when in Terminal).</p><p>Congratulations! Now you're ready to start testing your Computer&nbsp;Vision&nbsp;API locally, which we'll do in the next chapter!</p>