-
Notifications
You must be signed in to change notification settings - Fork 2
OpenCV
-
Introduction
-
OpenCV in the cloud
- Pythoanywhere configuration
- Creating your own server
OpenCV is a computer vision library released under a BSD license and hence it is free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications.
The goals of the OpenCV project were described as:
-
Advance vision research by providing not only open but also optimized code for basic vision infrastructure. No more reinventing the wheel.
-
Disseminate vision knowledge by providing a common infrastructure that developers could build on, so that code would be more readily readable and transferable.
-
Advance vision-based commercial applications by making portable, performance-optimized code available for free-with a license that did not require to be open or free themselves.
Detailed information of all the functionalities of the library can be found in [link] (http://www.cs.indiana.edu/cgi-pub/oleykin/website/OpenCVHelp/).
Although OpenCV 1.0 has been ported to the EoT board, it is also important to have the possibility of running newer releases. To achieve this goal, an external server running an API to use OpenCV 2.4.3 is used. With this approach, it is actually possible to run any OpenCV version.
Concretely, a free Pythonanywhere server has been used. This is a free service which provides a server with web2py and OpenCV 2.4.3 in which it is possible to program any computer vision capabilities.
In order to configure the Pythonanywhere server, the following steps should be followed:
- Register for a free account in PythonAnywhere.com
- Once you have registered and logged in, go to the Web tab, and Add a new web app
- Select web2py as your python framework.
- Select an admin password for web2py. Note that this is a web2py admin password, and it is different from your PythonAnywhere.com password.
- Open a new tab, and go to web2py admin interface located at: https://username.pythonanywhere.com/admin/default/index
- You should see your application folder under the Files tab on PythonAnywhere when you go down the directory structure home/username/web2py/appname
- To add OpenCV code to the web2py open the following code: home/username/web2py/applications/appname/controllers/default.py and add new functions there. For testing, the following functions which use OpenCV to obtain the dimensions of the image have been developed. The program can receive a URL with the image and an image embedded in the HTTP request:
def image_dimensions():
# Masquerade as Mozilla because some web servers may not
like python bots.
hdr = {'User-Agent': 'Mozilla/5.0'}
# Set up the request
req = urllib2.Request(request.vars.url, headers=hdr)
try:
# Obtain the content of the url
con = urllib2.urlopen( req )
# Read the content and convert it into an numpy array
im_array = np.asarray(bytearray(con.read()), dtype=np.uint8)
# Convert the numpy array into an image.
im = cv2.imdecode(im_array, cv2.IMREAD_GRAYSCALE)
# Get the width and heigh of the image.
height, width = im.shape
# Wrap up the width and height in an object and return the encoded JSON.
return json.dumps({"width" : width, "height" : height})
except urllib2.HTTPError, e:
return e.fp.read()
def process_image():
upfile = request.vars.imagen
fname = request.vars.imagen.filename
#return json.dumps({"Contenido del fichero" : upfile.file.read()})
#return json.dumps({"Nombre del fichero" : fname})
im_array = np.asarray(bytearray(upfile.file.read()), dtype=np.uint8)
# Convert the numpy array into an image.
im = cv2.imdecode(im_array, cv2.IMREAD_GRAYSCALE)
#cv2.imdecode(image, cv2.IMREAD_COLOR)
# PROCESS YOUR OPENCV IMAGE HERE. EXAMPLE: Get the width and heigh of the image.
height, width = im.shape
# Wrap up the width and height in an object and return the encoded JSON.
return json.dumps({"width" : width, "height" : height})If the server is running, it is possible to check its functionality using the well-known command line utility curl. The following are the commands to test both examples, sending an URL of a picture and sending the image file:
URL:
Image:
Website | Twitter | Linkedin | Flickr | Slideshare