• No se han encontrado resultados

Control en tiempo real

In document Control por computador basado en C++ (página 100-104)

6. Control y comunicación con dispositivos reales

6.1. Control en tiempo real

For now we just selected 4 images from the video

os.path home = expanduser("~") sys.path.append('/usr/local/Cellar/opencv/3.3.1_1/lib/python3.6/site-packages/') sys.path.append(home + '/.pyenv/versions/OPENCV/lib/python3.6/site-packages/') import cv2 cv2.__version__

! pip install numpy > tmp.log ! pip install matplotlib >> tmp.log %matplotlib inline

import cv2

import matplotlib.pyplot as plt

img1 = cv2.imread('secchi/secchi1.png') img2 = cv2.imread('secchi/secchi2.png') img3 = cv2.imread('secchi/secchi3.png') img4 = cv2.imread('secchi/secchi4.png') figures = []

fig = plt.figure(figsize=(18, 16))

for i in range(1,13):

figures.append(fig.add_subplot(4,3,i)) count = 0

for img in [img1,img2,img3,img4]: figures[count].imshow(img)

color = ('b','g','r')

for i,col in enumerate(color):

histr = cv2.calcHist([img],[i],None,[256],[0,256]) figures[count+1].plot(histr,color = col)

figures[count+2].hist(img.ravel(),256,[0,256])

count += 3

print("Legend")

print("First column = image of Secchi disk")

print("Second column = histogram of colors in image") print("Third column = histogram of all values") plt.show()

Figure 8: Histogram 3.6.8.3.1 Image Thresholding

See Figure 9, Figure 10, Figure 11, Figure 12

def threshold(img):

ret,thresh = cv2.threshold(img,150,255,cv2.THRESH_BINARY) plt.subplot(1,2,1), plt.imshow(img, cmap='gray')

plt.subplot(1,2,2), plt.imshow(thresh, cmap='gray') threshold(img1)

Figure 9: Threshold 1 Figure 10: Threshold 2 Figure 11: Threshold 3 Figure 12: Threshold 4 3.6.8.3.2 Edge Detection threshold(img2) threshold(img3) threshold(img4)

See Figure 13, Figure 14, Figure 15, Figure 16, Figure 17. Edge detection using Canny edge detection algorithm

Figure 13: Edge Detection 1

Figure 14: Edge Detection 2

Figure 15: Edge Detection 3

def find_edge(img): edges = cv2.Canny(img,50,200) plt.subplot(121),plt.imshow(img,cmap = 'gray') plt.subplot(122),plt.imshow(edges,cmap = 'gray') find_edge(img1) find_edge(img2) find_edge(img3) find_edge(img4)

Figure 16: Edge Detection 4 3.6.8.3.3 Black and white

Figure 17: Back White conversion

3.7 DATA

3.7.1 Data Formats

3.7.1.1 YAML

The term YAML stand for “YAML Ainot Markup Language”. According to the Web Page at

http://yaml.org/

“YAML is a human friendly data serialization standard for all programming

bw1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) plt.imshow(bw1, cmap='gray')

languages.” There are multiple versions of YAML existing and one needs to take care of that your software supports the right version. The current version is YAML 1.2.

YAML is often used for configuration and in many cases can also be used as XML replacement. Important is tat YAM in contrast to XML removes the tags while replacing them with indentation. This has naturally the advantage that it is mor easily to read, however, the format is strict and needs to adhere to proper indentation. Thus it is important that you check your YAML files for correctness, either by writing for example a python program that read your yaml file, or an online YAML checker such as provided at

http://www.yamllint.com/

An example on how to use yaml in python is provided in our next example. Please note that YAML is a superset of JSON. Originally YAML was designed as a markup language. However as it is not document oriented but data oriented it has been recast and it does no longer classify itself as markup language.

Resources:

http://yaml.org/

https://en.wikipedia.org/wiki/YAML http://www.yamllint.com/

3.7.1.2 JSON

The term JSON stand for JavaScript Object Notation. It is targeted as an open- standard file format that emphasizes on integration of human-readable text to transmit data objects. The data objects contain attribute value pairs. Although it

import os import sys import yaml try: yamlFilename = os.sys.argv[1] yamlFile = open(yamlFilename, "r") except:

print("filename does not exist") sys.exit()

try:

yaml.load(yamlFile.read()) except:

originates from JavaScript, the format itself is language independent. It uses brackets to allow organization of the data. PLease note that YAML is a superset of JSON and not all YAML documents can be converted to JSON. Furthermore JSON does not support comments. For these reasons we often prefer to us YAMl instead of JSON. However JSON data can easily be translated to YAML as well as XML.

Resources:

https://en.wikipedia.org/wiki/JSON https://www.json.org/

3.7.1.3 XML

XML stands for Extensible Markup Language. XML allows to define documents with the help of a set of rules in order to make it machine readable. The emphasize here is on machine readable as document in XML can become quickly complex and difficult to understand for humans. XML is used for documents as well as data structures.

A tutorial about XML is available at

https://www.w3schools.com/xml/default.asp

Resources:

https://en.wikipedia.org/wiki/XML

3.7.2 MongoDB in Python

Learning Objectives

Introduction to basic MongoDB knowledge Use of MongoDB via PyMongo

Use of MongoEngine MongoEngine and Object-Document mapper, Use of Flask-Mongo

In today’s era, NoSQL databases have developed an enormous potential to process the unstructured data efficiently. Modern information is complex, extensive, and may not have pre-existing relationships. With the advent of the advanced search engines, machine learning, and Artificial Intelligence, technology expectations to process, store, and analyze such data have grown tremendously [2]. The NoSQL database engines such as MongoDB, Redis, and Cassandra have successfully overcome the traditional relational database challenges such as scalability, performance, unstructured data growth, agile sprint cycles, and growing needs of processing data in real-time with minimal hardware processing power [3]. The NoSQL databases are a new generation of engines that do not necessarily require SQL language and are sometimes also called Not Only SQL databases. However, most of them support various third- party open connectivity drivers that can map NoSQL queries to SQL’s. It would be safe to say that although NoSQL databases are still far from replacing the relational databases, they are adding an immense value when used in hybrid IT environments in conjunction with relational databases, based on the application specific needs [3]. We will be covering the MongoDB technology, its driver PyMongo, its object-document mapper MongoEngine, and the Flask-PyMongo micro-web framework that make MongoDB more attractive and user-friendly.

In document Control por computador basado en C++ (página 100-104)