SlideShare a Scribd company logo
Python
CHAPTER 1: . . . . . . . . . . . . . . . . . . . 355
AI Is a Collection of Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Current Limitations of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
CHAPTER 2: . . . . . . . . . . . 365
Understanding Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Building a Simple Neural Network in Python . . . . . . . . . . . . . . . . . 370
. . . . . . . . . . . . . 383
CHAPTER 3: . . . . . . . . . . . . . . 393
Learning by Looking for Solutions in All the Wrong Places. . . . . . 394
Classifying Clothes with Machine Learning . . . . . . . . . . . . . . . . . . . 395
. . . . . . . . . . . . . . . . . . . . . . 395
. . . . . . . . . 396
Detecting Clothes Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Visualizing with MatPlotLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Learning More Machine Learning. . . . . . . . . . . . . . . . . . . . . . . . . . . 413
CHAPTER 4: . . . . . . . . . . . . . . . . . . . . . . 415
Limitations of the Raspberry Pi and AI. . . . . . . . . . . . . . . . . . . . . . . 415
Adding Hardware AI to the Raspberry Pi . . . . . . . . . . . . . . . . . . . . . 418
AI in the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
AI on a Graphics Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
. . . . . . . . . . . . . . . . . . . . . . 424
CHAPTER 1 355
A
»
»
»
»
356 BOOK 4
» Neural networks
» Machine learning
»
neural networks
CHAPTER 1
“What magical trick makes us intelligent? The trick is that there is no
trick. The power of intelligence stems from our vast diversity, not from
was coming to Pocatello. I made a few phone calls and managed to get him to meet
It was like we were Rolling Stones fans and Mick Jagger was coming to town to spend
BOOK 4
AI Winter refers to the moment that
researchers went to great lengths to avoid using the AI term in their proposals and
Game
CHAPTER 1
»
»
»
»
»
threshold
Learning
ing stack to do more processing for color.
BOOK 4
»
» Neural networks
» Decision trees
»
Machine learning
phones.
CHAPTER 1
is doing well and then select the new ones to move to the next generation. Survival of
BOOK 4
CHAPTER 1 363
tensor
TensorFlow
understanding
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
CHAPTER 2 Building a Neural Network in Python 365
Building a Neural
Networkin Python
N
eural networks and various other models of how the brain operates have
widespread “irrational exuberance” about how the perceptron was going to make
because of the application areas that have been developed.
-
IN THIS CHAPTER
» How machines learn
» Understanding the basics of machine
learning
» Teaching a machine to learn
something
» Using TensorFlow to do machine
learning
366 BOOK 4
Understanding Neural Networks
» The input layer of neurons
» An arbitrary amount of hidden layers of neurons
» An output layer of neurons connecting to the world
» A set of weights and biases between each neuron level
» A choice of activation function for each hidden layer of neurons
» A loss function that will provide “overtraining” of the network
Weights are given for each of the inter-neural connecting lines.
A neuron
are called weights.
A two-layer
neural network.
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 367
Layers of neurons
-
feed-forward input because the data feeds in
-
backpropagation and simulates what people do when performing
a task using an iterative approach for trial and error.
Feed-forward and
backpropagation.
368 BOOK 4
an epoch
to provide training to learn complex tasks.
-
-
retraining.
Weights and biases
-
thing we call the biases
-
BACKPROPAGATION
In the human brain, learning happens because of the addition of new connections
The methods used to propagate from results to previous layers (also called feedback)
-
tion of the math and how it is used, check out Machine Learning For Dummies, by John
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 369
The activation function
Loss function
loss function compares the result of our neural network to the desired results.
current results are.
channel that will improve our neural network.
of the network.
sigmoid function.
370 BOOK 4
example.
Building a Simple Neural
Network in Python
The neural-net Python code
calculations.
The Truth Table (a Three-Input XOR Gate)
for the Neural Network
X2 X3
0 0 0
0 0 0
0 0 0
0 0
0 0 0
0 0
0 0
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 371
USES OF XOR FUNCTIONS
-
ware. You can use an XOR gate as part of a one-bit adder that adds one bit to another
bit (and provides a carry bit to string them together to make big adders), as well as
stringing them together to build a pseudo-random number generator.
The coolest application we know of an XOR gate has to do with coding algorithms and
kind of a mashup of your data), and then you have a more robust data to transmit long
distances (like from Pluto, our former ninth planet), which can have all sorts of events
that cause noise in the data, corrupting bits and bytes.
correcting any errors (up to a point) so you have good data. This allows us to transmit
data much further with less power, because with the Reed-Solomon code you become
error-tolerant.
and derivatives ended up on projects like the Hubble Space Telescope and on John’s
those little XOR gates.
NumPy
tensor
algebra packages.
It is now the preferred library and is also a part of SciPy and MatPlotLib, two common
BOOK 4
-
# 2 Layer Neural Network in NumPy
import numpy as np
# X = input of our 3 input XOR gate
# set up the inputs of the neural network (right from the table)
X = np.array(([0,0,0],[0,0,1],[0,1,0], 
[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float)
# y = our output of our neural network
y = np.array(([1], [0], [0], [0], [0], 
[0], [0], [1]), dtype=float)
# what value we want to predict
xPredicted = np.array(([0,0,1]), dtype=float)
X = X/np.amax(X, axis=0) # maximum of X input array
# maximum of xPredicted (our input data for the prediction)
xPredicted = xPredicted/np.amax(xPredicted, axis=0)
# set up our Loss file for graphing
lossFile = open("SumSquaredLossList.csv", "w")
class Neural_Network (object):
def __init__(self):
#parameters
self.inputLayerSize = 3 # X1,X2,X3
self.outputLayerSize = 1 # Y1
self.hiddenLayerSize = 4 # Size of the hidden layer
# build weights of each layer
# set to random values
# look at the interconnection diagram to make sense of this
# 3x4 matrix for input to hidden
self.W1 = 
np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
# 4x1 matrix for hidden layer to output
self.W2 = 
np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 373
def feedForward(self, X):
# feedForward propagation through our network
# dot product of X (input) and first set of 3x4 weights
self.z = np.dot(X, self.W1)
# the activationSigmoid activation function - neural magic
self.z2 = self.activationSigmoid(self.z)
# dot product of hidden layer (z2) and second set of 4x1 weights
self.z3 = np.dot(self.z2, self.W2)
# final activation function - more neural magic
o = self.activationSigmoid(self.z3)
return o
def backwardPropagate(self, X, y, o):
# backward propagate through the network
# calculate the error in output
self.o_error = y - o
# apply derivative of activationSigmoid to error
self.o_delta = self.o_error*self.activationSigmoidPrime(o)
# z2 error: how much our hidden layer weights contributed to output
# error
self.z2_error = self.o_delta.dot(self.W2.T)
# applying derivative of activationSigmoid to z2 error
self.z2_delta = self.z2_error*self.activationSigmoidPrime(self.z2)
# adjusting first set (inputLayer --> hiddenLayer) weights
self.W1 = X.T.dot(self.z2_delta)
# adjusting second set (hiddenLayer --> outputLayer) weights
self.W2 = self.z2.T.dot(self.o_delta)
def trainNetwork(self, X, y):
# feed forward the loop
o = self.feedForward(X)
# and then back propagate the values (feedback)
self.backwardPropagate(X, y, o)
def activationSigmoid(self, s):
# activation function
# simple activationSigmoid curve as in the book
return 1/(1 np.exp(-s))
374 BOOK 4
def activationSigmoidPrime(self, s):
# First derivative of activationSigmoid
# calculus time!
return s * (1 - s)
def saveSumSquaredLossList(self,i,error):
lossFile.write(str(i) "," str(error.tolist()) 'n')
def saveWeights(self):
# save this in order to reproduce our cool network
np.savetxt("weightsLayer1.txt", self.W1, fmt="%s")
np.savetxt("weightsLayer2.txt", self.W2, fmt="%s")
def predictOutput(self):
print ("Predicted XOR output data based on trained weights: ")
print ("Expected (X1-X3): n" str(xPredicted))
print ("Output (Y1): n" str(self.feedForward(xPredicted)))
myNeuralNetwork = Neural_Network()
trainingEpochs = 1000
#trainingEpochs = 100000
for i in range(trainingEpochs): # train myNeuralNetwork 1,000 times
print ("Epoch # " str(i) "n")
print ("Network Input : n" str(X))
print ("Expected Output of XOR Gate Neural Network: n" str(y))
print ("Actual Output from XOR Gate Neural Network: n" 
str(myNeuralNetwork.feedForward(X)))
# mean sum squared loss
Loss = np.mean(np.square(y - myNeuralNetwork.feedForward(X)))
myNeuralNetwork.saveSumSquaredLossList(i,Loss)
print ("Sum Squared Loss: n" str(Loss))
print ("n")
myNeuralNetwork.trainNetwork(X, y)
myNeuralNetwork.saveWeights()
myNeuralNetwork.predictOutput()
Breaking down the code
# 2 Layer Neural Network in NumPy
import numpy as np
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 375
-
sudo apt-get install python3-numpy
# X = input of our 3 input XOR gate
# set up the inputs of the neural network (right from the table)
X = np.array(([0,0,0],[0,0,1],[0,1,0], 
[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float)
# y = our output of our neural network
y = np.array(([1], [0], [0], [0], [0], 
[0], [0], [1]), dtype=float)
# what value we want to predict
xPredicted = np.array(([0,0,1]), dtype=float)
X = X/np.amax(X, axis=0) # maximum of X input array
# maximum of xPredicted (our input data for the prediction)
xPredicted = xPredicted/np.amax(xPredicted, axis=0)
# set up our Loss file for graphing
lossFile = open("SumSquaredLossList.csv", "w")
the network.
class Neural_Network (object):
def __init__(self):
#parameters
self.inputLayerSize = 3 # X1,X2,X3
self.outputLayerSize = 1 # Y1
self.hiddenLayerSize = 4 # Size of the hidden layer
376 BOOK 4
Set all the network weights to random values to start.
# build weights of each layer
# set to random values
# look at the interconnection diagram to make sense of this
# 3x4 matrix for input to hidden
self.W1 = 
np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
# 4x1 matrix for hidden layer to output
self.W2 = 
np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
def feedForward(self, X):
# feedForward propagation through our network
# dot product of X (input) and first set of 3x4 weights
self.z = np.dot(X, self.W1)
# the activationSigmoid activation function - neural magic
self.z2 = self.activationSigmoid(self.z)
# dot product of hidden layer (z2) and second set of 4x1 weights
self.z3 = np.dot(self.z2, self.W2)
# final activation function - more neural magic
o = self.activationSigmoid(self.z3)
return o
And now we add the backwardPropagate function that implements the real trial-
and-error learning that our neural network uses.
def backwardPropagate(self, X, y, o):
# backward propagate through the network
# calculate the error in output
self.o_error = y - o
# apply derivative of activationSigmoid to error
self.o_delta = self.o_error*self.activationSigmoidPrime(o)
# z2 error: how much our hidden layer weights contributed to output
# error
self.z2_error = self.o_delta.dot(self.W2.T)
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 377
# applying derivative of activationSigmoid to z2 error
self.z2_delta = self.z2_error*self.activationSigmoidPrime(self.z2)
# adjusting first set (inputLayer --> hiddenLayer) weights
self.W1 = X.T.dot(self.z2_delta)
# adjusting second set (hiddenLayer --> outputLayer) weights
self.W2 = self.z2.T.dot(self.o_delta)
backwardPropagate
and the feedForward functions each time we train the network.
def trainNetwork(self, X, y):
# feed forward the loop
o = self.feedForward(X)
# and then back propagate the values (feedback)
self.backwardPropagate(X, y, o)
function follows.
def activationSigmoid(self, s):
# activation function
# simple activationSigmoid curve as in the book
return 1/(1 np.exp(-s))
def activationSigmoidPrime(self, s):
# First derivative of activationSigmoid
# calculus time!
return s * (1 - s)
weights.
def saveSumSquaredLossList(self,i,error):
lossFile.write(str(i) "," str(error.tolist()) 'n')
def saveWeights(self):
# save this in order to reproduce our cool network
np.savetxt("weightsLayer1.txt", self.W1, fmt="%s")
np.savetxt("weightsLayer2.txt", self.W2, fmt="%s")
378 BOOK 4
trained weights.
def predictOutput(self):
print ("Predicted XOR output data based on trained weights: ")
print ("Expected (X1-X3): n" str(xPredicted))
print ("Output (Y1): n" str(self.feedForward(xPredicted)))
myNeuralNetwork = Neural_Network()
trainingEpochs = 1000
#trainingEpochs = 100000
for i in range(trainingEpochs): # train myNeuralNetwork 1,000 times
print ("Epoch # " str(i) "n")
print ("Network Input : n" str(X))
print ("Expected Output of XOR Gate Neural Network: n" str(y))
print ("Actual Output from XOR Gate Neural Network: n" 
str(myNeuralNetwork.feedForward(X)))
# mean sum squared loss
Loss = np.mean(np.square(y - myNeuralNetwork.feedForward(X)))
myNeuralNetwork.saveSumSquaredLossList(i,Loss)
print ("Sum Squared Loss: n" str(Loss))
print ("n")
myNeuralNetwork.trainNetwork(X, y)
value.
myNeuralNetwork.saveWeights()
myNeuralNetwork.predictOutput()
Running the neural-network code
python3 2LayerNeuralNetworkCode.py
-
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 379
» weightsLayer1.txt:
» weightsLayer2.txt:
» SumSquaredLossList.csv:
Epoch # 999
Network Input :
[[0. 0. 0.]
[0. 0. 1.]
[0. 1. 0.]
[0. 1. 1.]
[1. 0. 0.]
[1. 0. 1.]
[1. 1. 0.]
[1. 1. 1.]]
Expected Output of XOR Gate Neural Network:
[[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]
[1.]]
Actual Output from XOR Gate Neural Network:
[[0.93419893]
[0.04425737]
[0.01636304]
[0.03906686]
[0.04377351]
[0.01744497]
[0.0391143 ]
[0.93197489]]
Sum Squared Loss:
0.0020575319565093496
Predicted XOR output data based on trained weights:
Expected (X1-X3):
[0. 0. 1.]
380 BOOK 4
Output (Y1):
[0.04422615]
weights with random numbers at the start of the run.
have given perfect results.
Epoch # 99999
Network Input :
[[0. 0. 0.]
[0. 0. 1.]
[0. 1. 0.]
[0. 1. 1.]
The Loss
function during
training.
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 381
[1. 0. 0.]
[1. 0. 1.]
[1. 1. 0.]
[1. 1. 1.]]
Expected Output of XOR Gate Neural Network:
[[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]
[1.]]
Actual Output from XOR Gate Neural Network:
[[9.85225608e-01]
[1.41750544e-04]
[1.51985054e-04]
[1.14829204e-02]
[1.17578404e-04]
[1.14814754e-02]
[1.14821256e-02]
[9.78014943e-01]]
Sum Squared Loss:
0.00013715041859631841
Predicted XOR output data based on trained weights:
Expected (X1-X3):
[0. 0. 1.]
Output (Y1):
[0.00014175]
Using TensorFlow for the
same neural network
applications and so has libraries and functions designed for those applications.
tensor is a multi-
-
BOOK 4
implemented in terms of performing operations matrix of data and then moving
Installing the TensorFlow Python library
https://www.tensorflow.org/install/pip.
-
the tutorial referenced above.
INTRODUCING TENSORS
data. But an additional discussion will be helpful in getting your head wrapped around
• Scalars: A scalar can be thought of as a single piece of data. There is one and
• Vectors: A vector
think of a vector as an arrow located on a plane. It looks like a ray on the plane.
have coordinates for 3D space: x, y, and z.
• Tensors: Vectors are special cases of tensors. A tensor -
-
vidual numbers, vectors as ordered sets of numbers, and tensors by a single or
multidimensional array of numbers. Here is a great non-mathematical introduction
to tensors: https://www.youtube.com/watch?v=f5liqUk0ZTw.
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 383
Building a Python Neural Network
-
code.
-
provides the excellent and intuitive set of abstractions and functions whereas
1. Load and format your data.
2.
3. Compile the model.
Our TensorFlow
three-layer neural
network.
384 BOOK 4
4. Fit and train your model.
5. Evaluate the model.
Loading your data
model and layers
Compiling your model
Fitting and training your model
TensorFlowKeras.py
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers import Activation, Dense
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 385
import numpy as np
# X = input of our 3 input XOR gate
# set up the inputs of the neural network (right from the table)
X = np.array(([0,0,0],[0,0,1],[0,1,0],
[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float)
# y = our output of our neural network
y = np.array(([1], [0], [0], [0], [0],
[0], [0], [1]), dtype=float)
model = tf.keras.Sequential()
model.add(Dense(4, input_dim=3, activation='relu',
use_bias=True))
#model.add(Dense(4, activation='relu', use_bias=True))
model.add(Dense(1, activation='sigmoid', use_bias=True))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy'])
print (model.get_weights())
history = model.fit(X, y, epochs=2000,
validation_data = (X, y))
model.summary()
# printing out to file
loss_history = history.history["loss"]
numpy_loss_history = np.array(loss_history)
np.savetxt("loss_history.txt", numpy_loss_history,
delimiter="n")
binary_accuracy_history = history.history["binary_accuracy"]
numpy_binary_accuracy = np.array(binary_accuracy_history)
np.savetxt("binary_accuracy.txt", numpy_binary_accuracy, delimiter="n")
print(np.mean(history.history["binary_accuracy"]))
result = model.predict(X ).round()
print (result)
386 BOOK 4
model and results.
Breaking down the code
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers import Activation, Dense
import numpy as np
# X = input of our 3 input XOR gate
# set up the inputs of the neural network (right from the table)
X = np.array(([0,0,0],[0,0,1],[0,1,0],
[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float)
# y = our output of our neural network
y = np.array(([1], [0], [0], [0], [0],
[0], [0], [1]), dtype=float)
relu
See the commented model.add
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 387
model = tf.keras.Sequential()
model.add(Dense(4, input_dim=3, activation='relu',
use_bias=True))
#model.add(Dense(4, activation='relu', use_bias=True))
model.add(Dense(1, activation='sigmoid', use_bias=True))
mean_squared_error -
binary_accuracy
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy'])
print (model.get_weights())
-
X y
validation_data -
work in each epoch and generates val_acc and val_loss
each epoch as stored in the history variable.
history = model.fit(X, y, epochs=2000,
validation_data = (X, y))
model.summary()
388 BOOK 4
history variable that we would like to
graph.
# printing out to file
loss_history = history.history["loss"]
numpy_loss_history = np.array(loss_history)
np.savetxt("loss_history.txt", numpy_loss_history,
delimiter="n")
binary_accuracy_history = history.history["binary_accuracy"]
numpy_binary_accuracy = np.array(binary_accuracy_history)
np.savetxt("binary_accuracy.txt", numpy_binary_accuracy, delimiter="n")
the inputs of X round
<0.1 = "0" and
>0.9 = "1". We also calculated the average of all the binary_accuracy values of
print(np.mean(history.history["binary_accuracy"]))
result = model.predict(X ).round()
print (result)
Evaluating the model
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime
version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not
match runtime version 3.5
return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type
size changed, may indicate binary incompatibility. Expected 432, got 412
return f(*args, **kwds)
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 389
python3 TensorFlowKeras.py in our termi-
Verbose model.fit
...
Epoch 1999/2000
8/8 [==============================] - 0s 2ms/step - loss: 0.0367 - binary_
accuracy: 1.0000 - val_loss: 0.0367 - val_binary_accuracy: 1.0000
Epoch 2000/2000
8/8 [==============================] - 0s 2ms/step - loss: 0.0367 - binary_
accuracy: 1.0000 - val_loss: 0.0367 - val_binary_accuracy: 1.0000
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 4) 16
_________________________________________________________________
dense_1 (Dense) (None, 1) 5
=================================================================
Total params: 21
Trainable params: 21
Non-trainable params: 0
_________________________________________________________________
0.8436875
[[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]
[1.]]
model.predict function call at the end matches our
relu -
390 BOOK 4
Changing to a three-layer neural
Results of
the two-layer
training.
BACKPROPAGATION IN KERAS
-
tion and how it was a fundamental part of neural networks. However, now we have
for you. If you want to modify how it is doing it, the easiest way is to modify the optimi-
module.compile
work to dramatically modify the backpropogation algorithm in Keras, but it can be done.
-
-
Building
a
Neural
Network
in
Python
CHAPTER 2 Building a Neural Network in Python 391
model.add(Dense(4, input_dim=3, activation='relu',
use_bias=True))
#model.add(Dense(4, activation='relu', use_bias=True))
model.add(Dense(1, activation='sigmoid', use_bias=True))
model.add(Dense(4, input_dim=3, activation='relu',
use_bias=True))
model.add(Dense(4, activation='relu', use_bias=True))
model.add(Dense(1, activation='sigmoid', use_bias=True))
8/8 [==============================] - 0s 2ms/step - loss: 0.0153 - binary_
accuracy: 1.0000 - val_loss: 0.0153 - val_binary_accuracy: 1.0000
Epoch 2000/2000
8/8 [==============================] - 0s 2ms/step - loss: 0.0153 - binary_
accuracy: 1.0000 - val_loss: 0.0153 - val_binary_accuracy: 1.0000
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 4) 16
_________________________________________________________________
dense_1 (Dense) (None, 4) 20
_________________________________________________________________
dense_2 (Dense) (None, 1) 5
=================================================================
Total params: 41
Trainable params: 41
Non-trainable params: 0
_________________________________________________________________
0.930375
[[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]
[1.]]
BOOK 4
parameters and make changes.
Results of the
three-layer
training.
GUI (GRAPHICAL USER
INTERFACE) TO RUN TensorFlow?
graphs in this chapter. Most of the time, we use our machines in a terminal window,
terminal windows for editing. That big advantage is called TensorBoard. TensorBoard
CHAPTER 3 Doing Machine Learning in Python 393
Doing Machine Learning
in Python
W
-
-
-
IN THIS CHAPTER
» Teaching a machine to learn
something
» How machines learn
» Understand the basics of machine
learning
» Using TensorFlow to do machine
learning
394 BOOK 4
-
Learning by Looking for Solutions
in All the Wrong Places
best
local maxima, and it may or may not be the best
» Supervised learning: This type of algorithm builds a model of data that
contains both inputs and outputs. The data is known as training data. This is
the kind of machine learning we show in this chapter.
» Unsupervised learning: For this type of algorithm, the data contains only the
inputs, and the algorithms look for the structures and patterns in the data.
» Reinforcement learning: This area is concerned with software taking actions
based on some kind of cumulative reward. These algorithms do not assume
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
knowledge of an exact mathematical model and are used when exact models
are unavailable. This is the most complex area of machine learning, and the
one that may bear the most fruit in the future.
Classifying Clothes with Machine Learning
Training and Learning with TensorFlow
A bit of the
Fashion-MNIST
database.
BOOK 4
1. Load and format your data.
2.
3. Compile the model.
4. Fit and train your model.
5. Evaluate the model.
Setting Up the Software Environment
-
www.raspberrypi.org
-
name of your machine
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python 397
Creating a Machine-Learning Network
MNIST
» 0 T-shirt/top
» 1 Trouser
» 2 Pullover
» 3 Dress
» 4 Coat
» 5 Sandal
» 6 Shirt
A full GUI on the
Raspberry Pi.
BOOK 4
» 7 Sneaker
» 8 Bag
» 9 Ankle boot
dataset
Training the network
Testing our network
Fashion_MNIST Fashion_
MNIST
Fashion_MNIST
Fashion_MNIST
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python 399
# Import Fashion MNIST
fashion_mnist = input_data.read_data_sets('input/data',
one_hot=True)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) 
= fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser',
'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu' ))
model.add(tf.keras.layers.Dense(10, activation='softmax' ))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
# test with 10,000 images
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('10,000 image Test accuracy:', test_acc)
Breaking down the code
BOOK 4
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
1. Load and format your data.
This time we are using the built-in data-set reading capability. It knows what
this data is because of the import statement from tensorflow.examples.
tutorials.mnist in the preceding code.
# Import Fashion MNIST
fashion_mnist = input_data.read_data_sets('input/data',
one_hot=True)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) 
= fashion_mnist.load_data()
Here we give some descriptive names to the ten classes within the Fashion_
MNIST data.
class_names = ['T-shirt/top', 'Trouser',
'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
Here we change all the images to be scaled from 0.0–1.0 rather than 0–255.
train_images = train_images / 255.0
test_images = test_images / 255.0
2.
Again, this is where the real power of Keras shines. It is very simple to add
more neural layers, and to change their sizes and their activation functions.
relu), in this case with
softmax
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu' ))
model.add(tf.keras.layers.Dense(10, activation='softmax' ))
3. Compile your model.
We are using the loss function sparse_categorical_crossentropy. This function
stochastic optimization) is a good default optimizer. It provides a method well
suited for problems that are large in terms of data and/or parameters.
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_
crossentropy',
metrics=['accuracy'])
Sparse categorical crossentropy is a loss function used to measure the
error between categories across the data set. Categorical refers to the fact that
Sparse refers to
Entropy
measure of disorder) refers to the mix of data between the categories.
4. Fit and train your model.
I chose the number of epochs as only 5 due to the time it takes to run the
model for our examples. Feel free to increase! Here we load the NumPy arrays
model.fit(train_images, train_labels, epochs=5)
5. Evaluate the model.
The model.evaluate function is used to compare the outputs of your trained
network in each epoch and generates test_acc and test_loss for your
information in each epoch as stored in the history variable.
# test with 10,000 images
test_loss, test_acc = model.evaluate(test_images,
test_labels)
print('10,000 image Test accuracy:', test_acc)
BOOK 4
Results of the training and evaluation
Epoch 1/5
60000/60000 [==============================] - 44s 726us/step - loss: 0.5009 -
acc: 0.8244
Epoch 2/5
60000/60000 [==============================] - 42s 703us/step - loss: 0.3751 -
acc: 0.8652
Epoch 3/5
60000/60000 [==============================] - 42s 703us/step - loss: 0.3359 -
acc: 0.8767
Epoch 4/5
60000/60000 [==============================] - 42s 701us/step - loss: 0.3124 -
acc: 0.8839
Epoch 5/5
60000/60000 [==============================] - 42s 703us/step - loss: 0.2960 -
acc: 0.8915
10000/10000 [==============================] - 4s 404us/step
10,000 image Test accuracy: 0.873
Testing a single test image
Fashion_MNIST
#run test image from Fashion_MNIST data
img = test_images[15]
img = (np.expand_dims(img,0))
singlePrediction = model.predict(img,steps=1)
print ("Prediction Output")
print(singlePrediction)
print()
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
print ("Our Network has concluded that the image number '15' is a "
class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
Prediction Output
[[1.2835168e-05 9.9964070e-01 6.2637120e-08 3.4126092e-04 4.4297972e-06
7.8450663e-10 6.2759432e-07 9.8717527e-12 1.2729484e-08 1.1002166e-09]]
Our Network has concluded that the image number '15' is a Trouser
99% Confidence Level
Testing on external pictures
Image 15 from
the Fashion-
MNIST test
database.
BOOK 4
# run Our test Image
# read test dress image
imageName = "Dress28x28.JPG"
dress hanging
The dress at
28x28 pixels.
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
testImg = Image.open(imageName)
testImg.load()
data = np.asarray( testImg, dtype="float" )
data = tf.image.rgb_to_grayscale(data)
data = data/255.0
data = tf.transpose(data, perm=[2,0,1])
singlePrediction = model.predict(data,steps=1)
print ("Prediction Output")
print(singlePrediction)
print()
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
The results, round 1
Prediction Output
[[1.2717753e-06 1.3373902e-08 1.0487850e-06 3.3525557e-11 8.8031484e-09
7.1847245e-10 1.1177938e-04 8.8322977e-12 9.9988592e-01 3.2957085e-12]]
Our Network has concluded that the file 'Dress28x28.JPG' is a Bag
99% Confidence Level
wait for
it
Prediction Output
[[3.4407502e-33 0.0000000e 00 2.5598763e-33 0.0000000e 00 0.0000000e 00
BOOK 4
0.0000000e 00 2.9322060e-17 0.0000000e 00 1.0000000e 00 1.5202169e-39]]
Our Network has concluded that the file 'Dress28x28.JPG' is a Bag
100% Confidence Level
The CNN model code
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
# Import Fashion MNIST
fashion_mnist = input_data.read_data_sets('input/data',
one_hot=True)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) 
= fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser',
'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0
test_images = test_images / 255.0
# Prepare the training images
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
# Prepare the test images
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
model = tf.keras.Sequential()
input_shape = (28, 28, 1)
model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu',
input_shape=input_shape))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.25))
BOOK 4
model.add(tf.keras.layers.Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
# test with 10,000 images
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('10,000 image Test accuracy:', test_acc)
#run test image from Fashion_MNIST data
img = test_images[15]
img = (np.expand_dims(img,0))
singlePrediction = model.predict(img,steps=1)
print ("Prediction Output")
print(singlePrediction)
print()
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print ("Our Network has concluded that the image number '15' is a "
class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
The results, round 2
10,000 image Test accuracy: 0.8601
Prediction Output
[[5.9128129e-06 9.9997270e-01 1.5681641e-06 8.1393973e-06 1.5611777e-06
7.0504888e-07 5.5174642e-06 2.2484977e-07 3.0045830e-06 5.6888598e-07]]
Our Network has concluded that the image number '15' is a Trouser
-
-
Visualizing with MatPlotLib
-
pip3 install matplotlib
We add the history model.fit
BOOK 4
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
# Import Fashion MNIST
fashion_mnist = input_data.read_data_sets('input/data', one_hot=True)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.
load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu' ))
model.add(tf.keras.layers.Dense(10, activation='softmax' ))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=2)
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
# Get training and test loss histories
training_loss = history.history['loss']
accuracy = history.history['acc']
# Create count of the number of epochs
epoch_count = range(1, len(training_loss) 1)
# Visualize loss history
plt.figure(0)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, accuracy, 'b--')
plt.legend(['Training Loss', 'Accuracy'])
plt.xlabel('Epoch')
plt.ylabel('History')
plt.show(block=False);
plt.pause(0.001)
test_loss, test_acc = model.evaluate(test_images, test_labels)
#run test image from Fashion_MNIST data
img = test_images[15]
plt.figure(1)
plt.imshow(img)
plt.show(block=False)
plt.pause(0.001)
img = (np.expand_dims(img,0))
singlePrediction = model.predict(img,steps=1)
print ("Prediction Output")
print(singlePrediction)
print()
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print ("Our Network has concluded that the image number '15' is a "
class_names[NumberElement])
BOOK 4
print (str(int(Element*100)) "% Confidence Level")
print('Test accuracy:', test_acc)
# read test dress image
imageName = "Dress28x28.JPG"
testImg = Image.open(imageName)
plt.figure(2)
plt.imshow(testImg)
plt.show(block=False)
plt.pause(0.001)
testImg.load()
data = np.asarray( testImg, dtype="float" )
data = tf.image.rgb_to_grayscale(data)
data = data/255.0
data = tf.transpose(data, perm=[2,0,1])
singlePrediction = model.predict(data,steps=1)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print(NumberElement)
print(Element)
print(singlePrediction)
print ("Our Network has concluded that the file '" imageName "' is a
" class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
plt.show()
-
Doing
Machine
Learning
in
Python
CHAPTER 3 Doing Machine Learning in Python
Learning More Machine Learning
» Machine Learning for Dummies, John Paul Mueller and Luca Massaron
» Deep Learning with Python, Francois Chollet
»
Dummies, John Paul Mueller and Luca Massaron
Our Raspberry
MatPlotLib
visualization.
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
CHAPTER 4 415
Exploring More AI
A
fter reading the previous three chapters, you have learned quite a bit
networks and machine learning. There is a lot more to AI than just these
two things, though. We could look at advanced searching (not Google searching
Instead, in this chapter, we talk about other ways of doing AI software beyond the
our large neural network? Sounds like we could use some bigger iron to accom-
plish more training in less time. That’s what this chapter is about.
The Raspberry Pi is an inexpensive full-blown computing device. The Raspberry Pi
3B
» CPU: Broadcom quad-core 64bit processor @ 1.4GHz
» GPU: Broadcom Videocore-IV
»
»
»
416 BOOK 4
» RAM: 1GB SDRAM
» Networking: Gigabit Ethernet, 802.11b/g/n/ac WiFi
» Storage: SD card
How does this stack up? For a $35 computer, very well. But for a dedicated AI
computer, not so much.
The problems are not enough RAM (1GB isn’t very much, especially for a Rasp-
processing chip.
There are two mitigating circumstances that keep the Raspberry Pi in the running
can use the Raspberry Pi to control processors and AI hardware up on the cloud for
all the computationally heavy lifting.
The Raspberry Pi
processing chip
containing the
Videocore-IV.
Exploring
More
AI
in
Python
CHAPTER 4 417
Remember from our previous chapter that the bulk of the computer time in build-
ing any kind of machine-learning AI system is for training and that when that
training is done, it doesn’t take a lot of processing to actually characterize an
unknown or new picture. This means you can train on one big machine and then
deploy on a simpler computer such as the Raspberry Pi in the application. This
doesn’t work all the time (especially if you want to keep learning as the program
programs on much simpler and less expensive hardware.
Performing AI analysis or training on the small computers that are connected to
a network, is called edge computing
edge of the network.
3B
The Videocore-IV is a low-power mobile graphics processor. It is a two-dimensional DSP
(digital signal processor) that is set up as basically a four-GPU core unit. These GPU core
units are called slices and can be very roughly compared to GPU computer units, such as
those used by AMD and Nvidia (which can have 256, 512, or more individual GPU units,
far outclassing the Videocore 4 units) to power their GPU cards, which are now proving
to be very popular with AI researchers and hobbyists.
This processor is really designed to be used in video encoding and decoding applica-
tions and not so much for AI use. However, some researchers have made use of the
four slices to accelerate neural-network processing on the Raspberry Pi to achieve up to
about three times the performance of the four-core main processor used alone.
One of the main barriers to using the Videocore on the Raspberry Pi for AI type of appli-
available under NDA (non-disclosure agreements), which do not go along with open-
source development. However, you can now get full documentation and the complete
source code for the Raspberry Pi 3B graphics stack under a very nonrestrictive BSD
license, which should provide a path forward.
418 BOOK 4
It turns out that there have been a number of companies starting to build special-
ized AI compute sticks, many of which can be used on the Raspberry Pi. Typically,
Python libraries, that support using these sticks. Two of the most interesting
ones are
» The Movidius NCS stick
plugs into the USB port of the Raspberry Pi or other compute and provides
hardware support for deep learning based analysis (refer to Chapters 1–3).
For example, you can use the Amazon cloud to perform image analysis, process-
moves your computationally expensive task from your Raspberry Pi to the cloud.
This does cost money and bandwidth (and latency in your system) to do this.
Doing the analysis with your trained deep learning neural network on the edge
by using a NCS stick can help and can possibly allow you to disconnect your
device running on the edge of the network from the Internet entirely. It runs
around 60X faster than doing image analysis on the Raspberry Pi and costs less
You can do facial recognition, text analysis, monitoring and maintenance
using this NCS stick. Pretty cool!
There is one concept that we need to emphasize here, however. The NCS
but it is
not used for training models! You still need to build and train the models. It
The Intel
Movidius Neural
Compute Stick 2.
Exploring
More
AI
in
Python
CHAPTER 4 419
done.
» The Google Edge TPU (tensor processing
unit) has a USB Type-C socket that can be plugged into an Linux-based system
to provide accelerated machine learning analysis and inferences. Does the
word tensor sound familiar? Tensors are matrices like in our neural-network
Well, it turns out, much like the Intel NCS stick above, this device is all about
executing trained machine-learning models. We still train the machine-learning
networks using other techniques, and then we execute the model on the stick.
The Google Edge
TPU accelerator.
Oh, boy. In the next four years, this type of specialized hardware for running machine-
come from Google, Intel, Nvidia, AMD, Qualcomm, and a number of other smaller
companies from around the world. Everybody is starting to climb on the AI accelerator
hardware bandwagon.
420 BOOK 4
In the tech industry, everyone loves to use buzzwords such as the cloud. Often, the
use of such language results in arbitrary and nebulous terms that leave consumers
When a company says, “your data is in the cloud” or that “you can work in the
the cloud” data is on the ground and is stored somewhere in a data center with
a bunch of servers that are more similar to your PC or Mac than you may think.
rather than on your local machine. This is correct to a degree, but nothing really
runs on the Internet; it runs on machines that are connected -
standing that in-the-cloud software runs on servers and is not “just out there”
tends to really quickly demystify the cloud and its functions.
If you have two computers networked together and use the other computer for a
data server, you have your own “cloud.”
This goes for basic services like storing your data in the cloud, but there is much
more than just storage available on the cloud and that is where it gets really
interesting.
The advantage of using the cloud is that you can use services and storage unavail-
able to you on your local network and (in one of the most important game chang-
computing needs on a dynamic basis.
-
nections some of the time. This limits the cloud in applications such as self-driving
cars that aren’t guaranteed to have good Internet access all the time. Interestingly,
don’t want to stay connected to the net all the time for power considerations.
So, how do you use the cloud? That depends on the service and vendor, but in
machine-learning applications, the most common way is to set up the Python on
a computer that calls cloud-based functions and applications. All cloud vendors
provide examples.
What is a great consumer example of cloud usage? The Amazon Echo and Alexa.
It listens to you, compresses the speech data, sends it to the Amazon AWS cloud,
translates and interprets your data and then sends back a verbal response or com-
mands to make your lights come on.
Exploring
More
AI
in
Python
CHAPTER 4 421
A number of cloud providers for storage and services exist and more are arriving
all the time. The top four cloud providers for AI at the time of this writing are
» Google cloud
» Amazon Web Services
» IBM cloud
» Microsoft Azure
stick above, can accelerate your AI applications. Much of the Google cloud’s func-
For example, the Cloud Vision API can detect objects, logos, and landmarks within
City application called ParkMyRide, which uses a Raspberry Pi–based solar-
powered camera to take pictures of the street and determines street parking avail-
street to Google and gets back the number of cars found and where they are in the
picture. They then supply this information to a smartphone app which displays it
graphically. Pretty neat.
-
AI-powered applications to create new services for customers to use.
and supplying this expertise to businesses. Many of these cloud services are built
on the consumer product versions, so as Alexa improves, for example, the cloud
services also improve.
learning visualization/creation tools, vision recognition, and analysis.
422 BOOK 4
The IBM cloud has gotten a bad rap over the past few years for being hard to use.
In the past couple of years, it has gotten much better. IBM merged its three big
available, so it is still hard to get going, but there is much better control and con-
sistency over the process.
Their machine-learning environment is called the Watson Studio and is used to
build and train AI models in one integrated environment. They also provide huge
management platforms available.
One of the cool things they have is a service called Watson Personality Insights
that predicts personality characteristics, needs, and values through written text.
What would Watson Personality make of the authors of this book? We will run the
-
» AI services
» AI tools and frameworks
» AI infrastructures
Similar to Amazon and Google, their AI applications are built on consumer prod-
ucts that Microsoft has produced. Azure also has support for specialized FPGA
-
-
erators. Microsoft is one of the largest, if not the largest, customer of the Intel
Movidius chips.
They have products for machine learning, IOT toolkits, and management services,
custom silicon AI infrastructure, and a container service that can turn your inside
applications into cloud apps.
Microsoft Azure is the one to watch for some pretty spectacular innovations.
Exploring
More
AI
in
Python
CHAPTER 4 423
part of the PC experience for decades. People often hunt for the latest and great-
est graphics card to make their PCs better gaming machines. One thing becomes
-
high-resolution graphics is computationally expensive, and the way to solve that
is to build graphics cards out of computers that were designed to do graphics to
computer core that is designed to work with graphics.
which dramatically improved video resolution and frame rates in games. One
thing to remember is that graphics algorithms are constructed using data struc-
tures called matrices (or tensors
Wait. Tensors? Matrices? This sounds suspiciously like the kind of data structures
we use in AI and machine learning. Because of the way machine learning and deep
Regardless of the type of neural network used, all the techniques rely on per-
a multitude of images or data points are fed to the network and then trained
Nvidia 256 Core
GPU chip.
424 BOOK 4
To speed up the training, these operations can be done in parallel, which turns out
-
puters perfect for machine learning applications. The combination of a powerful
-
grams. TensorFlow in particular has versions of the software that is designed to
To put it in perspective, our Raspberry Pi 3B has 4 processor cores and in some
can do a lot of fast training and executing machine learning networks using these
-
puters and hardware to support AI applications. There are starting to be even
more specialized chips. At last count, there are over 50 companies working on
chips that will accelerate AI functions.
Microsoft has built out infrastructure to support AI acceleration hardware in the
cloud. This is one of the big reasons to watch what Microsoft is doing.
The future is in more and more specialized hardware, especially as specialized
hardware gets easier and easier to deal with from the user software side.
If you are interested in furthering your knowledge and abilities in machine learn-
ing and AI, check out the following sources for project inspiration. The important
thing is to actually build programs and modify other people programs to really
learn the technology from experience.
Exploring
More
AI
in
Python
CHAPTER 4 425
» “Is Santa Claus Real?,” Varun Vohra, https://towardsdatascience.com/
is-santa-claus-real-9b7b9839776c
» “Keras and deep learning on the Raspberry Pi,” Adrian Rosebrock, https://www.
pyimagesearch.com/2017/12/18/keras-deep-learning-raspberry-pi/
»
Jain, https://medium.com/nanonets/how-to-easily-detect-objects-
with-deep-learning-on-raspberrypi-225f29635c74
» “Building a Cat Detector using Convolutional Neural Network,” Venelin Valkov,
https://medium.com/@curiousily/tensorflow-for-hackers-part-iii-
convolutional-neural-networks-c077618e590b
»
Reddy, https://medium.com/@bapireddy/real-time-image-classifier-
on-raspberry-pi-using-inception-framework-faccfa150909
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
Doing Data
Science with
Python
Contents at a Glance
CHAPTER 1: . . . . . . . . . . . . . . . . . . . 429
Working with Big, Big Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Cooking with Gas: The Five Step Process of Data Science. . . . . . . 432
CHAPTER 2: . . . . . . . . . . . . . . . . . . 437
Doing Your First Data Science Project . . . . . . . . . . . . . . . . . . . . . . . 440
CHAPTER 3: . . . . . . . . . . . 451
What Is Big Data?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Understanding the Google Cloud and BigQuery . . . . . . . . . . . . . . 452
Reading the Medicare Big Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Looking for the Most Polluted City in the World
on an Hourly Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
CHAPTER 1 429
TheFive Areas of
D
ata science impacts our modern lives in far more ways than you may think.
When you use Google or Bing or DuckDuckGo, you are using a very sophis-
ticated application of data science. The suggestions for other search terms
that come up when you are typing? Those come from data science.
Medical diagnoses and interpretations of images and symptoms are examples of
data science. Doctors rely on data science interpretations more and more these
days.
As with most of the topics in this book, data science looks intimidating to the
uninitiated. Inferences, data graphs, and statistics, oh my! However, just as in
examples, you can really get a handle on what data science is and what it isn’t.
In this chapter we cover just enough statistics and “asking questions of data” to
get you going and get some simple results. The purpose is to introduce you to the
use of Python in data science and talk about just enough theory to get you started.
If nothing else, we want to leave you with the process of data science and give
you a higher level of understanding of what is behind some of the talking heads
on television and the various press releases that come from universities. These
people are always citing results that come from big data analysis and are often
»
»
»
430 BOOK 5
overstating what they actually mean. An example of this is when one study says
what your results mean, beyond simple interpretations, is where the really hard
parts of data science and statistics meet and are worthy of a book all their own.
At the end of our data science journey, you will know more about the processes
involved in answering some of these questions.
There is a mystery to data science, but with just a little knowledge and a little
Python, we can penetrate the veil and do some data science.
Python and the myriad tools and libraries available can make data science much
more accessible. One thing to remember is that most scientists (including data
scientists) are not necessarily experts in computer science. They like to use tools
to simplify the coding and to allow them to focus on getting the answers and
performing the analysis of the data they want.
The media likes to throw around the notion of “big data” and how people can get
insights into consumer (and your) behavior from it. Big data is a term used to refer
to large and complex datasets that are too large for traditional data processing
software (read databases, spread sheets, and traditional statistics packages like
called the “Three V’s”: volume, variety, and velocity.
Volume
Volume refers to how big the dataset is that we are considering. It can be really,
than the population of China. There are over 250 billion
2.5 trillion posts. That is a lot of data. A really big amount of data.
And what about the upcoming world of IOT (Internet of Things)? Gartner, one
of the world’s leading analysis companies, estimates 22 billion devices by 2022.
That is 22 billion devices producing thousands of pieces of data. Imagine that you
are sampling the temperature in your kitchen once a minute for a year. That is
The
Five
Areas
of
Data
Science
CHAPTER 1 431
temperature and humidity measurements, and your house is producing 6 million
pieces of data from just one little IOT device per room. It gets crazy very quickly.
And look at your smartphone. Imagine how many pieces of data it produces in a
day. Location, usage, power levels, cellphone connectivity spews out of your phone
into databases and your apps and application dashboards like Blynk constantly.
Sometimes (as we just recently found out from cellphone companies) location
information is being collected and sold even without your consent or opt-in.
Data, data, and more data. Data science is how we make use of this.
or location information. Sometimes they go together and sometimes they don’t.
sophisticated data structures and are hard to interpret and hard to get machines
to classify. Throw audio recordings in on that and you have a rather varied set of
data types.
Let’s talk about voice for a minute. In Book 4, I talked about Alexa being very
good at translating voice to text but not so good at assigning meaning to the text.
that people ask for things, make comments, and so on. Imagine, then, Alexa (and
Amazon) keeping track of all the queries and then doing data science on them to
ask for them. That is a lot of data and a lot of information that can be gathered.
Not just for nefarious reasons, but to build a system that better services the con-
sumer. It goes both ways.
Data science has a much better chance of identifying patterns if the voice has been
translated to text. It is much easier. However, in this translation you do lose a lot
of information about tone of voice, emphasis, and so on.
Velocity refers to how fast the data is changing and how fast it is being added to
billion pictures a day, so in the next
trillion -
ity dataset. A low velocity dataset (not changing at all) may be the set of tempera-
432 BOOK 5
This is a very complex topic. Data scientists have developed many methods for
processing data with variations of the three V’s. The three V’s describe the dataset
and give you an idea of the parameters of your particular set of data. The process
of gaining insights in data is called data analytics. In the next chapters, we focus
on gaining knowledge about analytics and on learning how to ask some data ana-
lytics questions using Python. After doing data science for a few years, you will be
VVVery good at managing these.
We generally can break down the process of doing science on data (especially big
for the complexity of the tasks. These steps are
and more techniques are being develop to do data analysis on big data (not surprisingly
Currently, data science generally refers to the process of working out insights from large
datasets of unstructured data. This means using predicative analytics, statistics and
machine learning to wade through the mass of data.
sets of data to achieve insights on that data.
With these somewhat vague descriptions, you can see how the two areas are moving
nitely call data analytics a subset of data science.
The
Five
Areas
of
Data
Science
CHAPTER 1 433
1. Capture the data
2. Process the data
3. Analyze the data
4. Communicate the results
5.
To have something to do analysis on, you have to capture some data. In any real-
world situation, you probably have a number of potential sources of data. Inven-
tory them and decide what to include. Knowing what to include requires you to
the upcoming analysis. Sometimes your goals can be vague in that sometimes,
“you just want to see what you can get” out of the data.
If you can, integrate your data sources so it is easy to get to the information you
In my humble opinion, this is the part of data science that should be easy, but it
almost never is. I’ve seen data scientists spend months massaging their data so
they can process and trust the data. You need to identify anomalies and outliers,
-
sistent. And all this has to be done appropriately so as not to take out data that
is important to your upcoming analysis work. It’s not easy to do in many cases.
If you have house room temperatures that are 170 degrees C, it is easy to see that
this data is wrong and inconsistent. (Well, unless your house is burning down.)
Cleaning and processing your data needs to be done carefully or else you will bias
and maybe destroy the ability to do good inferences or get good answers down the
line. In the real world, expect to spend a lot of time doing this step.
Oh, and one more cleaning thing to worry about, budding data scientist con-
sumers are giving more and more false and misleading data online. According to
Marketing Week in 2015, 60 percent of consumers provide intentionally incorrect
information when submitting data online.
We humbly admit to doing this all the time to online marketing forms and even
to political pollsters, especially when we sense a political agenda in the questions.
Bad boys we are.
434 BOOK 5
Understand that it only takes a very small amount of disproportionate informa-
tion to dramatically devalue a database. More food for thought.
By the time you have expended all the energy to get to actually looking at the data
-
atively simple. It is not. Analyzing big datasets for insights and inferences or even
asking complex questions is the hardest challenge, one that requires the most
human intuition in all of data science. Some questions, like “What is the average
huge amounts of data. But then you have the really, really useful questions such
A question such as that has layers and layers of complexity behind it. You want a
more people. Do you really
mean more people, or do you mean more revenue? Change the price to $0.01 per box,
is already more complex.
That is the hard part of analysis: Making sure we are asking the right question in
the right way of the right kind of data.
Analyzing the data requires skill and experience in statistics techniques like linear
using a variety of probability algorithms and formulas such as the incredibly coolly
named “Naïve Bayes” formulas and concepts. Although a full discussion of these
techniques is out of the scope of this book, we go through some examples later.
After you have crunched and mangled your data into the format you need and
then have analyzed the data to answer your questions, you need to present the
results to management or the customer. Most people visualize information better
and faster when they see it in a graphical format rather than just in text. There
are two major Python packages that data science people us: The language “R” and
The
Five
Areas
of
Data
Science
CHAPTER 1 435
MatPlotLib. We use MatPlotLib in displaying our “big data graphics.” (If you have
read the chapters on AI (Book 4), then you have already experienced MatPlotLib
This is the step in data science that everyone ignores. After you have asked your
just basically shut down and walk away to the next project. The problem with that
way of thinking is that there is a very reasonable chance that you will have to ask
more questions of the same data, sometimes quite far in the future. Is important
to archive and document the following information so you can restart the project
quickly, or even more likely in the future you will run across a similar set of prob-
Take time to preserve:
» The data and sources
»
» The queries and results you got from the queries
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
CHAPTER 2 437
Exploring Big Data
In this chapter we get into some of the tools and processes used by data scien-
tists to format, process, and query their data.
There are a number of Python-based tools and libraries (such as “R”) available,
but we decided to use NumPy for three reasons. First, it is one of the two most
popular tools to use for data science in Python. Second, many AI-oriented proj-
ects use NumPy (such as the one in our last chapter). And third, the highly useful
Python data science package, Pandas, is built on NumPy.
Pandas is turning out to be a very important package in data science. The way it
encapsulates data in a more abstract way makes it easier to manipulate, docu-
ment, and understand the transformations you make in the base datasets.
Finally, MatPlotLib is a good visualization package for the results of big data. It’s
-
ever, this has been ameliorated to some degree by new add-on packages, such as
“seaborne.”
All in all, these are reasonable packages to attack the data science problem and get
»
»
»
»
438 BOOK 5
key Python packages keep coming up:
» NumPy
» Pandas
» MatPlotLib
These are discussed in the next few sections.
NumPy adds big data-manipulation tools to Python such as large-array manip-
ulation and high-level mathematical functions for data science. NumPy is best at
excels at the creation and manipulation of multidimensional arrays known as ten-
sors or matrices. In Book 4, we used NumPy extensively in manipulating data and
tensors in neural networks and machine learning. It is an exceptional tool for
There are numerous good tutorials for NumPy on the web. A selection of some of
good step-by-step ones are:
» https://www.machine
learningplus.com/python/numpy-tutorial-part1-array-python-
examples/): A good introduction to matrices (also known as tensors) and
» https://www.tutorialspoint.com/numpy): A nice
» https://www.guru99.com/
numpy-tutorial.html): Less theory, but a bunch of great examples to
then performs various matrix-oriented operations on the maxtrix:
import numpy as np
x = np.array([[1,2],[3,4]])
Exploring
Big
Data
with
Python
CHAPTER 2 439
print(np.sum(x)) # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"
Python is great for munging data and preparing data, but not so great for data
with relational or labeled data more intuitive. In our opinion, it is the fundamental
building block for doing real-world data analysis in Python. It performs well with
tabular type of data (such as SQL tables or Excel spreadsheets) and is really good
with time-series data (like, say, temperatures taken on a hourly basis).
Remember our discussion on data massaging? Dealing with missing or bad data?
This is one of things that Pandas is designed for and does really well. It also
allows for complex hierarchical data structures, which can be accessed using Pan-
das functions in a very intuitive way. You can merge and join datasets as well as
convert many types of data into the ubiquitous Pandas data objects, DataFrames.
Pandas is based on NumPy and shares the speed of that Python library, and it can
achieve a large increase of speed over straight Python code involving loops.
Pandas DataFrames are a way to store data in rectangular grids that can easily
be overviewed. A DataFrame can contain other DataFrames, a one-dimensional
Book 4 on neural networks and machine learning), and dictionaries for tensors
and matrices.
Besides data, you can also specify indexes and column names for your DataFrame.
This makes for more understandable code for data analysis and manipulation. You
can access, delete, and rename your DataFrame components as you bring in more
structures and join more related data into your DataFrame structure.
MatPlotLib is a library that adds the missing data visualization functions to
Python. It is designed to complement the use of NumPy in data analysis and
-
gramming interface) for embedded plots into applications using general-purpose
GUI interfaces. For those familiar with MatLab, MatPlotLib provides a procedural
version called PyLab.
440 BOOK 5
With MatPlotLib, you can make elaborate and professional-looking graphs, and
you can even build “live” graphs that update while your application is running.
This can be handy in machine-learning applications and data-analysis applica-
tions, where it is good to see the system making progress towards some goal.
Time for us to put NumPy and Pandas to work on a simple data science project.
I am going to choose our dataset from the website Kaggle.com. Kaggle, whose tag
and use the data under very open licenses, in most cases. Kaggle also supports a
robust set of competitions for solving machine-learning problems, often posted
by companies that really need the solution.
I chose the “diamonds” database from Kaggle.com because it has a fairly simple
computer to use. You can download it at https://www.kaggle.com/shivam2503/
diamonds. Using Kaggle will require you to register and sign in to the community,
but does not cost anything to do so.
The metadata (metadata is data describing data, hence metadata) consists of ten
Column Header Type of Data Description
Index counter Numeric
carat Numeric Carat weight of the diamond
cut Text
Good, Very Good, Premium, Ideal
color Text Color of the diamond, with D being the best and J the worst
Exploring
Big
Data
with
Python
CHAPTER 2
If you were to use this as a training set for a machine-learning program, you
would see a program using NumPy and TensorFlow very similar to the one we
show you in Book 4. In this chapter, we are going to show you a set of simple
pandas-based data analysis to read our data and ask some questions.
-
data. I am sticking with DataFrames in this example because DataFrames makes
If you are installing NumPy and pandas on the Raspberry Pi, use these commands:
sudo apt-get install python3-numpy
sudo apt-get install python3-pandas
Now it is time for an example.
FirstDiamonds.py
and enter the following code:
# Diamonds are a Data Scientist's Best Friend
#import the pandas and numpy library
import numpy as np
import pandas as pd
# read the diamonds CSV file
Column Header Type of Data Description
clarity Text How obvious inclusions are within the diamond: (in order from best to
depth Numeric Depth %: The height of a diamond, measured from the culet to the
table, divided by its average girdle diameter
table Numeric Table %: The width of the diamond’s table expressed as a percentage
of its average diameter
price Numeric The price of the diamond
x Numeric Length mm
y Numeric Width mm
x Numeric Depth mm
BOOK 5
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
print (df.head(10))
print()
# calculate total value of diamonds
sum = df.price.sum()
print ("Total $ Value of Diamonds: ${:0,.2f}".format( sum))
# calculate mean price of diamonds
mean = df.price.mean()
print ("Mean $ Value of Diamonds: ${:0,.2f}".format(mean))
# summarize the data
descrip = df.carat.describe()
print()
print (descrip)
descrip = df.describe(include='object')
print()
print (descrip)
Making sure you have the diamonds.csv
command:
python3 FirstDiamonds.py
And you should see the following results:
Unnamed: 0 carat cut color clarity depth table price x
y z
0 1 0.23 Ideal E SI2 61.5 55.0 326 3.95
3.98 2.43
1 2 0.21 Premium E SI1 59.8 61.0 326 3.89
3.84 2.31
2 3 0.23 Good E VS1 56.9 65.0 327 4.05
4.07 2.31
3 4 0.29 Premium I VS2 62.4 58.0 334 4.20
4.23 2.63
4 5 0.31 Good J SI2 63.3 58.0 335 4.34
4.35 2.75
5 6 0.24 Very Good J VVS2 62.8 57.0 336 3.94
3.96 2.48
6 7 0.24 Very Good I VVS1 62.3 57.0 336 3.95
3.98 2.47
Exploring
Big
Data
with
Python
CHAPTER 2 443
7 8 0.26 Very Good H SI1 61.9 55.0 337 4.07
4.11 2.53
8 9 0.22 Fair E VS2 65.1 61.0 337 3.87
3.78 2.49
9 10 0.23 Very Good H VS1 59.4 61.0 338 4.00
4.05 2.39
Total $ Value of Diamonds: $212,135,217.00
Mean $ Value of Diamonds: $3,932.80
count 53940.000000
mean 0.797940
std 0.474011
min 0.200000
25% 0.400000
50% 0.700000
75% 1.040000
max 5.010000
Name: carat, dtype: float64
cut color clarity
count 53940 53940 53940
unique 5 7 8
top Ideal G SI1
freq 21551 11292 13065
That’s a lot of data for a short piece of code!
# Diamonds are a Data Scientist's Best Friend
First, we import all the needed libraries:
#import the pandas and numpy library
import numpy as np
import pandas as pd
Read the diamonds
# read the diamonds CSV file
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
444 BOOK 5
print (df.head(10))
print()
get to use the column as part of the DataFrame object. It’s great that you can do
this with Python!
# calculate total value of diamonds
sum = df.price.sum()
print ("Total $ Value of Diamonds: ${:0,.2f}".format( sum))
# calculate mean price of diamonds
mean = df.price.mean()
print ("Mean $ Value of Diamonds: ${:0,.2f}".format(mean))
data about carat.
# summarize the data
descrip = df.carat.describe()
print()
print (descrip)
This next statement prints out a description for all the nonnumeric columns in
descrip = df.describe(include='object')
print()
print (descrip)
To install MatPlotLib on your Raspberry Pi, type pip3 install matplotlib.
Now we move to the data visualization of our data with MatPlotLib. In Book 4 we
use MatPlotLib to draw some graphs related to the way our machine-learning
program improved its accuracy during training. Now we use MatPlotLib to show
some interesting things about our dataset.
Exploring
Big
Data
with
Python
CHAPTER 2 445
For these programs to work, you need to be running them from a terminal window
Raspberry Pi headless.
One of the really useful things about pandas and MatPlotLib is that the NumPy
and DataFrame types are very compatible with the required graphic formats. They
are all based on matrices and NumPy arrays.
Plot_Clarity
VSCarat.py and enter the following code:
# Looking at the Shiny Diamonds
#import the pandas and numpy library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# read the diamonds CSV file
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
import matplotlib.pyplot as plt
carat = df.carat
clarity = df.clarity
plt.scatter(clarity, carat)
plt.show() # or plt.savefig("name.png")
Run your program. Now, how is that for ease in plotting? Pandas and MatPlotLib
go hand-in-hand.
Remember that diamond clarity is measured by how obvious inclusions (see
One would be tempted to make a statement that the largest diamonds are rated as
and so you really can’t draw such general conclusions. All you can say is that “In
this dataset, the clarity ‘IL’ has the largest diamonds.”
446 BOOK 5
Plot_Count
Clarity.py and enter the following code:
# Looking at the Shiny Diamonds
#import the pandas and numpy library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# read the diamonds CSV file
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
import matplotlib.pyplot as plt
# count the number of each textual type of clarity
clarityindexes = df['clarity'].value_counts().index.tolist()
claritycount= df['clarity'].value_counts().values.tolist()
print(clarityindexes)
print(claritycount)
plt.bar(clarityindexes, claritycount)
plt.show() # or plt.savefig("name.png")
Diamond clarity
(horizontal)
versus carat size
Exploring
Big
Data
with
Python
CHAPTER 2 447
Again, remember that diamond clarity is measured by how obvious inclusions are
diamonds in our diamond database.
most represented in our diamond dataset.
I looked at clarity, now let’s look at color type in our pile of diamonds. Using nano
Plot_CountColor.py and enter
# Looking at the Shiny Diamonds
#import the pandas and numpy library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# read the diamonds CSV file
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
import matplotlib.pyplot as plt
Diamond clarity
count in each
448 BOOK 5
# count the number of each textual type of color
colorindexes = df['color'].value_counts().index.tolist()
colorcount= df['color'].value_counts().values.tolist()
print(colorindexes)
print(colorcount)
plt.bar(colorindexes, colorcount)
plt.show() # or plt.savefig("name.png")
Run your program.
colorless. The general rule is less color, higher price. The exceptions to this are the
pinks and blues, which are outside of this color mapping and sample.
The last plot I am going to show you is called a heat plot. It is used to graphically
show correlations between numeric values inside our database. In this plot we take
all the numerical values and create a correlation matrix that shows how closely
they correlate with each other. To quickly and easily generate this graph, we use
another library for Python and MatPlotLib called seaborn. Seaborn provides an API
built on top of MatPlotLib that integrates with pandas DataFrames, which makes
it ideal for data science.
Diamond color
count in each
Exploring
Big
Data
with
Python
CHAPTER 2 449
If you don’t already have seaborn on your Raspberry Pi (and if you have installed
MatPlotLib, you probably already do). Run the example Python program Plot_
Heat.py
sudo apt-get install python3-seaborn
Plot_Heat.py and
enter the following code:
# Looking at the Shiny Diamonds
#import the pandas and numpy library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# read the diamonds CSV file
# build a DataFrame from the data
df = pd.read_csv('diamonds.csv')
# drop the index column
df = df.drop('Unnamed: 0', axis=1)
f, ax = plt.subplots(figsize=(10, 8))
corr = df.corr()
print (corr)
sns.heatmap(corr, mask=np.zeros_like(corr, dtype=np.bool),
cmap=sns.diverging_palette(220, 10, as_cmap=True),
square=True, ax=ax)
plt.show()
the correlation between the two variables. The diagonal stripe from top left to top
-
prise there. The x, y, and z variables quite correlate with each other, which says
that as the diamonds in our database increase in one dimension, they increase in
the other two dimensions as well.
Interestingly, depth (The height of a diamond, measured from the culet to the
table, divided by its average girdle diameter) does not correlated very strongly at
all with price and in fact is somewhat negatively correlated.
450 BOOK 5
maps are fabulous for spotting general cross-correlations in your data.
It would be interesting to see the correlation between color/clarity and price. Why
isn’t it on this chart? This is because those columns are textual, and you can do
heat chart again. The same technique can be used on diamond clarity.
Correlation
CHAPTER 3 451
Using Big Data from
U
p to this point, we have been dealing with some relatively small sets of
-
analysis work.
What Is Big Data?
Big data -
»
»
»
»
452 BOOK 5
Python and Pandas and then visualizing the results on a Raspberry Pi.
-
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 453
-
pages, there are layers and layers of software under that, doing things like trans-
won’t even have to pay at all during your trial.
start to analyze almost immediately.
We would be remiss if we didn’t talk just a little bit about maintaining good
454 BOOK 5
.json
following steps will show you how to do this:
1. https://console.developers.google.com/
Medicare is the national health insurance program (single payer) in the United States
administered by the Centers for Medicare and Medicade Services (CMS). It provides
health insurance for Americans aged 65 and over. It also provides health insurance to
younger people with certain disabilities and conditions. In 2017 it provided health insur-
ance to over 58 million individuals.
With 58 million individuals in the system, Medicare is generating a huge amount of big
data every year. Google and CMS teamed up to put a large amount of this data on the
BigQuery public database so you can take a peek at this data and do some analytics
without trying to load it all on your local machine. A home computer, PC or Raspberry Pi,
won’t hold all the data available.
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 455
2.
screen.
3.
4. MedicareProject
5. MedicareProject
Make sure you don’t leave this on the default “My Project” selection. Make sure
you change it to MedicareProject
and authentication for the wrong project. This is an easy mistake to make.
6. MedicareProject
7. BigQuery
The Select a
Project page on
the Google Cloud.
456 BOOK 5
8.
9.
10.
.
11. MedicareProject
12.
A message appears saying that the service account and key has been created.
MedicareProject-1223xxxxx413.json” is
downloaded to your computer.
13.
First credential
screen.
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 457
for analysis. There are several dozen datasets available now and there will be more
Second credential
screen.
Column Type Description
provider_id STRING
outpatient hospital services.
provider_name STRING The name of the provider.
provider_street_
address
STRING The street address in which the provider is physically located.
provider_city STRING The city in which the provider is physically located.
(continued)
458 BOOK 5
then save it as MedicareQuery1.py:
import pandas as pd
from google.cloud import bigquery
# set up the query
QUERY = """
SELECT provider_city, provider_state, drg_definition,
average_total_payments, average_medicare_payments
FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015`
WHERE provider_city = "GREAT FALLS" AND provider_state = "MT"
Column Type Description
provider_state STRING The state in which the provider is physically located.
provider_zipcode INTEGER The zip code in which the provider is physically located.
drg_definition STRING
(diagnoses) and the procedures furnished by the hospital
during the stay.
hospital_referral_
region_description
STRING The hospital referral region (HRR) in which the provider is
physically located.
total_discharges INTEGER The number of discharges billed by the provider for inpatient
hospital services.
average_covered_
charges
FLOAT The provider’s average charge for services covered by Medicare
average_total_payments FLOAT The average total payments to all providers for the MS-DRG
including the MSDRG amount, teaching, disproportionate share,
average total payments are co-payment and deductible amounts
that the patient is responsible for and any additional payments
average_medicare_
payments
FLOAT The average amount that Medicare pays to the provider
payment amounts include the MS-DRG amount, teaching,
disproportionate share, capital, and outlier payments for
all cases. Medicare payments do not
co-payments and deductible amounts nor any additional
TABLE (continued)
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 459
ORDER BY provider_city ASC
LIMIT 1000
"""
client = bigquery.Client.from_service_account_json(
'MedicareProject2-122xxxxxf413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
print ("Records Returned: ", df.shape )
print ()
print ("First 3 Records")
print (df.head(3))
MedicareProject2-122xxxxxf413.
json -
window on the Raspberry Pi:
pip3 install google-cloud-bigquery
SQL
SQL (Structured Query Language) is a query-oriented language used to interface with
databases and to extract information from those databases. Although it was designed
for relational database access and management, it has been extended to many other
types of databases, including the data being accessed by BigQuery and the Google
Cloud.
Here are some excellent tutorials to get your head around how to access data
using SQL:
• https://www.w3schools.com/sql/
• http://www.sql-tutorial.net/
• SQL For Dummies
• SQL All In One For Dummies 3rd Edition,
• SQL in 10 Minutes, Ben Forta
BOOK 5
google.cloud library and the bigquery
import:
import pandas as pd
from google.cloud import bigquery
-
# set up the query
QUERY = """
SELECT provider_city, provider_state, drg_definition,
average_total_payments, average_medicare_payments
FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015`
WHERE provider_city = "GREAT FALLS" AND provider_state = "MT"
ORDER BY provider_city ASC
LIMIT 1000
"""
SELECT
FROM the database bigquery-public-data.cms_medicare.
inpatient_charges_2015 only WHERE the provider_city is GREAT FALLS and the
provider_state is MT
provider_city
is somewhat redundant.
json
one won’t work.
client = bigquery.Client.from_service_account_json(
'MedicareProject2-122xxxxxef413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 461
print ("Records Returned: ", df.shape )
print ()
print ("First 3 Records")
print (df.head(3))
Run your program using python3 MedicareQuery1.py and you should see results
Records Returned: (112, 5)
First 3 Records
provider_city provider_state
drg_ definition average_total_payments average_medicare_payments
0 GREAT FALLS MT 064 - INTRACRANIAL HEMORRHAGE OR CEREBRAL
INFA... 11997.11 11080.32
1 GREAT FALLS MT 039 - EXTRACRANIAL PROCEDURES W/O
CC/MCC 7082.85 5954.81
2 GREAT FALLS MT 065 - INTRACRANIAL HEMORRHAGE OR CEREBRAL
INFA... 7140.80 6145.38
Visualizing your Data
-
inpatient_charges_2015 dataset looking for
462 BOOK 5
import pandas as pd
from google.cloud import bigquery
# set up the query
QUERY = """
SELECT provider_city, provider_state, drg_definition,
average_total_payments, average_medicare_payments
FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015`
WHERE drg_definition LIKE '554 %'
ORDER BY provider_city ASC
LIMIT 1000
"""
client = bigquery.Client.from_service_account_json(
'MedicareProject2-1223283ef413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
ICD10 is the well-established method for coding medical professional diagnoses for
-
tory in 2015 with great angst throughout the medical community. It consists of, at its
Fracture of Greater Toe. These codes are somewhat merged into the MS_DRG codes
that are used in the Medicare databases we examine here as they are used for hospital
admissions. John Shovic had a medical software startup that used ICD 10 codes for ten
years, and he got to have a love/hate relationship with these codes.
His favorite ICD-10 codes:
•
• Z63.1: Problems in relationship with in-laws.
•
•
•
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 463
print ("Records Returned: ", df.shape )
print ()
print ("First 3 Records")
print (df.head(3))
LIKE '554 %'
Running the program gets these results:
Records Returned: (286, 5)
First 3 Records
provider_city provider_state drg_definition
average_total_payments average_medicare_payments
0 ABINGTON PA 554 - BONE DISEASES & ARTHROPATHIES W/O MCC
5443.67 3992.93
1 AKRON OH 554 - BONE DISEASES & ARTHROPATHIES W/O MCC
5581.00 4292.47
2 ALBANY NY 554 - BONE DISEASES & ARTHROPATHIES W/O MCC
7628.94 5137.31
MedicareQuery3.py
import pandas as pd
from google.cloud import bigquery
# set up the query
QUERY = """
SELECT provider_city, provider_state, drg_definition,
average_total_payments, average_medicare_payments
FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015`
WHERE drg_definition LIKE '554 %'
ORDER BY provider_city ASC
LIMIT 1000
"""
client = bigquery.Client.from_service_account_json(
'MedicareProject2-1223283ef413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
464 BOOK 5
print ("Records Returned: ", df.shape )
print ()
total_payment = df.average_total_payments.sum()
medicare_payment = df.average_medicare_payments.sum()
percent_paid = ((medicare_payment/total_payment))*100
print ("Medicare pays {:4.2f}% of Total for 554 DRG".format(percent_paid))
print ("Patient pays {:4.2f}% of Total for 554 DRG".format(100-percent_paid))
Records Returned: (286, 5)
Medicare pays 77.06% of Total for 554 DRG
Patient pays 22.94% of Total for 554 DRG
MedicareQuery4.py:
import pandas as pd
from google.cloud import bigquery
# set up the query
QUERY = """
SELECT provider_city, provider_state, drg_definition,
average_total_payments, average_medicare_payments
FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015`
WHERE drg_definition LIKE '554 %'
ORDER BY provider_city ASC
LIMIT 1000
"""
client = bigquery.Client.from_service_account_json(
'MedicareProject2-1223283ef413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 465
print ("Records Returned: ", df.shape )
print ()
# find the unique values of State
states = df.provider_state.unique()
states.sort()
total_payment = df.average_total_payments.sum()
medicare_payment = df.average_medicare_payments.sum()
percent_paid = ((medicare_payment/total_payment))*100
print("Overall:")
print ("Medicare pays {:4.2f}% of Total for 554 DRG".format(percent_paid))
print ("Patient pays {:4.2f}% of Total for 554 DRG".format(100-percent_paid))
print ("Per State:")
# now iterate over states
print(df.head(5))
state_percent = []
for current_state in states:
state_df = df[df.provider_state == current_state]
state_total_payment = state_df.average_total_payments.sum()
state_medicare_payment = state_df.average_medicare_payments.sum()
state_percent_paid = ((state_medicare_payment/state_total_payment))*100
state_percent.append(state_percent_paid)
print ("{:s} Medicare pays {:4.2f}% of Total for 554 DRG".format
(current_state,state_percent_paid))
Medicare
Query4.py
# we could graph this using MatPlotLib with the two lists
# but we want to use DataFrames for this example
data_array = {'State': states, 'Percent': state_percent}
466 BOOK 5
df_states = pd.DataFrame.from_dict(data_array)
# Now back in dataframe land
import matplotlib.pyplot as plt
import seaborn as sb
print (df_states)
df_states.plot(kind='bar', x='State', y= 'Percent')
plt.show()
MedicareQuery4.
py
sudo apt-get install python3-seaborn
hourly, believe it or not.
Bar chart of
Medicare % paid
Using
Big
Data
from
the
Google
Cloud
CHAPTER 3 467
measured by air quality:
import pandas as pd
from google.cloud import bigquery
# sample query from:
QUERY = """
SELECT location, city, country, value, timestamp
FROM `bigquery-public-data.openaq.global_air_quality`
WHERE pollutant = "pm10" AND timestamp > "2017-04-01"
ORDER BY value DESC
LIMIT 1000
"""
client = bigquery.Client.from_service_account_json(
'MedicareProject2-1223283ef413.json')
query_job = client.query(QUERY)
df = query_job.to_dataframe()
print (df.head(3))
location city country value timestamp
1 Bukhiin urguu Ulaanbaatar MN 1428.00 2019-01-21 17:00:00 00:00
2 Chaiten Norte Chaiten Norte CL 999.83 2018-04-24 11:00:00 00:00
mainly due to intense industrialization.
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
Talking to
Hardware with
Python
Contents at a Glance
Introduction to Physical Computing. . . . . . . . . . . . . 471
Physical Computing Is Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
What Is a Raspberry Pi? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Making Your Computer Do Things . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Using Small Computers to Build Projects That
Do and Sense Things. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Computing in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Controlling the LED with Python on the Raspberry Pi . . . . . . . . . . 482
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
No Soldering! Grove Connectors
for Building Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
So What Is a Grove Connector? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Selecting Grove Base Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
The Four Types of Grove Connectors. . . . . . . . . . . . . . . . . . . . . . . . 492
The Four Types of Grove Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Using Grove Cables to Get Connected . . . . . . . . . . . . . . . . . . . . . . . 499
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Understanding I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
A Fun Experiment for Measuring Oxygen and a Flame . . . . . . . . . 517
Building a Dashboard on Your Phone Using Blynk
and Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Making Things Move with Python. . . . . . . . . . . . . . . . 537
Exploring Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Controlling Motors with a Computer . . . . . . . . . . . . . . . . . . . . . . . . 540
CHAPTER 1 Introduction to Physical Computing 471
Introduction to Physical
Computing
W
e have been talking about how to program in Python for the last several
hundred pages in this book. It is now time to use our newly acquired
Python skills to start doing things in the real world. We call this physical
computing — making a computer interact with the world around you!
about the hardware. That’s why this book is mostly focused on learning how to
program computers with Python. But now it is time to learn how to make your
computers do something with Python.
In this chapter, we hook up various sensors and motors to a Raspberry Pi computer.
to people, hooking up things incorrectly can burn out your computer or your sen-
sors. For this reason, follow these two rules assiduously:
» Rule 1: Turn all
» Rule 2:
IN THIS CHAPTER
» Discovering how to use a Raspberry Pi
» Understanding how to use small
computers
» Using a Raspberry Pi to sense the
environment around you
» Making your computer do physical
things
472 BOOK 6 Talking to Hardware with Python
Physical Computing Is Fun
One reason that we want to you to learn about physical computing is that little
embeddedsystems
around you. And we mean everywhere. Go up to your kitchen. Look around. Your
refrigerator has a computer, maybe two or three if it has a display. Your blender
has a computer. Your oven has a computer. Your microwave has a computer. If you
use Phillips Hue lights in your house, your light bulbs have a computer. Your car
will have upwards of 20 computers in the vehicle.
One more example. How about the lowly toaster? If you have a “Bagel Button” or
a display on your toaster, you have a computer in there. Why? Why are there so
all your gadgets using a computer than it is to design special hardware. Do you
Most of these computers are much simpler, slower, and carrying much less RAM
equal to one character in English. In most Asian countries, one character equals
two bytes.
So computers are everywhere. But the interesting thing is that all these little
computers are doing physical computing. They are sensing and interacting with
the environment. The refrigerator computer is checking to see whether it is at
the right temperature, and if it is not, it turns on the cooling machinery, paying
attention to what it is doing to minimize the amount of electricity it uses. The
stove is updating your display on the front panel, monitoring the buttons and
dials and controlling the temperature so you get a good lasagna for dinner.
All of these interactions and controllers are called physical computing.
What Is a Raspberry Pi?
In this book, we could use one of these very small computers but the functionality
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing
since about 2012. It was created by the Raspberry Pi Foundation to teach basic
science and engineering in schools around the world. It turned out to be wildly
popular and has sold more than 19 million computers around the world. There a
To demystify some of the technology that we deal with every day, let’s talk about
the major blocks of hardware on this computer. Remember, your smartphone has
computers inside that are very similar in terms of structure to the Raspberry Pi.
» GPIO connector:
» CPU/GPU:
» USB:
» Ethernet:
» WiFi:
» HDMI Out:
» Audio jack:
» Other ports:
• Micro USB:
• Camera CSI:
• Display DSI:
474 BOOK 6 Talking to Hardware with Python
Making Your Computer Do Things
In order to get our computer to do and sense things apart from the computer
or an actuator. A sensor is a small piece of electronics that can detect something
actuator is a
fancy word for a motor or cable that does things in the real world.
In the remainder of this chapter, we are going to learn about the necessary ingre-
is the physical computing version of “Hello World” that we all do when we are
learning software. Blinking LED, here we come!
grabbing a mouse, keyboard, and monitor to do the set-up for beginners, but
Again, the best place to start is with www.raspberrypi.org.
Using Small Computers to Build Projects
That Do and Sense Things
Earlier in this chapter, we talked about computers in the kitchen. All those com-
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing
controlled by a computer.
after you get acquainted with the hardware, you are going to be able to design
Then, in further chapters, we build more complex things that will be the launch-
ing point to your own projects, all programmed in Python!
One last remark before we move on. The software you will be writing in Python
is the key to getting all these projects and computers working. Is the hardware
think the software is the more important part, and the easier part to learn for
beginners. Now before that statement unleashes a hundred nasty emails, let me
humbly acknowledge that none, and we mean none, of this is possible if it wasn’t
is a book on Python!
WHAT ARE SOME OF THE OTHER SMALL
COMPUTERS AVAILABLE?
www.raspberrypi.org
(continued)
BOOK 6 Talking to Hardware with Python
The Raspberry Pi: A Perfect Platform
By now you have your Raspberry Pi computer set up and running on your
www.
raspberrypi.org
computer to work with!
The Raspberry Pi is the perfect platform to do physical computing with Python
because it has a multiscreen environment, lots of RAM and storage to play with
and all the tools to build the projects we want.
We have been talking a lot about the computers in this chapter and not much
about Python. Time to change that.
(continued)
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing 477
-
into which we can plug a large number of sensors and controllers to do amazing
things to expand your Raspberry Pi.
GPIO pins
An GPIO pin output pin “outputs” a 1 or a 0 from the computer to the pin. See next
GPIO libraries
There are a number of GPIO Python libraries that are usable for building projects.
The one we use throughout the rest of this book is the gpiozero library that is
installed on all Raspberry Pi desktop software releases. The library documenta-
https://gpiozero.
readthedocs.io/en/stable/.
Now we are going to jump into the “Hello World” physical computing project with
our Raspberry Pi.
478 BOOK 6 Talking to Hardware with Python
The hardware for “Hello World”
To do this project, we need some hardware. Because we are using Grove connec-
hardware that we need for this project:
» Pi2Grover:
» Grove blue LED:
Assembling the hardware
physical computer based product. because of this, we’ll give you the step-by-step
process:
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing 479
1. Identify the Pi2Grover board from Figure 1-3 above.
2. Making sure you align the pins correctly gently press the Pi2Grover
1-6
480 BOOK 6 Talking to Hardware with Python
3.
pins, making sure the pins are aligned. There will be no pins showing on
Figure 1-7
Figure 1-8
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing 481
5. If your blue LED is not plugged into the Grove blue LED board, then plug
the board as in Figure 1-9.
6. Plug the other end of the Grove cable into the slot marked D12/D13 on
482 BOOK 6 Talking to Hardware with Python
Controlling the LED with Python
on the Raspberry Pi
Now that we have the hardware all connected, we can apply the power to the
Raspberry Pi. If all is well, then you will see your Grove blue LED light up, a blue
The Grove blue LED lights up when we turn the Raspberry Pi power on because
the GPIO pins on the Raspberry Pi power up as inputs. Because it is an input and
and so the LED will turn on. When you turn your GPIO pin to an output in the code
To get started, follow these steps:
1. Go to your keyboard and open up a terminal window.
https://www.raspberrypi.org/
documentation/usage/terminal/
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing
2.
HelloWorld.py.
from gpiozero import LED
from time import sleep
blue = LED(12)
while True:
blue.on()
print( "LED On")
sleep(1)
blue.off()
print( "LED Off")
sleep(1)
https://www.raspberrypi.org/
magpi/edit-text/
3. Now the big moment. Start your program by running this on the
command line your terminal window:
sudo python3 HelloWorld.py
appear on the screen in the terminal window:
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
The keyword sudo stands for super user do. We use sudo in front of the python3
command in this type of code because some versions of the Raspberry Pi operating
system restricts access to certain pins and functions from a regular user. By using
sudo, we are running this as a super user. This means it will run no matter how
the particular version of the operating system is set up. In the newer versions of
the Raspberry Pi OS, you can just type python3 HelloWorld.py and it will work.
If it doesn’t, go back to sudo python3 HelloWorld.py.
484 BOOK 6 Talking to Hardware with Python
In the code, the following statement imports the function LED from the Python
gpiozero library:
from gpiozero import LED
This statement imports the function sleep from the Python time library:
from time import sleep
blue = LED(12)
Now you start the loop that will go on forever:
while True:
Turn the LED on:
blue.on()
print( "LED On")
sleep(1)
blue.off()
print( "LED Off")
sleep(1)
Rinse and repeat.
Wow, you have now entered the world of physical computing. Just wait until you
Introduction
to
Physical
Computing
CHAPTER 1 Introduction to Physical Computing
Because we have all this hardware set up, how about we do one more interesting
Pulse-width modulation (PWM) is a technique by which you vary the amount of time
a signal is at a 1 versus the amount of time the signal is at a 0. Because our LED
a 0 then we can control the brightness to the human eye. This ratio is called the
duty cycle
time the signal is on will change the brightness of the LED.
Enter this Python code into nano and save it as HelloWorld2.py:
from gpiozero import PWMLED
from time import sleep
led = PWMLED(12)
while True:
led.value = 0 # off
sleep(1)
led.value = 0.5 # half brightness
BOOK 6 Talking to Hardware with Python
sleep(1)
led.value = 1 # full brightness
sleep(1)
Now run the code:
sudo python3 HelloWorld2.py
You will see the brightness change every second.
And one more thing, here is how to change your brightness in a continuous
fashion:
from gpiozero import PWMLED
from signal import pause
led = PWMLED(12)
led.pulse()
pause()
With this code, we see a smooth continuous brightening and darkening of the LED.
Boy, you accomplished a lot in this chapter. You have now started to see the
possibilities of physical computing. And you have a blue LED!
THE LED CHANGING IS NOT TOTALLY
SMOOTH
ps xaf
CHAPTER 2 No Soldering! Grove Connectors for Building Things 487
No Soldering! Grove
Connectors for Building
Things
O
kay, okay. We all have been talking about Python for the past several
hundred pages. Time to build something! But before we get to that, we
need to talk about how to plug things together.
Grove is a modular, standardized connecter prototyping system. Grove takes a
building-block approach to assembling electronics. Compared to the jumper or
solder-based system, it is easier to connect, experiment, and build, and it simpli-
Some of the other prototype systems out there take the level down to building
to build real systems. However, it requires some learning and expertise to hook
things up.
The Grove system consists of a base unit and various modules (with standardized
connectors).
IN THIS CHAPTER
» Discovering how to plug hardware
together
» Avoiding the Box of Death!
» Working with the four types of
sensors
» Understanding using Patch cables
488 BOOK 6 Talking to Hardware with Python
The base unit, generally a microprocessor, allows for easy connection of any input
or output from the Grove modules, and every Grove module typically addresses a
single function, from a simple button to a more complex heart-rate sensor.
You don’t need a base unit to connect up to Grove modules. You can use a cable
(Grove-to-pin-header converter) to run from the pins on the Raspberry Pi or
Arduino to the Grove connectors. See some examples of how to do this later in
this chapter.
So What Is a Grove Connector?
Normally, when you’re wiring up a board, you have to pay attention. If you plug
things in backwards or connect boards incorrectly, you can damage or destroy
boards. All it takes is an incorrectly attached wire, and your board is gone forever.
while taking no chances on hooking up power and ground incorrectly.
A Grove connector is a four-pin standardized size connector used to plug into
standardized connectors (common to all types of Grove connectors) are the key
to making this system work. They are keyed so that you cannot plug them in
backwards, and the four types of connectors (see “The Four Types of Grove Con-
nectors,” later in this chapter) are all designed so that if you plug the wrong
type of device into the wrong type of base unit, there is no problem. They aren’t
destroyed; they just won’t work. This is a good thing, a very good thing.
A Grove
connector.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 489
avoid such situations by making sure everything we do is 5V!
Selecting Grove Base Units
A Grove base unit is a controller or shield to which you attach the Grove modules.
sensors and output actuators of your system.
For the Arduino
We most talk about the Raspberry Pi in this book, but there are other computers
out there too! Arduinos are one of the more popular ones. There are a number of
good base unit shields available for the Arduino that provide a lot of Grove con-
They are also available for the Arduino Mega, Due, and others.
-
nectors built right into the board so you don’t even need a base unit.
The Arduino
Uno Grove
base board.
490 BOOK 6 Talking to Hardware with Python
On the Raspberry Pi side, the pickings are much slimmer. The base unit devices
available tend to be “too smart” and isolate you from the Raspberry Pi hardware
and software. This is a huge problem when you want to connect to hardware using
-
ibility. You can still mask the complexity with software drivers.
shifter (from the Raspberry Pi 3.3V to 5V for all the Grove sensors), and it does not
The Arduino
Mini Pro LB board
with Grove.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things
A TOAST OF WATER TO VOLTAGES
V refers to voltage, which is similar
to the water pressure in a pipe. The higher the pressure, the more water comes out.
With voltage, the higher the voltage, the more current (like water) will come out of the
pipe. If the water pressure is too high, it can break the pipe. Similarly, if the voltage is too
high, you can break the computer input. Raspberry Pi’s are pretty particular about liking
-
thing, usually called ground. This is why grounds are so important to connect and to
have a common ground so your voltages running around always know to what they are
referenced.
Not having a common ground in a system (thus confusing the voltages!) leads to very
You can always trust your mother,
but you can never trust your ground.” For those who may be interested in what the First
Law of Shovic is, that one is a bit easier to understand. The First Law is “It works better if
you plug it in!”
The Pi2Grover
board at work on
the Raspberry Pi.
BOOK 6 Talking to Hardware with Python
The Four Types of Grove Connectors
First of all, all Grove cables are physically identical and can be interchanged. The
out power and ground by plugging in one type of Grove connector in the other.
Although you do need to be careful and think about what you are doing, it is a lot
less risky than soldering or using jumpers to wire up devices to your Pi or Arduino.
ground.
» Pin 1: Yellow (for example, SCL on I2C Grove connectors)
» White (for example, SDA on I2C Grove connectors)
» Pin 3:
» Pin 4: Black (GND on all Grove connectors)
I DON’T WANT TO USE A BASE UNIT!
You do not have to have a hat or shield to use Grove with your Raspberry Pi or Arduino.
All you need is to connect the I2C, digital, or analog inputs to the Grove devices by using
a Grove-to-pin-header converter.
cables.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 493
The Four Types of Grove Signals
sensors and devices. It’s not hard, but pay attention. By using the wrong con-
nector, you may not fry your board, but your project still may not work correctly!
Many sensors only need one or two bits. A bit is the basis of all digital computer
the bits are represented by voltage levels (see the discussion on Voltage earlier in
“0” value.
Computers often communicate with each other and with external devices by using
digital bits. It turns out that there are two ways of getting information from bits.
about later.
A digital Grove connector consists of the standard four lines coming into the Grove
use D0, but some (like the LED Bar Grove display) use both. Often base units will
digital Grove connector.
Examples of Grove digital modules are: Switch modules, the Fan module, and the
the schematic for the LED Grove module. They range from the simple to the very
complex.
The Grove Digital Connector
Pin Name Description
Pin 1 - Yellow D0 Primary digital input/output
Pin 2 - White D1 Secondary digital input/output
Pin 4 - Black GND Ground
494 BOOK 6 Talking to Hardware with Python
aren’t enough
A Grove analog connector consists of the standard four lines coming into the Grove
switch and of course, the voltage present across the green connector on the left
A Grove analog
simple voltage
divider.
The Grove Analog Connector
Pin Name Description
Pin 1 - Yellow A0 Primary analog input
Pin 2 - White A1 Secondary analog input
Pin 4 - Black GND Ground
A simple digital
Grove module
with LED.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 495
Examples of Grove analog modules are: Potentiometer, voltage divider and a
Grove air quality sensor.
bit transmission
Remember when we talked about digital signals? How you can convey informa-
letter A. The speed at which the bit is sent is called a baud rate. (Baud comes from
Emile Baudot, who was an inventor and scientist making great progress in the late
uses the digital level and the timing of the signal to receive and transmit data. It
called a serial interface) plug is labeled from the base unit’s point of view. In other
The Grove UART Serial Connector
Pin Name Description
Pin 1 - Yellow RX
Grove board’s)
Pin 2 - White TX
Grove board’s
Pin 4 - Black GND Ground
BOOK 6 Talking to Hardware with Python
ANALOG VERSUS DIGITAL: THE DEBATE
CONTINUES
-
by a digital port. Okay, okay. Enough about that. Let’s just treat signals for this book as
digital or analog and leave it at that. Whew!
An analog signal is used when it is important to know what voltage is present at the sig-
is, it is important for us to know what the actual voltage number is. Later, we discuss
how to read an analog voltage into a digital computer by converting the analog voltage
into a digital number by the use of an ADC (analog-to-digital converter). Then our com-
puter can tell whether the plant is dry or not!
A Grove UART
RFID reader.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 497
sense of the world
plug and go!
more! It not only calculates the visible sunlight strength, it also measures the
can tell you whether you are going to get sunburned as well as if your plants are
happy!
You just have to love the things you can do these days with computers.
clear top to let the light through to measure.
the pin descriptions of the Grove connector.
The Grove I2C
sunlight sensor.
498 BOOK 6 Talking to Hardware with Python
is the SDA signal. Power and ground are the same as the other connectors. This
the 90s.
Pin Name Description
Pin 1 - Yellow SCL I2C clock
Pin 2 - White SDA I2C data
Pin 4 - Black GND Ground
An I2C bus is often used to communicate with chips or sensors that are on the same
licensing issues, sometimes the bus will be called TWI (two wire interface). SMBus, devel-
I2C provides good support for slow, close peripheral devices that only need be
addressed occasionally. For example, a temperate measuring device will generally only
change very slowly and so is a good candidate for the use of I2C, whereas a camera will
generate lots of data quickly and potentially changes often.
I2C uses only two bidirectional open-drain lines, SCL (serial clock) and SDA (serial data).
Kind of like two serial data lines next to each other. Open-drain means the I2C device
pulses, very much like a dance between SDA and SCL.
many devices on the same I2C bus, which is a very cool feature.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 499
Using Grove Cables to Get Connected
the Raspberry Pi. These are easy. They come with a Grove connector on each end
and are interchangeable.
Grove cables also come as patch cables (between Grove and pins) and we talk
about them next.
Grove Patch Cables
There always seems to be some kind of device or sensor that does not have Grove
connectors and yet you want to use it in your system. The solution to this is to use
a patch cable!
It turns out there are easy ways of converting pin headers to Grove connectors
using Grove adaptor cables. There are two types of Grove adaptor cables. One con-
verts the Grove connector to female header pins
The second type of Grove adaptor cables are Grove-connector-to-male-header-
20cm Grove
cables.
500 BOOK 6 Talking to Hardware with Python
The power of the patch cable is that you can connect to non-Grove sensors.
Basically, you map the Grove connector to your pin headers. Be careful and make
sure you check twice before applying power!
How you map depends on what kind of a sensor you have and what the interface
is. Grove connectors support four kinds of interfaces as we talk about earlier in
this chapter.
Grove female
header cables.
A close-up of a
Grove female
header cables.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things
An example of the power of the patch!
SunAirPlus, a solar power controller and data collector, is an example of convert-
on the pin header that we often want to convert to Grove connectors. We connect
adaptor board on the Raspberry Pi.
Second example: The Adafruit Ultimate GPS
-
ing way:
Grove male
header cables.
BOOK 6 Talking to Hardware with Python
It’s time to start building!
A Grove adaptor
cable attached to
Pi2Grover.
The SunAirPlus
board with
the Grove
female header
patch cable.
No
Soldering!
Grove
Connectors
for
Building
Things
CHAPTER 2 No Soldering! Grove Connectors for Building Things 503
A close-up of
the Adafruit GPS
with a Grove
patch cable.
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
CHAPTER 3 Sensing the World with Python: The World of I2C 505
Sensing the World with
Python: The World of I2C
B
efore we get into how to sense the world in Python, let’s go through a few
of the hardware issues. You can skip all this and still use Python to talk to
these devices, of course, but some background is a good thing to have You
can always go back over it later when you have some experience with these devices.
The available sensors for the Raspberry Pi and other small computers number in
the thousands. From detecting people in front of your computer (PIR) to detecting
a myriad of environmental conditions (temperature/humidity/air quality/and so
on), there are many inexpensive ways to have your computer monitor the physical
world. As always, the major thing you have to know about these sensors is how
you can talk to them with a computer, which is commonly through the interface.
The interface consists of two things: The hardware interface, which contains pins,
types, and voltage levels, and the software interface, which is usually called a driver
or an API (application programming interface).
There are four major ways of getting data to your computer from your outside
sensors:
»
»
IN THIS CHAPTER
» Discovering how to use I2C sensors
» Sensing your environment with a
Raspberry Pi
» Collecting and saving data
» Connecting Python to your
smartphone
506 Talking to Hardware with Python
»
»
In this book, we deal with sensors using digital inputs, analog inputs and I2C inter-
faces. Why not SPI? Just for simplicity. Most SPI parts also have an I2C interface on
the chip, and most small computer boards have an I2C interface built into the board.
Understanding I2C
address. For example, the address of the HDC1080 temperature and humidity sen-
sor we use in this chapter has an address of 0x40. What does the “0x” mean in
this address? It means that the number that follows is in hexadecimal notation,
base 16 instead of base 10 (our normal numbering system).
To understand this interface, let’s look at what an I2C bus is. An I2C bus is often
used to communicate with chips or sensors that are on the same board or located
-
conductors). To get around licensing issues (that have largely gone away), often
the bus will be called TWI (Two Wire Interface). SMBus, developed by Intel, is a
policies and rules from SMBus, sometimes supporting both with minimal recon-
I2C provides good support for slow, close peripheral devices that need be addressed
only occasionally. For example, a temperature-measuring device will generally
only change very slowly and so is a good candidate for the use of I2C, whereas a
camera will generate lots of data quickly and potentially changes often.
I2C uses only two bidirectional open-drain lines (open-drain means the device can
pull a level down to ground, but cannot pull the line up to Vdd. Hence the name
open-drain. Thus a requirement of I2C bus is that both lines are pulled up to Vdd.
use in this book contains 10K Ohm pullup resistors so you should not have to worry
about this. The two lines are SDA (serial data line) and the SCL (serial clock line).
There are two types of devices you can connect to an I2C bus: Master devices and
Slave devices. Typically, you have one Master device (The Raspberry Pi, in our case)
When used on the Raspberry Pi, the Raspberry Pi acts as the Master and all other
devices are connected as Slaves.
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 507
The I2C protocol uses three types of messages:
»
»
»
Lucky for us, most of the complexity of dealing with the I2C bus is hidden by
Python drivers and libraries.
Exploring I2C on the Raspberry Pi
-
minal window, command line, and text editors. If you haven’t done that yet, refer
To use the I2C bus on the Raspberry Pi, you need to make sure that it is enabled
in the operating system. Here is a good tutorial from Adafrui9t on how to do just
that: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-
setup/configuring-i2c.
Did you do it right? The easy way to check for this is to type the following
command in your terminal window:
I2cdetect -y 1
508 Talking to Hardware with Python
If it returns:
-bash: i2cdetect: command not found
On the other hand, if it returns:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
the I2C bus. In our next section, we are going to add a simple one.
Talking to I2C devices with Python
In order to talk to an I2C device, you should have one on the bus. A good one to start
get one of these inexpensive sensors on store.switchdoc.com or on amazon.com.
HDC1080
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 509
1. Shut down your Raspberry Pi. When the yellow LED has stopped blinking,
unplug the power from your Raspberry Pi.
2. Plug a Grove cable into the HDC1080. (See Figure 3-3.)
sudo halt
THE TEXAS INSTRUMENTS HDC1080
TEMPERATURE AND HUMIDITY SENSOR
510 Talking to Hardware with Python
3. Plug the other end of the Grove cable into one of the Grove connectors
marked I2C on the Pi2Grover that plugged on top of your Raspberry Pi.
(See Figure 3-4.)
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 511
4. Power up the Raspberry Pi and open a terminal window.
5. Type into the terminal sudo i2cdetect -y 1 and you will be rewarded
with this:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
from this sensor.
Reading temperature and humidity from
an I2C device using Python
The use of Python libraries are key to being productive in writing Python applica-
To read the temperature and humidity, follow these steps:
1. First, create a directory in your main directory:
cd
mkdir I2CTemperature
cd I2CTemperature
I2CTemperature
2. Before looking at the Python code for reading your temperature, install
the library on our Raspberry Pi. You do this by “cloning” the library
located at github.com by using the following command in your terminal
window:
git clone https://github.com/switchdoclabs/SDL_Pi_HDC1080_Python3.git
512 Talking to Hardware with Python
git clone
ls
pi@RPi3-60:~/I2CTemperature $ ls
SDL_Pi_HDC1080_Python3
pi@RPi3-60:~/I2CTemperature $
3. temperature
Test.py and enter the following code:
import sys
sys.path.append('./SDL_Pi_HDC1080_Python3')
import time
import SDL_Pi_HDC1080
# Main Program
print
print ("")
print ("Read Temperature and Humidity from HDC1080 using I2C bus ")
print ("")
hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080()
while True:
print ("-----------------")
print ("Temperature = %3.1f C" % hdc1080.readTemperature())
print ("Humidity = %3.1f %%" % hdc1080.readHumidity())
print ("-----------------")
time.sleep(3.0)
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C
4. Run the code by typing:
sudo python3 temperatureTest.py
Read Temperature and Humidity from HDC1080 using I2C bus
-----------------
Temperature = 24.2 C
Humidity = 32.9 %
-----------------
-----------------
Temperature = 24.2 C
Humidity = 32.9 %
-----------------
-----------------
Temperature = 24.2 C
Humidity = 32.9 %
-----------------
GITHUB, A REPOSITORY FOR GOOD THINGS
514 Talking to Hardware with Python
Try this experiment. Blow on the HDC1080 sensor board and watch the humidity
go up! You will see something like this:
-----------------
Temperature = 24.2 C
Humidity = 32.9 %
-----------------
-----------------
Temperature = 24.1 C
Humidity = 33.6 %
-----------------
-----------------
Temperature = 24.1 C
Humidity = 33.9 %
-----------------
-----------------
Temperature = 24.1 C
Humidity = 36.3 %
-----------------
-----------------
Temperature = 24.1 C
Humidity = 36.5 %
-----------------
-----------------
Breaking down the program
sys library:
import sys
The next line tells Python to search the SDL_Pi_HDC1080_Python3 directory below
sys.path.append('./SDL_Pi_HDC1080_Python3')
More imports:
import time
import SDL_Pi_HDC1080
# Main Program
print
print ("")
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 515
print ("Read Temperature and Humidity from HDC1080 using I2C bus ")
print ("")
hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080()
These statements read the temperature and humidity and print them out to the
hidden by use of the HDC1080 library:
while True:
print ("-----------------")
print ("Temperature = %3.1f C" % hdc1080.readTemperature())
print ("Humidity = %3.1f %%" % hdc1080.readHumidity())
Sleep for three seconds and then repeat:
print ("-----------------")
time.sleep(3.0)
turning on a red LED if it gets too hot, or turning on a blue LED if it gets to cold.
You could even tweet your temperature and humidity by using the https://
python-twitter.readthedocs.io Python library.
LOOKING AT AN I2C DRIVER
(continued)
516 Talking to Hardware with Python
def readTemperature(self):
s = [HDC1080_TEMPERATURE_REGISTER] # temp
s2 = bytearray( s )
HDC1080_fw.write( s2 )
time.sleep(0.0625) # From the data sheet
#read 2 byte temperature data
data = HDC1080_fr.read(2)
buf = array.array('B', data)
# Convert the data
temp = (buf[0] * 256) buf[1]
cTemp = (temp / 65536.0) * 165.0 - 40
return cTemp
def readTemperature(self):
s = [HDC1080_TEMPERATURE_REGISTER] # temp
s2 = bytearray( s )
HDC1080_fw.write( s2 )
(continued)
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 517
time.sleep(0.0625) # From the data sheet
#read 2 byte temperature data
data = HDC1080_fr.read(2)
buf = array.array('B', data)
# Convert the data
temp = (buf[0] * 256) buf[1]
cTemp = (temp / 65536.0) * 165.0 – 40
return cTemp
A Fun Experiment for Measuring
Oxygen and a Flame
under a more or less sealed glass jar with a lit candle. The idea is to measure the
oxygen in the glass jar and watch the level go down as the candle consumes the
14.7 percent oxygen.
518 Talking to Hardware with Python
by using MatPlotLib. You could also easily read this data into an Excel spreadsheet
and graph it using Excel.
MatPlotLib is a Python library for making publication quality plots using methods
Then we lit the candle and watched the data on the browser window connected to
the Raspberry Pi.
What we need to do this experiment:
» Analog-to-digital converter:
» Grove oxygen sensor:
» A candle:
» A large glass bowl:
Analog-to-digital converters (ADC)
digital signal (16 bits, in this case) for a computer to read.
When you have the digital number in the computer, you can scale it back to volts
-
ing volts.
the Raspberry Pi.
four-channel, 16-bit analog-to-digital converter available on store.switchdoc.
wanted to use a 16-bit ADC converter for greater accuracy and the fact it has four
channels instead of just one channel.
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 519
The Grove oxygen sensor
2
) is a sensor to test the oxygen concentration in the air.
values proportional to the concentration of oxygen. You can interpret these num-
bers by referring to the oxygen concentration linear characteristic graph.
in a permissible error range, it does not represent the exact oxygen gas concen-
tration. The detection of certain components in the air usually requires a more
precise and costly instrument, which cannot be done with a single gas sensor.
520 Talking to Hardware with Python
Hooking up the oxygen experiment
Raspberry Pi. Follow these steps to set up the oxygen sensor:
1. Disconnect the power from the Raspberry Pi.
2. Plug a Grove cable into the Grove oxygen sensor and then into the Grove
connector marked A1 on the Grove four-channel, 16-bit ADC board.
3. Plug another Grove cable into the Grove connector marked I2C on the
Grove four-channel, 16-bit ADC board. Plug the other end of that Grove
cable into one of the connectors marked I2C on the Pi2Grover board
plugged into the Raspberry Pi. (See Figure 3-7.)
4. Apply the power to the Raspberry Pi.
5. Run the command i2cdetect -y 1 inside a terminal window. You should
see this output:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 521
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
First make a new directory for the program:
cd
mkdir oxygenProject
cd oxygenProject
git clone https://github.com/switchdoclabs/SDL_Pi_Grove4Ch16BitADC
senseOxygen.py in your terminal
window using nano:
import time, sys
sys.path.append('./SDL_Pi_Grove4Ch16BitADC/SDL_Adafruit_ADS1x15')
import SDL_Adafruit_ADS1x15
ADS1115 = 0x01 # 16-bit ADC
# Select the gain
gain = 6144 # /- 6.144V
# Select the sample rate
sps = 250 # 250 samples per second
# Initialize the ADC using the default mode (use default I2C address)
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
dataFile = open("oxygenData.csv",'w')
totalSeconds = 0
while (1):
# Read oxygen channel in single-ended mode using the settings above
522 Talking to Hardware with Python
print ("--------------------")
voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000
rawCh1 = adc.readRaw(1, gain, sps)
# O2 Sensor
sensorVoltage = voltsCh1 *(5.0/6.144)
AMP = 121
K_O2 = 7.43
sensorVoltage = sensorVoltage/AMP*10000.0
Value_O2 = sensorVoltage/K_O2 - 1.05
print ("Channel 1 =%.6fV raw=0x%4X O2 Percent=%.2f" % (voltsCh1, rawCh1,
Value_O2 ))
print ("--------------------")
dataFile.write("%d,%.2fn" % (totalSeconds, Value_O2))
totalSeconds = totalSeconds 1
dataFile.flush()
time.sleep(1.0)
When you are done using the oxygen sensor, make sure you put it back in the
included capped container and seal the top. Humidity will destroy the sensor over
time. (We have destroyed these sensors in the past.)
When you run the program, here are the results:
--------------------
Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05
--------------------
--------------------
Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05
--------------------
--------------------
Channel 1 =2.436375V raw=0x32C1 O2 Percent= 22.05
--------------------
--------------------
Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05
--------------------
--------------------
Channel 1 =2.436187V raw=0x32C1 O2 Percent= 22.05
--------------------
Breaking down the code
In these statements, we set the parameters for the ADC module:
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C
import time, sys
sys.path.append('./SDL_Pi_Grove4Ch16BitADC/SDL_Adafruit_ADS1x15')
import SDL_Adafruit_ADS1x15
Normal Imports. Notice the path goes to the subdirectory in the your directory.
ADS1115 = 0x01 # 16-bit ADC
# Select the gain
gain = 6144 # /- 6.144V
# Select the sample rate
sps = 250 # 250 samples per second
other method:
# Initialize the ADC using the default mode (use default I2C address)
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
dataFile = open("oxygenData.csv",'w')
totalSeconds = 0
while (1):
# Read oxygen channel in single-ended mode using the settings above
print ("--------------------")
voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000
rawCh1 = adc.readRaw(1, gain, sps)
from the voltage from the ADC:
# O2 Sensor
sensorVoltage = voltsCh1 *(5.0/6.144)
AMP = 121
K_O2 = 7.43
sensorVoltage = sensorVoltage/AMP*10000.0
Value_O2 = sensorVoltage/K_O2 - 1.05
524 Talking to Hardware with Python
print ("Channel 1 =%.6fV raw=0x%4X O2 Percent=%.2f" % (voltsCh1, rawCh1,
Value_O2 ))
print ("--------------------")
dataFile.write("%d,%.2fn" % (totalSeconds, Value_O2))
-
ally terminate this program with a Ctrl-C:
totalSeconds = totalSeconds 1
dataFile.flush()
time.sleep(1.0)
Looking at the numbers, we determined that we started with about 21 percent
oxygen and the candle went out at about 15.8 percent oxygen, a reduction of about
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 525
One more thing to note: Look at the graph right after the candle went out. You can
see that the seal wasn’t perfect as the oxygen started to creep up.
Building a Dashboard on Your Phone
Using Blynk and Python
When you drive a car, all the information about how fast you are going, how much
fuel remains, and other car information is on your dashboard. We’re going to show
you how construct a simple dashboard so you can view your project data on your
smartphone. To illustrate how to do this, we’ll use the free app Blynk (free for small
dashboards; they charge you a bit for more energy to build more controls). This app
is available on the various app stores for both Android and iPhones. We’ll use the
iPhone to show the usage, but it is pretty much identical for Android phones.
HDC1080 temperature and humidity
sensor redux
Earlier in this chapter, you built a temperature and humidity sensing project using
526 Talking to Hardware with Python
us what our dashboard will look like.
OTHER DASHBOARDS
•
•
•
•
•
•
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 527
How to add the Blynk dashboard
First, we show you how to set up the Blynk app. This is done on an iPhone, but
it is very similar to using it on an Android phone. And the Python is identical in
both cases!
1. Install the Blynk app on your mobile phone. (See Figure 3-11.)
2. Open the Blynk app and create an account. (See Figure 3-12.)
3. Click the button to scan a QR (see Figure 3-13).
4. Scan the QR code shown in Figure 3-14.
5. You will now see the MyTemperature app on your screen.
(See Figure 3-15.)
528 Talking to Hardware with Python
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C 529
6. Click the middle of the project to select the project. Then click the
indicated button to go to project settings.
AUTH TOKEN
temperatureTest.py
the software to support the Blynk app.
Talking to Hardware with Python
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C
software for the Blynk app
To modify the software to support the Blynk app, follow these steps:
1. Create a directory in your main directory by entering the following:
cd
mkdir myTemperature
cd myTemperature
myTemperature
2. Before looking at the Python code for reading and then “Blynking” your
temperature, install the library on the Raspberry Pi. You do this by
“cloning” the library located up at github.com by using the following
command in your terminal window:
git clone https://github.com/switchdoclabs/SDL_Pi_HDC1080_Python3.git
3. myTemperature.py using nano or
your favorite editor.
#!/usr/bin/env python3
#imports
import sys
sys.path.append('./SDL_Pi_HDC1080_Python3')
import time
import SDL_Pi_HDC1080
import requests
import json
BLYNK_URL = 'http://blynk-cloud.com/'
BLYNK_AUTH = 'xxxx'
# Main Program
print
print ("")
Talking to Hardware with Python
print ("Read Temperature and Humidity from HDC1080 using I2C bus and send
to Blynk ")
print ("")
hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080()
def blynkUpdate(temperature, humidity):
print ("Updating Blynk")
try:
put_header={"Content-Type": "application/json"}
val = temperature
put_body = json.dumps(["{0:0.1f}".format(val)])
r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V0', data=put_body,
headers=put_header)
put_header={"Content-Type": "application/json"}
val = humidity
put_body = json.dumps(["{0:0.1f}".format(val)])
r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V1', data=put_body,
headers=put_header)
put_header={"Content-Type": "application/json"}
val = time.strftime("%Y-%m-%d %H:%M:%S")
put_body = json.dumps([val])
r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V2', data=put_body,
headers=put_header)
return 1
except Exception as e:
print ("exception in updateBlynk")
print (e)
return 0
while True:
temperature = hdc1080.readTemperature()
humidity = hdc1080.readHumidity()
print ("-----------------")
print ("Temperature = %3.1f C" % hdc1080.readTemperature())
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C
print ("Humidity = %3.1f %%" % hdc1080.readHumidity())
print ("-----------------")
blynkUpdate(temperature, humidity)
time.sleep(3.0)
4. The last thing you need to do before you run the code is to replace the
'xxxx' with your Blynk authorization code, which will look something
like this: 445730794c1c4c8ea7852a31555f44444.
BLYNK_AUTH = 'xxxx'
BLYNK_AUTH = '445730794c1c4c8ea7852a31555f44444'
Breaking down the code
This code is very similar to the HDC1080 code from earlier in this chapter with the
exception of the blynkUpdate code.
def blynkUpdate(temperature, humidity):
print ("Updating Blynk")
try:
Why do we have a ’try’ here? Because sometimes the requests library will throw
an error if the Internet is being funky.
http header for the requests library:
put_header={"Content-Type": "application/json"}
Talking to Hardware with Python
The following code sets the number of digits to the right of the decimal point to 1
so we won’t have long numbers of relatively meaningless digits because of the
accuracy of the HDC1080:
val = temperature
put_body = json.dumps(["{0:0.1f}".format(val)])
The following code does the actual transfer of the data to the Blynk server in the
form of an http request:
r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V0', data=put_body,
headers=put_header)
out if you have set the Blynk authentication code incorrectly:
except Exception as e:
print ("exception in updateBlynk")
print (e)
return 0
Sudo python3 myTemperature.py
You will see this type of output on your terminal screen:
-----------------
Temperature = 22.6 C
Humidity = 36.8 %
-----------------
THE REQUESTS LIBRARY
http
http
Sensing
the
World
with
Python:
The
World
of
I2C
CHAPTER 3 Sensing the World with Python: The World of I2C
Updating Blynk
-----------------
Temperature = 22.5 C
Humidity = 36.8 %
-----------------
Updating Blynk
Hit the Run key at the top-right of your Blynk app on the phone, and then watch
your data start to come in. If you don’t get data in a few seconds, check your
authentication code and make sure you have started your app by hitting the Start
button in the upper-right corner of the app.
-
Talking to Hardware with Python
Where to Go from Here
In this chapter, you have learned a lot about how to connect to the real world
Python on your Raspberry Pi. We suggest these other interesting things to do,
building upon your new expertise:
»
»
»
»
»
CHAPTER 4 Making Things Move with Python 537
Making Things Move
with Python
Making things move around with Python is undeniably cool. With motors,
physical computing goes to a whole new level.
Robots, microwaves, refrigerators, and electric cars all use electric motors to
move around, blow air, pump coolant, and take you 60mph wherever you want to
go. Electric motors are everywhere!
At its simplest, an electric motor is a machine that converts electrical energy into
mechanical energy. In this chapter, we talk about DC (direct current) motors.
(AC), on the other hand, is what you get out of your house outlets.
Interestingly, electric motors consume more than half of the electric energy pro-
duced in the United States.
IN THIS CHAPTER
» How to make things move with
Python
» Understanding DC motors and
software
» Using a servo motor
» Making a stepper motor step
538 BOOK 6 Talking to Hardware with Python
Exploring Electric Motors
An electric motor is all about magnetism. All motors use magnets to create motion.
All magnets have a north and a south pole. North to north and south to south repel
use this fact to create motion. We are all familiar with permanent magnets, like
the ones you use to hang things on the front of your refrigerator. However, you
can also create magnets by running a current around a coiled wire, which creates
you can create force, which then becomes motion. There are many ways to build
motors, but this is the fundamental basis of all of them.
In this chapter, we are going to talk about three common types of motors used in
small projects and robots. They are:
» Small DC motors
» Servo motors
» Stepper motors
Small DC motors
A DC motor has two wires, power and ground. When you supply power (putting
power and ground wires, and the motor will spin in the opposite direction. You
control the speed of a DC motor by using pulse width modulation (PWM), a tech-
wheels. Sometimes you will put an “encoder” on the motor shaft so you read into
a computer how far the shaft has turned, giving the computer some feedback that
can be useful.
Use a DC motor anytime you want something to be spun at a RPM (revolutions per
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
Servo motors
Servo motors are a generally a combination of three things: a DC motor, a simple
is a variable resistor) that will give position feedback like the “encoder” in the DC
-
torque rating of that servo motor.
range of motion.
a servo motor is often the answer.
Use a servo motor for fast, high torque and for pretty accurate rotation to a spe-
Stepper motors
A stepper motor
shaft. Whereas a servo motor uses a DC motor, a stepper motor uses multiple-
toothed electromagnets surrounding a central-toothed shaft.
A DC motor on a
small robot.
BOOK 6 Talking to Hardware with Python
that will sequence the electromagnets surrounding that central shaft to make the
central shaft turn in “steps,” hence the name stepper motor. The design of a stepper
motor provides a steady holding torque even when not powered up. Contrast that to
the servo motor, which has to be powered up to supply torque. As long as the load
is within the limits of the servo motor torque, then there are no positional errors.
Stepper motors are for slow, precise rotation. They are superior to servo motors in
motors. Stepper motors also don’t require a feedback system to determine where
Controlling Motors with a Computer
through all three types of motors and show you how to control them with Python.
Python and DC Motors
are dozens of robot controllers and motor controller boards that will work for
Sun-tracking solar
panels using a
stepper motor.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
our projects from the last chapter), which gives us control over two motors, their
individual direction and their individual speed. Pretty cool.
Here is the parts list:
» Pi2Grover Grove interface board: Try store.switchdoc.com or Amazon.com.
» Grove I2C motor drive: Available at www.seeedstudio.com or Amazon.com
(comes with a Grove cable).
» Two small DC motors: Try https://www.adafruit.com/product/711 or
Amazon.com.
of doing things.
Grove I2C motor drive
motors, so we will just use the Raspberry Pi power supply. If you are using bigger
The Grove I2C
motor drive.
BOOK 6 Talking to Hardware with Python
motors, which gets us up and running quickly.
There are a couple of interesting things about this diagram: First of all, there is
another computer on this board! It is an Atmega 8L and is another small computer
boards for little computers have little computers on them. Computers are every-
where! You can see the two motor connections on the left side of the board and
also what the LEDs mean in the middle of the board.
drive on the www.seeedstudio.com product page. You could change the program-
ming if you want or at least understand how you can make a little computer look
before you hook this up:
1. Loosen the set of two screw terminals on the end of the and insert the
bare end of the wires on the motors (see Figure 4-5) into the holes and
tighten the screws. (See Figure 4-6.)
Annotated
diagram of the
I2C motor drive
board.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
Note that it really doesn’t matter which color goes in which hole with a DC
motor. It will just rotate in the opposite direction. Just match them both as in
shows the installed motor on your Grove I2C motor drive board.
2. Plug a Grove cable into the Grove connector on the Grove I2 motor drive
(called “Output for external MCU selector” in the diagram). It comes that way
wired if you don’t have that jumper installed.
The Adafruit
DC motor.
The wires in the
I2C motor drive
screw terminals.
BOOK 6 Talking to Hardware with Python
If your board and motor don’t respond after hookup, try hitting the Reset button
Motors
installed on the
motor drive.
The DC
motor setup.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
Now, let’s power the Raspberry Pi up and start writing some Python! After pow-
not, shut the Pi down again and check your wiring.
Python DC motor software
DC motors are often used for robot wheels, and so the words forward and backward
should start to give you some ideas for later in the book when we are building a
robot car.
The use of Python libraries are key to being productive in writing Python applica-
tions. We will be using the SDL_Pi_HDC1080_Python3
To set up the software, follow these steps:
1. Create a directory in your main directory by entering:
cd
mkdir dcMotor
cd dcMotor
Now you are in the dcMotor directory.
2. Before looking at the Python code for running your motors, install the
library on the Raspberry Pi. You do this by “cloning” the library located up
at github.com by using the following command in your terminal window:
git clone https://github.com/switchdoclabs/SDL_Pi_GroveI2CMotorDrive.git
The git clone clones the git repository located at the address and copies it to
your Raspberry Pi. If you enter ls
following output:
pi@RPi3-60:~/dcMotor $ ls
SDL_Pi_GroveI2CMotorDrive
pi@RPi3-60:~/I2CTemperature $
BOOK 6 Talking to Hardware with Python
3. dcmotorTest.py
and enter the following code:
import sys
sys.path.append("./SDL_Pi_GroveI2CMotorDrive")
import SDL_Pi_GroveI2CMotorDrive
import time
#"0b1010" defines the output polarity
#"10" means the M is "positive" while the M- is "negative"
MOTOR_FORWARD = 0b1010
MOTOR_BACKWARD = 0b0101
try:
m= SDL_Pi_GroveI2CMotorDrive.motor_drive()
#FORWARD
print("Forward")
#defines the speed of motor 1 and motor 2;)
m.MotorSpeedSetAB(100,100)
m.MotorDirectionSet(MOTOR_FORWARD)
time.sleep(2)
#BACK
print("Back")
m.MotorSpeedSetAB(100,100)
#0b0101 Rotating in the opposite direction
m.MotorDirectionSet(MOTOR_BACKWARD)
time.sleep(2)
#STOP
print("Stop")
m.MotorSpeedSetAB(0,0)
time.sleep(1)
#Increase speed
for i in range (100):
print("Speed:",i)
m.MotorSpeedSetAB(i,i)
time.sleep(.02)
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
print("Stop")
m.MotorSpeedSetAB(0,0)
except IOError:
print("Unable to find the I2C motor drive")
print("Hit Reset Button on I2C Motor Drive and Try Again")
speed, stops the motors, and then runs them backward at a slow speed ramping
up to full speed, and then stops the motors entirely.
The key aspects of this software are calls to the SDL_Pi_GroveI2CMotorDrive
library. The library supports the following functions:
» MotorSpeedSetAB( MotorSpeedA, MotorSpeedB): Motor speed for the A
motor (M1) and the B motor (M2). Range 0–100.
» MotorDirectionSet(Direction): Forward or backward —constants set in
the program. MOTOR_FORWARD = 0b1010, MOTOR_BACKWARD = 0b0101
SDL_Pi_GroveI2CMotorDrive library is that it uses
smbus. In the library, you send the com-
block write
byte, and then the arguments. Here is the call for setting the motor direction:
#Set motor direction
def MotorDirectionSet(self,Direction):
bus.write_i2c_block_data(self.I2CMotorDriveAdd, self.DirectionSet,
[Direction,0])
time.sleep(.02)
Time to run the DC motors now. Type this into your terminal window:
sudo python3 testMotor.py
You will be rewarded by seeing the LEDs change and seeing your motors go
through a sequence that you have programmed in Python. You should be able to
All these motors take power from the Raspberry Pi when running, so disconnect
BOOK 6 Talking to Hardware with Python
Python and running a servo motor
inside, but it also has a controller circuit that allows us to position the DC
wait there for further orders.
You control servo motors by using PWM (pulse width modulation). Although you
can buy boards that will do PWM (and support bigger servo motors!) under control
of your computer, for this small servo we will be using the built-in PWM capabil-
Here is the parts list:
» Pi2Grover Grove interface board: Look for it at store.switchdoc.com or
amazon.com.
» SG90 micro servo motor:
are inexpensive so you may end up buying two or more for under $10.
» A package of Grove male patch cables: Grove-connector-to-male-pin cables:
Available at store.switchdoc.com or amazon.com.
three wires are:
»
»
»
and then you can plug it into your Raspberry Pi.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
The SG90 micro
servo with wires.
HOW MUCH CURRENT CAN THE RASPBERRY
PI SUPPLY TO THE 5V PINS?
you have connected to the Raspberry Pi 3 and what kind of a USB power supply you
Grove
male-pin-to
Grove-connector
patch cable.
550 BOOK 6 Talking to Hardware with Python
Now let’s connect the wires and make a servo motor rock!
1. Shut down your Raspberry Pi and remove power.
2. Plug the Grove patch cable into your SB90 servo motor following this wire
chart in Table 4-1. (See Figure 4-11.)
Check your wiring carefully. You can damage your Pi and motor if you reverse
these wires.
3. Plug the end of the Grove cable in the Pi2Grover Grove connector marked
D4/D5.
4. Put a piece of electrical tape or blue tape over the white exposed pin on
the Grove patch cable to keep it from shorting anything out. Also put one
of the supplied rocker arms on the servo motor gear so we can see more
easily its range of motion. (See Figure 4-12.)
Now let’s look at the Python software.
Servo Motor to Patch Cable Wiring
SG90 Servo Grove Patch Cable Function
Power
Black wire Ground
Servo motor
correctly wired to
patch cable.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
Python servo software
We are not going to use a higher-level servo library (and there are many available
for the Raspberry Pi in Python); instead, we are going to show you how to control
RPi GPIO built-
are using a library (RPi.GPIO), but we’re not adding
layers of API (application programming interface) calls like we normally would
1. Create a directory in your main directory by entering:
cd
mkdir Servo
cd Servo
2. servoTest.py
and enter the following Python code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
ServoPin = 4
GPIO.setup(ServoPin, GPIO.OUT)
p = GPIO.PWM(ServoPin, 50)
Fully connected Pi
and servo motor.
BOOK 6 Talking to Hardware with Python
p.start(7.5)
try:
while True:
p.ChangeDutyCycle(7.5) # turn towards 90 degree
print ("90 degrees")
time.sleep(1) # sleep 1 second
print ("0 degrees")
p.ChangeDutyCycle(2.5) # turn towards 0 degree
time.sleep(1) # sleep 1 second
print ("180 degrees")
p.ChangeDutyCycle(12.5) # turn towards 180 degree
time.sleep(1) # sleep 1 second
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
Breaking down the code
board:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
ServoPin = 4
GPIO.setup(ServoPin, GPIO.OUT)
This command sets an object p to the ServoPin
50Hz is a good number for this type of servo motor:
p = GPIO.PWM(ServoPin, 50)
The following line starts the servo motor at 7.5 percent duty cycle. Remember how
PWM works on a servo? It goes from one end of the servo turn to the by going from
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python 553
the servo type. We just looked at the numbers and empirically determined these
p.start(7.5)
Now we move the servo through its entire range:
try:
while True:
p.ChangeDutyCycle(7.5) # turn towards 90 degree
print ("90 degrees")
time.sleep(1) # sleep 1 second
print ("0 degrees")
p.ChangeDutyCycle(2.5) # turn towards 0 degree
time.sleep(1) # sleep 1 second
print ("180 degrees")
p.ChangeDutyCycle(12.5) # turn towards 180 degree
time.sleep(1) # sleep 1 second
this so when your control-c out of the program, the program will shut down the
-
pins directly.
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
Now it is time to run the program. Type the following into a terminal window:
sudo python3 servoTest.py
You should be rewarded by your screen printing out the following liens and your
-
ent angles and sequences. You can’t hurt the servo by trying things.
90 degrees
0 degrees
180 degrees
90 degrees
0 degrees
180 degrees
90 degrees
0 degrees
BOOK 6 Talking to Hardware with Python
Now we have a servo motor working on our Raspberry Pi. Remember at the begin-
airplane or a rudder on an RC boat? Watching the servo motor go thorough its
programmed sequence should spark your thoughts of what to do with a servo
motor. You can see why you use a DC motor for wheels and a servo to do things in
a non-rotating manner.
line to our last major motor, a stepper motor.
Python and making a stepper motor step
used for accurate positioning of items with a digital interface. You can accurately
-
nitely needs what is called positional feedback.
A stepper motor gets around this by accurately moving from one “step” to another
under command of software. The motor is constructed to use two motor coils to
be implementing this “stepping” sequence in the Python software controlling the
stepper motor.
A stepper motor typically has two coils used to move the motor from one step to
step. This pattern of steps will be very obvious in our Python software.
A diagram of a
stepper motor.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python 555
project.
Forward Stepping the Stepper
Coil_A_1 (Pin 12) Coil_A_2 (Pin 20) Coil_B_1 (Pin 13) Coil_B_2 (Pin 21)
1 0 1 0
0 1 1 0
0 1 0 1
1 0 0 1
Backward Stepping the Stepper
Coil_A_1 (Pin 12) Coil_A_2 (Pin 20) Coil_B_1 (Pin 13) Coil_B_2 (Pin 21)
1 0 0 1
0 1 0 1
0 1 1 0
1 0 1 0
Logic analyzer
showing the
motor stepping
556 BOOK 6 Talking to Hardware with Python
Here is the parts list:
» Pi2Grover Grove interface board: Try store.switchdoc.com or amazon.com.
» 28BYJ-48 ULN2003 5 V stepper motor: Look for these at eBay.com or
https://amzn.to/2BuNDVl
as the ones at the preceding Amazon.com link).
» A package of Grove female patch cables, Grove-connector-to-female-
pins: Available at store.switchdoc.com or amazon.com.
FEEDBACK: WHAT A USEFUL THING!
Feedback occurs when you route the output of a system back into the inputs of a sys-
and article and ask for comments. People supply comments (hopefully nice ones) and
you change the article based on some of those comments. That is feedback!
You use feedback in electrical circuits to achieve a better and more accurate position-
on the feedback.
we think of positive feedback as being good comments or at least constructive criti-
article is.
-
get larger. Ever hear a speaker wail when you put a microphone too close the speaker?
That’s positive feedback.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python 557
The 28BYJ-48
stepper motor
and UNL2003
driver board.
A Grove-
connector-to-
female-pin-header
patch cable.
558 BOOK 6 Talking to Hardware with Python
1. Shut down your Raspberry Pi and remove power.
2. Take a Grove female patch cord and connect it to the UNL2003 driver
board, as in the wire chart in Table 4-4.
Note we put a wire tie on the cable to keep things neat and tidy.
Look very carefully at your red and black wire on the Grove patch cord to make
First Grove Female Patch Cord to UNL2003 Driver Board
Grove Patch Cable UNL2003 Driver Board Function
IN1 Coil A_1
IN2 Coil B_1
Power
Black wire Ground
Close-up
of power
connections on
the UNL2003
driver board.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
3. Take a second Grove female patch cord and connect it as in the wire
chart in Table 4-5.
Use a wire tie or a piece of tape to keep the unused red and black wires up and
4. Plug your 28BYJ-4 stepper motor cable into the UNL2003 driver board
connector. It is keyed and only goes in one way. (See Figure 4-20.)
Second Grove Female Patch Cord to UNL2003 Driver Board
Grove Patch Cable UNL2003 Driver Board Function
IN3 Coil A_2
IN4 Coil B_3
No Connect
Black wire No Connect
Second Grove
patch cable
attached.
560 BOOK 6 Talking to Hardware with Python
5.
the UNL2003 driver board) into the Pi2Grover Grove connector marked D12/13
and the second Grove patch cable (the one that only has the yellow and
white wires connected) into the Pi2Grove Grove connector marked D20/21.
6. Put a cardboard arrow on your stepper motor shaft so you can really see
it move. (See Figure 4-22.)
Stepper motor
and driver board
connected.
All patch wires
installed on
UNL2003 driver
board.
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python
Fully wired
Raspberry Pi and
stepper motor
project.
ready to step.
BOOK 6 Talking to Hardware with Python
Python stepper software
Similar to what we did with the servo motor, we are not going to use a higher level
all work with all stepper motors!), and instead we are going to show you how to
1. Create a directory in your main directory by entering:
cd
mkdir Stepper
cd Servo
2. stepperTest.py
and enter the following Python code:
import sys
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
coil_A_1_pin = 12
coil_B_1_pin = 13
coil_A_2_pin = 20
coil_B_2_pin = 21
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
def forward(delay, steps):
for i in range(0, steps):
setStep(1, 0, 1, 0)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(1, 0, 0, 1)
time.sleep(delay)
Making
Things
Move
with
Python
CHAPTER 4 Making Things Move with Python 563
def backwards(delay, steps):
for i in range(0, steps):
setStep(1, 0, 0, 1)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(1, 0, 1, 0)
time.sleep(delay)
def setStep(w1, w2, w3, w4):
GPIO.output(coil_A_1_pin, w1)
GPIO.output(coil_A_2_pin, w2)
GPIO.output(coil_B_1_pin, w3)
GPIO.output(coil_B_2_pin, w4)
while True:
try:
# Delay between steps (milliseconds)
delay = 10
# How many Steps forward
steps = 50
forward(int(delay) / 1000.0, int(steps))
# How many Steps backwards
steps = 50
backwards(int(delay) / 1000.0, int(steps))
except KeyboardInterrupt:
# shut off all coils
setStep(0,0,0,0)
sys.exit()
Breaking down the code
The stepperTest.py
outputs when you hit Ctrl-C to stop the program.
BOOK 6 Talking to Hardware with Python
Time to run! Power up your Pi and open a terminal window. Note that all four of
You will see the stepper motor turn 50 steps to the left and then 50 steps to the
right. Try changing those variables in the program above to move your stepper
motor to other positions. Do you see how these motors can be used in 3D print-
ers and robots to accurately position printing heads, bed height, and robot arms?
The Raspberry Pi
running the
stepper motor.
Building Robots
with Python
Contents at a Glance
Introduction to Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . 567
A Robot Is Not Always like a Human. . . . . . . . . . . . . . . . . . . . . . . . . 567
Not Every Robot Has Arms or Wheels . . . . . . . . . . . . . . . . . . . . . . . 568
Understanding the Main Parts of a Robot. . . . . . . . . . . . . . . . . . . . 572
Programming Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Building Your First Python Robot . . . . . . . . . . . . . . . . 575
Introducing the Mars Rover PiCar-B . . . . . . . . . . . . . . . . . . . . . . . . . 575
Assembling the Robot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Programming Your Robot Rover in Python . . . . . 595
Building a Simple High-Level Python Interface . . . . . . . . . . . . . . . . 595
Making a Single Move with Python. . . . . . . . . . . . . . . . . . . . . . . . . . 597
Functions of the RobotInterface Class . . . . . . . . . . . . . . . . . . . . . . . 598
Coordinating Motor Movements with Sensors. . . . . . . . . . . . . . . . 610
Making a Python Brain for Our Robot . . . . . . . . . . . . . . . . . . . . . . . 613
. . . . . . . . . 623
This Chapter’s Project: Going to the Dogs . . . . . . . . . . . . . . . . . . . . 624
Setting Up the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Machine Learning Using TensorFlow . . . . . . . . . . . . . . . . . . . . . . . . 625
Testing the Trained Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Taking Cats and Dogs to Our Robot . . . . . . . . . . . . . . . . . . . . . . . . . 640
Other Things You Can Do with AI Techniques and the Robot . . . 645
AI and the Future of Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
CHAPTER 1 Introduction to Robotics 567
Introduction to Robotics
R
obots. That’s a name that has been bandied about for a hundred years. It
comes from a Czech word, robota, which means “involuntary labor.” It was
-
robot
A Robot Is Not Always like a Human
» Robots have only two features, a computer and an actuator.
» Robots are dumb; they are not people.
IN THIS CHAPTER
» Understanding what a robot is
» Types of robots
» Knowing the parts of a robot
568 BOOK 7 Building Robots with Python
Not Every Robot Has Arms or Wheels
Cobots cooperative robots).
-
-
-
Introduction
to
Robotics
CHAPTER 1 Introduction to Robotics
The Wilkinson Bread-Making Robot
www.wilkinsonbaking.
com
-
the consumer.
Inputs for the
BWM Robot
Driving System.
BOOK 7 Building Robots with Python
-
https://youtu.be/zVL8760H768
robotics class, and after three months and thousands of lines of code, they suc-
customer’s table.
A robot
making bread.
Introduction
to
Robotics
CHAPTER 1 Introduction to Robotics
because he wants a connected toaster.
https://www.youtube.com/
watch?v=Z7h8-f-k8C8.
Baxter
BOOK 7 Building Robots with Python
-
Understanding the Main Parts of a Robot
» Computers
» Motors and actuators
» Communications
» Sensors
Computers
The Toasteroid
Internet-
connected
toaster.
Introduction
to
Robotics
CHAPTER 1 Introduction to Robotics
embedded systems. They may
what to do.
Motors and actuators
Actuator motor
Communications
devices.
Sensors
-
drivers.
-
BOOK 7 Building Robots with Python
Programming Robots
CHAPTER 2 Building Your First Python Robot 575
Building Your First
Python Robot
I
n this chapter, we open up a robot to take a look inside, and we show you how
to talk to all the parts with Python. We tell you how to program a robot after
is inexpensive compared to buying a prebuilt one. Second, by building a robot you
get to know how a robot works and how you can use Python to control it.
We have chosen a robot that is based on our friend the Raspberry Pi. You can get
robots that are based on many other computers, including the Arduino, but with
ligence that you can on the Raspberry Pi.
After all, this is Python All-In-One For Dummies. Wouldn’t you like to be able to use
Introducing the Mars Rover PiCar-B
When we were deciding which robot to build in this book, we looked at and
and each of them had some drawbacks. However, after careful consideration,
IN THIS CHAPTER
» Building a robot
» Understanding the components
» Learning to program the components
576 BOOK 7 Building Robots with Python
» The assembly manual was clear with lots of pictures and diagrams.
» The supplied software was compatible with Python 3 (and the Stretch version
of the Raspberry Pi operating system).
» The PiCar-B required no soldering.
» It had a reasonable price and good availability.
A radio control car can be considered a robot too. Why did we choose this
is because you can easily get inside the software and add more software to make
own Python software inside the robot than we are in playing with the joystick.
What you need for the build
There are three things you need in order to build the robot used in this chapter, in
The assembled
PiCar-B robot.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot 577
» Raspberry Pi 3B+: Yes, you could get by with a smaller Raspberry Pi (like the
Raspberry Pi Zero), but we recommend you get the fast one so you can do
more sophisticated processing onboard the robot. The price you pay for a
faster Pi (like the 3B ) is in power consumption and battery life. For our
Among other places, you can buy the Raspberry Pi 3B at
• Amazon.com (make sure you buy the 3B )
• Newark.com
• Adafruit.com
• Shop.switchdoc.com
» Adeept Raspberry Pi PiCar-B: The Adeept Raspberry Pi PiCar-B is not quite
as available as the Raspberry Pi. When you buy this, make sure you are buying
the PiCar-B and not Mars Rover to the name of this
product in their catalog, so look for the “Adeept Mars Rover PiCar-B.”
You can buy the PiCar-B at these places:
• Amazon.com (https://amzn.to/2B7mtop)
• eBay.com
• Adeept.com
• Shop.switchdoc.com
» 18650 LiPo batteries: The PiCar-B requires two 18650 3.7V LiPo 5000mAh
removing the batteries) and supplying power for the Raspberry Pi from the
micro USB plug, which then powers both the robot and the Raspberry Pi. The
power for both the robot and the Raspberry Pi are connected together.
The package we chose had two sets of batteries and an included wall charger.
You can buy these kinds of batteries all over the place, including
• Amazon.com (https://amzn.to/2TgPsx1)
• Many, many other places.
Understanding the robot components
on the mechanical structure of the robots but rather on each of the active compo
nents. We’ll also talk about the Python software used to communicate with each
device used in the Python system test software later on in this chapter and in our
578 BOOK 7 Building Robots with Python
We will be giving small Python code snippets to show you how each of the sensors
Tests on Your Rover in Python” section, later in this chapter.
Controller board
This motor controller is designed to interface the Raspberry Pi to the sensors and
vide power to the main drive motor. The rest of the board is used to connect up the
Servo motors
The PiCar-B
motor controller
board.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot 579
milliamp is
» Yellow: PWM control signal
» Red: Power (5V, in our case)
» Brown: Ground
range of motion.
The SG90 micro
servo motor.
BOOK 7 Building Robots with Python
To control a servo motor, we just have to set the value to the position we want the
print ("-------------------")
print ("Servo Test - Head Left")
print ("-------------------")
pwm.set_pwm(HEAD_TURN_SERVO, 0, calValues.look_left_max)
time.sleep(1.0)
calValues.look_
left_max
in this chapter.
Drive motor
computer how far the shaft has turned, giving the computer some feedback that
can be useful.
direction.
The intricacies of controlling this motor are well hidden from the user. Here is the
motor.motor_left(MOTOR_START, forward,left_spd*spd_ad)
motor.motor_right(MOTOR_START,backward,right_spd*spd_ad)
Why are we turning both a left and right motor on when there is only one drive
causes the robot to move backward, you just move the motor to the other motor
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot
RGB LED
print ()
print ("-------------------")
print ("Left Front LED Test - Red ")
print ("-------------------")
led.side_on(led.left_R)
time.sleep(1.0)
led.side_off(led.left_R)
The main
drive motor.
BOOK 7 Building Robots with Python
Pixel RGB programmable LEDs
lights, if one goes out, then all the rest of the string goes too. That is because they
are controlled by a single serial data stream that is sent through all the lights by
pretty special software on the Raspberry Pi to make it work.
print ()
print ("-------------------")
print ("12 RGB Pixel LED Test - On ")
print ("-------------------")
rainbowCycle(strip, wait_ms=20, iterations=3)
many projects.
A single RGB LED.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot
The 12
programmable
RGB LEDs.
PIXEL RGB STRINGS ON THE RASPBERRY PI
The Raspberry Pi has a complex, multifaceted operating system based on Linux. It is a
multitasking preemptive operating system, which means virtually any task (and all user
tasks) can be interrupted (meaning stopped) and thus our serial stream to the Pixel
LEDs stopped and corrupted to some degree.
The library we are using solves the real-time control problem by using the PWM and
DMA hardware on the Raspberry Pi’s processor. The PWM (pulse-width modulation)
sequence of bytes to the PWM module, the Pixel data signal can be generated without
being interrupted by the Raspberry Pi’s operating system.
Because the Arduino type of processors don’t really have an operating system, it is
pretty easy to generate these signals on an Arduino compared to an Raspberry Pi.
Processors like the ESP8266 and the ESP32 do have tasks running in the background
(like WiFi) and so require special drivers to compensate for that to avoid data corruption
, the LEDs do
not always work well with the older and smaller Raspberry Pis (A , 3B, Pi Zero, or Pi Zero
W, for example).
BOOK 7 Building Robots with Python
Pi camera
The Pi camera talks to the Raspberry Pi via a parallel data ribbon cable directly
into the Raspberry Pi board.
print ()
print ("-------------------")
print ("Open Video Window")
print ("-------------------")
camera.resolution = (1024, 768)
camera.start_preview(fullscreen=False,window=
(100,100,256,192))
time.sleep(20)
camera.preview.window=(200,200,256,192)
time.sleep(2)
Raspberry Pi
camera and
cable.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot 585
camera.preview.window=(0,0,512,384)
time.sleep(2)
camera.close()
Ultrasonic sensor
ultra.checkdisk() calls the software that sets
which we convert to centimeters.
print ()
print ("-------------------")
print ("Ultrasonic Distance Test")
print ("-------------------")
An ultrasonic
distance sensor.
586 BOOK 7 Building Robots with Python
average_dist = 0.0
for i in range(0,10):
distance = ultra.checkdist()
average_dist = average_distance distance
print ("Distance = {:6.3f}cm ".format( distance*100))
time.sleep(1.0)
average_distance = average_distance / 10
print ("average_dist={:6.3f}cm".format(average_dist*100))
Assembling the Robot
An example of
the assembly
manual diagrams.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot 587
testing.
Note:
starts installing all the Adeept software on the Raspberry Pi and you will want to
test the robot using the software in this book to better understand the parts of the
robot before continuing.
So, go build your robot and then meet us back here to start testing your new robot.
shows the assembled robot.
» There is a power switch on the left side of the motor drive board (viewed
power from the batteries.
» Make sure you use the longer of the supplied ribbon cables for the Pi camera.
The short one will not quite reach. Change it before you install the camera
into the robot.
»
plastic wire ties to hold things in place, allowing for room for the servos and
The assembled
PiCar-B showing
wiring.
588 BOOK 7 Building Robots with Python
» Pay close attention to the orientation of the plastic parts and servos during
assembly. Almost all of them have an asymmetrical top and bottom and need
to be oriented correctly to be assembled.
»
Calibrating your servos
Now that you have assembled the robot, it is time to start testing things. The
assembly, they will not necessarily be completely in the right place.
The calibrateServo.py program runs each of the three servos from one end to
and center of each servo from the display on the terminal window. Then you place
these values in the calValues.py program for the rest of the programs to access.
The values in the calValues.py are right for our robot and will probably be pretty
close for yours, but you should run the program to be sure.
The calibrateServo
#!/usr/bin/python3
# calibrate servos
PROPERLY TURNING OFF YOUR
RASPBERRY PI
However, like many other computers, just pulling the plug on a Raspberry Pi can have
dire consequences, in this case, corrupting the SDCard that the Raspberry Pi uses for
program and data storage. Before you shut down the Raspberry Pi, enter the following
in a terminal window: sudo halt.
This safely shuts down the Raspberry Pi. When you run this command, after a bit, you’ll
see the “ACT” light (the green one) blink 10 times (at 0.5 second intervals). When it stops
the plug.
The red power LED will remain on as long as there is power applied to the Raspberry Pi.
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot 589
import time
import Adafruit_PCA9685
import calValues
#import the settings for servos
pwm = Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(60)
#servo mapping
# pmw 0 head tilt
HEAD_TILT_SERVO = 0
# pwm 1 head turn
HEAD_TURN_SERVO = 1
# pwm 2 wheels turn
WHEELS_TURN_SERVO = 2
if __name__ == '__main__':
print("--------------------")
print("calibrate wheel turn")
print("--------------------")
for i in range(calValues.turn_right_max,
calValues.turn_left_max,10):
pwm.set_pwm(WHEELS_TURN_SERVO,0, i)
print("servoValue = ", i)
time.sleep(0.5)
print("--------------------")
print("calibrate head turn")
print("--------------------")
for i in range(calValues.look_right_max,
calValues.look_left_max,10):
pwm.set_pwm(HEAD_TURN_SERVO,0, i)
print("servoValue = ", i)
time.sleep(0.5)
print("--------------------")
print("calibrate head up/down")
print("--------------------")
BOOK 7 Building Robots with Python
for i in range(calValues.look_up_max,
calValues.look_down_max,10):
pwm.set_pwm(HEAD_TILT_SERVO,0, i)
print("servoValue = ", i)
time.sleep(0.5)
The code is pretty straight forward, but let me talk about one of the servo
program loops.
for i in range(calValues.look_right_max,
calValues.look_left_max,10):
pwm.set_pwm(HEAD_TURN_SERVO,0, i)
print("servoValue = ", i)
time.sleep(0.5)
This loop steps through the servo range as given in calValues.py from the right
you can add those values to calValues.py.
The calValues.py
You replace the values in this program with your own values from calibrate
Servos.py
# Servo calibration values
# head
look_up_max = 150
look_down_max = 420
look_tilt_middle = 330
# head turn
look_right_max = 200
look_left_max = 450
look_turn_middle = 310
# wheels
turn_right_max = 180
turn_left_max = 460
turn_middle = 320
# turn_speed
look_turn_speed = 5
# motor speed
left_spd = 100 #Speed of the car
right_spd = 100 #Speed of the car
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot
Running tests on your rover in Python
installed the Adeept software on your Raspberry Pi, you have to disable the auto
startup of their software.
To do so, change the following line in the ~/.config/autostart/car.desktop
Exec=sudo python3 /home/Adeept_PiCar-B/server/server.py
To
#Exec=sudo python3 /home/Adeept_PiCar-B/server/server.py
sudo reboot.
https://youtu.be/UvxRBJ-tFw8.
That is what you will be running very shortly.
Installing software for the
CarPi-B Python test
www.dummies.com
Go into the Testing directory and then complete these instructions, which are
1. Install some developer libraries, which allow us to compile the software.
This is installed using the normal Raspberry Pi installer, as follows:
sudo apt-get install build-essential python3-dev git scons swig
2. Download the neopixel code from github using the clone command,
which copies all the source code to your local computer:
git clone https://github.com/jgarff/rpi_ws281x.git
BOOK 7 Building Robots with Python
3. Change to that directory and run scons to compile the software:
cd rpi_ws281x
scons
4. Change to the python directory and install the Python module from
there:
cd python
5.
sudo python3 setup.py install
The PiCar-B Python test code
the Testing directory.
www.dummies.com
sudo python3 PiCar-B-Test.py
https://youtu.be/UvxRBJ-tFw8.
Pi camera video testing
Building
Your
First
Python
Robot
CHAPTER 2 Building Your First Python Robot
PiCar-B-Video-Test.py
#!/usr/bin/python3
DEBUG = True
VIDEOTEST = True
# runs through a video tests for the PiCar-B
import RPi.GPIO as GPIO
import motor
import ultra
import socket
import time
import threading
import turn
import led
import os
import picamera
from picamera.array import PiRGBArray
import cv2
Setting the VNC
viewer option.
BOOK 7 Building Robots with Python
import calValues
if __name__ == '__main__':
camera = picamera.PiCamera() #Camera initialization
camera.resolution = (640, 480)
camera.framerate = 7
rawCapture = PiRGBArray(camera, size=(640, 480))
try:
print ("-------------------")
print ("-------------------")
print (" PiCar2- Video Test")
print (" Must be run from a GUI")
print ("-------------------")
print ("-------------------")
print ()
print ()
if (VIDEOTEST):
print ()
print ("-------------------")
print ("Open Video Window")
print ("-------------------")
camera.resolution = (1024, 768)
camera.start_preview(fullscreen=False,
window=(100,100,256,192))
time.sleep(20)
camera.preview.window=(200,200,256,192)
time.sleep(2)
camera.preview.window=(0,0,512,384)
time.sleep(2)
camera.close()
except KeyboardInterrupt:
destroy()
then closes the window.
Now you are done building and testing your robot. Next, we are going to add some
Python brains and have some fun with the robot.
CHAPTER 3 Programming Your Robot Rover in Python 595
Programming Your
Robot Rover in Python
O
kay, let’s review where you are now. You have a basic understanding of
robots and (more importantly) of the major components of robots and
how they work. You understand that these components can be controlled
with Python and that they can work together to accomplish robotic tasks. That’s
really a lot of information. Next, we show you how to string these things together
to make a very, very simple “robotic brain” to make our robot move by itself. This
won’t quite be a fully self-driving car, but after doing this you will have some
sense of how those cars are programmed.
Building a Simple High-Level
Python Interface
more complicated programs while hiding the complexity of the robot hardware.
Our high-level interface is a python RobotInterface.py. The code
length is beyond what we want to show in this book, so let us describe a couple of
functions and then document the rest.
IN THIS CHAPTER
» Learning how to move and sense with
your robot
» Understanding autonomous vehicles
» Playing and using the Adeept
software
596 BOOK 7 Building Robots with Python
The motorForward function
The motorForward function is typical of the motor functions located within the
RobotInterface class:
def motorForward(self, speed, delay):
motor.motor_left(self.MOTOR_START, self.forward,speed)
motor.motor_right(self.MOTOR_START, self.backward,speed)
time.sleep(delay)
motor.motor_left(self.MOTOR_STOP, self.forward,speed)
motor.motor_right(self.MOTOR_STOP, self.backward,speed)
This function drives the robot forward for the number of seconds passed into the
function in the delay argument. This means that when you call this function, you
must use an actual number in the delay argument.
It basically starts the motors running forward, waits a delay and then shuts
The wheelsLeft function
def wheelsLeft(self):
pwm.set_pwm(self.WHEELS_TURN_SERVO, 0,
calValues.turn_left_max)
time.sleep(0.05)
The wheelsLeft function sets the WHEELS_TURN_SERVO to the leftmost position of
the wheels and then delays 50ms. Why the delay? You will see these delays scat-
tered through the RobotInterface
commands back-to-back from exceeding the current capacity of the power sup-
ply. By delaying the next servo command 50ms, the high current transient caused
-
mand is executed.
The wheelsPercent function
This function allows the user to set the servo to a percent of the total range of
the servo motor. It goes from the full left (0) to full right (100) for the wheels;
if you have an asymmetric range of motion of your servo. If you do, then use the
wheelsMiddle() function to center your wheels.
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 597
This code calculates the total range of motion of the servo motor and then multi-
plies it by the percentage requested. It then sets the servo motor to the requested
range:
def wheelsPercent(self,percent):
adder = (calValues.turn_left_max –
calValues.turn_right_max)*(percent/100.0)
pwm.set_pwm(self.WHEELS_TURN_SERVO, 0,
int(calValues.turn_right_max adder))
time.sleep(0.05)
Making a Single Move with Python
First of all, go to this book’s support page at www.dummies.com (refer to the Intro-
In the following code, we move the robot a short distance ahead with a motor
Forward command and then back to its original position with a motorBackward()
command. Hopefully, you’re starting to see the magic of this approach.
The “Single Move” code:
#!/usr/bin/python3
# Robot Interface Test
import RobotInterface
import time
RI = RobotInterface.RobotInterface()
print ("Short Move Test")
RI.wheelsMiddle()
RI.motorForward(100,1.0)
time.sleep(1.0)
RI.motorBackward(100,1.0)
First, we import the RobotInterface class library and also the time library (for
sleep()). Note the simplicity of this. The complexity underlying robot interface
libraries are hidden by this class:
import RobotInterface
import time
598 BOOK 7 Building Robots with Python
Then we initialize the RobotInterface module and assign the module to the vari-
able RI:
RI = RobotInterface.RobotInterface()
print ("Short Move Test")
We center the wheels with the wheelsMiddle() function command:
RI.wheelsMiddle()
Now comes the good part. We drive the robot forward for one second, pause a
second, and then run it backwards for one second to the robot’s original position:
RI.motorForward(100,1.0)
time.sleep(1.0)
RI.motorBackward(100,1.0)
singleMove.py.
Here is a video of what you should see when you run this code on your robot in a
terminal window: https://youtu.be/UT0PG7z2ccE.
to document the RobotInterface class functions.
Functions of the RobotInterface Class
In this section, we document the functions of the RobotInterface class. We show
robot software! The RobotInterface class is derived from both original software
by the author and also from internal drivers from the PiCar-B Adeept software.
Front LED functions
The following functions control the two LEDs on the front of the robot.
set_Front_LED_On()
This function sets the front LED to On:
set_Front_LED_On(colorLED)
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 599
Remember that these two front LEDs are tricolor with red, green, and blue LEDs,
each of which is individually controllable. The parameter, colorLED, controls the
side of the robot to turn on and the color to turn on. You set the color and select
the side by using the following constants from the RobotInterface class:
RobotInterface.left_R
RobotInterface.left_G
RobotInterface.left_B
RobotInterface.right_R
RobotInterface.right_G
RobotInterface.right_B
For example, RobotInterface.left_R turns on the red LED on the Robot Left
side. Robot Left refers to the left side of the robot as viewed from the rear of the
robot. You can make multiple calls to this program to turn on all three of the LEDs.
Turning on an already on LED does not hurt anything and is ignored.
A more sophisticated driver could be written driving the LEDs GPIOs with PWM
(pulse-width modulation) allowing even greater color mixing. Note that unless
of the LEDs when using this technique because of the Raspberry Pi multitasking
operating system. You could, however, write drivers for the PCA9685 servo driver
problem.
This function sets the Front LED to Off:
set_Front_LED_Off(colorLED)
Remember that these two front LEDs are tricolor with red, green, and blue LEDs,
each of which is individually controllable.
The parameter, colorLED, controls the side of the robot to turn on and the color
to turn on. You set the color and select the side by using the following constants
from the RobotInterface class:
RobotInterface.left_R
RobotInterface.left_G
RobotInterface.left_B
RobotInterface.right_R
RobotInterface.right_G
RobotInterface.right_B
600 BOOK 7 Building Robots with Python
For example, RobotInterface.left_R turns on the red LED on the Robot Left
side. Robot Left refers to the left side of the robot as viewed from the rear of the
robot. You can make multiple calls to this program to turn on all three of the LEDs.
Turning on an already on LED does not hurt anything and is ignored.
Pixel strip functions
There are 12 LEDs on the robot strung together as a single 12 LED strip. These RGB
LEDs are called Pixels and are controlled by a single serial line that runs through
all 12 of the LEDs. They are controlled by a fairly sophisticated and touchy serial
sequence of precisely timed pulses from the Raspberry Pi. The Raspberry Pi (again
because of the operating system) cannot generate these pulses accurately enough
using Python and GPIO signals. Therefore a complex driver using the DMA (direct
signals. Our RobotInterface software hides all this complexity from the user.
rainbowCycle()
This call starts a rainbow cycle that uses all 12 of the Pixel LEDs and runs through
many colors:
rainbowCycle(wait_ms = 20, iterations = 3)
The parameter wait_ms sets the delay (in milliseconds) between each color
change. It defaults to 20ms. Interations sets the number of full color cycles to
colorWipe()
This function sets all 12 Pixel LEDs to the same color:
colorWipe(color)
For example, colorWipe(color(0,0,0)) sets all the Pixels to Off. This is a handy
way to set all 12 pixels to the same color. The parameter, color
be used using the color() function. (See the color() function later in this chapter.)
theaterChaseRainbow()
This function starts a 40-second-long pattern of chasing LEDs on all 12 of the
LEDs:
theaterChaseRainbow(wait_ms = 50)
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 601
The wait_ms parameter sets the delay between each of the movements in mil-
liseconds. It defaults to 50 milliseconds.
setPixelColor()
setPixelColor(pixel, color, brightness)
The pixel
The color color() function.
The brightness all the pixel string.
Color()
This function is a helper function that converts the R, G and B values into a single
24 bit integer used by the internal Pixel driver:
Color(red, green, blue, white = 0)
The parameters red, green, and blue
(fully on). The white=0 parameter is for RGBW Pixel LEDs. The Pixels on the robot
are RGB LEDs.
allLEDSOff()
Ultrasonic distance sensor function
The ultrasonic distance sensor functions by sending out a pulse of high frequency
sound and then counting the time before it bounces back to the receiver. Because
we know the speed of sound, we can calculate the distance in front of the sensor.
It’s not a perfect method (we would rather be using a laser!), but it is pretty good,
and it makes for a good starting distance sensor.
602 BOOK 7 Building Robots with Python
fetchUltraDistance()
This function does an immediate measurement from the ultrasonic distance sen-
sor in the head of the robot and returns the distance in centimeters (cm):
fetchUltraDistance()
Main motor functions
The main motor on our robot is what drives the back wheels and makes our robot
move. The motor functions are used to tell how fast and how long to run the main
motor.
motorForward()
This function drives the robot forward at speed for the delay number of seconds
motorForward(speed, delay)
The parameter speed sets the duty cycle of the PWM GPIO pin driving the inter-
The parameter delay tells the driver how long to run the motor in seconds.
motorBackward()
This function drives the robot backwards at speed for the delay number of sec-
motorBackward(speed, delay)
The parameter speed sets the duty cycle of the PWM GPIO pin driving the inter-
The parameter delay tells the driver how long to run the motor in seconds.
stopMotor()
This function immediately stops the main motor:
stopMotor()
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python
This is really only useful if, for example, you were driving the motor in another
thread. You can think of a thread as another program running at the same time as
your main program. This is a sophisticated programming technique that has some
Servo functions
This group of functions control the three servos on the robot: head-turning,
head-tilting, and front wheels.
headTurnLeft()
This function turns the robot head all the way to the left:
headTurnLeft()
calValues.py
this minibook for information on the calibration values and how to set them using
the calibrateServos.py program.
headTurnRight()
This function turns the robot head all the way to the right:
headTurnRight()
calValues.py
this minibook for information on the calibration values and how to set them using
the calibrateServos.py program.
headTurnMiddle()
This function turns the robot head towards the front:
headTurnMiddle()
calValues.py -
book for information on the calibration values and how to set them using the
calibrateServos.py program.
604 BOOK 7 Building Robots with Python
headTurnPercent()
This function turns the head from 0 (all the way to the left) to 100 (all the way to
the right):
headTurnPercent(percent)
This is useful for more precisely aiming the head. Again, “all the way to the left”
calValues.py
set them using the calibrateServos.py program.
The parameter percent
left to right. Note that the value 50 may not be quite in the middle because your
servos may not be set exactly in the middle of their range.
headTiltDown()
This function tilts the robot head all the way down:
headTiltDown()
calValues.py
minibook for information on the calibration values and how to set them using the
calibrateServos.py program.
headTiltUp()
This function tilts the robot head all the way up:
headTiltUp()
calValues.py
minibook for information on the calibration values and how to set them using the
calibrateServos.py program.
headTiltMiddle()
This function tilts the robot head towards the front:
headTiltMiddle()
calValues.py -
book for information on the calibration values and how to set them using the
calibrateServos.py program.
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 605
headTiltPercent()
This function turns the head from 0 (all the way down) to 100 (all the way to
the up):
headTiltPercent(percent)
This is useful for more precisely aiming the head. Again, “all the way down and
calValues.py
minibook for information on the calibration values and how to set them using the
calibrateServos.py program.
The parameter percent
down to up. Note that the value 50 may not be quite in the middle because your
servos may not be set exactly in the middle of their range as set by the servo
calibration process and because of the way your robot was physically built.
wheelsLeft()
This function turns the robot front wheels all the way to the left:
wheelsLeft()
calValues.py
this minibook for information on the calibration values and how to set them using
the calibrateServos.py program.
wheelsRight()
This function turns the robot front wheels all the way to the right:
wheelsRight()
calValues.py
this minibook for information on the calibration values and how to set them using
the calibrateServos.py program.
wheelsMiddle()
This function turns the robot front wheels to the middle:
wheelsMiddle()
calValues.py
for information on the calibration values and how to set them using the calibra-
teServos.py program.
606 BOOK 7 Building Robots with Python
wheelsPercent()
This function turns the head from 0 (all the way to the left) to 100 (all the way to
the right):
wheelsPercent(percent)
This is useful for more precisely setting the direction of the robot front wheels.
calValues.py
calibration values and how to set them using the calibrateServos.py program.
The parameter percent has values 0-100 and represents the linear percent from
down to up. Note that the value 50 may not be quite in the middle because of how
your servos may not be set exactly in the middle of their range as set by the servo
calibration process and how your robot was physically built.
General servo function
We have included general functions to control all the servos at once. Calling this
function moves all servos to the center position.
centerAllServos()
This function puts all the servos on the robot to the center of their ranges as
calValues.py
centerAllServos()
The Python Robot Interface Test
let’s run the system test using the RobotInterface Python class. This program
is useful for two reasons. First, it tests all our functions in the RobotInterface
class. Second, it shows how to use each of the functions in a Python program.
The code for RITest.py:
#!/usr/bin/python3
# Robot Interface Test
import RobotInterface
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 607
import time
RI = RobotInterface.RobotInterface()
print ("Robot Interface Test")
print ("LED tests")
RI.set_Front_LED_On(RI.left_R)
time.sleep(0.1)
RI.set_Front_LED_On(RI.left_G)
time.sleep(0.1)
RI.set_Front_LED_On(RI.left_B)
time.sleep(1.0)
RI.set_Front_LED_On(RI.right_R)
time.sleep(0.1)
RI.set_Front_LED_On(RI.right_G)
time.sleep(0.1)
RI.set_Front_LED_On(RI.right_B)
time.sleep(1.0)
RI.set_Front_LED_Off(RI.left_R)
time.sleep(0.1)
RI.set_Front_LED_Off(RI.left_G)
time.sleep(0.1)
RI.set_Front_LED_Off(RI.left_B)
time.sleep(1.0)
RI.set_Front_LED_Off(RI.right_R)
time.sleep(0.1)
RI.set_Front_LED_Off(RI.right_G)
time.sleep(0.1)
RI.set_Front_LED_Off(RI.right_B)
time.sleep(1.0)
RI.rainbowCycle(20, 1)
time.sleep(0.5)
# Runs for 40 seconds
#RI.theaterChaseRainbow(50)
#time.sleep(0.5)
print ("RI.Color(0,0,0)=", RI.Color(0,0,0))
RI.colorWipe(RI.Color(0,0,0))
time.sleep(1.0)
for pixel in range (0,12):
RI.setPixelColor(pixel,RI.Color(100,200,50),50)
time.sleep(0.5)
608 BOOK 7 Building Robots with Python
print ("Servo Tests")
RI.headTurnLeft()
time.sleep(1.0)
RI.headTurnRight()
time.sleep(1.0)
RI.headTurnMiddle()
time.sleep(1.0)
RI.headTiltDown()
time.sleep(1.0)
RI.headTiltUp()
time.sleep(1.0)
RI.headTiltMiddle()
time.sleep(1.0)
RI.wheelsLeft()
time.sleep(1.0)
RI.wheelsRight()
time.sleep(1.0)
RI.wheelsMiddle()
time.sleep(1.0)
print("servo scan tests")
for percent in range (0,100):
RI.headTurnPercent(percent)
for percent in range (0,100):
RI.headTiltPercent(percent)
for percent in range (0,100):
RI.wheelsPercent(percent)
print("motor test")
RI.motorForward(100,1.0)
time.sleep(1.0)
RI.motorBackward(100,1.0)
print("ultrasonic test")
print ("distance in cm=", RI.fetchUltraDistance())
print("general function test")
RI.allLEDSOff()
RI.centerAllServos()
Note: We have commented out the test code for RI.theaterChaseRainbow()
because it runs for 40 seconds.
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 609
Run the program by typing sudo python3 RITest.py into a terminal window.
Note that you have to use sudo because the Pixel LEDs require root permission
(granted by sudo) to correctly run.
Robot Interface Test
LED tests
RI.Color(0,0,0)= 0
Servo Tests
servo scan tests
motor test
ROS: THE ROBOT OPERATING SYSTEM
We have written a fairly simple interface class for the PiCar-B robot. This allows us to
control the robot from a Python program. If we had more room in this book (actually
another whole book could be written about the use of ROS for a robot like ours), we
would connect up our robot to the ROS (Robot Operating System). ROS is a system spe-
the Robot Operating System, it really isn’t an operating system.
ROS is what is called middleware. Middleware is software that is designed to manage
the complexity of writing software in a complex and heterogenous (meaning lots of dif-
robots in a very similar manner.
ROS operates using what is called a publish-subscribe system. It works like a newspaper.
A newspaper publishes stories, but only the people that subscribe to the newspaper see
those stories. You might have a subscriber that only wants a subscription to the comics.
Or the front page.
ROS works like that. A robot like ours may publish the current value of the ultrasonic
sensor or the current camera image (or even a video stream) and other computers or
robots on the network could subscribe to the video stream and see what your robot is
seeing. And your robot could subscribe to other sensors (such as a temperate sensor
located in the middle of the room) or even look at what the other robots are seeing. The
power of this technique is that now you can make your robot part of an ecosystem con-
sisting of computers, sensors, and even people making use of your data and contribut-
ing information to your robot.
We could build a ROS interface on our robot and then we could control it remotely and
feed sensor data to other computers.
In many ways, ROS really rocks. Find out more about ROS at http://www.ros.org/.
610 BOOK 7 Building Robots with Python
ultrasonic test
distance in cm= 16.87312126159668
general function test
Here’s a link to the video of the RobotInterface class test: https://youtu.
be/1vi-UGao0oI
Coordinating Motor Movements
with Sensors
The ability to modify and coordinate motor movements with sensor movements is
key to movement in the environment. Sensors give information to be acted upon
as well as feedback from our motions. Think of the act of catching a baseball with
a glove. Your sensors? Eyes and the sense of touch. Your eyes see the ball and then
move your hand and arm to intercept the ball. That’s coordinating your movement
with a sensor. The feedback? Knowing you have caught the ball in your mitt by
the feeling of it hitting your gloved hands. Of course, you are also updating your
internal learning system to become better at catching the ball.
PiCar-B has two sensors that read information from the outside world. The ultra-
sonic sensor can detect what is in front of the robot while the camera can photo-
to remember, however, is that robot vision is hard. Very hard. We touch upon
-
ple of how to do this using machine learning.
For our example, we will focus on the simpler sensor, the ultrasonic distance
sensor.
Here is an example of code that will move the robot forward or backwards depend-
ing on the distance from the object in front of the robot. Here is the Python code
for simpleFeedback.py:
#!/usr/bin/python3
# Robot Interface Test
import RobotInterface
import time
DEBUG = True
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 611
RI = RobotInterface.RobotInterface()
print ("Simple Feedback Test")
RI.centerAllServos()
RI.allLEDSOff()
# Ignore distances greater than one meter
DISTANCE_TO_IGNORE = 1000.0
# Close to 10cm with short moves
DISTANCE_TO_MOVE_TO = 10.0
# How many times before the robot gives up
REPEAT_MOVE = 10
def bothFrontLEDSOn(color):
RI.allLEDSOff()
if (color == "RED"):
RI.set_Front_LED_On(RI.right_R)
RI.set_Front_LED_On(RI.left_R)
return
if (color == "GREEN"):
RI.set_Front_LED_On(RI.right_G)
RI.set_Front_LED_On(RI.left_G)
return
if (color == "BLUE"):
RI.set_Front_LED_On(RI.right_B)
RI.set_Front_LED_On(RI.left_B)
return
try:
Quit = False
moveCount = 0
bothFrontLEDSOn("BLUE")
while (Quit == False):
current_distance = RI.fetchUltraDistance()
if (current_distance >= DISTANCE_TO_IGNORE):
bothFrontLEDSOn("BLUE")
if (DEBUG):
print("distance too far ={:6.2f}cm"
.format(current_distance))
else:
if (current_distance <= 10.0):
# reset moveCount
# the Robot is close enough
612 BOOK 7 Building Robots with Python
bothFrontLEDSOn("GREEN")
moveCount = 0
if (DEBUG):
print("distance close enough ={:6.2f}cm"
.format(current_distance))
time.sleep(5.0)
# back up and do it again
RI.motorBackward(100,1.0)
else:
if (DEBUG):
print("moving forward ={:6.2f}cm"
.format(current_distance))
# Short step forward
bothFrontLEDSOn("RED")
RI.motorForward(90,0.50)
moveCount = moveCount 1
# Now check for stopping our program
time.sleep(1.0)
if (moveCount > REPEAT_MOVE):
Quit = True
except KeyboardInterrupt:
print("program interrupted")
print ("program finished")
see if it is less than one meter (1000cm) from the wall. If it is, it slowly starts to
advance towards the wall in short little steps. When it is closer than 10cm to the
It also gives up if it takes more than 10 moves to get to the wall, if somehow we
have moved further than 1000cm away from the wall, or if the user has hit Ctrl-C
to interrupt the program.
Note how we use the LEDs to give feedback to surrounding people as to what
the robot is doing. This sort of visual feedback is an important part of making
The main structure of the program is contained within a Python while loop. As
long as we haven’t interrupted the program (or one of the other quit criteria hasn’t
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python
Copy the code into simpleFeedback.py and give it a try by executing sudo python3
simpleFeedback.py. Here are the printed results:
Simple Feedback Test
moving forward = 55.67cm
moving forward = 44.48cm
moving forward = 34.22cm
moving forward = 26.50cm
moving forward = 17.53cm
distance close enough = 9.67cm
moving forward = 66.64cm
moving forward = 54.25cm
moving forward = 43.55cm
moving forward = 36.27cm
moving forward = 28.44cm
moving forward = 21.08cm
moving forward = 13.55cm
distance close enough = 6.30cm
moving forward = 64.51cm
moving forward = 52.89cm
moving forward = 43.75cm
moving forward = 33.95cm
moving forward = 26.79cm
^Cprogram interrupted
program finished
And you can see the feedback video here: https://youtu.be/mzZIMxch5k4.
results.
Making a Python Brain for Our Robot
Now we are going to create a simple self-driving car. In a sense, we are going to
apply the results above to create an autonomous vehicle that is not very smart but
illustrates the use of feedback in decision-making.
This Python brain we are writing is nothing more than a combination of our code
for sensing the wall (from earlier in this chapter) and for generating a random
walk based on the information. After running the code for a while, we saw where
the robot would get stuck and added code to detect back out of stuck positions.
614 BOOK 7 Building Robots with Python
Note: Make sure you have fully charged up batteries to run this code. When the
bigger batteries.
The “Robot Brain” code:
#!/usr/bin/python3
# Robot Brsin
import RobotInterface
import time
from random import randint
DEBUG = True
RI = RobotInterface.RobotInterface()
print ("Simple Robot Brain")
RI.centerAllServos()
RI.allLEDSOff()
# Close to 20cm
CLOSE_DISTANCE = 20.0
# How many times before the robot gives up
REPEAT_TURN = 10
def bothFrontLEDSOn(color):
RI.allLEDSOff()
if (color == "RED"):
RI.set_Front_LED_On(RI.right_R)
RI.set_Front_LED_On(RI.left_R)
return
if (color == "GREEN"):
RI.set_Front_LED_On(RI.right_G)
RI.set_Front_LED_On(RI.left_G)
return
if (color == "BLUE"):
RI.set_Front_LED_On(RI.right_B)
RI.set_Front_LED_On(RI.left_B)
return
STUCKBAND = 2.0
# check for stuck car by distance not changing
def checkForStuckCar(cd,p1,p2):
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 615
if (abs(p1-cd) < STUCKBAND):
if (abs(p2-cd) < STUCKBAND):
return True
return False
try:
Quit = False
turnCount = 0
bothFrontLEDSOn("BLUE")
previous2distance = 0
previous1distance = 0
while (Quit == False):
current_distance = RI.fetchUltraDistance()
if (current_distance >= CLOSE_DISTANCE ):
bothFrontLEDSOn("BLUE")
if (DEBUG):
print("Continue straight ={:6.2f}cm"
.format(current_distance))
if (current_distance > 300):
# verify distance
current_distance = RI.fetchUltraDistance()
if (current_distance > 300):
# move faster
RI.motorForward(90,1.0)
else:
RI.motorForward(90,0.50)
turnCount = 0
else:
if (DEBUG):
print("distance close enough so turn ={:6.2f}cm"
.format(current_distance))
bothFrontLEDSOn("RED")
# now determine which way to turn
# turn = 0 turn left
# turn = 1 turn right
turn = randint(0,1)
if (turn == 0): # turn left
# we turn the wheels right since
# we are backing up
RI.wheelsRight()
else:
616 BOOK 7 Building Robots with Python
# turn right
# we turn the wheels left since
# we are backing up
RI.wheelsLeft()
time.sleep(0.5)
RI.motorBackward(100,1.00)
time.sleep(0.5)
RI.wheelsMiddle()
turnCount = turnCount 1
print("Turn Count =", turnCount)
# check for stuck car
if (checkForStuckCar(current_distance,
previous1distance, previous2distance)):
# we are stuck. Try back up and try Random turn
bothFrontLEDSOn("RED")
if (DEBUG):
print("Stuck - Recovering ={:6.2f}cm"
.format(current_distance))
RI.wheelsMiddle()
RI.motorBackward(100,1.00)
# now determine which way to turn
# turn = 0 turn left
# turn = 1 turn right
turn = randint(0,1)
if (turn == 0): # turn left
# we turn the wheels right since
# we are backing up
RI.wheelsRight()
else:
# turn right
# we turn the wheels left since
# we are backing up
RI.wheelsLeft()
time.sleep(0.5)
RI.motorBackward(100,2.00)
time.sleep(0.5)
RI.wheelsMiddle()
# load state for distances
previous2distance = previous1distance
previous1distance = current_distance
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 617
# Now check for stopping our program
time.sleep(0.1)
if (turnCount > REPEAT_TURN-1):
bothFrontLEDSOn("RED")
if (DEBUG):
print("too many turns in a row")
Quit = True
except KeyboardInterrupt:
print("program interrupted")
print ("program finished")
This seems to be a much more complex program than our ultrasonic sensor pro-
gram earlier in this chapter, but it is really not.
We took the same structure of the program (the while loop) and added several
features.
First, we added a clause to speed up the car when we were far away from an
if (current_distance >= CLOSE_DISTANCE ):
bothFrontLEDSOn("BLUE")
if (DEBUG):
print("Continue straight ={:6.2f}cm"
.format(current_distance))
if (current_distance > 300):
# verify distance
current_distance = RI.fetchUltraDistance()
if (current_distance > 300):
# move faster
RI.motorForward(90,1.0)
else:
RI.motorForward(90,0.50)
turnCount = 0
We continued to move in short little hops as the robot gets closer to the wall.
When the robot gets within about 10cm of the wall, the robot decides to turn its
front wheels in a random direction and backs up to try a new direction:
if (DEBUG):
print("distance close enough so turn ={:6.2f}cm"
.format(current_distance))
bothFrontLEDSOn("RED")
# now determine which way to turn
# turn = 0 turn left
618 BOOK 7 Building Robots with Python
# turn = 1 turn right
turn = randint(0,1)
if (turn == 0): # turn left
# we turn the wheels right since
# we are backing up
RI.wheelsRight()
else:
# turn right
# we turn the wheels left since
# we are backing up
RI.wheelsLeft()
time.sleep(0.5)
RI.motorBackward(100,1.00)
time.sleep(0.5)
RI.wheelsMiddle()
turnCount = turnCount 1
print("Turn Count =", turnCount)
We ran the robot for quite a while with just this logic, and we would see it get
stuck if part of the robot was blocked, but the ultrasonic sensor was still picking
up greater than 10cm distance.
-
ings, and if you had three readings /- 2.0cm, then the robot would decide it was
stuck and back up, turn randomly, and proceed again to wandering. Worked like
a champ:
if (checkForStuckCar(current_distance,
previous1distance, previous2distance)):
# we are stuck. Try back up and try Random turn
bothFrontLEDSOn("RED")
if (DEBUG):
print("Stuck - Recovering ={:6.2f}cm"
.format(current_distance))
RI.wheelsMiddle()
RI.motorBackward(100,1.00)
# now determine which way to turn
# turn = 0 turn left
# turn = 1 turn right
turn = randint(0,1)
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 619
if (turn == 0): # turn left
# we turn the wheels right since
# we are backing up
RI.wheelsRight()
else:
# turn right
# we turn the wheels left since
# we are backing up
RI.wheelsLeft()
time.sleep(0.5)
RI.motorBackward(100,2.00)
time.sleep(0.5)
RI.wheelsMiddle()
We set the robot down in a room that has furniture and a complex set of walls and
let it loose. Here are the results from the console:
Simple Robot Brain
Continue straight =115.44cm
Continue straight =108.21cm
Continue straight =101.67cm
Continue straight = 95.67cm
Continue straight = 88.13cm
Continue straight = 79.85cm
Continue straight = 70.58cm
Continue straight = 63.89cm
Continue straight = 54.36cm
Continue straight = 44.65cm
Continue straight = 36.88cm
Continue straight = 28.32cm
Continue straight = 21.10cm
distance close enough so turn = 11.33cm
Turn Count = 1
Continue straight = 33.75cm
Continue straight = 25.12cm
distance close enough so turn = 18.20cm
Turn Count = 1
Continue straight = 40.51cm
Continue straight = 33.45cm
Continue straight = 24.73cm
distance close enough so turn = 14.83cm
Turn Count = 1
Continue straight = 35.72cm
Continue straight = 26.13cm
distance close enough so turn = 18.56cm
620 BOOK 7 Building Robots with Python
Turn Count = 1
Continue straight = 43.63cm
Continue straight = 37.74cm
Continue straight = 27.33cm
Continue straight = 84.01cm
way out and then continue on. in the video here: https://youtu.be/U7_FJzRbsRw.
A Better Robot Brain Architecture
If you look at the robotBrain.py software from an software architectural perspec-
tive, one thing jumps out. The main part of the program is a single while loop
that polls the sensor (the ultrasonic sensor) and then does one thing at a time
(moves, turns, and so on) and then polls it again. This leads to the somewhat
jerky behavior of the robot (move a little, sense, move a little, sense, and so on).
Although this is the simplest architecture we could use for our example, there are
better, albeit more complicated, ways of doing this that are beyond the scope of
our project today.
These better architectures are based on what are called threads. You can think
of threads as separate programs that run at the same time and communicate to
each other by using things called semaphores and data queues. Semaphores and
data queues are simply methods by which a thread can communicate with other
threads in a safe manner. Because both threads are running at the same time, you
have to be careful how they talk and exchange information. This is not compli-
cated, if you follow the rules.
A better architecture for our robot brain would be like this:
» Motor thread: This thread controls the motors. It makes them run on
command and can stop the motors anytime.
» Sensor thread: This thread reads the ultrasonic sensor (and any other
sensors you may have) periodically so you always have the current distance
available.
» Head thread: This thread controls the head servos using commands from the
Command thread.
» Command thread: This is the brains of software. It takes current information
from the Sensor thread and sends commands to the motors and head to their
respective thread.
Programming
Your
Robot
Rover
in
Python
CHAPTER 3 Programming Your Robot Rover in Python 621
This architecture leads to a much smoother operation of the robot. You can have
the motors running while you are taking sensor values and sending commands
simultaneously. This is the architecture that is used in the Adeept software
server.py
Overview of the Included Adeept Software
server model in which the client is a control panel on another computer and the
-
trol the robot remotely and has a lot of interesting features, such as object track-
ing using OpenCV and a radarlike ultrasonic mapping capability. You can also see
the video coming from the robot and use that to navigate manually.
It is pretty complicated to install, however, so pay close attention to the
instructions.
-
ming. The software is all open source, so you can look inside to see how they are
doing things. Especially check out server.py under the server directory and look
at the way they use threading to get smooth motion out of the robot.
Adeept remote
control software.
622 BOOK 7 Building Robots with Python
Where to Go from Here?
You now have a small robot that can display quite complex behavior based on the
ultrasonic sensor built in. You can add a lot to this robot in terms of adding sen-
Light conditions?) Refer back to Book 6 and combine some of the motors and sen-
sors you used there with this robot. Because we choose the PiCar-B, you can plug
the Pi2Grover on top of the motor controller so you can use all the Grove devices
you have accumulated.
The sky is the limit!
CHAPTER 4 623
—DICTIONARY.COM
S
o, AI is meant to replace people? Well, not really. Modern AI looks to enhance
machine intelligence at certain tasks that are normally done by people.
Even saying the words “machine intelligence” is somewhat of a misnomer
because it is hard to claim that machines have intelligence at all, at least as we
think of it in people.
Instead of the philosophical debate, let’s focus on how to use some modern AI
techniques in a real robot example.
For a better overview of AI and some of the philosophy involved, check out Book 4.
So, what AI technique can we use in our robotic car? Turns out there is a Pi camera
on the car, and computer vision is really hard, so let’s do something with that.
»
»
»
624 BOOK 7
Making robots see is easy, but making them understand what they are seeing
is exceptionally hard. If you want to study up on computer vision using Python,
check out by Matthew Rever.
In this chapter, we show you how to build a machine-learning neural network
and train it to recognize cats versus dogs. (This is a skill all robots should have.)
We will train the network using a 1,000-image subset of the 25,000 element data-
base of pictures of cats and dogs from the Kaggle cats and dog database using
TensorFlow.
TensorFlow is a Python package that is also designed to support neural networks
major respect: TensorFlow is designed for use in machine learning and AI appli-
cations, and so it has libraries and functions designed for those applications.
If you need to, refer back to Book 4 as it has extensive information and examples
about using TensorFlow in Python and on the Raspberry Pi.
link: https://www.tensorflow.org/install/pip. Download TensorFlow and
install according to the directions.
Download the truncated list of the Cats and Dogs database here: https://
github.com/switchdoclabs/CatsAndDogsTruncatedData. It is about 65mb and
is included with our software at dummies.com.
For more experimentation, download the full Cats and Dogs dataset from this link:
https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data.
Another source of the full data is: https://www.microsoft.com/en-us/download/
details.aspx?id=54765.
Unzip the main folder and then unzip the test.zip and the train.zip subfolders.
Test train contains our
training data that we will use to train our neural network.
CHAPTER 4 625
Run the following command in your program directory to download the data:
git clone https://github.com/switchdoclabs/CatsAndDogsTruncatedData.git
Now that we have our data ready, let’s go train that network.
Our goal in this section is to fully train our machine learning neural network on
trained neural network so we can actually use it on our robot. Then the real fun
will begin!
When you run the following program, if you see ImportError: No module named
'seaborn', type sudo pip3 install seaborn.
We starting using a pretty simple two-layer neural network for our cats and dogs
machine learning network. There are many more complex networks available and
may give better results, but we were hoping this would be good enough for our
needs.
There is also the option of using a much larger dataset (our training dataset has
1,000 cats and 1,000 dogs but over 25,000 images are available in the full dataset).
Using a simple two-layer neural network on the cats and dog dataset did not really
work very well because we achieved only about a 51 percent detection rate (50 per-
cent is as good as just guessing randomly) so we needed to go to a more complex
neural network that works better on complex images.
You can use CNN (convolutional neural networks) in place of simple neural net-
works, data augmentation (increasing the training samples by rotating, shifting,
and zooming that pictures) and a variety of other techniques that are beyond the
scope of this book. We are going to use a pretty standard six-layer CNN instead of
our simple neural network.
We changed the model layers in our program to use the following six-level convo-
lutional layer model. You just have to love how easy Keras and TensorFlow makes
it to dramatically change the neural network.
626 BOOK 7
You can see the complexity of our new network by looking at the model.summary()
results:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 150, 150, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 75, 75, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 75, 75, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 37, 37, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 37, 37, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 18, 18, 64) 0
_________________________________________________________________
dropout (Dropout) (None, 18, 18, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 20736) 0
_________________________________________________________________
dense (Dense) (None, 64) 1327168
_________________________________________________________________
dropout_1 (Dropout) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 2) 130
=================================================================
Total params: 1,355,938
Trainable params: 1,355,938
Non-trainable params: 0
CNNs work by scanning images and analyzing them chunk by chunk, say at a 5x5
window that moves by a stride length of two pixels each time until it spans the entire
message. It’s like looking at an image using a microscope; you see only a small part of
the picture at any one time, but eventually you see the whole picture. Every time we
loop through the data is called an epoch.
Going to a CNN network on a Raspberry Pi increased the single epoch time to 1,000
seconds from the 10-seconds epoch we had on the simple two-layer network. And it
has a CPU utilization of 352 percent, which means it is using 3.5 cores on the Raspberry
Pi, a machine that only has a total of 4. This amounts to a pretty high utilization of the
Raspberry Pi 3B . The little board is using almost 3.8W up from about 1.5W normally.
CHAPTER 4 627
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import *
# load data
img_width = 150
img_height = 150
train_data_dir = 'data/train'
valid_data_dir = 'data/validation'
datagen = ImageDataGenerator(rescale = 1./255)
train_generator = datagen.flow_from_directory(
directory=train_data_dir,
target_size=(img_width,img_height),
classes=['dogs','cats'],
class_mode='binary',
batch_size=16)
validation_generator = datagen.flow_from_directory(directory=valid_data_dir,
target_size=(img_width,img_height),
classes=['dogs','cats'],
class_mode='binary',
batch_size=32)
# build model
model = tf.keras.Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3), padding='same',
activation='relu'))
628 BOOK 7
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
print (model.summary())
# train model
print('starting training....')
history = model.fit_generator(generator=train_generator,
steps_per_epoch=2048 // 16,epochs=20,
validation_data=validation_generator,validation_steps=832//16)
print('training finished!!')
# save coefficients
model.save("CatsVersusDogs.trained")
# Get training and test loss histories
training_loss = history.history['loss']
accuracy = history.history['acc']
# Create count of the number of epochs
epoch_count = range(1, len(training_loss) 1)
# Visualize loss history
plt.figure(0)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, accuracy, 'b--')
plt.legend(['Training Loss', 'Accuracy'])
plt.xlabel('Epoch')
plt.ylabel('History')
plt.grid(True)
plt.show(block=True);
CHAPTER 4 629
CatsVersusDogs.py.
The code has 93 lines. It’s pretty amazing what we can do with so few lines of
code.
If you want to learn more about neural networks and machine learning, refer back
to Book 4.
First, we import all the libraries:
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import *
Next, we massage the data into tensors (matrices) for training through our neural
network. The cat and dog images are stored under separate directories under data:
# load data
img_width = 150
img_height = 150
train_data_dir = 'data/train'
valid_data_dir = 'data/validation'
datagen = ImageDataGenerator(rescale = 1./255)
train_generator = datagen.flow_from_directory(
directory=train_data_dir,
target_size=(img_width,img_height),
classes=['dogs','cats'],
class_mode='binary',
batch_size=16)
630 BOOK 7
validation_generator = datagen.flow_from_directory(directory=valid_data_dir,
target_size=(img_width,img_height),
classes=['dogs','cats'],
class_mode='binary',
batch_size=32)
Now we build the neural network that forms the basis of the machine-learning
model. It is a six-layer neural network:
# build model
model = tf.keras.Sequential()
activation function. This is quite common:
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3), padding='same',
activation='relu'))
This next layer, MaxPooling2D
model.add(MaxPooling2D(pool_size=(2, 2)))
Next, another two layers of convolutional neural networks are added, each fol-
lowed by a pooling layer:
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
machine matches the data too closely such that the network won’t match any
input units to zero. We are removing 25 percent of the input units in this layer:
model.add(Dropout(0.25))
CHAPTER 4 631
connected 64-neuron layer:
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
print (model.summary())
# train model
:
print('starting training....')
history = model.fit_generator(generator=train_generator,
steps_per_epoch=2048 // 16,epochs=20,
validation_data=validation_generator,validation_steps=832//16)
print('training finished!!')
per run!
# save coefficients
model.save("CatsVersusDogs.trained")
Finally, we generate a graph using MatPlotLib that shows how the accuracy
improves with each epoch:
# Get training and test loss histories
training_loss = history.history['loss']
accuracy = history.history['acc']
632 BOOK 7
# Create count of the number of epochs
epoch_count = range(1, len(training_loss) 1)
# Visualize loss history
plt.figure(0)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, accuracy, 'b--')
plt.legend(['Training Loss', 'Accuracy'])
plt.xlabel('Epoch')
plt.ylabel('History')
plt.grid(True)
plt.show(block=True);
Install the h5py library before running this program. Otherwise the save state-
ment will not work.
sudo apt-get install python-h5py
Showtime! Run the following command in your terminal window:
python3 CatsVersusDogs.py
to generate 20 epochs of training
and save the results into our CatsVersusDogs.training
last epoch is as follows:
Epoch 20/20
128/128 [==============================] - 894s 7s/step - loss: 0.0996 - acc:
0.9609 - val_loss: 1.1069 - val_acc: 0.7356
training finished!!
You can safely ignore warnings from TensorFlow, such as:
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime
version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not
match runtime version 3.5
return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type
size changed, may indicate binary incompatibility. Expected 432, got 412
return f(*args, **kwds)
CHAPTER 4 633
By the shape of the curve, we might have been able to get a little better by running
more epochs, but this is good enough for our example.
Time to do a test on an external cat/dog image.
We’re going to use the trained data from our neural network training session
above to do a couple of predictions on new pictures to the neural network.
Cats and dogs
recognition
accuracy per
epoch.
634 BOOK 7
#import libraries
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from PIL import Image
Panther the
Cat on salmon.
Winston the Dog.
CHAPTER 4 635
print("import complete")
# load model
img_width = 150
img_height = 150
class_names = ["Dog", "Cat"]
model = tf.keras.models.load_model(
"CatsVersusDogs.trained",compile=True)
print (model.summary())
# do cat single image
imageName = "Cat150x150.jpeg"
testImg = Image.open(imageName)
testImg.load()
data = np.asarray( testImg, dtype="float" )
data = np.expand_dims(data, axis=0)
singlePrediction = model.predict(data, steps=1)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print(NumberElement)
print(Element)
print(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
# do dog single image
imageName = "Dog150x150.JPG"
testImg = Image.open(imageName)
testImg.load()
data = np.asarray( testImg, dtype="float" )
data = np.expand_dims(data, axis=0)
singlePrediction = model.predict(data, steps=1)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print(NumberElement)
print(Element)
print(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
636 BOOK 7
This code uses the training that we generated by training with the cats and dogs
dataset earlier in this chapter.
First, we import our libraries:
#import libraries
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from PIL import Image
print("import complete")
# load model
img_width = 150
img_height = 150
class_names = ["Dog", "Cat"]
Here is the place where we load the training data that we generated earlier for the
neural network machine learning model:
model = tf.keras.models.load_model(
"CatsVersusDogs.trained",compile=True)
print (model.summary())
Now, we test a single cat image:
# do cat single image
imageName = "Cat150x150.jpeg"
testImg = Image.open(imageName)
testImg.load()
Convert to a NumPy tensor:
data = np.asarray( testImg, dtype="float" )
Expand the dimension, since this function looks for an array of images:
data = np.expand_dims(data, axis=0)
CHAPTER 4 637
Now, we do the predication based on our image:
singlePrediction = model.predict(data, steps=1)
We print out the raw data:
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print(NumberElement)
print(Element)
print(singlePrediction)
Interpret the prediction:
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
Next, we do the same with a single dog image:
# do dog single image
imageName = "Dog150x150.JPG"
testImg = Image.open(imageName)
testImg.load()
data = np.asarray( testImg, dtype="float" )
data = np.expand_dims(data, axis=0)
singlePrediction = model.predict(data, steps=1)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print(NumberElement)
print(Element)
print(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
print (str(int(Element*100)) "% Confidence Level")
Save the code into singleTestImage.py and run using sudo python3 singleTest
Image.py.
638 BOOK 7
Here are the results:
import complete
WARNING:tensorflow:No training configuration found in save file: the model
was *not* compiled. Compile it manually.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 150, 150, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 75, 75, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 75, 75, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 37, 37, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 37, 37, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 18, 18, 64) 0
_________________________________________________________________
dropout (Dropout) (None, 18, 18, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 20736) 0
_________________________________________________________________
dense (Dense) (None, 64) 1327168
_________________________________________________________________
dropout_1 (Dropout) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 2) 130
=================================================================
Total params: 1,355,938
Trainable params: 1,355,938
Non-trainable params: 0
_________________________________________________________________
None
1
1.0
[[ 0. 1.]]
Our Network has concluded that the file 'Cat150x150.jpeg' is a Cat
100% Confidence Level
0
1.0
[[ 1. 0.]]
Our Network has concluded that the file 'Dog150x150.JPG' is a Dog
100% Confidence Level
CHAPTER 4 639
99.99 percent or something like that and is just rounded to 100 percent by the
formatting.
Now that we have the trained model and have tested it with some real images,
it is time to put it into our robot and use the Pi camera for some dog and cat
investigation.
A real limitation of the way we built this neural network is that it really is only
looking at images of cats and dogs and determining whether the image is a cat or
as either a cat or dog. If we were
test of the network using the much maligned dress picture from Book 4.
And, as expected, the network got it wrong:
Our Network has concluded that the file 'Dress150x150.JPG' is a Cat
There is a lot more to the practice of teaching a machine to learn in a general
manner.
If you get an error such as: undefined symbol: cblas_sgemm from your import
numpy, try running your program with sudo.
A picture
of a dress?
640 BOOK 7
Time to add a new experience to last chapter’s robot. We are going to install the
trained cats and dogs neural network on the PiCar-B robot and use the onboard
LEDs to display whether the onboard Pi camera is looking at a cat or a dog.
when the ultrasonic sensor changes, such as when a dog or cat may walk in front
of the robot.
It has the robot staring at the screen (triggering the ultrasonic sensor) showing a
PowerPoint presentation of various cats and dogs.
#!/usr/bin/python3
#using a neural network with the Robot
#import libraries
import numpy as np
import tensorflow as tf
Robot vision
neural network
test setup.
CHAPTER 4 641
from tensorflow.python.framework import ops
from PIL import Image
import RobotInterface
import time
import picamera
print("import complete")
RI = RobotInterface.RobotInterface()
# load neural network model
img_width = 150
img_height = 150
class_names = ["Dog", "Cat"]
model = tf.keras.models.load_model("CatsVersusDogs.trained",compile=True)
RI.centerAllServos()
RI.allLEDSOff()
# Ignore distances greater than one meter
DISTANCE_TO_IGNORE = 1000.0
# How many times before the robot gives up
DETECT_DISTANCE = 60
def bothFrontLEDSOn(color):
RI.allLEDSOff()
if (color == "RED"):
RI.set_Front_LED_On(RI.right_R)
RI.set_Front_LED_On(RI.left_R)
return
if (color == "GREEN"):
RI.set_Front_LED_On(RI.right_G)
RI.set_Front_LED_On(RI.left_G)
return
if (color == "BLUE"):
RI.set_Front_LED_On(RI.right_B)
RI.set_Front_LED_On(RI.left_B)
return
642 BOOK 7
def checkImageForCat(testImg):
# check dog single image
data = np.asarray( testImg, dtype="float" )
data = np.expand_dims(data, axis=0)
singlePrediction = model.predict(data, steps=1)
print ("single Prediction =", singlePrediction)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
return class_names[NumberElement]
try:
print("starting sensing")
Quit = False
trigger_count = 0
bothFrontLEDSOn("RED")
#RI.headTiltPercent(70)
camera = picamera.PiCamera()
camera.resolution = (1024, 1024)
camera.start_preview(fullscreen=False,
window=(150,150,100,100))
# Camera warm-up time
time.sleep(2)
while (Quit == False):
current_distance = RI.fetchUltraDistance()
print ("current_distance = ", current_distance)
if (current_distance < DETECT_DISTANCE):
trigger_count = trigger_count 1
print("classifying image")
camera.capture('FrontView.jpg')
imageName = "FrontView.jpg"
testImg = Image.open(imageName)
new_image = testImg.resize((150, 150))
new_image.save("FrontView150x150.jpg")
CHAPTER 4 643
if (checkImageForCat(new_image) == "Cat"):
bothFrontLEDSOn("GREEN")
else:
bothFrontLEDSOn("BLUE")
time.sleep(2.0)
bothFrontLEDSOn("RED")
time.sleep(7.0)
except KeyboardInterrupt:
print("program interrupted")
print ("program finished")
Most of the preceding code is pretty straightforward and similar to the robot brain
software earlier in the chapter. One part does deserve to be talked about, however,
def checkImageForCat(testImg):
# check dog single image
data = np.asarray( testImg, dtype="float" )
data = np.expand_dims(data, axis=0)
singlePrediction = model.predict(data, steps=1)
print ("single Prediction =", singlePrediction)
NumberElement = singlePrediction.argmax()
Element = np.amax(singlePrediction)
print ("Our Network has concluded that the file '"
imageName "' is a " class_names[NumberElement])
return class_names[NumberElement]
This function takes the incoming test vision (taken from the Pi camera and then
robotVision.py sudo
python3 robotVision.py
644 BOOK 7
really should run this program on the Raspberry Pi GUI so you can see the small
camera preview on the screen.
current_distance = 20.05481719970703
classifying image
single Prediction = [[ 0. 1.]]
Our Network has concluded that the file 'FrontView.jpg' is a Cat
100.00% Confidence Level
current_distance = 20.038604736328125
classifying image
single Prediction = [[ 1. 0.]]
Our Network has concluded that the file 'FrontView.jpg' is a Dog
100.00% Confidence Level
current_distance = 19.977807998657227
Overall, the results were pretty good. We found variations in recognition due to
lighting, which wasn’t a big surprise. However, the network consistently identi-
out what is wrong with a neural network machine learning model like this is there
is no way to really know how the machine made its decision. Looking at the net-
work you can look at 1.3 parameters and weights and you can’t tell where
it is going wrong. After a while, you get a feel for what works and what doesn’t
wrong, you are just out of luck.
The cat who is
apparently a dog.
CHAPTER 4 645
We have a nefarious plan here for a variant of this neural network in the future.
We’re going to use it on a new project, the Raspberry Pi based MouseAir, that will
launch toy mice when the camera spots a cat but not when it spots a dog. You can
be fun.
As part of an advanced Python brain on the PiCar-B, there are a number of
things you could do with neural networks and other AI techniques. Here are a few
examples:
Retrain our Cat/Dog network to focus only on cats; it lumps everything else into
the “Not Cat” category. You can kind of do that with the current network because
this training set tends to somewhat classify anything that doesn’t look like a cat
as a dog. It doesn’t do that all the time though. Remember the dress picture in
MouseAir, an AI
mouse-launching
cat toy.
646 BOOK 7
Take the streaming video from our Raspberry Pi and grab a frame once in a
while and check with a neural network (trained on Santa images) to determine
whether Santa is here for Christmas. Impossible! No, wait. It’s been done in a
very cool series of articles here: https://www.pyimagesearch.com/2017/12/18/
keras-deep-learning-raspberry-pi/.
You can program the Raspberry Pi using a library called OpenCV (Open-Source
Computer Vision) to detect a ball in your camera stream and give you the
change the tilt to look for it too. A version of this project is part of the Adeept soft-
server.py
under the server directory.
You can use the power of the Amazon Alexa program to order your robot to do
things. If you’d like to see how to connect up your Raspberry Pi to Alexa to do just
that kind of project, check out the ebook
by John Shovic.
becomes less and less expensive and the software techniques more easily accessi-
ble, there will be more and more uses for robots, not only manufacturing but also
in the home and on the road.
We expect to see new AI in many new consumer products in the coming years. The
only question we have is “How long before we have AI in my toaster?” We’ll buy
one when they come out.
Index 647
Index
Special Characters
- operator, 70
/ (division), 117
//
/ operator, 70
 (backslash), 68
n, 95
+ operator, 70
= assignment operator, 152
= operator, 80
== operator, 71, 80, 126
!= operator, 71, 126
# sign, 195
$, number containing, 65
% operator, 70
%% directive, 110
%20 ASCII code, 329
%a, %b %d %Y format string, 111
%A, %B %d at %I:%M%p format string, 114
%A %B %d is day number %j of %Y format
string, 111
%A directive, 110
%a directive, 110
%b directive, 110
%B directive, 110
%c directive, 110
%c format string, 114
%d directive, 110
%f directive, 110
%H directive, 110
%H:%M:%S and %f microseconds format
string, 113
%I directive, 110
%I:%M %p format string, 113
%I:%M %p on %b %d format string, 114
%I:%M %p on %m/%d/%y format string, 114
%j directive, 110
%M directive, 110
%m directive, 110
%m/%d/%y at %H:%M%p format string, 114
%m/%d/%y at %I:%M %p format string, 114
%m-%d-%y format string, 111
%m/%d/%Y format string, 160
%p directive, 110
%S directive, 110
%U directive, 110
%w directive, 110
%W directive, 110
%X directive, 110
%x directive, 110
%x format string, 111
%X format string, 113
%x format string, 114
%Y directive, 110
%y directive, 110
%Z directive, 110
%z directive, 110
* operator, 70
** operator, 70
*args, 204
@ decorator, 231
@staticmethod decorator, 231
< operator, 71, 126
<= operator, 71, 126
<article>...</article> tags, 333
> operator, 71, 126
>= operator, 71, 126
Numbers
3.3V base units, 497
3.3V I2C Grove module, 489
3D printers, 540
5cm-long Grove cables, 492
648 Python All-in-One For Dummies
5V base units, 497
5V I2C Grove connector, 489
5V pins, 549
5V stepper motor, 556
12C device
building dashboard on phone, 525–536
adding Blynk dashboard, 527–530
breaking down code, 533–535
building on experience, 536
HDC1080 temperature and humidity, 525–526
temperatureTest.py software, 531–533
analog-to-digital converter (ADC), 518–519
breaking down code, 522–525
Grove gas sensor (O2), 519
oxygen experiment, 520–522
understanding, 506–517
breaking down program, 514–517
on Raspberry Pi, 507–508
reading temperature and humidity from,
511–514
talking to, 508–511
28BYJ-48 ULN2003 5 V stepper motor, 556, 557
125KHz RFID reader, 495
200 HTTP Status code, 326
400 HTTP Status code, 326
403 HTTP Status code, 326
404 HTTP Status code, 326
A
a: (Append) mode, 269, 280
A0 signal line, 494
A1 signal line, 494
A12 chip, 360
abs() function, 86, 87
abstraction, 453
AC (alternating current), 537
.activate() method, 227
activation function, 369, 400
actuators, 474, 568, 573
Adafruit DC motor, 543
Adafruit Ultimate GPS, 501–503
ADAM method, 387, 390, 401
adaptor cables, 499, 502
ADC (analog digital converter), 476, 496, 522–523
.add() method, 166
Adeept Mars Rover PiCar-B
assembling, 586–594
calibrating servos, 588–590
installing software for test, 591–592
Pi camera video testing, 592–594
running tests in Python, 591
test code, 592
components of, 577–586
controller board, 578
drive motor, 580–581
Pi camera, 584–585
Pixel RGB programmable LEDs, 582–583
RGB LED, 581–582
servo motors, 578–580
ultrasonic sensor, 585–586
materials for, 576–577
Adeept software
disabling auto startup, 591
overview, 621
current limitations of, 363
Google Cloud Platform and, 452
NumPy library and, 438
in robotics, 623–646
future of, 646
machine-learning neural network, 624–633
Python Mars Rover PiCar-B with, 640–645
techniques of, 356–363
machine learning, 359–360
neural networks, 356–359
TensorFlow, 361–363
in Cloud, 420–422
Amazon Web Services (AWS), 421
Google cloud, 421
IBM cloud, 422
Microsoft Azure, 422
Index 649
on graphics cards, 423–424
project inspiration, 424–425
Raspberry Pi
adding to, 417–419
limitations with, 415–417
AI Accelerator, 416
AI compute sticks, 418–419
AI Winter, 358
Alexa. See Amazon Alexa
algorithms, 361
alias name, 59
alignment, formatting, 96–97
allLEDSOff() function, 601
alphabetize() function, 202
alphabetizing lists, 159–161
alternating current (AC), 537
Amazon Alexa, 570
data science and, 431
neural network for controls with, 646
Amazon AWS Cloud, 570
Amazon Web Services (AWS), 421
America/Adak time zone, 119
America/Anchorage time zone, 119
America/Chicago time zone, 119
America/Denver time zone, 119
America/Detroit time zone, 119
America/Indiana/Indianapolis time zone, 119
America/Indiana/Knox time zone, 119
America/Los_Angeles time zone, 119
American Standard Code for Information
Interchange (ASCII), 104, 105, 207
America/New_York time zone, 119
America/Phoenix time zone, 119
Anaconda development environment, 13–17
Anaconda Navigator
home page, 17
latest version of, 57
opening in Mac, 16
opening in Windows, 16
pip (Pip Installs Packages) in, 345
analog, 494–495, 496
analog digital converter (ADC), 476, 496, 522–523
analyzing data, 434, 461–464
and operator, 71, 126
anomalies, 433
anonymous functions, 206–212
API (application programming interface), 382, 452,
505, 574
opening, 62
typed into VS Code, 76
Append (a:) mode, 269, 280
.append() method, 151, 163
Apple smartphones, 360
application programming interface (API), 382, 452,
505, 574
applications, building
data types, 64–69
numbers, 65–66
True/false Booleans, 68–69
words (strings), 66–68
with operators, 69–72
arithmetic operators, 69–70
Boolean operators, 71–72
comparison operators, 70–71
putting code together, 82
typing and using comments, 63–64
using variables, 72–77
creating in code, 74
creating names for, 73–74
manipulating, 75–76
running app in VS Code, 76–77
saving work, 76
.archive() method, 216
Arduino Mini Pro LB board, 490, 498
Arduino Raspberry Pi. See Raspberry Pi
Arduino Uno Grove base board, 489
arguments
of functions, 86
passing information to functions, 204–205
650 Python All-in-One For Dummies
arithmetic operators, 69–70
array JSON data conversion, 307
arrays. See lists
arrow module, 123
current limitations of, 363
Google Cloud Platform and, 452
NumPy library and, 438
in robotics, 623–646
future of, 646
machine-learning neural network, 624–633
Python Mars Rover PiCar-B with, 640–645
techniques of, 356–363
machine learning, 359–360
neural networks, 356–359
TensorFlow, 361–363
in Cloud, 420–422
Amazon Web Services (AWS), 421
Google cloud, 421
IBM cloud, 422
Microsoft Azure, 422
on graphics cards, 423–424
project inspiration, 424–425
Raspberry Pi
adding to, 417–419
limitations with, 415–417
Massaron), 413
ASCII (American Standard Code for Information
Interchange), 104, 105, 207
assignment operator, 74
associative arrays. See data dictionaries
Atmega 8L computer, 542
attributes
changing values of, 222
general discussion of, 214
of objects in classes, 219–222
audio jack, 473
authentication token (AUTH TOKEN), 529, 530
Auto Save, 41
average_covered_ charges, inpatient_
charges_2015 Dataset, 458
average_medicare_ payments, inpatient_
charges_2015 Dataset, 458
average_total_payments, inpatient_
charges_2015 Dataset, 458
aware datetime, 120
AWS (Amazon Web Services), 421
Azure, 422–423
B
b: (Binary) mode, 270
backpropagation, 367, 368, 390
backslash (), 68
backward stepping, 555
backwardPropagate function, 376–377
Base 2 system, 98
Base 8 system, 98
Base 16 system, 98
base classes, 236, 261
base units
3.3V base units, 497
5V base units, 497
Grove connectors, 489–492
Pi2Grover, 478, 490–491
Raspberry Pi, 490–491
batteries
18650 LiPo, 577
Raspberry Pi and life of, 577
self-driving car robot and, 614
baud rate, 495
Baxter robot, 361–362, 570–571
BeautifulSoup object, 332
biases, 368
bidirectional open-drain lines, 506
big data
analytics, 432
data science project, 440–450
breaking down code, 443–444
Index 651
choosing data, 440–443
heat plots with pandas, 448–450
Google Cloud Platform, 452–467
managing volume, variety, and velocity, 432
MatPlotLib library, 439–440
NumPy library, 438–439
pandas library, 439
public datasets, 453
variety, 431
velocity, 431
visualizing data with MatPlotLib, 444–448
diamond clarity versus carat size, 445–446
diamonds in each clarity type, 446–447
diamonds in each color type, 447–448
volume, 430–431
BigQuery
cloud computer security, 453–454
importing, 460
Medicare database, 454–466
analyzing, 461–464
big-data code, 457–459
breaking down code, 460–461
setting up project, 454–457
visualizing data, 465–466
OpenAQ database, 466–467
signing up for, 454
bin() function, 87, 98
b: (Binary) mode, 270
compressed, 268
documents, 268
executable, 268
fonts, 268
images, 268
reading and copying, 283–286
and text, 267–269
binary numbers, 98–99
binary_accuracy values, 387, 388
binarycopy.py
birth_year variable, 291
bit banging, 498
bits, 493
block write command, 547
blocks, hardware, 473
BlueTooth-enabled robots, 571, 573
Blynk app, building dashboard on phone using
breaking down code, 533–535
building on expertise, 536
HDC1080 temperature and humidity, 525–526
BMW X3s, 568
BOM (Byte Order Mark), 289
Booleans, 68–69
converting to, 293
operators, 71–72, 126
values, 85, 186
break statements
force stopping for loops with, 138–139
while loops and, 144–146
brightness parameter, 601
built-in functions, 208, 343
descrip function, 444
Math Module, 89–90
for numbers, 87
built-in methods, 103
Byte Order Mark (BOM), 289
C
cables
5cm-long Grove cables, 492
adaptor cables, 499, 502
connecting with
Adafruit Ultimate GPS, 501–503
Grove patch cable, 499–501
female header cables, 500
female patch cables, 556, 557
male header cables, 501
male patch cables, 548
male-pin-to Grove-connector patch cable, 549
patch cables, 503, 550
calibrateServo.py program, 588, 603
calibrating servos, 588–590, 603
652 Python All-in-One For Dummies
calling functions, 194
calValues.py program, 588
Camel caps, 74
camera CSI, 473
cameras, Pi, 584–585, 592–594
candles, 518
Capek, Karel, 567
capitalize method, 342
capturing data, 433
carat, diamond database, 440
cats, machine learning neural network for
recognizing, 645
centerAllServos() function, 606
central processing units (CPU), 360, 415, 473
CES (Consumer Electronic Show), 571
charts, heat plots with pandas, 448
cheat sheets, 34
chips, 371
Chollet, Francois, 413
chr() function, 142
chunk variable, 285
clarity, diamond database, 441
class help, 33
class methods, 230–232
class variables, 228–230
classes
creating, 216–217
creating objects from, 217–218
empty classes error, 217
as Errors in Python, 261
general discussion of, 213–216
giving attributes to objects in, 218–224
changing values of, 222
creating objects, 219–222
giving methods to, 224–233
calling class method by class name, 227–228
passing parameters to methods, 226–227
using class methods, 230–232
using class variables, 228–230
using static methods, 232–233
inheritance, 234–246
adding extra parameters from subclasses,
239–241
calling base class method, 242
creating base classes, 236–237
overriding default value from subclasses, 239
using same name twice, 243–246
clear() method, 156, 163, 181, 184
clients, 324
clone command, 591
.close() method, 270, 272
clothes
classifying, 395
creating network for detecting, 397–409
breaking down code, 399–401
CNN model code, 406–409
evaluation results, 402–403
external pictures, 403–405
Fashion-MNISTl database, 398
testing, 398–399, 405–406, 409
training, 402–403
learning more about, 413
420–422
Amazon Web Services (AWS), 421
Google cloud, 421
IBM cloud, 422
Microsoft Azure, 422
CNN (convolutional neural networks), 406–409,
625, 626
cobots (cooperative robots), 568, 570
code
breaking down, 443–444, 460–461
calibrateServo, 588
creating data dictionary, 171
creating variables in, 74
debugging, 42–45
drive motor, 580
folders for, 37–39
Index 653
putting together, 82
reading, 54–55
running in VS Code, 41–42
saving, 41
servo motor, 552–554
stepper motor step, 563–564
Style Guide for, 50–51
for web scraping, 331, 335
writing, 40–41, 45–48
code blocks, 80
code editors, 12
colons, in data dictionary code, 171
color, diamond database, 440
color() function, 600
Color() function, 601
color parameter, 601
color schemes
choosing, 29
for comments, 64
colorWipe() function, 600
combining lists, 153–154
comma, number containing, 65
converting
to Boolean, 293
to date, 292–293
to integers, 291
to objects and dictionaries, 295–302
strings, 290–291
importing
to dictionaries, 299–302
to objects, 296–299
in Microsoft Excel, 286
opening, 288–290
reading into lists, 300–301
saving scraped data to, 336–338
storing information in, 518
in text editor, 287
Command Prompt, 42
command thread, 620
commands
entering in Interactive Mode, 30–31
git clone, 512, 545
import, 346
model.fit, 389
module.compile, 390
pip, 57
print(pi), 346, 348
ps xaf, 486
python, 30
sudo, 609
sudo halt, 509, 588
sudo reboot, 591
commas, separating multiple values in
functions with, 199
comments
on functions, 195
typing and using, 63–64
using color for, 64
communicating results, 434–435
communications, for robotics, 573
comparison operators (relational operators),
70–71
!= is not equal to, 126
< is less than, 126
<= is less than or equal to, 126
== is equal to, 126
> is greater than, 126
>= is greater than or equal to, 126
complex() function, 100
complex numbers, 66, 99–100
Computer Vision Projects with OpenCV and
Python 3 (Rever), 624
computers
building projects, 474–476
electric motors controlling with, 540–564
Python and DC motors, 540–547
Python and making stepper motor step,
554–564
Python and running servo motor,
548–551
654 Python All-in-One For Dummies
computers (continued)
physical computing, 471–486
controlling LED, 482–484
in everyday life, 472
making computers do things, 474
Pulse-width modulation (PWM), 485–486
Raspberry Pi, 476–482
assembling hardware, 478–482
GPIO pins, 477
GPIO Python libraries, 477
hardware for “Hello World,” 478
using small computers to build projects, 474–476
robotics and, 568, 572–573
SBC (single board computer), 473
small systems, 475
VNC (virtual network computer), 396
concatenating
numbers to strings, 197
strings, 101–102
connectors. See also Grove connectors
digital connectors, 493
GND connector, 498
GPIO connector, 473
SCL connector, 498
SDA connector, 498
standardized connectors, 487–488
VCC connector, 498
consumer behavior, big data and, 430
Consumer Electronic Show (CES), 571
content variable, 276, 333
context manager, 270–271
contextual coding, 270–271
continue statements
with for loops, 140
with while loops, 143–144
control circuits, 578
controller boards, Python Mars Rover PiCar-B, 578
converting
to Boolean, 293
to date, 292–293
to integers, 291
to objects and dictionaries, 295–302
strings, 290–291
data to JSON format, 304
Excel dates to JSON dates, 309–310
Excel spreadsheet, 305
convolutional neural networks (CNN), 406–409,
625, 626
cooperative robots (cobots), 568, 570
copy() method, 162, 163, 181
copying
data dictionaries, 182
lists, 162
core language, 56
correlation heat plots, 450
.count() method, 157, 163, 164
CPU (central processing units), 360, 415, 473
crashing, keeping app from, 253–255
Create (x:) mode, 269
CSS, 8
converting
to Boolean, 293
to date, 292–293
to integers, 291
to objects and dictionaries, 295–302
strings, 290–291
importing
to dictionaries, 299–302
to objects, 296–299
in Microsoft Excel, 286
opening, 288–290
reading into lists, 300–301
saving scraped data to, 336–338
storing information in, 518
in text editor, 287
csv writer, 337
curly braces, 54
cut, diamond database, 440
Index 655
D
D0 signal line, 493
D1 signal line, 493
D4/5 Grove connector, 552
dashboards, building on phones
breaking down code, 533–535
building on experience, 536
HDC1080 temperature and humidity, 525–526
how to add Blynk dashboard, 527–530
data
analyzing, 432
from Medicare database, 461–464
with pandas library, 439
choosing for data science project, 440–443
converted to JSON format, 304
downloading from Kaggle, 440
dumping to JSON data, 318–321
loading in TensorFlow language, 384
metadata, 440
misleading online, 433
removing from dictionaries, 317–318
saving scraped, 335–338
visualizing with MatPlotLib, 444–448, 465–466
diamond clarity versus carat size, 445–446
diamonds in each clarity type, 446–447
diamonds in each color type, 447–448
data dictionaries
copying, 182
creating, 171–179
accessing, 172–174
adding items, 177–179
changing items, 177–179
changing value of keys, 177
with get() method, 176
getting length of, 174–175
verifying key existence, 175
deleting data from, 182–185
looping through, 179–181
methods for, 181–182
multiple key dictionaries, 186–192
nesting, 190–192
using fromkeys() method, 188–190
using setdefault() methods, 188–190
data massaging, pandas library and, 439
data queues, 620
data science
big data, 430–432
managing volume, variety, and velocity, 432
variety, 431
velocity, 431
volume, 430–431
data analytics and, 432
Google Cloud Platform and, 452
projects using libraries, 440–450
breaking down code, 443–444
choosing data, 440–443
heat plots with pandas, 448–450
steps to, 433–435
analyzing data, 434
capturing data, 433
communicating results, 434–435
maintaining data, 435
processing data, 433–434
visualizing data with MatPlotLib, 444–448
diamond clarity versus carat size, 445–446
diamonds in each clarity type, 446–447
diamonds in each color type, 447–448
data types
audio recordings, 431
correlations between, 434
numbers, 65–66
photos, 431
tabular, 439
time-series, 439
True/false Booleans, 68–69
words (strings), 66–68
DataFrames from pandas library, 439
datasets, large, downloading, 451
date directives, 110
date format strings, 111, 113
656 Python All-in-One For Dummies
date() method, 160
dates
converting, 292–293
displaying in lists, 160
formatting strings for, 107–112
datetime format strings, 114
datetime module, 108, 160, 292, 309, 313
datetime .utcfromtimestamp() method, 313
datetime.date data type, 108, 109
datetime.datetime data type, 108
datetime.now() data type, 131
datetime.time data type, 108, 112
dateutil module, 121
DC (direct current) motors, 538–547, 578, 580
Grove I2C motor drive, 542–545
Python DC motor software, 545–547
dcMotor directory, 545
dcmotorTest.py
Debug pane, 44
debugging
built-in VS Code editor, 43–45
code, 42–43
decimal point, 65
decision trees, 360
decorators, 231
deep learning, 359
Deep Learning with Python (Chollet), 413
def keyword
creating functions with, 194
custom functions and, 208
default values, 198, 222–224
del() command, 156
del keyword, 182–183
delay argument, 596
delay parameter, 602
depth, diamond database, 441
derivatives, 371
descrip function, 444
deserialization, 306
development workspace, 34–37
diamonds
creating data science project, 440–450
breaking down code, 443–444
choosing data, 440–443
heat plots with pandas, 448–450
visualizing data with MatPlotLib, 444–448
diamond clarity versus carat size, 445–446
diamonds in each clarity type, 446–447
diamonds in each color type, 447–448
Dickinson, John, 357, 360
dict data conversion, 307
__dict__ method, 245
dictionaries
changing value of key, 318
converting, 295–302
importing, 299–302
one value at time, 312, 315
in pandas DataFrames, 439
removing data from, 317–318
digital
Grove digital, 493–495
vs.analog, 496
digital analog input, 505
digital bits, 493
digital combined messages, 507
digital connectors, 493
digital humidity sensor, 509
digital I2C, 506
digital input, 505
digital signal processor (DSP), 417
digital single message, 507
Digital SPI (serial peripheral interface), 506
dir() function, 340–341, 343, 346, 347
direct current (DC) motors, 538–547, 578, 580
Grove I2C motor drive, 542–545
Python DC motor software, 545–547
direct memory access (DMA), 583, 600
directional controls for robots, with GPIO
lines, 580
directives, date and time, 110
display DSI, 473
division (/), 117
Index 657
DMA (direct memory access), 583, 600
docs.python.org, 343
docstrings, 63–64, 195
double-quotation marks, 67–68
drg_definition, inpatient_charges_2015
Dataset, 458
drive motors, Python Mars Rover PiCar-B,
580–581
driver boards, 560
drivers, 505
DSP (digital signal processor), 417
dumps() method, 318–319
dunder init method, 217
dunder named items, 341
duplicates, 433
duty cycle, 485
E
e constant, 90
Easter egg, 49–50
edge computing, 417
editing
hello.py
editors, 11–12
electric motors
controlling with computers, 540–564
Grove I2C motor drive, 542–545
making stepper motor step, 554–564, 562–563,
563–564
running a servo motor, 548–551, 551–552,
552–554
using Python and DC motors, 540–547
exploring, 538–540
DC motors, 538–539
servo motor, 539
stepper motor, 539–540
elif statements, 131–133
ellipsis, in data dictionary code, 171
else keyword, using with errors,
255–258
else statements, adding to if statements,
130–131
embedded systems, 472, 573
EmptyFileError exception, 261
encoders, 580
encrypted data, 324
engineer neurons, 358–359
entropy, 401
enumerate() function, 280
epochs, 368, 378–380, 626, 631
error messages, 81–82
HTTP error, 329
import error, 375
OS Error, 313
error package, 327
errors
concatenating numbers, 197
exceptions, 247–250
from functions without parameters, 197
keeping app from crashing, 253–255
programming, 253
raising own errors, 259–263
replacing error message code, 251
specifying exceptions, 252–253
syntax error for non-default argument, 199
using else keyword, 255–257
using try . . . except . . . else . . .
finally, 257–258
escape characters, 68
ESP32 boards, 476
ESP8266 boards, 476, 498
Etc/UCT timezone, 119
Etc/UTC timezone, 119
Ethernet, 473
evolutionary algorithms, 360–361
evolutionary computing, 359
Excel reader (xlrd), 309
Excel spreadsheets
converting to JSON, 304–305
data in, 304
dates in, 309–310
web scraped data, 337
658 Python All-in-One For Dummies
except keyword, 251, 253, 257–258
exceptions, 247–250, 251
eXclusive OR gate (XOR gate), 370, 371
exponents, 70
extend() function, 153, 163
converting strings, 290–291
converting to Boolean, 293
converting to date, 292–293
converting to integers, 291
importing to dictionaries, 299–302
importing to objects, 296–299
opening, 288–290
with readline(), 279–280
with readlines(), 277–279
using seek(), 283
using tell(), 281–283
F
Facebook
dataset volume and, 430
as high velocity dataset, 431
False data conversion, 307
false JSON data conversion, 307
Fan module, 493
fashion database, 397
Standards and Technology) database, 395,
398–399, 402–403
feedback, 368, 556
feedForward function, 376–377
feed-forward input, 367
female header cables, 500
female patch cables, 556, 557
fetchUltraDistance() function, 602
FileNotFoundError message, 250, 261
See also
b: (Binary) mode, 270
compressed, 268
documents, 268
executable, 268
fonts, 268
images, 268
reading and copying, 283–286
and text, 267–269
closing, 269–275
editing, 268
looping through, 281–283
opening, 269–275
overwriting, 280–281
finally keyword, 257–258
find_all method, 333
Firebase data, 320, 321
First Grove Female Patch Cord, 558
first_name variable, 290
float data type, 288
float() function, 87
FLOAT type, inpatient_charges_2015 Dataset, 458
(//), 70, 117
FMTensorFlowPlot.py
FMTensorFlow.py
for loops, operations with, 134–141
with continue statements, 140
force stopping with break statements, 138–139
nesting loops, 140–141
through list, 137–138
through numbers in ranges, 134–136
through strings, 136–137
Index 659
format() function, 87
format strings (f-strings)
in data dictionaries, 192
for dates, 111
expression part of, 91
formatting percent numbers, 93–94
literal part of, 91
making multiline, 95
showing dollar amounts, 92–93
width and alignment, formatting, 96–97
formatting numbers
with f-strings, 91–92
making multiline format strings, 95
percent numbers, 93–94
showing dollar amounts, 92–93
width and alignment, 96–97
Forta, Ben, 459
fossil fuels, robotics and, 569
fragment, of URL, 324
from . command, 347
from statement, 348
fromkeys() method, 181, 188–190
f-strings (format strings)
in data dictionaries, 192
for dates, 111
expression part of, 91
formatting percent numbers, 93–94
literal part of, 91
making multiline, 95
showing dollar amounts, 92–93
width and alignment, formatting, 96–97
full_name variable, 290
functions
abs(), 86, 87
activation, 369, 400
allLEDSOff(), 601
alphabetize(), 202
anonymous, 206–212
backwardPropagate, 376–377
bin(), 87, 98
built-in, 208, 343
descrip function, 444
Math Module, 89–90
for numbers, 87
calculating numbers with, 86–90
centerAllServos(), 606
chr(), 142
color(), 600
Color(), 601
colorWipe(), 600
commenting on, 195
complex(), 100
creating, 194–195
descrip, 444
dir(), 340–341, 343, 346, 347
enumerate(), 280
extend(), 153, 163
feedForward, 376–377
fetchUltraDistance(), 602
float(), 87
format(), 87
headTiltDown(), 604
headTiltMiddle(), 604
headTiltPercent(), 605
headTiltUp(), 604
headTurnLeft(), 603
headTurnMiddle(), 603
headTurnPercent(), 604
headTurnRight(), 603
help(), 243, 341–343, 346, 347
hex(), 87, 98
int(), 87, 144, 291
lambdas. See anonymous functions
len(), 102, 150
for data dictionaries, 174
transform and, 207
loss, 369–370, 387, 401
math module, 88–90
660 Python All-in-One For Dummies
functions (continued)
math.acos(), 89
math.atan(), 89
math.atan2(), 89
math.ceil(), 89
math.cos(), 89
math.degrees(), 89
math.e, 89
math.exp(), 90
math.factorial(), 90
math.floor(), 90
math.isnan(), 90
math.log(), 90
math.log2(), 90
math.pi, 90
math.pow(), 90
math.radians(), 90
math.sin(), 90
math.sqrt(), 89, 90
math.tan(), 90
math.tau(), 90
max(), 87
mean_squared_error, 387
min(), 87
model.evaluate, 401
model.predict, 389
motorBackward(), 602
MotorDirectionSet, 547
motorForward(), 596, 602
MotorSpeedSetAB, 547
neuron activation, 630
newline="," 308
newline='', 337
oct(), 87, 98
ord(), 104
passing information to, 196–205
arguments, 204–205
multiple values, 199–200
multiple values in lists, 202–203
optional parameters with defaults, 198
using keyword arguments (kwargs), 200–201
print(), 274
adding spaces in, 197
for data dictionaries, 172
rainbowCycle(), 600
range(), in for loop, 134–136
returning values from, 205–206
round(), 86–87, 388
set_Front_LED_Off(), 599–600
set_Front_LED_On(), 598–599
setPixelColor(), 601
sigmoid, 369, 377
sleep(), 597
softmax, 400
sparse categorical crossentropy, 401
stopMotor(), 602–603
str(), 87, 158
theaterChaseRainbow(), 600–601
type(), 87, 340
ultra.checkdisk(), 585
wheelsLeft(), 596, 605
wheelsMiddle(), 596, 598, 605
wheelsPercent, 596–597
wheelsPercent(), 606
wheelsRight(), 605
G
Gartner analysis company, 430
gearing sets, 578
general purpose input-output (GPIO), 477, 482,
486, 540, 548, 555, 562–563, 578
.get() method, 175, 176, 181, 334
gettz, 121
Git, 18
git clone command, 512, 545
GitHub website, 363, 513
GMT (Greenwich Mean Time), 119
GND connector, 498
GND signal line, 493, 495
Google Brain Groups, 363
Google Chrome Extensions, 325, 326
Index 661
Google Cloud Platform, 421
BigQuery, 452–467
cloud computer security, 453–454
Medicare database, 454–466
OpenAQ database, 466–467
signing up for, 454
Google Edge TPU accelerator, 419
Google Firebase Realtime Database
data in, 305
Google trend searches, 9
google.cloud library
importing, 460
installing, 459
GPIO (general purpose input-output), 477, 482,
486, 540, 548, 555, 562–563, 578
GPIO connector, 473
GPIO function, 551
GPIO lines
LEDs controlled by, 581
on PiCar-B robots, 580
GPIO Python libraries, 477
gpiozero library, 477
GPU (graphics processing unit), 360, 415, 423–424
graphical user interface (GUI), 392, 396, 476
graphics cards, 423–424
graphics processing unit (GPU), 360, 415, 423–424
graphics processor, 417
graphs
bar graphs, 447, 466
live graphs, 440
with MatPlotLib, 440
green errors, 81–82
Greenwich Mean Time (GMT), 119
ground, 491
Grove 12C, 497–498
Grove analog, 494–495
Grove blue LED, 478, 482
Grove cables, 479. See also cables
5cm-long, 492
adaptor cables, 499, 502
female patch cables, 556, 557
male patch cables, 548
male-pin-to Grove-connector patch cable, 549
patch cables, 499–501
plugged into Grove blue LED board, 481
Grove connectors, 487–503. See also connectors
connecting with Grove cables, 499–503
Adafruit Ultimate GPS, 501–503
Grove patch cable, 499–501
D4/5 Grove connector, 552
selecting base units, 489–492
Arduinos, 489–490
Pi2Grover, 490–491
signals, 493–498
Grove 12C, 497–498
Grove analog, 494–495
Grove digital, 493–494
Grove UART, 495–496
types of, 492
Grove digital, 493–494
Grove female patch cables, 556, 557
Grove Female Patch Cord, 558, 559
Grove four-channel 16-bit ADC, 519
Grove gas sensor (O2), 519
Grove hardware, 478
Grove I2C motor drive, 541, 542–545
Grove male patch cables, 548
Grove male-pin-to Grove-connector patch
cable, 549
Grove oxygen sensor, 518, 519–520
Grove patch cable, 499–501
Grove UART, 495–496
Grove-connector-to-female-pins, 556, 557
Grove-connector-to-male-header pins, 499
Grove-to-pin-header converter, 488
GUI (graphical user interface), 392, 396, 476
H
h5py library, 632
happy_pickle_copy.png, 286
happy_pickle.jpg, 273
662 Python All-in-One For Dummies
hardware
assembling, 478–482
for “Hello World,” 478
major blocks of, 473
hardware interface, 505
HDC1000 compatible temperature and humidity
sensor, 509
HDC1080 sensor board, 514
HDC1080 temperature and humidity sensor,
508–509, 525–526
HDMI Out, 473
head thread, 620
header, 287
headTiltDown() function, 604
headTiltMiddle() function, 604
headTiltPercent() function, 605
headTiltUp() function, 604
headTurnLeft() function, 603
headTurnMiddle() function, 603
headTurnPercent() function, 604
headTurnRight() function, 603
heat plots
correlation heat plots, 450
pandas library, 448–450
“Hello World” physical computing project,
474, 478–482
hello.py
comment in, 64
editing in VS code, 62
help
searching topics online, 33–34
using in Interactive Mode, 31–33
help comment, 31
help() function, 243, 341–343, 346, 347
hex() function, 87, 98
hexadecimal numbers, 98–99
history variable, 387–388, 409
hits object, 311
hook up, 471
hospital_referral_ region_description,
inpatient_charges_2015 Dataset, 458
hot-plugging, 509
HTML, 8, 453
html5lib, 332
HTTP (Hypertext Transfer Protocol), 324
HTTP headers, 325–327
HTTPResponse object, 328
Hubble Space Telescope, 371
humidity
HDC1080, 525–526
reading from 12C device, 511–514
sensors, 508–509
Hypertext Transfer Protocol (HTTP), 324
hyphen, number containing, 65
I
I2C bus, 498
I2C compass and accelerometer, 536
I2C controller board, 540–541, 578
I2C motor drive, 541, 542
I2CTemperature directory, 511
IAM (identity and access management), 453
IBM cloud, 422
ICD codes, 462
IDE (integrated development environment), 476
identity and access management (IAM), 453
if statements
checking list contents for items with, 150
operations with, 126–134
adding else to, 130–131
elif, 131–133
ternary operations, 133–134
proving false example, 127
proving true example, 127
imaginary numbers, 99
import command, 346
import modulename syntax, 58
import random, 56
import statement, 400
importing
BigQuery, 460
to dictionaries, 299–302
to objects, 296–299
Index 663
google.cloud library, 460
modules, 58
in keyword, 175
inconsistent data, 433
indent= option, 318
indentations, 54–55, 80
for comments, 134
errors and, 256
four lines of code example, 129
with functions, 194
with if statements, 128
nesting loops and, 140
index counter, diamond database, 440
.index() method, 158, 163
indexes
changing items in lists with, 153
“index out of range” error, 149
in pandas DataFrames, 439
infrared (IR) components, 497
init method, 217
__init__ method, 217
insert() method, 152, 163
installing
Anaconda, 13–17
google.cloud library, 459
h5py library, 632
HDC1080 I2C sensor, 509
libraries, 396
MatPlotLib, 409, 444
modules, 57
NumPy library, 375, 441
pandas library, 441
seaborn library, 449, 466
software for CarPi-B Python test, 591–592
TensorFlow language library, 382
VS Code, 13–17
instance methods, 230
instance term, 339
instances. See objects
int and float data conversion, 307
int() function, 87, 144, 291
INTEGER type, inpatient_charges_2015
Dataset, 458
integers, 66, 85, 186, 291
integrated development environment (IDE), 476
Intel Modvidius Neural Compute Stick (NCS),
418–419
Intel Movidius chips, 422, 423
interactive mode
cheat sheets for, 34
entering commands, 30–31
exiting interactive help, 33
going to Python Interpreter, 30
opening Terminal, 28
searching help topics online, 33–34
using built-in help, 31–33
interations, 600
interface, Python, 505
building, 595–597
motorForward function, 596
wheelsLeft function, 596
wheelsPercent function, 596–597
Inter-IC device bus, 498
Internet, interacting with
HTTP headers, 325–327
posting to Web with Python, 328–330
scraping Web with Python, 330–338
parsing part of pages, 333
storing parsed content, 333–335
URLs, 324–328
Internet of Things (IOT), 430
devices, 420
robotics and, 568
IR (infrared) components, 497
is not operator, 71
is operator, 71
.items() method, 180, 181
iterables, 162, 188, 202
664 Python All-in-One For Dummies
J
JavaScript, 8, 54–55, 133
JSON (JavaScript Object Notation), 303–321, 454
dumping Python data to, 318–321
Excel spreadsheet converting to, 305
changing JSON data, 316–317
converting Excel dates to JSON dates, 309–310
loading keyed JSON from string, 315–316
loading unkeyed JSON, 314–315
removing data from dictionaries, 317–318
organizing, 303–306
removing data from dictionaries, 317–318
saving scraped data to, 335–336
serialization, 306–307
web scraping data in, 336
json module, 307, 335
json.dump() method, 307, 318
json.dumps() method, 307, 318
json.load() method, 307
jumpers, 543
Jupyter Notebook, 21–25
creating, 24
launching from home page, 22
opening page, 23
running code in cell, 24
string operators in, 104
writing code in, 45–48
K
k variable, 316–317
Kaggle online community
Cats and Dogs database, 624
diamonds database project from, 440–450
Keras open source neural-network library,
383–384, 390
key= expression, 207
loading from string, 315–316
looping through, 310–312
keys, data dictionary
changing value of, 177
removing, 184–185
verifying, 175
keys() method, 181
keyword help, 32–33
kit-based robots, 575
kwargs (keyword arguments), 200–201
L
L289P controller board, 578
lambda functions. See anonymous functions
large number, containing decimal point, 65
last_name variable, 290
layers, neural-network, 367–368, 384
LED, 482–484, 493
dimming, 583
functions, 598–600
set_Front_LED_Off() function, 599–600
set_Front_LED_On() function, 598–599
Pixel RGB programmable, 582–583
pulse width modulation and, 579
RGB, 581–582
len() function, 102, 150
for data dictionaries, 174
transform and, 207
lengths
of data dictionaries, 174–175
of lists, 151
of strings, 102
of tuples, 164
libraries, 339–343
built-in functions, 343
data science project using, 440–450
breaking down code, 443–444
choosing data, 440–443
heat plots with pandas, 448–450
visualizing data with MatPlotLib, 444–448
Index 665
GPIO Python libraries, 477
gpiozero library, 477
importing, 443
installing, 396
installing TensorFlow language in, 382
MatPlotLib, 439–440, 518
NumPy, 370, 371–373, 375, 386, 438–439
open source neural-network library,
383–384, 390
pandas, 439
Python3 library, 382
requests library, 533–534
RPi GPIO library, 551
SDL_Pi_GroveI2CMotorDriver library, 547
SDL_Pi_HDC1080_Python3 library, 545
SMBUS library, 517
smbus library, 547
sys library, 514
using dir() function, 340–341
using help() function, 341–343
lights
dimming, 583
functions, 598–600
set_Front_LED_Off() function, 599–600
set_Front_LED_On() function, 598–599
Pixel RGB programmable, 582–583
pulse width modulation and, 579
RGB, 581–582
line (of code), 78
linear regressions, 434
link: variable, 333
Linting:Pep8Enabled, 82
Linux, 475, 583
LiPo batteries, 577, 578
list, tuple data conversion, 307
list object, 308
lists, 147–163. See also data dictionaries; sets
adding items to end of, 151–152
alphabetizing, 159–161
changing items in, 153
checking contents for items, 150–151
clearing out, 156–157
combining, 153–154
copying, 162
counting item appearance in, 157–158
getting length of, 151
inserting items into, 152–153
looping through, 150
looping with for, 137–138
NumPy library and, 438
referencing items by position number, 148–149
removing items from, 154–156
reversing, 161–162
sorting, 159–161
tuples, 163–165
as values in data dictionary keys, 170
working with in functions, 202
.ljust() method, 212
load() method, 314
loading
unkeyed JSON, 314–315
local maxima, 394
local scope, 196
logic analyzer, 555
logical operators, 126
logistic regressions, 434
with readline(), 279–280
with readlines(), 277–279
using seek(), 283
using tell(), 281–283
loops
with for, 134–141
with continue, 140
force stopping loops, 138–139
nesting loops, 140–141
through list, 137–138
through numbers in ranges, 134–136
through strings, 136–137
666 Python All-in-One For Dummies
loops (continued)
pandas library and, 439
through data dictionaries, 179–181
through lists, 150
with while, 141–146
breaking with break, 144–146
with continue, 143–144
loss function, 369–370, 387, 401
M
Mac computers
creating folders for code, 37
development environments for, 35
opening Anaconda Navigator in, 16
machine learning, 359–360. See also neural
networks
classifying clothes with, 395
creating network for detecting clothes types
breaking down code, 399–401
CNN model code, 406–409
Fashion-MNIST database, 398
test results, 405–406, 409
testing external pictures, 403–405
testing network, 398–399
training and evaluation results, 402–403
training network for, 398
detecting clothes types, 397–409
learning more about, 413
looking for solutions, 394–395
setting up software environment, 396–397
using TensorFlow, 395–396
409–413
machine learning accelerators, 419
Machine Learning For Dummies (Mueller
and Massaron), 368, 413
magic methods, 341
magnets, 538
maintaining data, 435
male header cables, 501
male patch cables, 548
margins, 434
Markdown language, 23, 47–48
Mars Rover PiCar-B robot
assembling, 586–594
calibrating servos, 588–590
installing software for test, 591–592
Pi camera video testing, 592–594
running tests in Python, 591
test code, 592
Cat/Not Cat recognition, 645
cats and dogs neural network on, 640–646
code, 640–643
results, 643–645
components of, 577–586
controller board, 578
drive motor, 580–581
Pi camera, 584–585
Pixel RGB programmable LEDs, 582–583
RGB LED, 581–582
servo motors, 578–580
ultrasonic sensor, 585–586
controls with Alexa, 646
Follow ball, 646
materials for, 576–577
programming for
coordinating motor movements with sensors,
610–613
front LED functions, 598–600
high-level Python interface, 595–597
main motor functions, 602–603
Pixel strip functions, 600–601
Python Robot Interface Test, 606–610
self-driving, 613–622
servo functions, 603–606
“Single Move” code, 597–598
ultrasonic distance sensor function, 601–602
Santa/Not Santa, 646
marshalling format, 303
massaging data, into matrices, 629
Massaron, Luca, 368
Material Color Theme, 29
Material Icon Theme, 29
Index 667
math module, 346–347, 348
math module functions, 88–90
math.acos() function, 89
math.atan() function, 89
math.atan2() function, 89
math.ceil() function, 89
math.cos() function, 89
math.degrees() function, 89
math.e function, 89
math.exp() function, 90
math.factorial() function, 90
math.floor() function, 90
math.isnan() function, 90
math.log() function, 90
math.log2() function, 90
math.pi function, 90
math.pow() function, 90
math.radians() function, 90
math.sin() function, 90
math.sqrt() function, 89, 90
math.tan() function, 90
math.tau() function, 90
MatLab library, 439
MatPlotLib, 371, 396, 409–413, 439–440, 518
displaying big data with, 435
visualizing data with, 444–448, 465–466
diamond clarity versus carat size, 445–446
diamonds in each clarity type, 446–447
diamonds in each color type, 447–448
matrices, 363, 423
converting image to, 636
for neural networks, 626
NumPy library and, 438
pandas library and, 439
max() function, 87
max(s) operator, 103
mdy(any_date), 349
mean_squared_error function, 387
analog-to-digital converter (ADC), 518–519
breaking down code, 522–525
Grove gas sensor (O2), 519
oxygen experiment, 520–522
Medicare database, on BigQuery
analyzing, 461–464
big-data code, 457–459
breaking down code, 460–461
setting up project, 454–457
visualizing data, 465–466
metadata, 440
Metcalf, Robert, 358
methods
.activate(), 227
ADAM, 387, 390, 401
.add(), 166
.append(), 151, 163
.archive(), 216
built-in, 103
capitalize, 342
class, 230–232
clear(), 156, 163, 181, 184
.close(), 270, 272
copy(), 162, 163, 181
.count(), 157, 163, 164
for data dictionaries, 181–182
date(), 160
datetime .utcfromtimestamp(), 313
__dict__, 245
dumps(), 318–319
dunder init, 217
find_all, 333
fromkeys(), 181, 188–190
general discussion of, 214
.get(), 175, 176, 181, 334
giving to classes, 224–233
calling class method by class name, 227–228
passing parameters to methods, 226–227
using class methods, 230–232
using class variables, 228–230
using static methods, 232–233
.index(), 158, 163
init, 217
__init__, 217
insert(), 152, 163
668 Python All-in-One For Dummies
methods (continued)
instance, 230
.items(), 180, 181
json.dump(), 307, 318
json.dumps(), 307, 318
json.load(), 307
keys(), 181
for lists, 163
.ljust(), 212
load(), 314
magic, 341
manipulating strings with, 105–107
now(), 113
open, 270, 307
pop(), 155, 163, 181, 184–185
popitem(), 181, 185
read(), 276
read([size]), 276
readline(), 276, 277, 279–280, 282
readlines(), 276, 277–279
remove(), 154, 163
resolution order, 245
reverse(), 161, 163
rjust(), 212
s.capitalize(), 106
s.count(x,[y.z]), 106
seek(), 283
setdefault(), 182, 188–190
s.find(x,[y.z]), 106
showexpiry(), 242
s.index(x,[y.z]), 106
s.isalpha(), 106
s.isdecimal(), 106
s.islower(), 106
s.isnumeric(), 106
s.isprintable(), 106
s.istitle(), 106
s.isupper(), 106
s.lower(), 106
s.lstrip(), 106
sort(), 159, 163, 202, 207
.sort(key=lambda s:s.lower()), 159
sort(reverse=True), 202
s.replace(x,y), 106
s.rfind(x,[y,z]), 106
s.rindex(), 106
s.rstrip(), 106
s.strip(), 106
s.swapcase(), 106
static s, 232–233
s.title(), 106
strip(), 294
s.upper(), 106
tell(), 281–283
today(), 108
update(), 166, 177–178, 182
urlopen, 332
.values(), 180, 182
micro servo motors, 579
micro USB, 473
Microsoft Azure, 422–423
Microsoft Excel spreadsheets
converting to JSON, 304–305
data in, 304
dates in, 309–310
web scraped data, 337
middleware, 609
milliamps, 579
min() function, 87
Mini Pro LB board, 490
min(s) operator, 103
Minsky, Marvin, 357, 365
and Technology) database, 395
mobile graphics processor, 417
model.add statement, 386–387
model.evaluate function, 401
model.fit command, 389
model.predict function, 389
models
CNN model code, 406–409
compiling, 384
evaluating, 388–390
Index 669
modes
Append (a:), 269, 280
Binary (b:), 270
Create (x:), 269
interactive, 27–34
cheat sheets for, 34
entering commands, 30–31
exiting interactive help, 33
going to Python Interpreter, 30
opening Terminal, 28
searching help topics online, 33–34
using built-in help, 31–33
r: (Read), 269
r+: (Read/Write), 269
Read (r:), 269
Read/Write (r+:), 269
t: (Text), 270
Text (t:), 270
w: (Write), 269
wb, 285
Write (w:), 269
x: (Create), 269
and Technology (MNIST) database, 395,
398–399, 402–403
modular programming, 343
module.compile command, 390
modules, 345–352
3.3V I2C Grove, 489
arrow, 123
datetime, 108, 160, 292, 309, 313
dateutil, 121
Fan, 493
installing, 57
json, 307, 335
making, 348–352
math, 346–347, 348
request, 332
Switch s, 493
syntax for importing, 58
using, 56–59
using alias with, 59
modulus, 70
motor thread, 620
motorBackward() function, 602
MotorDirectionSet function, 547
motorForward() function, 596, 602
motors. See also servo motors
main motor functions, 602–603
motorBackward() function, 602
motorForward() function, 602
stopMotor() function, 602–603
for robotics, 573
MotorSpeedSetAB function, 547
MouseAir robot, 645
Mueller, Paul, 368, 413
multiline comments, 63–64
multiple key dictionaries, 186–192
nesting, 190–192
using fromkeys() method, 188–190
using setdefault() methods, 188–190
multiple-toothed electromagnets, 539
MyTemperature app, 526, 529, 530
N
n * s operator, 103
naïve datetime, 120
names, variables, 73–74
names.txt
naming convention, 80
National Oceanic and Atmospheric Agency (NOAA)
database, 453
NCS (Intel Modvidius Neural Compute Stick),
418–419
negative feedback, 556
negative numbers, 65
nesting
loops with for, 140–141
multiple key dictionaries, 190–192
Network News Transfer Protocol (NNTP), 112
670 Python All-in-One For Dummies
networking, 415
neural networks
building in Python, 370–382
code, 370–378
installing TensorFlow Python library, 382
running neural-network code, 378–381
using TensorFlow for same neural network,
381–382
building in TensorFlow, 383–392
breaking down code, 386–388
changing to three-layer neural network in
TensorFlow/Keras, 390–392
compiling models, 384
evaluating models, 388–390
loading data, 384
creating machine learning, 624–633
with Python Mars Rover PiCar-B, 640–645
setting up, 624–625
testing, 633–639
using TensorFlow, 625–633
machine learning recognition
Cat/Not Cat, 645
controls with Alexa, 646
Follow ball, 646
Santa/Not Santa, 646
understanding, 366–370
activation function, 369
layers of, 367–368
loss function, 369–370
weights and biases, 368
Neural_Network class, 375
neural-network code, 370–374, 378–381
neural-network model and layers, 384
neuron activation function, 630
neurons, 358–359, 366
New Horizons space probe, 371
new_dict string, 318
newline="" function, 308
newline='' function, 337
nicknames, 59
NNTP (Network News Transfer Protocol), 112
No such file or directory message, 250
NOAA (National Oceanic and Atmospheric Agency)
database, 453
None data conversion, 307
not operator, 71, 126
now() method, 113
null JSON data conversion, 307
num variable, 72
number JSON data conversion, 307
numbers, 64–66
binary, 98–99
built-in functions for, 87
calculating with functions, 86–90
complex, 99–100
formatting, 91–97
with f-strings, 91–92
making multiline format strings, 95
percent numbers, 93–94
showing dollar amounts, 92–93
width and alignment, 96–97
hexadecimal, 98–99
looping with for, 134–136
octal, 98–99
quotation marks and, 148
NumPy library, 370, 371–373, 386, 437,
438–439, 636
O
O2 (Grove gas sensor), 519
object JSON data conversion, 307
object term, 339
object-oriented programming (OOP), 53, 213, 216
objects
converting, 295–302
creating from classes, 217–218, 219–222
Index 671
as exceptions, 261
general discussion of, 213
importing, 296–299
tuples versus, 222
oct() function, 87, 98
octal numbers, 98–99
Olson Database, 119
online resources
buying LiPo batteries, 577
buying PiCar-B, 577
buying Raspberry Pi 3B , 577
Cats and Dogs database, 624
Google cloud, 454
Kaggle online community, 440
NumPy library tutorials, 438
Python code feedback video, 613
Python exceptions list, 259
Python test on PiCar-B robot video, 591
“Robot Brain” code video, 620
Robot Operating System (ROS), 609
RobotInterface class test video, 609
Santa/Not Santa video, 646
Single Move code on robot video, 598
SQL tutorials, 459
support page for this book, 592
TensorFlow download link, 624
Wilkinson Baking, 569
OOP (object-oriented programming), 53, 213, 216
Open Editors bar, 38
open method, 270, 307
open source neural-network library, 383–384
OpenAQ database, 466–467
OpenCV (Open-Source Computer Vision),
584, 621, 646
open-drain lines, 498, 506
opening
Terminal in interactive mode, 28
URLs from Python, 327–328
Open-Source Computer Vision (OpenCV), 584,
621, 646
operating system command prompt, 28
operations
with if statements, 126–134
adding else to, 130–131
elif, 131–133
ternary operations, 133–134
with for loops, 134–141
with continue, 140
force stopping with break statements, 138–139
nesting loops, 140–141
through list, 137–138
through numbers in ranges, 134–136
through strings, 136–137
main operators, 125–126
with while loops, 141–146
breaking with break, 144–146
with continue, 143–144
operators
-, 70
!=, 71, 126
%, 70
*, 70
**, 70
/, 70
//, 70, 117
+, 70
<, 71, 126
<=, 71, 126
=, 80, 152
==, 71, 80
>, 71, 126
>=, 71, 126
and, 71, 126
arithmetic s, 69–70
assignment, 74
comparison (relational), 70–71, 125
!= is not equal to, 126
< is less than, 126
672 Python All-in-One For Dummies
operators (continued)
<= is less than or equal to, 126
== is equal to, 126
> is greater than, 126
>= is greater than or equal to, 126
(//), 117
is, 71
is not, 71
logical, 126
max(s), 103
min(s), 103
n * s, 103
not, 71, 126
or, 71, 126
s * n, 103
s[i], 103
s[i:j], 103
s[i:j:k], 103
s.count(x), 103
sequence s for strings, 103
s.index(x[, i[, j]]), 103
string, 102–105
x in s, 103
x not in s, 103
or operator, 71, 126
ord() function, 104
OS Error, 313
outliers, 433
analog-to-digital converter (ADC), 518–519
breaking down code, 522–525
Grove gas sensor (O2), 519
oxygen experiment, 520–522
P
Pacific/Honolulu time zone, 119
Pacific/Pago_Pago time zone, 119
packages, 12, 343–345
pages, parsing part of, 333
pandas DataFrames, 439
2D data, 441
setting up SQL query, 460
pandas library, 437, 439
heat plots with, 448–450
using BigQuery with, 453
parameters, 86
= value argument, 201
brightness, 601
color, 601
delay, 602
names, 196
optional, 198
passing to methods, 226–227
percent, 604, 605, 606
pixel, 601
speed, 602
validation_data, 387
Verbose, 389
wait_ms, 600, 601
parentheses
with constants in functions, 90
number containing, 65
for tuples, 164
ParkMyRide application, 421
parse package, 327
parsing
parts of pages, 333
storing content, 333–335
pass keyword, 217, 237
patch cables, 499–501, 503, 550
people object, 308
PEP (Python enhancement proposals), 50
PEP 8, 50
errors, 81–82
workspace settings enabled with, 52
PEP 20, 50
percent parameter, 604, 605, 606
persisted data, 224
physical computing, 471–486
controlling LED, 482–484
in everyday life, 472
Index 673
making computers do things, 474
Pulse-width modulation (PWM), 485–486
Raspberry Pi, 476–482
assembling hardware, 478–482
GPIO pins, 477
GPIO Python libraries, 477
hardware for “Hello World,” 478
using small computers to build projects, 474–476
Pi camera, 623
Python Mars Rover PiCar-B, 584–585
video testing, 592–594
pi constant, 90
Pi2Grover, 502
base unit, 478, 490–491
board, 480, 552
Grove interface board, 541, 548, 556
PiCar-B robot
assembling, 586–594
calibrating servos, 588–590
installing software for test, 591–592
Pi camera video testing, 592–594
running tests in Python, 591
test code, 592
components of, 577–586
controller board, 578
drive motor, 580–581
Pi camera, 584–585
Pixel RGB programmable LEDs, 582–583
RGB LED, 581–582
servo motors, 578–580
ultrasonic sensor, 585–586
materials for, 576–577
PiCar-B-Video-Test.py software, 593
pin headers, 500
pip (Pip Installs Packages), 344
pip commands, 57
PIR detectors, 536
pixel parameter, 601
Pixel RGB programmable LEDs, 582–583
functions
allLEDSOff() function, 601
Color() function, 601
colorWipe() function, 600
rainbowCycle() function, 600
setPixelColor() function, 601
theaterChaseRainbow() function, 600–601
root permission for, 609
pointers
moving with seek(), 283
using tell(), 281–283
pop() method, 155, 163, 181, 184–185
popitem() method, 181, 185
populating attributes, 218
position numbers, referencing items in list by,
148–149
positive feedback, 556
Positive number, 72
potentiometers, 539, 578
power consumption, Raspberry Pi and, 577
PowerShell task-based command-line shell, 42
prebuilt robots, 575
Preceptrons, 358
price, diamond database, 441
print() function, 274
adding spaces in, 197
for data dictionaries, 172
print statement, 278, 280
print(pi) command, 346, 348
processing data, 433–434
project inspiration, 424–425
property of objects. See attributes
provider_city, inpatient_charges_2015
Dataset, 457
provider_id column, inpatient_charges_2015
Dataset, 457
provider_name, inpatient_charges_2015
Dataset, 457
provider_state, inpatient_charges_2015
Dataset, 458
provider_street_ address, inpatient_
charges_2015 Dataset, 457
674 Python All-in-One For Dummies
provider_zipcode, inpatient_charges_2015
Dataset, 458
ps xaf command, 486
pseudo-random number generator, 371
publish-subscribe system, 609
pulse width modulation (PWM), 485–486, 538, 539,
548, 551, 579, 580, 583, 599
punctuation in Python
colons, 171
commas, 199
parentheses
with constants in functions, 90
for tuples, 164
PyLab package, 439
Pylint tool, 30, 51
workspace settings enabled with, 52
Python
to access Web, 323
building dashboard on phone using, 525–536
breaking down code, 533–535
building on expertise, 536
HDC1080 temperature and humidity, 525–526
how to add the Blynk dashboard, 527–530
Blynk app, 531–533
choosing interpreter, 19
choosing version of, 9–11
DC motor software, 545–547
elements
object-oriented programming (OOP), 53
use of indentations, 54–55
using modules, 56–59
Zen of Python, 49–53
exiting out of, 21
interpreters, 11–12
choosing, 19
going to, 30
objects, 70
path, 36
popularity of, 8–9
servo software, 551–552
stepper software, 562–563
tools used with, 11–17
writing in VS Code, 17–21
Python 3 workspace, 38
Python Coding Style Guidelines, 50–51
python command, 30
Python enhancement proposals (PEP), 50
Python Mars Rover PiCar-B robot, 575–594
assembling, 586–594
calibrating servos, 588–590
installing software for test, 591–592
Pi camera video testing, 592–594
running tests in Python, 591
test code, 592
Cat/Not Cat recognition, 645
cats and dogs neural network on,
640–646
code, 640–643
results, 643–645
components of, 577–586
controller board, 578
drive motor, 580–581
Pi camera, 584–585
Pixel RGB programmable LEDs, 582–583
RGB LED, 581–582
servo motors, 578–580
ultrasonic sensor, 585–586
controls with Alexa, 646
Follow ball, 646
materials for, 576–577
programming for
coordinating motor movements with sensors,
610–613
front LED functions, 598–600
high-level Python interface, 595–597
main motor functions, 602–603
Pixel strip functions, 600–601
Python Robot Interface Test, 606–610
self-driving, 613–622
Index 675
servo functions, 603–606
“Single Move” code, 597–598
ultrasonic distance sensor function, 601–602
Santa/Not Santa, 646
Python Nano editor, 441
Python Package Index, 344
Python Robot Interface Test, 606–610
Python3 library, 382
Q
quotation marks, 67–68
quotes.txt, 270–272
R
r: (Read) mode, 269
R library, 434
r+: (Read/Write) mode, 269
radio control cars, 576
rainbowCycle() function, 600
raise error statement, 259
raising errors, 259–263
raising exceptions, 249
RAM (Random Access Memory) , 415, 472, 567
range() function, in for loop, 134–136
Raspberry Pi, 474, 476–482, 489–490, 575
A , 583
3B, 583
3B , 409
building robots with, 577
CNN network on, 626
12C device on, 507–508
adding hardware AI to, 417–419
assembling hardware, 478–482
base unit, 490–491
bits on, 498
building robots with, 575
cameras, 584
CNN network on, 626
GPIO pins, 474, 477, 555
GPIO Python libraries, 477
GUI, 441, 643
hardware for “Hello World,” 478
installing google.cloud library, 459
installing MatPlotLib on, 444
installing NumPy on, 375, 441
installing pandas on, 441
installing seaborn library, 449
limitations with, 415–417
MouseAir robot, 645
Pixel RGB pulses with, 600
Pixel RGB strings on, 583
running headless, 396
shutting down, 509
stepper motor project, 561–564
Stretch version, 576
supplying current to 5V pins, 549
using for physical computing, 476–482
Zero, 577, 583
Zero W, 583
Raspberry Pi/ADC/oxygen sensor hookup, 520
rb (read binary), 273
RC airplanes/boats, 539
Read (r:) mode, 269
read binary (rb), 273
read() loop, 283
read() method, 276
read([size]) method, 276
reading
code, 54–55
temperature and humidity, 511–514
readline() method, 276, 277, 279–280, 282
readlines() method, 276, 277–279
Read/Write (r+:) mode, 269
red errors, 81–82
Reed-Solomon error-correction algorithm, 371
regular expressions, 295
reinforcement learning algorithm, 394–395
676 Python All-in-One For Dummies
relational operators, 70–71
!= is not equal to, 126
< is less than, 126
<= is less than or equal to, 126
== is equal to, 126
> is greater than, 126
>= is greater than or equal to, 126
remove() method, 154, 163
removing items from lists, 154–156
Representational State Transfer (REST), 452
request module, 332
request package, 327
requests library, 533–534
Reset button, 544
resources
buying LiPo batteries, 577
buying PiCar-B, 577
buying Raspberry Pi 3B , 577
Cats and Dogs database, 624
Google cloud, 454
Kaggle online community, 440
NumPy library tutorials, 438
Python code feedback video, 613
Python exceptions list, 259
Python test on PiCar-B robot video, 591
“Robot Brain” code video, 620
Robot Operating System (ROS), 609
RobotInterface class test video, 609
Santa/Not Santa video, 646
Single Move code on robot video, 598
SQL tutorials, 459
support page for this book, 592
TensorFlow download link, 624
Wilkinson Baking, 569
response package, 327
REST (Representational State Transfer), 452
Rethink Robotics, 570
returned values, from functions, 205–206
reverse() method, 161, 163
reversing lists, 161–162
revolutions per minute (RPM), 538
RGB LED, Python Mars Rover PiCar-B, 581–582
ribbon cables, 587
RITest.py code, 606–608
rjust() method, 212
“Robot Brain” code, 614–617
Robot Operating System (ROS), 570, 609
robotBrain.py software, 620
robotics
future of, 646
machine learning neural network, 624–633
Python Mars Rover PiCar-B with, 640–645
general discussion of, 567–568
main parts of
communications, 573
computers, 572–573
motors and actuators, 573
sensors, 573
programming, 574
Python Mars Rover PiCar-B, 575–594
assembling, 586–594
components of, 577–586
materials for, 576–577
programming, 595–622
types of, 568–572
Wilkinson Bread-Making Robot, 569–570
RobotInterface class functions, 598–610
front LED functions, 598–600
set_Front_LED_Off() function, 599–600
set_Front_LED_On() function, 598–599
main motor functions, 602–603
motorBackward() function, 602
motorForward() function, 602
stopMotor() function, 602–603
Pixel strip functions, 600–601
allLEDSOff() function, 601
Color() function, 601
colorWipe() function, 600
Index 677
rainbowCycle() function, 600
setPixelColor() function, 601
theaterChaseRainbow() function, 600–601
Python Robot Interface Test, 606–610
servo functions, 603–606
centerAllServos() function, 606
headTiltDown() function, 604
headTiltMiddle() function, 604
headTiltPercent() function, 605
headTiltUp() function, 604
headTurnLeft() function, 603
headTurnMiddle() function, 603
headTurnPercent() function, 604
headTurnRight() function, 603
wheelsLeft() function, 605
wheelsMiddle() function, 605
wheelsPercent() function, 606
wheelsRight() function, 605
ultrasonic distance sensor function, 601–602
RobotInterface.py, 595
robotparser package, 327
ROS (Robot Operating System), 570, 609
round() function, 86–87, 388
RPi GPIO library, 551
RPM (revolutions per minute), 538
Run Python File option, 76
Run-As-Administrator, 14–15
RX signal line, 495
S
s * n operator, 103
s[i] operator, 103
s[i:j] operator, 103
s[i:j:k] operator, 103
Samsung smartphones, 360
Santa, machine learning neural network
for recognizing, 646
saving
code, 41
current settings as workspace settings, 36
scraped data, 335–338
work, 76
SBC (single board computer), 473
SC-90 9g micro servo motors, 579
scalar value, 65
scalars, 382
s.capitalize() method, 106
scatter plots, 445
SCL (serial clock line), 498, 506
s.count(x,[y.z]) method, 106
s.count(x) operator, 103
scraped data
scraper.py program, 338
scraping. See web scraping
screw terminals, 543
scripting language, 42
SD cards, 588
SDA (serial data line), 498, 506
SDA connector, 498
SDL_Pi_GroveI2CMotorDriver library, 547
SDL_Pi_HDC1080_Python3 directory, 514, 516
SDL_Pi_HDC1080_Python3 library, 545
seaborn library, 437
generating heat plots with, 448–450
installing, 466
Second Grove Female Patch Cord, 559
Secure SHell (SSH), 474
security, cloud computer, 453–454
seek() method, 283
self keyword, 226, 229
self-driving programming for Python Mars Rover
PiCar-B, 613–622
Adeept software overview, 621
using threads, 620–621
semaphores, 620
senseOxygen.py
sensor thread, 620
678 Python All-in-One For Dummies
sensors, 474
coordinating motor movements with, 610–613
distance function, 601–602
Python Mars Rover PiCar-B, 585–586
for robotics, 573
ultrasonic, 585–586, 601–602
sequence operators for strings, 103
serial clock line (SCL), 498, 506
serial data line (SDA), 498, 506
serial dates, 306, 309
serial interface, 495
serial peripheral interface (Digital SPI), 506
serial signal, 495
serialization, 306–307
servers, 324
servo motors, 539, 548–551
breaking down code, 552–554
calibrating, 588–590
functions, 603–606
centerAllServos(), 606
headTiltDown(), 604
headTiltMiddle(), 604
headTiltPercent(), 605
headTiltUp(), 604
headTurnLeft(), 603
headTurnMiddle(), 603
headTurnPercent(), 604
headTurnRight(), 603
wheelsLeft(), 605
wheelsMiddle(), 605
wheelsPercent(), 606
wheelsRight(), 605
Python Mars Rover PiCar-B, 578–580
Python servo software, 551–552
SC-90 9g micro, 579
SG90 micro, 579
set_Front_LED_Off() function, 599–600
set_Front_LED_On() function, 598–599
setdefault() method, 182, 188–190
setPixelColor() function, 601
sets, 165–167, 170
settings
saving as workspace settings, 36
VS Code editor, 36
s.find(x,[y.z]) method, 106
SG90 micro servo motors, 548, 549, 579
Shovic, John, 357, 462
showexpiry() method, 242
shutting down Raspberry Pi, 509
sigmoid function, 369, 377
signals, 493–498
Grove 12C, 497–498
Grove analog, 494–495
Grove digital, 493–494
Grove UART, 495–496
simpleFeedback.py code, 610–612
s.index(x,[y.z]) method, 106
s.index(x[, i[, j]]) operator, 103
single board computer (SBC), 473
“Single Move” code, 597–598
singleMove.py, 598
s.isalpha() method, 106
s.isdecimal() method, 106
s.islower() method, 106
s.isnumeric() method, 106
s.isprintable() method, 106
s.istitle() method, 106
s.isupper() method, 106
SKU (Stock Keeping Unit), 188
slave, 506–507
sleep() function, 597
slices, 417
s.lower() method, 106
s.lstrip() method, 106
Smart City application, 421
SMBus, 498, 506, 517
smbus library, 547
The Society of Mind (Minsky), 357
softmax function, 400
software environment, 396–397
sort() method, 159, 163, 202, 207
sorting lists, 159–161
.sort(key=lambda s:s.lower()) method, 159
Index 679
sort(reverse=True) method, 202
source code, 268, 330
spaces, number containing, 65
sparse categorical crossentropy
function, 401
special characters, 281
special variables, 341
speed, of robots, controlling with GPIO lines, 580
speed parameter, 602
SQL (Structured Query Language), 459
SQL All In One For Dummies 3rd Edition (Tayor), 459
SQL For Dummies (Taylor), 459
SQL in 10 Minutes (Forta), 459
s.replace(x,y) method, 106
s.rfind(x,[y,z]) method, 106
s.rindex() method, 106
s.rstrip() method, 106
SSH (Secure SHell), 474
s.strip() method, 106
s.swapcase() method, 106
standardized connectors, 487–488
statements, 78
static methods, 232–233
statistical analysis, 360
stepper motors, 539–540, 554–564
breaking down code, 563–564
and driver boards, 560
project, 561
Python stepper software, 562–563
stepperTest.py code, 563
s.title() method, 106
Stock Keeping Unit (SKU), 188
Stop button on Jupyter Notebook, 142–143
stopMotor() function, 602–603
storage, 415
on cloud, 420–422
parsed content, 333–335
str class, 340, 342
str data conversion, 307
str() function, 87, 158
str object, 105
Stretch version, Raspberry Pi, 576
strftime, 111
string concatenation, 101
string JSON data conversion, 307
STRING type, inpatient_charges_2015 Dataset,
457–458
strings, 66
concatenating, 101–102
converting, 290–291
dates, working with, 107–112
keyed JSON loading from, 315–316
in lists, 148
looping with for, 136–137
manipulating with methods, 105–107
passing for functions, 197
Pixel string, 582
sequence operators for, 103
string operators, 102–105
times, formatting for, 112–123
calculating timespans, 114–118
time zones, 118–123
as values in data dictionary keys, 170
strip() method, 294
Structured Query Language (SQL), 459
Style Guidelines, 50–51
subclasses, 235, 237–238
sub-dictionaries, 312
sudo command, 609
sudo halt command, 509, 588
sudo keyword, 483
sudo python3 RITest.py, 609
sudo reboot command, 591
Sum Squared Loss, 375, 380
SumSquaredLossList.csv
SunAirPlus board, 501, 502
sunlight sensor, 497
sun-tracking solar panels, 540
supervised learning algorithm, 394
s.upper() method, 106
Switch modules, 493
SwitchDoc Labs HDC1080, 509
680 Python All-in-One For Dummies
synapses, 368
syntax
to create variable, 74
for importing modules, 58
rule, 80
SyntaxError, 79
sys library, 514
T
t: (Text) mode, 270
table, diamond database, 441
tabular data, 303–304, 439
tau constant, 90
Taylor, Allen G., 459
TCP/IP networks, 573
telecommunications network, 358
tell() method, 281–283
temperature
HDC1080, 508–509, 525–526
MyTemperature app, 526, 529, 530
reading from 12C device, 511–514
temperatureTest.py, 529, 531–533
tensor processing unit (TPU), 419, 421
TensorBoard, 392
TensorFlow language, 361–363
building neural-network in, 383–392
breaking down code, 386–388
changing to three-layer neural network,
390–392
compiling models, 384
evaluating models, 388–390
loading data, 384
installing in library, 382
for machine learning, 395–396
code, 627–629
examining code, 629–632
results, 632–633
machine learning neural network in robotics,
625–633
or same neural network, 381–382
TensorFlowKeras.py
tensors, 363, 371, 381–382, 423. See also matrices
Terminal, 275
opening in Interactive Mode, 28
Run Python File option in, 76
in VS Code, 20
ternary operations, operations with if
statements, 133–134
testing
for detecting clothes, 405–406, 409
external clothes pictures, 403–405
for Mars Rover PiCar-B, 591
networks, 398–399
trained networks, 633–639
code, 634–635
explaining code, 636–637
results, 637–639
Text (t:) mode, 270
text editor, 287
adding in Jupyter Notebook, 47
editing, 268
and numbers, 64–65
text: variable, 334
theaterChaseRainbow() function, 600–601
This %A %B %d format string, 111
threads, 620–621
three-input XOR gate, 370
three-layer neural network, 384
threshold, 359
throwing exceptions. See raising exceptions
time directives, 110
time library, 597
time zones, 118–123
current date and time for multiple, 121–122
map of, 118
Olson Database, 119
scheduled event in multiple, 121–122
Index 681
timedelta object, 114, 120
times, formatting strings for, 112–123
calculating timespans, 114–118
time zones, 118–123
time-series data types, pandas library and, 439
timespans, calculating, 114–118
timestamps, 107
to_curr(any_num, len), 349
to_date(any_str), 349
Toasteroid robot, 571
today() method, 108
torque rating, 539
total_discharges, inpatient_charges_2015
Dataset, 458
TPU (tensor processing unit), 419, 421
trailing whitespace, 81–82
training networks, 398, 402–403
for detecting cats and dogs, 631
code, 634–635
explaining code, 636–637
results, 637–639
trainingEpochs variable, 378
triple quotation marks, 95
True data conversion, 307
true JSON data conversion, 307
True/false Booleans, 340
building applications, 71–72
building data types, 68–69
converting, 293
Truth Table, 370
try keyword
preventing crashes with, 256
using with errors, 251, 257–258
tuples, 163–165
error messages in, 165
inside functions, 204
objects versus, 222
as values in data dictionary keys, 170
TWI (Two Wire Interface), 498, 506
two-layer neural networks, 366, 625
TX signal line, 495
type() function, 87, 340
type term, 339
U
UART, 495–496
UART RFID reader, 496
UART serial interface, 501
Ubuntu system, 375
ULN2003 motor drive chip, 557
ultra.checkdisk() function, 585
ultrasonic mapping, 621
ultraviolet (UV) components, 497
274, 275
Universal Product Code (UPC), 188
universal serial bus (USB) ports, 473
Universal Time Coordinated (UTC), 119
unkeyed JSON, 314–315
UNL2003 driver board, 558, 559, 560
Uno Grove base board, 489
unsupervised learning algorithm, 394
UPC (Universal Product Code), 188
update() method, 166, 177–178, 182
url: variable, 334
urllib package, 327
urlopen method, 332
URLs, 324–325
USB (universal serial bus) ports, 473
user agents, 324
users, 324
UTC (Universal Time Coordinated), 119
274, 275
UV (ultraviolet) components, 497
V
v object, 311
v variable, 316–317
validation_data parameter, 387
682 Python All-in-One For Dummies
values, 74
changing attributes of, 222
to data dictionary keys, 169–170
functions with multiple, 199–203
.values() method, 180, 182
variables, 72–77
chunk, 285
class, 228–230
content, 276, 333
creating in code, 74
creating names for, 73–74
first_name, 290
full_name, 290
history, 387–388, 409
inside functions, 196
k, 316–317
last_name, 290
link:, 333
manipulating, 75–76
num, 72
running app in VS Code, 76–77
saving work, 76
special, 341
text:, 334
trainingEpochs, 378
url:, 334
using syntax to create, 74
v, 316–317
variety, 431, 432
VCC connector, 498
VCC signal line, 493, 495
vectors, 382
velocity, 431, 432
Verbose parameter, 389
video
adding in Jupyter Notebook, 47
Pi camera testing, 592–594
Videocore-IV mobile graphics processor, 417
Virtual Network Computing (VNC), 396, 445, 585
Voice Time (Shovic), 646
voltage, 491
dividers, 494
of SG90 micro servo motors, 579
volume, 430–432
VS Code editor, 12–13
built-in debugger, 43–45
exiting out of, 21
extensions, 17
installing, 13–17
Python 3 workspace in, 38
running apps in, 76–77
running code in, 41–42
settings, 36
Terminal in, 20
using alphabetize function in, 202–203
welcome screen, 17
VS Code IntelliSense, 195
.vscode icon, 38
W
w: (Write) mode, 269
wafer level chip scale package (WLCSP), 509
wait_ms parameter, 600, 601
Watson Personality Insights, 422
wb mode, 285
Web, 316–317, 323–327
HTTP headers, 325–327
scraping, 323–327
understanding URLs, 324–325
web scraping, 330–338
parsing part of pages on Web, 333
storing parsed content on Web, 333–335
web services, 452–453
weights, 366, 368
weightsLayer1.txt
weightsLayer2.txt
Index 683
wheelsLeft function, 596
wheelsLeft() function, 605
wheelsMiddle() function, 596, 598, 605
wheelsPercent function, 596–597
wheelsPercent() function, 606
wheelsRight() function, 605
whence value, 283
while loops, 280
operations with, 141–146
breaking with break, 144–146
with continue, 143–144
removing items from lists with, 155
“Robot Brain” program with, 620
whole number, 65, 66
width, formatting, 96–97
WiFi, 473, 573
Wilkinson Bread-Making Robot, 569–570
Windows computers
creating folders for code, 37
development environments for, 35
opening Anaconda Navigator in, 16
WLCSP (wafer level chip scale package), 509
words (strings), 66–68
words, number containing, 65
workspace
creating, 34–37
on Python 3, 38
settings, 36, 52
Write (w:) mode, 269
X
x: (Create) mode, 269
x, diamond database
depth, 441
length, 441
x in s operator, 103
x not in s operator, 103
XBee wireless sockets, 495
xlrd (Excel reader), 309
XOR gate (eXclusive OR gate), 370, 371
Y
y, diamond database, 441
Z
Zen of Python principles, 49–53, 348
Zulu time, 119
Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf
About the Author
John Shovic has been working with software and electronics since he talked his
school into letting him use their IBM 1130 computer for the whole summer of 1973.
has founded multiple companies: Advance Hardware Architectures; TriGeo Net-
over 70 invited talks and has published over 50 papers on a variety of topics on
-
bunch of students that are as excited about technology and computers as he is.
Alan Simpson -
-
tinue to get rave reviews from his many students and followers.
Author’s Acknowledgments
John Shovic:
curled up on my lap.
Alan Simpson: -
-
-
Dedication
John Shovic:
making sure my socks match in the morning. Thank you!
Alan Simpson:
Publisher’s Acknowledgments
Executive Editor:
Project Editor:
Copy Editor:
Technical Editor:
Production Editor: Vasanth Koilraj
Cover Image:

More Related Content

PDF
Deep Learning (DL) from Scratch
PDF
Introduction to Neural Networks
PDF
Neural network book. Interesting and precise
PDF
Machine learning with py torch
PDF
Introduction to Neural Networks in Tensorflow
PDF
Deep Learning in Spark with BigDL by Petar Zecevic at Big Data Spain 2017
PPTX
Deep Learning Tutorial | Deep Learning Tutorial For Beginners | What Is Deep ...
PDF
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Deep Learning (DL) from Scratch
Introduction to Neural Networks
Neural network book. Interesting and precise
Machine learning with py torch
Introduction to Neural Networks in Tensorflow
Deep Learning in Spark with BigDL by Petar Zecevic at Big Data Spain 2017
Deep Learning Tutorial | Deep Learning Tutorial For Beginners | What Is Deep ...
Separating Hype from Reality in Deep Learning with Sameer Farooqui

Similar to Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf (20)

PDF
Deep Learning Basics (lecture notes).pdf
PDF
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
PDF
GANS Project for Image idetification.pdf
DOCX
Designing a neural network architecture for image recognition
PDF
Artificial neural network for machine learning
PDF
Scaling Deep Learning with MXNet
PDF
Deep learning with kafka
PPTX
TensorFrames: Google Tensorflow on Apache Spark
PDF
Understanding Deep Learning & Parameter Tuning with MXnet, H2o Package in R
PDF
Lab 6 Neural Network
PDF
Cybersecurity Ops With Bash Attack Defend And Analyze From The Command Line 1...
PDF
APPLIED MACHINE LEARNING
PDF
Introducing Elixir Getting Started In Functional Programming 2nd Edition Simo...
PPTX
An Incomplete Introduction to Artificial Intelligence
PDF
Getting Started with Keras and TensorFlow - StampedeCon AI Summit 2017
PDF
[Ruxcon 2011] Post Memory Corruption Memory Analysis
PPTX
Getting your hands dirty with deep learning in java
PPTX
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
PDF
Towards neuralprocessingofgeneralpurposeapproximateprograms
Deep Learning Basics (lecture notes).pdf
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
GANS Project for Image idetification.pdf
Designing a neural network architecture for image recognition
Artificial neural network for machine learning
Scaling Deep Learning with MXNet
Deep learning with kafka
TensorFrames: Google Tensorflow on Apache Spark
Understanding Deep Learning & Parameter Tuning with MXnet, H2o Package in R
Lab 6 Neural Network
Cybersecurity Ops With Bash Attack Defend And Analyze From The Command Line 1...
APPLIED MACHINE LEARNING
Introducing Elixir Getting Started In Functional Programming 2nd Edition Simo...
An Incomplete Introduction to Artificial Intelligence
Getting Started with Keras and TensorFlow - StampedeCon AI Summit 2017
[Ruxcon 2011] Post Memory Corruption Memory Analysis
Getting your hands dirty with deep learning in java
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
Towards neuralprocessingofgeneralpurposeapproximateprograms
Ad

More from SANTIAGO PABLO ALBERTO (20)

PDF
Electroneumatica con circuitos secueciales con logica cableada
PDF
Arduino Blocks: Programacion visual con bloques para arduino 2 Edicion por Ju...
PDF
Principios de electrónica 7 Edicion por Albert Malvino y David J. Bates
PDF
Principios digitales por Roger L. Tokheim
PDF
Solicitud de empleo para el trabajo para
DOCX
secuencia electroneumática parte 1
DOCX
secuencia electroneumática parte 2
PDF
Manual de teoría y practica electroneumática avanzada
PDF
Programacion de PLC basado en Rslogix 500 por Roni Domínguez
PDF
Programación de microcontroladores PIC en C con Fabio Pereira
PDF
Análisis y Diseño de Sistemas de Control Digital por Ricardo Fernandez del Bu...
PDF
Arduino: Arduino de cero a experto
PDF
PDF
Manual básico PLC OMRON
PDF
Programación de autómatas PLC OMRON CJ/CP1
PDF
Manual del sistema del controlador programable S7-200 SMART
PDF
Catálogo de PLC S7-200 SMART
PDF
PLC: Automatismos industriales
PDF
PLC: Buses industriales y de campo practicas de laboratorio por Jose Miguel R...
Electroneumatica con circuitos secueciales con logica cableada
Arduino Blocks: Programacion visual con bloques para arduino 2 Edicion por Ju...
Principios de electrónica 7 Edicion por Albert Malvino y David J. Bates
Principios digitales por Roger L. Tokheim
Solicitud de empleo para el trabajo para
secuencia electroneumática parte 1
secuencia electroneumática parte 2
Manual de teoría y practica electroneumática avanzada
Programacion de PLC basado en Rslogix 500 por Roni Domínguez
Programación de microcontroladores PIC en C con Fabio Pereira
Análisis y Diseño de Sistemas de Control Digital por Ricardo Fernandez del Bu...
Arduino: Arduino de cero a experto
Manual básico PLC OMRON
Programación de autómatas PLC OMRON CJ/CP1
Manual del sistema del controlador programable S7-200 SMART
Catálogo de PLC S7-200 SMART
PLC: Automatismos industriales
PLC: Buses industriales y de campo practicas de laboratorio por Jose Miguel R...
Ad

Recently uploaded (20)

PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPT
Total quality management ppt for engineering students
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Feature types and data preprocessing steps
PPTX
Management Information system : MIS-e-Business Systems.pptx
PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
R24 SURVEYING LAB MANUAL for civil enggi
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Total quality management ppt for engineering students
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Module 8- Technological and Communication Skills.pptx
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Soil Improvement Techniques Note - Rabbi
Safety Seminar civil to be ensured for safe working.
Feature types and data preprocessing steps
Management Information system : MIS-e-Business Systems.pptx
Visual Aids for Exploratory Data Analysis.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Current and future trends in Computer Vision.pptx
Information Storage and Retrieval Techniques Unit III
R24 SURVEYING LAB MANUAL for civil enggi

Raspberry Pi: Python todo en uno para dummies por John Shovic parte 2.pdf

  • 2. CHAPTER 1: . . . . . . . . . . . . . . . . . . . 355 AI Is a Collection of Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Current Limitations of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 CHAPTER 2: . . . . . . . . . . . 365 Understanding Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 Building a Simple Neural Network in Python . . . . . . . . . . . . . . . . . 370 . . . . . . . . . . . . . 383 CHAPTER 3: . . . . . . . . . . . . . . 393 Learning by Looking for Solutions in All the Wrong Places. . . . . . 394 Classifying Clothes with Machine Learning . . . . . . . . . . . . . . . . . . . 395 . . . . . . . . . . . . . . . . . . . . . . 395 . . . . . . . . . 396 Detecting Clothes Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Visualizing with MatPlotLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Learning More Machine Learning. . . . . . . . . . . . . . . . . . . . . . . . . . . 413 CHAPTER 4: . . . . . . . . . . . . . . . . . . . . . . 415 Limitations of the Raspberry Pi and AI. . . . . . . . . . . . . . . . . . . . . . . 415 Adding Hardware AI to the Raspberry Pi . . . . . . . . . . . . . . . . . . . . . 418 AI in the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 AI on a Graphics Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 . . . . . . . . . . . . . . . . . . . . . . 424
  • 4. 356 BOOK 4 » Neural networks » Machine learning » neural networks
  • 5. CHAPTER 1 “What magical trick makes us intelligent? The trick is that there is no trick. The power of intelligence stems from our vast diversity, not from was coming to Pocatello. I made a few phone calls and managed to get him to meet It was like we were Rolling Stones fans and Mick Jagger was coming to town to spend
  • 6. BOOK 4 AI Winter refers to the moment that researchers went to great lengths to avoid using the AI term in their proposals and Game
  • 7. CHAPTER 1 » » » » » threshold Learning ing stack to do more processing for color.
  • 8. BOOK 4 » » Neural networks » Decision trees » Machine learning phones.
  • 9. CHAPTER 1 is doing well and then select the new ones to move to the next generation. Survival of
  • 13. CHAPTER 2 Building a Neural Network in Python 365 Building a Neural Networkin Python N eural networks and various other models of how the brain operates have widespread “irrational exuberance” about how the perceptron was going to make because of the application areas that have been developed. - IN THIS CHAPTER » How machines learn » Understanding the basics of machine learning » Teaching a machine to learn something » Using TensorFlow to do machine learning
  • 14. 366 BOOK 4 Understanding Neural Networks » The input layer of neurons » An arbitrary amount of hidden layers of neurons » An output layer of neurons connecting to the world » A set of weights and biases between each neuron level » A choice of activation function for each hidden layer of neurons » A loss function that will provide “overtraining” of the network Weights are given for each of the inter-neural connecting lines. A neuron are called weights. A two-layer neural network.
  • 15. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 367 Layers of neurons - feed-forward input because the data feeds in - backpropagation and simulates what people do when performing a task using an iterative approach for trial and error. Feed-forward and backpropagation.
  • 16. 368 BOOK 4 an epoch to provide training to learn complex tasks. - - retraining. Weights and biases - thing we call the biases - BACKPROPAGATION In the human brain, learning happens because of the addition of new connections The methods used to propagate from results to previous layers (also called feedback) - tion of the math and how it is used, check out Machine Learning For Dummies, by John
  • 17. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 369 The activation function Loss function loss function compares the result of our neural network to the desired results. current results are. channel that will improve our neural network. of the network. sigmoid function.
  • 18. 370 BOOK 4 example. Building a Simple Neural Network in Python The neural-net Python code calculations. The Truth Table (a Three-Input XOR Gate) for the Neural Network X2 X3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  • 19. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 371 USES OF XOR FUNCTIONS - ware. You can use an XOR gate as part of a one-bit adder that adds one bit to another bit (and provides a carry bit to string them together to make big adders), as well as stringing them together to build a pseudo-random number generator. The coolest application we know of an XOR gate has to do with coding algorithms and kind of a mashup of your data), and then you have a more robust data to transmit long distances (like from Pluto, our former ninth planet), which can have all sorts of events that cause noise in the data, corrupting bits and bytes. correcting any errors (up to a point) so you have good data. This allows us to transmit data much further with less power, because with the Reed-Solomon code you become error-tolerant. and derivatives ended up on projects like the Hubble Space Telescope and on John’s those little XOR gates. NumPy tensor algebra packages. It is now the preferred library and is also a part of SciPy and MatPlotLib, two common
  • 20. BOOK 4 - # 2 Layer Neural Network in NumPy import numpy as np # X = input of our 3 input XOR gate # set up the inputs of the neural network (right from the table) X = np.array(([0,0,0],[0,0,1],[0,1,0], [0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float) # y = our output of our neural network y = np.array(([1], [0], [0], [0], [0], [0], [0], [1]), dtype=float) # what value we want to predict xPredicted = np.array(([0,0,1]), dtype=float) X = X/np.amax(X, axis=0) # maximum of X input array # maximum of xPredicted (our input data for the prediction) xPredicted = xPredicted/np.amax(xPredicted, axis=0) # set up our Loss file for graphing lossFile = open("SumSquaredLossList.csv", "w") class Neural_Network (object): def __init__(self): #parameters self.inputLayerSize = 3 # X1,X2,X3 self.outputLayerSize = 1 # Y1 self.hiddenLayerSize = 4 # Size of the hidden layer # build weights of each layer # set to random values # look at the interconnection diagram to make sense of this # 3x4 matrix for input to hidden self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize) # 4x1 matrix for hidden layer to output self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
  • 21. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 373 def feedForward(self, X): # feedForward propagation through our network # dot product of X (input) and first set of 3x4 weights self.z = np.dot(X, self.W1) # the activationSigmoid activation function - neural magic self.z2 = self.activationSigmoid(self.z) # dot product of hidden layer (z2) and second set of 4x1 weights self.z3 = np.dot(self.z2, self.W2) # final activation function - more neural magic o = self.activationSigmoid(self.z3) return o def backwardPropagate(self, X, y, o): # backward propagate through the network # calculate the error in output self.o_error = y - o # apply derivative of activationSigmoid to error self.o_delta = self.o_error*self.activationSigmoidPrime(o) # z2 error: how much our hidden layer weights contributed to output # error self.z2_error = self.o_delta.dot(self.W2.T) # applying derivative of activationSigmoid to z2 error self.z2_delta = self.z2_error*self.activationSigmoidPrime(self.z2) # adjusting first set (inputLayer --> hiddenLayer) weights self.W1 = X.T.dot(self.z2_delta) # adjusting second set (hiddenLayer --> outputLayer) weights self.W2 = self.z2.T.dot(self.o_delta) def trainNetwork(self, X, y): # feed forward the loop o = self.feedForward(X) # and then back propagate the values (feedback) self.backwardPropagate(X, y, o) def activationSigmoid(self, s): # activation function # simple activationSigmoid curve as in the book return 1/(1 np.exp(-s))
  • 22. 374 BOOK 4 def activationSigmoidPrime(self, s): # First derivative of activationSigmoid # calculus time! return s * (1 - s) def saveSumSquaredLossList(self,i,error): lossFile.write(str(i) "," str(error.tolist()) 'n') def saveWeights(self): # save this in order to reproduce our cool network np.savetxt("weightsLayer1.txt", self.W1, fmt="%s") np.savetxt("weightsLayer2.txt", self.W2, fmt="%s") def predictOutput(self): print ("Predicted XOR output data based on trained weights: ") print ("Expected (X1-X3): n" str(xPredicted)) print ("Output (Y1): n" str(self.feedForward(xPredicted))) myNeuralNetwork = Neural_Network() trainingEpochs = 1000 #trainingEpochs = 100000 for i in range(trainingEpochs): # train myNeuralNetwork 1,000 times print ("Epoch # " str(i) "n") print ("Network Input : n" str(X)) print ("Expected Output of XOR Gate Neural Network: n" str(y)) print ("Actual Output from XOR Gate Neural Network: n" str(myNeuralNetwork.feedForward(X))) # mean sum squared loss Loss = np.mean(np.square(y - myNeuralNetwork.feedForward(X))) myNeuralNetwork.saveSumSquaredLossList(i,Loss) print ("Sum Squared Loss: n" str(Loss)) print ("n") myNeuralNetwork.trainNetwork(X, y) myNeuralNetwork.saveWeights() myNeuralNetwork.predictOutput() Breaking down the code # 2 Layer Neural Network in NumPy import numpy as np
  • 23. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 375 - sudo apt-get install python3-numpy # X = input of our 3 input XOR gate # set up the inputs of the neural network (right from the table) X = np.array(([0,0,0],[0,0,1],[0,1,0], [0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float) # y = our output of our neural network y = np.array(([1], [0], [0], [0], [0], [0], [0], [1]), dtype=float) # what value we want to predict xPredicted = np.array(([0,0,1]), dtype=float) X = X/np.amax(X, axis=0) # maximum of X input array # maximum of xPredicted (our input data for the prediction) xPredicted = xPredicted/np.amax(xPredicted, axis=0) # set up our Loss file for graphing lossFile = open("SumSquaredLossList.csv", "w") the network. class Neural_Network (object): def __init__(self): #parameters self.inputLayerSize = 3 # X1,X2,X3 self.outputLayerSize = 1 # Y1 self.hiddenLayerSize = 4 # Size of the hidden layer
  • 24. 376 BOOK 4 Set all the network weights to random values to start. # build weights of each layer # set to random values # look at the interconnection diagram to make sense of this # 3x4 matrix for input to hidden self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize) # 4x1 matrix for hidden layer to output self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize) def feedForward(self, X): # feedForward propagation through our network # dot product of X (input) and first set of 3x4 weights self.z = np.dot(X, self.W1) # the activationSigmoid activation function - neural magic self.z2 = self.activationSigmoid(self.z) # dot product of hidden layer (z2) and second set of 4x1 weights self.z3 = np.dot(self.z2, self.W2) # final activation function - more neural magic o = self.activationSigmoid(self.z3) return o And now we add the backwardPropagate function that implements the real trial- and-error learning that our neural network uses. def backwardPropagate(self, X, y, o): # backward propagate through the network # calculate the error in output self.o_error = y - o # apply derivative of activationSigmoid to error self.o_delta = self.o_error*self.activationSigmoidPrime(o) # z2 error: how much our hidden layer weights contributed to output # error self.z2_error = self.o_delta.dot(self.W2.T)
  • 25. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 377 # applying derivative of activationSigmoid to z2 error self.z2_delta = self.z2_error*self.activationSigmoidPrime(self.z2) # adjusting first set (inputLayer --> hiddenLayer) weights self.W1 = X.T.dot(self.z2_delta) # adjusting second set (hiddenLayer --> outputLayer) weights self.W2 = self.z2.T.dot(self.o_delta) backwardPropagate and the feedForward functions each time we train the network. def trainNetwork(self, X, y): # feed forward the loop o = self.feedForward(X) # and then back propagate the values (feedback) self.backwardPropagate(X, y, o) function follows. def activationSigmoid(self, s): # activation function # simple activationSigmoid curve as in the book return 1/(1 np.exp(-s)) def activationSigmoidPrime(self, s): # First derivative of activationSigmoid # calculus time! return s * (1 - s) weights. def saveSumSquaredLossList(self,i,error): lossFile.write(str(i) "," str(error.tolist()) 'n') def saveWeights(self): # save this in order to reproduce our cool network np.savetxt("weightsLayer1.txt", self.W1, fmt="%s") np.savetxt("weightsLayer2.txt", self.W2, fmt="%s")
  • 26. 378 BOOK 4 trained weights. def predictOutput(self): print ("Predicted XOR output data based on trained weights: ") print ("Expected (X1-X3): n" str(xPredicted)) print ("Output (Y1): n" str(self.feedForward(xPredicted))) myNeuralNetwork = Neural_Network() trainingEpochs = 1000 #trainingEpochs = 100000 for i in range(trainingEpochs): # train myNeuralNetwork 1,000 times print ("Epoch # " str(i) "n") print ("Network Input : n" str(X)) print ("Expected Output of XOR Gate Neural Network: n" str(y)) print ("Actual Output from XOR Gate Neural Network: n" str(myNeuralNetwork.feedForward(X))) # mean sum squared loss Loss = np.mean(np.square(y - myNeuralNetwork.feedForward(X))) myNeuralNetwork.saveSumSquaredLossList(i,Loss) print ("Sum Squared Loss: n" str(Loss)) print ("n") myNeuralNetwork.trainNetwork(X, y) value. myNeuralNetwork.saveWeights() myNeuralNetwork.predictOutput() Running the neural-network code python3 2LayerNeuralNetworkCode.py -
  • 27. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 379 » weightsLayer1.txt: » weightsLayer2.txt: » SumSquaredLossList.csv: Epoch # 999 Network Input : [[0. 0. 0.] [0. 0. 1.] [0. 1. 0.] [0. 1. 1.] [1. 0. 0.] [1. 0. 1.] [1. 1. 0.] [1. 1. 1.]] Expected Output of XOR Gate Neural Network: [[1.] [0.] [0.] [0.] [0.] [0.] [0.] [1.]] Actual Output from XOR Gate Neural Network: [[0.93419893] [0.04425737] [0.01636304] [0.03906686] [0.04377351] [0.01744497] [0.0391143 ] [0.93197489]] Sum Squared Loss: 0.0020575319565093496 Predicted XOR output data based on trained weights: Expected (X1-X3): [0. 0. 1.]
  • 28. 380 BOOK 4 Output (Y1): [0.04422615] weights with random numbers at the start of the run. have given perfect results. Epoch # 99999 Network Input : [[0. 0. 0.] [0. 0. 1.] [0. 1. 0.] [0. 1. 1.] The Loss function during training.
  • 29. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 381 [1. 0. 0.] [1. 0. 1.] [1. 1. 0.] [1. 1. 1.]] Expected Output of XOR Gate Neural Network: [[1.] [0.] [0.] [0.] [0.] [0.] [0.] [1.]] Actual Output from XOR Gate Neural Network: [[9.85225608e-01] [1.41750544e-04] [1.51985054e-04] [1.14829204e-02] [1.17578404e-04] [1.14814754e-02] [1.14821256e-02] [9.78014943e-01]] Sum Squared Loss: 0.00013715041859631841 Predicted XOR output data based on trained weights: Expected (X1-X3): [0. 0. 1.] Output (Y1): [0.00014175] Using TensorFlow for the same neural network applications and so has libraries and functions designed for those applications. tensor is a multi- -
  • 30. BOOK 4 implemented in terms of performing operations matrix of data and then moving Installing the TensorFlow Python library https://www.tensorflow.org/install/pip. - the tutorial referenced above. INTRODUCING TENSORS data. But an additional discussion will be helpful in getting your head wrapped around • Scalars: A scalar can be thought of as a single piece of data. There is one and • Vectors: A vector think of a vector as an arrow located on a plane. It looks like a ray on the plane. have coordinates for 3D space: x, y, and z. • Tensors: Vectors are special cases of tensors. A tensor - - vidual numbers, vectors as ordered sets of numbers, and tensors by a single or multidimensional array of numbers. Here is a great non-mathematical introduction to tensors: https://www.youtube.com/watch?v=f5liqUk0ZTw.
  • 31. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 383 Building a Python Neural Network - code. - provides the excellent and intuitive set of abstractions and functions whereas 1. Load and format your data. 2. 3. Compile the model. Our TensorFlow three-layer neural network.
  • 32. 384 BOOK 4 4. Fit and train your model. 5. Evaluate the model. Loading your data model and layers Compiling your model Fitting and training your model TensorFlowKeras.py import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.layers import Activation, Dense
  • 33. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 385 import numpy as np # X = input of our 3 input XOR gate # set up the inputs of the neural network (right from the table) X = np.array(([0,0,0],[0,0,1],[0,1,0], [0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float) # y = our output of our neural network y = np.array(([1], [0], [0], [0], [0], [0], [0], [1]), dtype=float) model = tf.keras.Sequential() model.add(Dense(4, input_dim=3, activation='relu', use_bias=True)) #model.add(Dense(4, activation='relu', use_bias=True)) model.add(Dense(1, activation='sigmoid', use_bias=True)) model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy']) print (model.get_weights()) history = model.fit(X, y, epochs=2000, validation_data = (X, y)) model.summary() # printing out to file loss_history = history.history["loss"] numpy_loss_history = np.array(loss_history) np.savetxt("loss_history.txt", numpy_loss_history, delimiter="n") binary_accuracy_history = history.history["binary_accuracy"] numpy_binary_accuracy = np.array(binary_accuracy_history) np.savetxt("binary_accuracy.txt", numpy_binary_accuracy, delimiter="n") print(np.mean(history.history["binary_accuracy"])) result = model.predict(X ).round() print (result)
  • 34. 386 BOOK 4 model and results. Breaking down the code import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.layers import Activation, Dense import numpy as np # X = input of our 3 input XOR gate # set up the inputs of the neural network (right from the table) X = np.array(([0,0,0],[0,0,1],[0,1,0], [0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]), dtype=float) # y = our output of our neural network y = np.array(([1], [0], [0], [0], [0], [0], [0], [1]), dtype=float) relu See the commented model.add
  • 35. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 387 model = tf.keras.Sequential() model.add(Dense(4, input_dim=3, activation='relu', use_bias=True)) #model.add(Dense(4, activation='relu', use_bias=True)) model.add(Dense(1, activation='sigmoid', use_bias=True)) mean_squared_error - binary_accuracy model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy']) print (model.get_weights()) - X y validation_data - work in each epoch and generates val_acc and val_loss each epoch as stored in the history variable. history = model.fit(X, y, epochs=2000, validation_data = (X, y)) model.summary()
  • 36. 388 BOOK 4 history variable that we would like to graph. # printing out to file loss_history = history.history["loss"] numpy_loss_history = np.array(loss_history) np.savetxt("loss_history.txt", numpy_loss_history, delimiter="n") binary_accuracy_history = history.history["binary_accuracy"] numpy_binary_accuracy = np.array(binary_accuracy_history) np.savetxt("binary_accuracy.txt", numpy_binary_accuracy, delimiter="n") the inputs of X round <0.1 = "0" and >0.9 = "1". We also calculated the average of all the binary_accuracy values of print(np.mean(history.history["binary_accuracy"])) result = model.predict(X ).round() print (result) Evaluating the model /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5 return f(*args, **kwds) /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412 return f(*args, **kwds)
  • 37. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 389 python3 TensorFlowKeras.py in our termi- Verbose model.fit ... Epoch 1999/2000 8/8 [==============================] - 0s 2ms/step - loss: 0.0367 - binary_ accuracy: 1.0000 - val_loss: 0.0367 - val_binary_accuracy: 1.0000 Epoch 2000/2000 8/8 [==============================] - 0s 2ms/step - loss: 0.0367 - binary_ accuracy: 1.0000 - val_loss: 0.0367 - val_binary_accuracy: 1.0000 _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 4) 16 _________________________________________________________________ dense_1 (Dense) (None, 1) 5 ================================================================= Total params: 21 Trainable params: 21 Non-trainable params: 0 _________________________________________________________________ 0.8436875 [[1.] [0.] [0.] [0.] [0.] [0.] [0.] [1.]] model.predict function call at the end matches our relu -
  • 38. 390 BOOK 4 Changing to a three-layer neural Results of the two-layer training. BACKPROPAGATION IN KERAS - tion and how it was a fundamental part of neural networks. However, now we have for you. If you want to modify how it is doing it, the easiest way is to modify the optimi- module.compile work to dramatically modify the backpropogation algorithm in Keras, but it can be done. - -
  • 39. Building a Neural Network in Python CHAPTER 2 Building a Neural Network in Python 391 model.add(Dense(4, input_dim=3, activation='relu', use_bias=True)) #model.add(Dense(4, activation='relu', use_bias=True)) model.add(Dense(1, activation='sigmoid', use_bias=True)) model.add(Dense(4, input_dim=3, activation='relu', use_bias=True)) model.add(Dense(4, activation='relu', use_bias=True)) model.add(Dense(1, activation='sigmoid', use_bias=True)) 8/8 [==============================] - 0s 2ms/step - loss: 0.0153 - binary_ accuracy: 1.0000 - val_loss: 0.0153 - val_binary_accuracy: 1.0000 Epoch 2000/2000 8/8 [==============================] - 0s 2ms/step - loss: 0.0153 - binary_ accuracy: 1.0000 - val_loss: 0.0153 - val_binary_accuracy: 1.0000 _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 4) 16 _________________________________________________________________ dense_1 (Dense) (None, 4) 20 _________________________________________________________________ dense_2 (Dense) (None, 1) 5 ================================================================= Total params: 41 Trainable params: 41 Non-trainable params: 0 _________________________________________________________________ 0.930375 [[1.] [0.] [0.] [0.] [0.] [0.] [0.] [1.]]
  • 40. BOOK 4 parameters and make changes. Results of the three-layer training. GUI (GRAPHICAL USER INTERFACE) TO RUN TensorFlow? graphs in this chapter. Most of the time, we use our machines in a terminal window, terminal windows for editing. That big advantage is called TensorBoard. TensorBoard
  • 41. CHAPTER 3 Doing Machine Learning in Python 393 Doing Machine Learning in Python W - - - IN THIS CHAPTER » Teaching a machine to learn something » How machines learn » Understand the basics of machine learning » Using TensorFlow to do machine learning
  • 42. 394 BOOK 4 - Learning by Looking for Solutions in All the Wrong Places best local maxima, and it may or may not be the best » Supervised learning: This type of algorithm builds a model of data that contains both inputs and outputs. The data is known as training data. This is the kind of machine learning we show in this chapter. » Unsupervised learning: For this type of algorithm, the data contains only the inputs, and the algorithms look for the structures and patterns in the data. » Reinforcement learning: This area is concerned with software taking actions based on some kind of cumulative reward. These algorithms do not assume
  • 43. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python knowledge of an exact mathematical model and are used when exact models are unavailable. This is the most complex area of machine learning, and the one that may bear the most fruit in the future. Classifying Clothes with Machine Learning Training and Learning with TensorFlow A bit of the Fashion-MNIST database.
  • 44. BOOK 4 1. Load and format your data. 2. 3. Compile the model. 4. Fit and train your model. 5. Evaluate the model. Setting Up the Software Environment - www.raspberrypi.org - name of your machine
  • 45. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python 397 Creating a Machine-Learning Network MNIST » 0 T-shirt/top » 1 Trouser » 2 Pullover » 3 Dress » 4 Coat » 5 Sandal » 6 Shirt A full GUI on the Raspberry Pi.
  • 46. BOOK 4 » 7 Sneaker » 8 Bag » 9 Ankle boot dataset Training the network Testing our network Fashion_MNIST Fashion_ MNIST Fashion_MNIST Fashion_MNIST #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image
  • 47. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python 399 # Import Fashion MNIST fashion_mnist = input_data.read_data_sets('input/data', one_hot=True) fashion_mnist = tf.keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] train_images = train_images / 255.0 test_images = test_images / 255.0 model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) model.add(tf.keras.layers.Dense(128, activation='relu' )) model.add(tf.keras.layers.Dense(10, activation='softmax' )) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=5) # test with 10,000 images test_loss, test_acc = model.evaluate(test_images, test_labels) print('10,000 image Test accuracy:', test_acc) Breaking down the code
  • 48. BOOK 4 #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image 1. Load and format your data. This time we are using the built-in data-set reading capability. It knows what this data is because of the import statement from tensorflow.examples. tutorials.mnist in the preceding code. # Import Fashion MNIST fashion_mnist = input_data.read_data_sets('input/data', one_hot=True) fashion_mnist = tf.keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() Here we give some descriptive names to the ten classes within the Fashion_ MNIST data. class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] Here we change all the images to be scaled from 0.0–1.0 rather than 0–255. train_images = train_images / 255.0 test_images = test_images / 255.0 2. Again, this is where the real power of Keras shines. It is very simple to add more neural layers, and to change their sizes and their activation functions. relu), in this case with softmax
  • 49. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) model.add(tf.keras.layers.Dense(128, activation='relu' )) model.add(tf.keras.layers.Dense(10, activation='softmax' )) 3. Compile your model. We are using the loss function sparse_categorical_crossentropy. This function stochastic optimization) is a good default optimizer. It provides a method well suited for problems that are large in terms of data and/or parameters. model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_ crossentropy', metrics=['accuracy']) Sparse categorical crossentropy is a loss function used to measure the error between categories across the data set. Categorical refers to the fact that Sparse refers to Entropy measure of disorder) refers to the mix of data between the categories. 4. Fit and train your model. I chose the number of epochs as only 5 due to the time it takes to run the model for our examples. Feel free to increase! Here we load the NumPy arrays model.fit(train_images, train_labels, epochs=5) 5. Evaluate the model. The model.evaluate function is used to compare the outputs of your trained network in each epoch and generates test_acc and test_loss for your information in each epoch as stored in the history variable. # test with 10,000 images test_loss, test_acc = model.evaluate(test_images, test_labels) print('10,000 image Test accuracy:', test_acc)
  • 50. BOOK 4 Results of the training and evaluation Epoch 1/5 60000/60000 [==============================] - 44s 726us/step - loss: 0.5009 - acc: 0.8244 Epoch 2/5 60000/60000 [==============================] - 42s 703us/step - loss: 0.3751 - acc: 0.8652 Epoch 3/5 60000/60000 [==============================] - 42s 703us/step - loss: 0.3359 - acc: 0.8767 Epoch 4/5 60000/60000 [==============================] - 42s 701us/step - loss: 0.3124 - acc: 0.8839 Epoch 5/5 60000/60000 [==============================] - 42s 703us/step - loss: 0.2960 - acc: 0.8915 10000/10000 [==============================] - 4s 404us/step 10,000 image Test accuracy: 0.873 Testing a single test image Fashion_MNIST #run test image from Fashion_MNIST data img = test_images[15] img = (np.expand_dims(img,0)) singlePrediction = model.predict(img,steps=1) print ("Prediction Output") print(singlePrediction) print() NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction)
  • 51. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python print ("Our Network has concluded that the image number '15' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") Prediction Output [[1.2835168e-05 9.9964070e-01 6.2637120e-08 3.4126092e-04 4.4297972e-06 7.8450663e-10 6.2759432e-07 9.8717527e-12 1.2729484e-08 1.1002166e-09]] Our Network has concluded that the image number '15' is a Trouser 99% Confidence Level Testing on external pictures Image 15 from the Fashion- MNIST test database.
  • 52. BOOK 4 # run Our test Image # read test dress image imageName = "Dress28x28.JPG" dress hanging The dress at 28x28 pixels.
  • 53. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python testImg = Image.open(imageName) testImg.load() data = np.asarray( testImg, dtype="float" ) data = tf.image.rgb_to_grayscale(data) data = data/255.0 data = tf.transpose(data, perm=[2,0,1]) singlePrediction = model.predict(data,steps=1) print ("Prediction Output") print(singlePrediction) print() NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") The results, round 1 Prediction Output [[1.2717753e-06 1.3373902e-08 1.0487850e-06 3.3525557e-11 8.8031484e-09 7.1847245e-10 1.1177938e-04 8.8322977e-12 9.9988592e-01 3.2957085e-12]] Our Network has concluded that the file 'Dress28x28.JPG' is a Bag 99% Confidence Level wait for it Prediction Output [[3.4407502e-33 0.0000000e 00 2.5598763e-33 0.0000000e 00 0.0000000e 00
  • 54. BOOK 4 0.0000000e 00 2.9322060e-17 0.0000000e 00 1.0000000e 00 1.5202169e-39]] Our Network has concluded that the file 'Dress28x28.JPG' is a Bag 100% Confidence Level The CNN model code #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf
  • 55. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image # Import Fashion MNIST fashion_mnist = input_data.read_data_sets('input/data', one_hot=True) fashion_mnist = tf.keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] train_images = train_images / 255.0 test_images = test_images / 255.0 # Prepare the training images train_images = train_images.reshape(train_images.shape[0], 28, 28, 1) # Prepare the test images test_images = test_images.reshape(test_images.shape[0], 28, 28, 1) model = tf.keras.Sequential() input_shape = (28, 28, 1) model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) model.add(tf.keras.layers.Dropout(0.25)) model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.Dropout(0.25))
  • 56. BOOK 4 model.add(tf.keras.layers.Conv2D(128, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) model.add(tf.keras.layers.Dropout(0.25)) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(512, activation='relu')) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(128, activation='relu')) model.add(tf.keras.layers.BatchNormalization()) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(10, activation='softmax')) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=5) # test with 10,000 images test_loss, test_acc = model.evaluate(test_images, test_labels) print('10,000 image Test accuracy:', test_acc) #run test image from Fashion_MNIST data img = test_images[15] img = (np.expand_dims(img,0)) singlePrediction = model.predict(img,steps=1) print ("Prediction Output") print(singlePrediction) print() NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print ("Our Network has concluded that the image number '15' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level")
  • 57. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python The results, round 2 10,000 image Test accuracy: 0.8601 Prediction Output [[5.9128129e-06 9.9997270e-01 1.5681641e-06 8.1393973e-06 1.5611777e-06 7.0504888e-07 5.5174642e-06 2.2484977e-07 3.0045830e-06 5.6888598e-07]] Our Network has concluded that the image number '15' is a Trouser - - Visualizing with MatPlotLib - pip3 install matplotlib We add the history model.fit
  • 58. BOOK 4 #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image # Import Fashion MNIST fashion_mnist = input_data.read_data_sets('input/data', one_hot=True) fashion_mnist = tf.keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist. load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] train_images = train_images / 255.0 test_images = test_images / 255.0 model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) model.add(tf.keras.layers.Dense(128, activation='relu' )) model.add(tf.keras.layers.Dense(10, activation='softmax' )) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(train_images, train_labels, epochs=2)
  • 59. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python # Get training and test loss histories training_loss = history.history['loss'] accuracy = history.history['acc'] # Create count of the number of epochs epoch_count = range(1, len(training_loss) 1) # Visualize loss history plt.figure(0) plt.plot(epoch_count, training_loss, 'r--') plt.plot(epoch_count, accuracy, 'b--') plt.legend(['Training Loss', 'Accuracy']) plt.xlabel('Epoch') plt.ylabel('History') plt.show(block=False); plt.pause(0.001) test_loss, test_acc = model.evaluate(test_images, test_labels) #run test image from Fashion_MNIST data img = test_images[15] plt.figure(1) plt.imshow(img) plt.show(block=False) plt.pause(0.001) img = (np.expand_dims(img,0)) singlePrediction = model.predict(img,steps=1) print ("Prediction Output") print(singlePrediction) print() NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print ("Our Network has concluded that the image number '15' is a " class_names[NumberElement])
  • 60. BOOK 4 print (str(int(Element*100)) "% Confidence Level") print('Test accuracy:', test_acc) # read test dress image imageName = "Dress28x28.JPG" testImg = Image.open(imageName) plt.figure(2) plt.imshow(testImg) plt.show(block=False) plt.pause(0.001) testImg.load() data = np.asarray( testImg, dtype="float" ) data = tf.image.rgb_to_grayscale(data) data = data/255.0 data = tf.transpose(data, perm=[2,0,1]) singlePrediction = model.predict(data,steps=1) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print(NumberElement) print(Element) print(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") plt.show() -
  • 61. Doing Machine Learning in Python CHAPTER 3 Doing Machine Learning in Python Learning More Machine Learning » Machine Learning for Dummies, John Paul Mueller and Luca Massaron » Deep Learning with Python, Francois Chollet » Dummies, John Paul Mueller and Luca Massaron Our Raspberry MatPlotLib visualization.
  • 63. CHAPTER 4 415 Exploring More AI A fter reading the previous three chapters, you have learned quite a bit networks and machine learning. There is a lot more to AI than just these two things, though. We could look at advanced searching (not Google searching Instead, in this chapter, we talk about other ways of doing AI software beyond the our large neural network? Sounds like we could use some bigger iron to accom- plish more training in less time. That’s what this chapter is about. The Raspberry Pi is an inexpensive full-blown computing device. The Raspberry Pi 3B » CPU: Broadcom quad-core 64bit processor @ 1.4GHz » GPU: Broadcom Videocore-IV » » »
  • 64. 416 BOOK 4 » RAM: 1GB SDRAM » Networking: Gigabit Ethernet, 802.11b/g/n/ac WiFi » Storage: SD card How does this stack up? For a $35 computer, very well. But for a dedicated AI computer, not so much. The problems are not enough RAM (1GB isn’t very much, especially for a Rasp- processing chip. There are two mitigating circumstances that keep the Raspberry Pi in the running can use the Raspberry Pi to control processors and AI hardware up on the cloud for all the computationally heavy lifting. The Raspberry Pi processing chip containing the Videocore-IV.
  • 65. Exploring More AI in Python CHAPTER 4 417 Remember from our previous chapter that the bulk of the computer time in build- ing any kind of machine-learning AI system is for training and that when that training is done, it doesn’t take a lot of processing to actually characterize an unknown or new picture. This means you can train on one big machine and then deploy on a simpler computer such as the Raspberry Pi in the application. This doesn’t work all the time (especially if you want to keep learning as the program programs on much simpler and less expensive hardware. Performing AI analysis or training on the small computers that are connected to a network, is called edge computing edge of the network. 3B The Videocore-IV is a low-power mobile graphics processor. It is a two-dimensional DSP (digital signal processor) that is set up as basically a four-GPU core unit. These GPU core units are called slices and can be very roughly compared to GPU computer units, such as those used by AMD and Nvidia (which can have 256, 512, or more individual GPU units, far outclassing the Videocore 4 units) to power their GPU cards, which are now proving to be very popular with AI researchers and hobbyists. This processor is really designed to be used in video encoding and decoding applica- tions and not so much for AI use. However, some researchers have made use of the four slices to accelerate neural-network processing on the Raspberry Pi to achieve up to about three times the performance of the four-core main processor used alone. One of the main barriers to using the Videocore on the Raspberry Pi for AI type of appli- available under NDA (non-disclosure agreements), which do not go along with open- source development. However, you can now get full documentation and the complete source code for the Raspberry Pi 3B graphics stack under a very nonrestrictive BSD license, which should provide a path forward.
  • 66. 418 BOOK 4 It turns out that there have been a number of companies starting to build special- ized AI compute sticks, many of which can be used on the Raspberry Pi. Typically, Python libraries, that support using these sticks. Two of the most interesting ones are » The Movidius NCS stick plugs into the USB port of the Raspberry Pi or other compute and provides hardware support for deep learning based analysis (refer to Chapters 1–3). For example, you can use the Amazon cloud to perform image analysis, process- moves your computationally expensive task from your Raspberry Pi to the cloud. This does cost money and bandwidth (and latency in your system) to do this. Doing the analysis with your trained deep learning neural network on the edge by using a NCS stick can help and can possibly allow you to disconnect your device running on the edge of the network from the Internet entirely. It runs around 60X faster than doing image analysis on the Raspberry Pi and costs less You can do facial recognition, text analysis, monitoring and maintenance using this NCS stick. Pretty cool! There is one concept that we need to emphasize here, however. The NCS but it is not used for training models! You still need to build and train the models. It The Intel Movidius Neural Compute Stick 2.
  • 67. Exploring More AI in Python CHAPTER 4 419 done. » The Google Edge TPU (tensor processing unit) has a USB Type-C socket that can be plugged into an Linux-based system to provide accelerated machine learning analysis and inferences. Does the word tensor sound familiar? Tensors are matrices like in our neural-network Well, it turns out, much like the Intel NCS stick above, this device is all about executing trained machine-learning models. We still train the machine-learning networks using other techniques, and then we execute the model on the stick. The Google Edge TPU accelerator. Oh, boy. In the next four years, this type of specialized hardware for running machine- come from Google, Intel, Nvidia, AMD, Qualcomm, and a number of other smaller companies from around the world. Everybody is starting to climb on the AI accelerator hardware bandwagon.
  • 68. 420 BOOK 4 In the tech industry, everyone loves to use buzzwords such as the cloud. Often, the use of such language results in arbitrary and nebulous terms that leave consumers When a company says, “your data is in the cloud” or that “you can work in the the cloud” data is on the ground and is stored somewhere in a data center with a bunch of servers that are more similar to your PC or Mac than you may think. rather than on your local machine. This is correct to a degree, but nothing really runs on the Internet; it runs on machines that are connected - standing that in-the-cloud software runs on servers and is not “just out there” tends to really quickly demystify the cloud and its functions. If you have two computers networked together and use the other computer for a data server, you have your own “cloud.” This goes for basic services like storing your data in the cloud, but there is much more than just storage available on the cloud and that is where it gets really interesting. The advantage of using the cloud is that you can use services and storage unavail- able to you on your local network and (in one of the most important game chang- computing needs on a dynamic basis. - nections some of the time. This limits the cloud in applications such as self-driving cars that aren’t guaranteed to have good Internet access all the time. Interestingly, don’t want to stay connected to the net all the time for power considerations. So, how do you use the cloud? That depends on the service and vendor, but in machine-learning applications, the most common way is to set up the Python on a computer that calls cloud-based functions and applications. All cloud vendors provide examples. What is a great consumer example of cloud usage? The Amazon Echo and Alexa. It listens to you, compresses the speech data, sends it to the Amazon AWS cloud, translates and interprets your data and then sends back a verbal response or com- mands to make your lights come on.
  • 69. Exploring More AI in Python CHAPTER 4 421 A number of cloud providers for storage and services exist and more are arriving all the time. The top four cloud providers for AI at the time of this writing are » Google cloud » Amazon Web Services » IBM cloud » Microsoft Azure stick above, can accelerate your AI applications. Much of the Google cloud’s func- For example, the Cloud Vision API can detect objects, logos, and landmarks within City application called ParkMyRide, which uses a Raspberry Pi–based solar- powered camera to take pictures of the street and determines street parking avail- street to Google and gets back the number of cars found and where they are in the picture. They then supply this information to a smartphone app which displays it graphically. Pretty neat. - AI-powered applications to create new services for customers to use. and supplying this expertise to businesses. Many of these cloud services are built on the consumer product versions, so as Alexa improves, for example, the cloud services also improve. learning visualization/creation tools, vision recognition, and analysis.
  • 70. 422 BOOK 4 The IBM cloud has gotten a bad rap over the past few years for being hard to use. In the past couple of years, it has gotten much better. IBM merged its three big available, so it is still hard to get going, but there is much better control and con- sistency over the process. Their machine-learning environment is called the Watson Studio and is used to build and train AI models in one integrated environment. They also provide huge management platforms available. One of the cool things they have is a service called Watson Personality Insights that predicts personality characteristics, needs, and values through written text. What would Watson Personality make of the authors of this book? We will run the - » AI services » AI tools and frameworks » AI infrastructures Similar to Amazon and Google, their AI applications are built on consumer prod- ucts that Microsoft has produced. Azure also has support for specialized FPGA - - erators. Microsoft is one of the largest, if not the largest, customer of the Intel Movidius chips. They have products for machine learning, IOT toolkits, and management services, custom silicon AI infrastructure, and a container service that can turn your inside applications into cloud apps. Microsoft Azure is the one to watch for some pretty spectacular innovations.
  • 71. Exploring More AI in Python CHAPTER 4 423 part of the PC experience for decades. People often hunt for the latest and great- est graphics card to make their PCs better gaming machines. One thing becomes - high-resolution graphics is computationally expensive, and the way to solve that is to build graphics cards out of computers that were designed to do graphics to computer core that is designed to work with graphics. which dramatically improved video resolution and frame rates in games. One thing to remember is that graphics algorithms are constructed using data struc- tures called matrices (or tensors Wait. Tensors? Matrices? This sounds suspiciously like the kind of data structures we use in AI and machine learning. Because of the way machine learning and deep Regardless of the type of neural network used, all the techniques rely on per- a multitude of images or data points are fed to the network and then trained Nvidia 256 Core GPU chip.
  • 72. 424 BOOK 4 To speed up the training, these operations can be done in parallel, which turns out - puters perfect for machine learning applications. The combination of a powerful - grams. TensorFlow in particular has versions of the software that is designed to To put it in perspective, our Raspberry Pi 3B has 4 processor cores and in some can do a lot of fast training and executing machine learning networks using these - puters and hardware to support AI applications. There are starting to be even more specialized chips. At last count, there are over 50 companies working on chips that will accelerate AI functions. Microsoft has built out infrastructure to support AI acceleration hardware in the cloud. This is one of the big reasons to watch what Microsoft is doing. The future is in more and more specialized hardware, especially as specialized hardware gets easier and easier to deal with from the user software side. If you are interested in furthering your knowledge and abilities in machine learn- ing and AI, check out the following sources for project inspiration. The important thing is to actually build programs and modify other people programs to really learn the technology from experience.
  • 73. Exploring More AI in Python CHAPTER 4 425 » “Is Santa Claus Real?,” Varun Vohra, https://towardsdatascience.com/ is-santa-claus-real-9b7b9839776c » “Keras and deep learning on the Raspberry Pi,” Adrian Rosebrock, https://www. pyimagesearch.com/2017/12/18/keras-deep-learning-raspberry-pi/ » Jain, https://medium.com/nanonets/how-to-easily-detect-objects- with-deep-learning-on-raspberrypi-225f29635c74 » “Building a Cat Detector using Convolutional Neural Network,” Venelin Valkov, https://medium.com/@curiousily/tensorflow-for-hackers-part-iii- convolutional-neural-networks-c077618e590b » Reddy, https://medium.com/@bapireddy/real-time-image-classifier- on-raspberry-pi-using-inception-framework-faccfa150909
  • 76. Contents at a Glance CHAPTER 1: . . . . . . . . . . . . . . . . . . . 429 Working with Big, Big Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 Cooking with Gas: The Five Step Process of Data Science. . . . . . . 432 CHAPTER 2: . . . . . . . . . . . . . . . . . . 437 Doing Your First Data Science Project . . . . . . . . . . . . . . . . . . . . . . . 440 CHAPTER 3: . . . . . . . . . . . 451 What Is Big Data?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 Understanding the Google Cloud and BigQuery . . . . . . . . . . . . . . 452 Reading the Medicare Big Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Looking for the Most Polluted City in the World on an Hourly Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
  • 77. CHAPTER 1 429 TheFive Areas of D ata science impacts our modern lives in far more ways than you may think. When you use Google or Bing or DuckDuckGo, you are using a very sophis- ticated application of data science. The suggestions for other search terms that come up when you are typing? Those come from data science. Medical diagnoses and interpretations of images and symptoms are examples of data science. Doctors rely on data science interpretations more and more these days. As with most of the topics in this book, data science looks intimidating to the uninitiated. Inferences, data graphs, and statistics, oh my! However, just as in examples, you can really get a handle on what data science is and what it isn’t. In this chapter we cover just enough statistics and “asking questions of data” to get you going and get some simple results. The purpose is to introduce you to the use of Python in data science and talk about just enough theory to get you started. If nothing else, we want to leave you with the process of data science and give you a higher level of understanding of what is behind some of the talking heads on television and the various press releases that come from universities. These people are always citing results that come from big data analysis and are often » » »
  • 78. 430 BOOK 5 overstating what they actually mean. An example of this is when one study says what your results mean, beyond simple interpretations, is where the really hard parts of data science and statistics meet and are worthy of a book all their own. At the end of our data science journey, you will know more about the processes involved in answering some of these questions. There is a mystery to data science, but with just a little knowledge and a little Python, we can penetrate the veil and do some data science. Python and the myriad tools and libraries available can make data science much more accessible. One thing to remember is that most scientists (including data scientists) are not necessarily experts in computer science. They like to use tools to simplify the coding and to allow them to focus on getting the answers and performing the analysis of the data they want. The media likes to throw around the notion of “big data” and how people can get insights into consumer (and your) behavior from it. Big data is a term used to refer to large and complex datasets that are too large for traditional data processing software (read databases, spread sheets, and traditional statistics packages like called the “Three V’s”: volume, variety, and velocity. Volume Volume refers to how big the dataset is that we are considering. It can be really, than the population of China. There are over 250 billion 2.5 trillion posts. That is a lot of data. A really big amount of data. And what about the upcoming world of IOT (Internet of Things)? Gartner, one of the world’s leading analysis companies, estimates 22 billion devices by 2022. That is 22 billion devices producing thousands of pieces of data. Imagine that you are sampling the temperature in your kitchen once a minute for a year. That is
  • 79. The Five Areas of Data Science CHAPTER 1 431 temperature and humidity measurements, and your house is producing 6 million pieces of data from just one little IOT device per room. It gets crazy very quickly. And look at your smartphone. Imagine how many pieces of data it produces in a day. Location, usage, power levels, cellphone connectivity spews out of your phone into databases and your apps and application dashboards like Blynk constantly. Sometimes (as we just recently found out from cellphone companies) location information is being collected and sold even without your consent or opt-in. Data, data, and more data. Data science is how we make use of this. or location information. Sometimes they go together and sometimes they don’t. sophisticated data structures and are hard to interpret and hard to get machines to classify. Throw audio recordings in on that and you have a rather varied set of data types. Let’s talk about voice for a minute. In Book 4, I talked about Alexa being very good at translating voice to text but not so good at assigning meaning to the text. that people ask for things, make comments, and so on. Imagine, then, Alexa (and Amazon) keeping track of all the queries and then doing data science on them to ask for them. That is a lot of data and a lot of information that can be gathered. Not just for nefarious reasons, but to build a system that better services the con- sumer. It goes both ways. Data science has a much better chance of identifying patterns if the voice has been translated to text. It is much easier. However, in this translation you do lose a lot of information about tone of voice, emphasis, and so on. Velocity refers to how fast the data is changing and how fast it is being added to billion pictures a day, so in the next trillion - ity dataset. A low velocity dataset (not changing at all) may be the set of tempera-
  • 80. 432 BOOK 5 This is a very complex topic. Data scientists have developed many methods for processing data with variations of the three V’s. The three V’s describe the dataset and give you an idea of the parameters of your particular set of data. The process of gaining insights in data is called data analytics. In the next chapters, we focus on gaining knowledge about analytics and on learning how to ask some data ana- lytics questions using Python. After doing data science for a few years, you will be VVVery good at managing these. We generally can break down the process of doing science on data (especially big for the complexity of the tasks. These steps are and more techniques are being develop to do data analysis on big data (not surprisingly Currently, data science generally refers to the process of working out insights from large datasets of unstructured data. This means using predicative analytics, statistics and machine learning to wade through the mass of data. sets of data to achieve insights on that data. With these somewhat vague descriptions, you can see how the two areas are moving nitely call data analytics a subset of data science.
  • 81. The Five Areas of Data Science CHAPTER 1 433 1. Capture the data 2. Process the data 3. Analyze the data 4. Communicate the results 5. To have something to do analysis on, you have to capture some data. In any real- world situation, you probably have a number of potential sources of data. Inven- tory them and decide what to include. Knowing what to include requires you to the upcoming analysis. Sometimes your goals can be vague in that sometimes, “you just want to see what you can get” out of the data. If you can, integrate your data sources so it is easy to get to the information you In my humble opinion, this is the part of data science that should be easy, but it almost never is. I’ve seen data scientists spend months massaging their data so they can process and trust the data. You need to identify anomalies and outliers, - sistent. And all this has to be done appropriately so as not to take out data that is important to your upcoming analysis work. It’s not easy to do in many cases. If you have house room temperatures that are 170 degrees C, it is easy to see that this data is wrong and inconsistent. (Well, unless your house is burning down.) Cleaning and processing your data needs to be done carefully or else you will bias and maybe destroy the ability to do good inferences or get good answers down the line. In the real world, expect to spend a lot of time doing this step. Oh, and one more cleaning thing to worry about, budding data scientist con- sumers are giving more and more false and misleading data online. According to Marketing Week in 2015, 60 percent of consumers provide intentionally incorrect information when submitting data online. We humbly admit to doing this all the time to online marketing forms and even to political pollsters, especially when we sense a political agenda in the questions. Bad boys we are.
  • 82. 434 BOOK 5 Understand that it only takes a very small amount of disproportionate informa- tion to dramatically devalue a database. More food for thought. By the time you have expended all the energy to get to actually looking at the data - atively simple. It is not. Analyzing big datasets for insights and inferences or even asking complex questions is the hardest challenge, one that requires the most human intuition in all of data science. Some questions, like “What is the average huge amounts of data. But then you have the really, really useful questions such A question such as that has layers and layers of complexity behind it. You want a more people. Do you really mean more people, or do you mean more revenue? Change the price to $0.01 per box, is already more complex. That is the hard part of analysis: Making sure we are asking the right question in the right way of the right kind of data. Analyzing the data requires skill and experience in statistics techniques like linear using a variety of probability algorithms and formulas such as the incredibly coolly named “Naïve Bayes” formulas and concepts. Although a full discussion of these techniques is out of the scope of this book, we go through some examples later. After you have crunched and mangled your data into the format you need and then have analyzed the data to answer your questions, you need to present the results to management or the customer. Most people visualize information better and faster when they see it in a graphical format rather than just in text. There are two major Python packages that data science people us: The language “R” and
  • 83. The Five Areas of Data Science CHAPTER 1 435 MatPlotLib. We use MatPlotLib in displaying our “big data graphics.” (If you have read the chapters on AI (Book 4), then you have already experienced MatPlotLib This is the step in data science that everyone ignores. After you have asked your just basically shut down and walk away to the next project. The problem with that way of thinking is that there is a very reasonable chance that you will have to ask more questions of the same data, sometimes quite far in the future. Is important to archive and document the following information so you can restart the project quickly, or even more likely in the future you will run across a similar set of prob- Take time to preserve: » The data and sources » » The queries and results you got from the queries
  • 85. CHAPTER 2 437 Exploring Big Data In this chapter we get into some of the tools and processes used by data scien- tists to format, process, and query their data. There are a number of Python-based tools and libraries (such as “R”) available, but we decided to use NumPy for three reasons. First, it is one of the two most popular tools to use for data science in Python. Second, many AI-oriented proj- ects use NumPy (such as the one in our last chapter). And third, the highly useful Python data science package, Pandas, is built on NumPy. Pandas is turning out to be a very important package in data science. The way it encapsulates data in a more abstract way makes it easier to manipulate, docu- ment, and understand the transformations you make in the base datasets. Finally, MatPlotLib is a good visualization package for the results of big data. It’s - ever, this has been ameliorated to some degree by new add-on packages, such as “seaborne.” All in all, these are reasonable packages to attack the data science problem and get » » » »
  • 86. 438 BOOK 5 key Python packages keep coming up: » NumPy » Pandas » MatPlotLib These are discussed in the next few sections. NumPy adds big data-manipulation tools to Python such as large-array manip- ulation and high-level mathematical functions for data science. NumPy is best at excels at the creation and manipulation of multidimensional arrays known as ten- sors or matrices. In Book 4, we used NumPy extensively in manipulating data and tensors in neural networks and machine learning. It is an exceptional tool for There are numerous good tutorials for NumPy on the web. A selection of some of good step-by-step ones are: » https://www.machine learningplus.com/python/numpy-tutorial-part1-array-python- examples/): A good introduction to matrices (also known as tensors) and » https://www.tutorialspoint.com/numpy): A nice » https://www.guru99.com/ numpy-tutorial.html): Less theory, but a bunch of great examples to then performs various matrix-oriented operations on the maxtrix: import numpy as np x = np.array([[1,2],[3,4]])
  • 87. Exploring Big Data with Python CHAPTER 2 439 print(np.sum(x)) # Compute sum of all elements; prints "10" print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]" print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]" Python is great for munging data and preparing data, but not so great for data with relational or labeled data more intuitive. In our opinion, it is the fundamental building block for doing real-world data analysis in Python. It performs well with tabular type of data (such as SQL tables or Excel spreadsheets) and is really good with time-series data (like, say, temperatures taken on a hourly basis). Remember our discussion on data massaging? Dealing with missing or bad data? This is one of things that Pandas is designed for and does really well. It also allows for complex hierarchical data structures, which can be accessed using Pan- das functions in a very intuitive way. You can merge and join datasets as well as convert many types of data into the ubiquitous Pandas data objects, DataFrames. Pandas is based on NumPy and shares the speed of that Python library, and it can achieve a large increase of speed over straight Python code involving loops. Pandas DataFrames are a way to store data in rectangular grids that can easily be overviewed. A DataFrame can contain other DataFrames, a one-dimensional Book 4 on neural networks and machine learning), and dictionaries for tensors and matrices. Besides data, you can also specify indexes and column names for your DataFrame. This makes for more understandable code for data analysis and manipulation. You can access, delete, and rename your DataFrame components as you bring in more structures and join more related data into your DataFrame structure. MatPlotLib is a library that adds the missing data visualization functions to Python. It is designed to complement the use of NumPy in data analysis and - gramming interface) for embedded plots into applications using general-purpose GUI interfaces. For those familiar with MatLab, MatPlotLib provides a procedural version called PyLab.
  • 88. 440 BOOK 5 With MatPlotLib, you can make elaborate and professional-looking graphs, and you can even build “live” graphs that update while your application is running. This can be handy in machine-learning applications and data-analysis applica- tions, where it is good to see the system making progress towards some goal. Time for us to put NumPy and Pandas to work on a simple data science project. I am going to choose our dataset from the website Kaggle.com. Kaggle, whose tag and use the data under very open licenses, in most cases. Kaggle also supports a robust set of competitions for solving machine-learning problems, often posted by companies that really need the solution. I chose the “diamonds” database from Kaggle.com because it has a fairly simple computer to use. You can download it at https://www.kaggle.com/shivam2503/ diamonds. Using Kaggle will require you to register and sign in to the community, but does not cost anything to do so. The metadata (metadata is data describing data, hence metadata) consists of ten Column Header Type of Data Description Index counter Numeric carat Numeric Carat weight of the diamond cut Text Good, Very Good, Premium, Ideal color Text Color of the diamond, with D being the best and J the worst
  • 89. Exploring Big Data with Python CHAPTER 2 If you were to use this as a training set for a machine-learning program, you would see a program using NumPy and TensorFlow very similar to the one we show you in Book 4. In this chapter, we are going to show you a set of simple pandas-based data analysis to read our data and ask some questions. - data. I am sticking with DataFrames in this example because DataFrames makes If you are installing NumPy and pandas on the Raspberry Pi, use these commands: sudo apt-get install python3-numpy sudo apt-get install python3-pandas Now it is time for an example. FirstDiamonds.py and enter the following code: # Diamonds are a Data Scientist's Best Friend #import the pandas and numpy library import numpy as np import pandas as pd # read the diamonds CSV file Column Header Type of Data Description clarity Text How obvious inclusions are within the diamond: (in order from best to depth Numeric Depth %: The height of a diamond, measured from the culet to the table, divided by its average girdle diameter table Numeric Table %: The width of the diamond’s table expressed as a percentage of its average diameter price Numeric The price of the diamond x Numeric Length mm y Numeric Width mm x Numeric Depth mm
  • 90. BOOK 5 # build a DataFrame from the data df = pd.read_csv('diamonds.csv') print (df.head(10)) print() # calculate total value of diamonds sum = df.price.sum() print ("Total $ Value of Diamonds: ${:0,.2f}".format( sum)) # calculate mean price of diamonds mean = df.price.mean() print ("Mean $ Value of Diamonds: ${:0,.2f}".format(mean)) # summarize the data descrip = df.carat.describe() print() print (descrip) descrip = df.describe(include='object') print() print (descrip) Making sure you have the diamonds.csv command: python3 FirstDiamonds.py And you should see the following results: Unnamed: 0 carat cut color clarity depth table price x y z 0 1 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43 1 2 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31 2 3 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 3 4 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63 4 5 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75 5 6 0.24 Very Good J VVS2 62.8 57.0 336 3.94 3.96 2.48 6 7 0.24 Very Good I VVS1 62.3 57.0 336 3.95 3.98 2.47
  • 91. Exploring Big Data with Python CHAPTER 2 443 7 8 0.26 Very Good H SI1 61.9 55.0 337 4.07 4.11 2.53 8 9 0.22 Fair E VS2 65.1 61.0 337 3.87 3.78 2.49 9 10 0.23 Very Good H VS1 59.4 61.0 338 4.00 4.05 2.39 Total $ Value of Diamonds: $212,135,217.00 Mean $ Value of Diamonds: $3,932.80 count 53940.000000 mean 0.797940 std 0.474011 min 0.200000 25% 0.400000 50% 0.700000 75% 1.040000 max 5.010000 Name: carat, dtype: float64 cut color clarity count 53940 53940 53940 unique 5 7 8 top Ideal G SI1 freq 21551 11292 13065 That’s a lot of data for a short piece of code! # Diamonds are a Data Scientist's Best Friend First, we import all the needed libraries: #import the pandas and numpy library import numpy as np import pandas as pd Read the diamonds # read the diamonds CSV file # build a DataFrame from the data df = pd.read_csv('diamonds.csv')
  • 92. 444 BOOK 5 print (df.head(10)) print() get to use the column as part of the DataFrame object. It’s great that you can do this with Python! # calculate total value of diamonds sum = df.price.sum() print ("Total $ Value of Diamonds: ${:0,.2f}".format( sum)) # calculate mean price of diamonds mean = df.price.mean() print ("Mean $ Value of Diamonds: ${:0,.2f}".format(mean)) data about carat. # summarize the data descrip = df.carat.describe() print() print (descrip) This next statement prints out a description for all the nonnumeric columns in descrip = df.describe(include='object') print() print (descrip) To install MatPlotLib on your Raspberry Pi, type pip3 install matplotlib. Now we move to the data visualization of our data with MatPlotLib. In Book 4 we use MatPlotLib to draw some graphs related to the way our machine-learning program improved its accuracy during training. Now we use MatPlotLib to show some interesting things about our dataset.
  • 93. Exploring Big Data with Python CHAPTER 2 445 For these programs to work, you need to be running them from a terminal window Raspberry Pi headless. One of the really useful things about pandas and MatPlotLib is that the NumPy and DataFrame types are very compatible with the required graphic formats. They are all based on matrices and NumPy arrays. Plot_Clarity VSCarat.py and enter the following code: # Looking at the Shiny Diamonds #import the pandas and numpy library import numpy as np import pandas as pd import matplotlib.pyplot as plt # read the diamonds CSV file # build a DataFrame from the data df = pd.read_csv('diamonds.csv') import matplotlib.pyplot as plt carat = df.carat clarity = df.clarity plt.scatter(clarity, carat) plt.show() # or plt.savefig("name.png") Run your program. Now, how is that for ease in plotting? Pandas and MatPlotLib go hand-in-hand. Remember that diamond clarity is measured by how obvious inclusions (see One would be tempted to make a statement that the largest diamonds are rated as and so you really can’t draw such general conclusions. All you can say is that “In this dataset, the clarity ‘IL’ has the largest diamonds.”
  • 94. 446 BOOK 5 Plot_Count Clarity.py and enter the following code: # Looking at the Shiny Diamonds #import the pandas and numpy library import numpy as np import pandas as pd import matplotlib.pyplot as plt # read the diamonds CSV file # build a DataFrame from the data df = pd.read_csv('diamonds.csv') import matplotlib.pyplot as plt # count the number of each textual type of clarity clarityindexes = df['clarity'].value_counts().index.tolist() claritycount= df['clarity'].value_counts().values.tolist() print(clarityindexes) print(claritycount) plt.bar(clarityindexes, claritycount) plt.show() # or plt.savefig("name.png") Diamond clarity (horizontal) versus carat size
  • 95. Exploring Big Data with Python CHAPTER 2 447 Again, remember that diamond clarity is measured by how obvious inclusions are diamonds in our diamond database. most represented in our diamond dataset. I looked at clarity, now let’s look at color type in our pile of diamonds. Using nano Plot_CountColor.py and enter # Looking at the Shiny Diamonds #import the pandas and numpy library import numpy as np import pandas as pd import matplotlib.pyplot as plt # read the diamonds CSV file # build a DataFrame from the data df = pd.read_csv('diamonds.csv') import matplotlib.pyplot as plt Diamond clarity count in each
  • 96. 448 BOOK 5 # count the number of each textual type of color colorindexes = df['color'].value_counts().index.tolist() colorcount= df['color'].value_counts().values.tolist() print(colorindexes) print(colorcount) plt.bar(colorindexes, colorcount) plt.show() # or plt.savefig("name.png") Run your program. colorless. The general rule is less color, higher price. The exceptions to this are the pinks and blues, which are outside of this color mapping and sample. The last plot I am going to show you is called a heat plot. It is used to graphically show correlations between numeric values inside our database. In this plot we take all the numerical values and create a correlation matrix that shows how closely they correlate with each other. To quickly and easily generate this graph, we use another library for Python and MatPlotLib called seaborn. Seaborn provides an API built on top of MatPlotLib that integrates with pandas DataFrames, which makes it ideal for data science. Diamond color count in each
  • 97. Exploring Big Data with Python CHAPTER 2 449 If you don’t already have seaborn on your Raspberry Pi (and if you have installed MatPlotLib, you probably already do). Run the example Python program Plot_ Heat.py sudo apt-get install python3-seaborn Plot_Heat.py and enter the following code: # Looking at the Shiny Diamonds #import the pandas and numpy library import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # read the diamonds CSV file # build a DataFrame from the data df = pd.read_csv('diamonds.csv') # drop the index column df = df.drop('Unnamed: 0', axis=1) f, ax = plt.subplots(figsize=(10, 8)) corr = df.corr() print (corr) sns.heatmap(corr, mask=np.zeros_like(corr, dtype=np.bool), cmap=sns.diverging_palette(220, 10, as_cmap=True), square=True, ax=ax) plt.show() the correlation between the two variables. The diagonal stripe from top left to top - prise there. The x, y, and z variables quite correlate with each other, which says that as the diamonds in our database increase in one dimension, they increase in the other two dimensions as well. Interestingly, depth (The height of a diamond, measured from the culet to the table, divided by its average girdle diameter) does not correlated very strongly at all with price and in fact is somewhat negatively correlated.
  • 98. 450 BOOK 5 maps are fabulous for spotting general cross-correlations in your data. It would be interesting to see the correlation between color/clarity and price. Why isn’t it on this chart? This is because those columns are textual, and you can do heat chart again. The same technique can be used on diamond clarity. Correlation
  • 99. CHAPTER 3 451 Using Big Data from U p to this point, we have been dealing with some relatively small sets of - analysis work. What Is Big Data? Big data - » » » »
  • 100. 452 BOOK 5 Python and Pandas and then visualizing the results on a Raspberry Pi. -
  • 101. Using Big Data from the Google Cloud CHAPTER 3 453 - pages, there are layers and layers of software under that, doing things like trans- won’t even have to pay at all during your trial. start to analyze almost immediately. We would be remiss if we didn’t talk just a little bit about maintaining good
  • 102. 454 BOOK 5 .json following steps will show you how to do this: 1. https://console.developers.google.com/ Medicare is the national health insurance program (single payer) in the United States administered by the Centers for Medicare and Medicade Services (CMS). It provides health insurance for Americans aged 65 and over. It also provides health insurance to younger people with certain disabilities and conditions. In 2017 it provided health insur- ance to over 58 million individuals. With 58 million individuals in the system, Medicare is generating a huge amount of big data every year. Google and CMS teamed up to put a large amount of this data on the BigQuery public database so you can take a peek at this data and do some analytics without trying to load it all on your local machine. A home computer, PC or Raspberry Pi, won’t hold all the data available.
  • 103. Using Big Data from the Google Cloud CHAPTER 3 455 2. screen. 3. 4. MedicareProject 5. MedicareProject Make sure you don’t leave this on the default “My Project” selection. Make sure you change it to MedicareProject and authentication for the wrong project. This is an easy mistake to make. 6. MedicareProject 7. BigQuery The Select a Project page on the Google Cloud.
  • 104. 456 BOOK 5 8. 9. 10. . 11. MedicareProject 12. A message appears saying that the service account and key has been created. MedicareProject-1223xxxxx413.json” is downloaded to your computer. 13. First credential screen.
  • 105. Using Big Data from the Google Cloud CHAPTER 3 457 for analysis. There are several dozen datasets available now and there will be more Second credential screen. Column Type Description provider_id STRING outpatient hospital services. provider_name STRING The name of the provider. provider_street_ address STRING The street address in which the provider is physically located. provider_city STRING The city in which the provider is physically located. (continued)
  • 106. 458 BOOK 5 then save it as MedicareQuery1.py: import pandas as pd from google.cloud import bigquery # set up the query QUERY = """ SELECT provider_city, provider_state, drg_definition, average_total_payments, average_medicare_payments FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015` WHERE provider_city = "GREAT FALLS" AND provider_state = "MT" Column Type Description provider_state STRING The state in which the provider is physically located. provider_zipcode INTEGER The zip code in which the provider is physically located. drg_definition STRING (diagnoses) and the procedures furnished by the hospital during the stay. hospital_referral_ region_description STRING The hospital referral region (HRR) in which the provider is physically located. total_discharges INTEGER The number of discharges billed by the provider for inpatient hospital services. average_covered_ charges FLOAT The provider’s average charge for services covered by Medicare average_total_payments FLOAT The average total payments to all providers for the MS-DRG including the MSDRG amount, teaching, disproportionate share, average total payments are co-payment and deductible amounts that the patient is responsible for and any additional payments average_medicare_ payments FLOAT The average amount that Medicare pays to the provider payment amounts include the MS-DRG amount, teaching, disproportionate share, capital, and outlier payments for all cases. Medicare payments do not co-payments and deductible amounts nor any additional TABLE (continued)
  • 107. Using Big Data from the Google Cloud CHAPTER 3 459 ORDER BY provider_city ASC LIMIT 1000 """ client = bigquery.Client.from_service_account_json( 'MedicareProject2-122xxxxxf413.json') query_job = client.query(QUERY) df = query_job.to_dataframe() print ("Records Returned: ", df.shape ) print () print ("First 3 Records") print (df.head(3)) MedicareProject2-122xxxxxf413. json - window on the Raspberry Pi: pip3 install google-cloud-bigquery SQL SQL (Structured Query Language) is a query-oriented language used to interface with databases and to extract information from those databases. Although it was designed for relational database access and management, it has been extended to many other types of databases, including the data being accessed by BigQuery and the Google Cloud. Here are some excellent tutorials to get your head around how to access data using SQL: • https://www.w3schools.com/sql/ • http://www.sql-tutorial.net/ • SQL For Dummies • SQL All In One For Dummies 3rd Edition, • SQL in 10 Minutes, Ben Forta
  • 108. BOOK 5 google.cloud library and the bigquery import: import pandas as pd from google.cloud import bigquery - # set up the query QUERY = """ SELECT provider_city, provider_state, drg_definition, average_total_payments, average_medicare_payments FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015` WHERE provider_city = "GREAT FALLS" AND provider_state = "MT" ORDER BY provider_city ASC LIMIT 1000 """ SELECT FROM the database bigquery-public-data.cms_medicare. inpatient_charges_2015 only WHERE the provider_city is GREAT FALLS and the provider_state is MT provider_city is somewhat redundant. json one won’t work. client = bigquery.Client.from_service_account_json( 'MedicareProject2-122xxxxxef413.json') query_job = client.query(QUERY) df = query_job.to_dataframe()
  • 109. Using Big Data from the Google Cloud CHAPTER 3 461 print ("Records Returned: ", df.shape ) print () print ("First 3 Records") print (df.head(3)) Run your program using python3 MedicareQuery1.py and you should see results Records Returned: (112, 5) First 3 Records provider_city provider_state drg_ definition average_total_payments average_medicare_payments 0 GREAT FALLS MT 064 - INTRACRANIAL HEMORRHAGE OR CEREBRAL INFA... 11997.11 11080.32 1 GREAT FALLS MT 039 - EXTRACRANIAL PROCEDURES W/O CC/MCC 7082.85 5954.81 2 GREAT FALLS MT 065 - INTRACRANIAL HEMORRHAGE OR CEREBRAL INFA... 7140.80 6145.38 Visualizing your Data - inpatient_charges_2015 dataset looking for
  • 110. 462 BOOK 5 import pandas as pd from google.cloud import bigquery # set up the query QUERY = """ SELECT provider_city, provider_state, drg_definition, average_total_payments, average_medicare_payments FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015` WHERE drg_definition LIKE '554 %' ORDER BY provider_city ASC LIMIT 1000 """ client = bigquery.Client.from_service_account_json( 'MedicareProject2-1223283ef413.json') query_job = client.query(QUERY) df = query_job.to_dataframe() ICD10 is the well-established method for coding medical professional diagnoses for - tory in 2015 with great angst throughout the medical community. It consists of, at its Fracture of Greater Toe. These codes are somewhat merged into the MS_DRG codes that are used in the Medicare databases we examine here as they are used for hospital admissions. John Shovic had a medical software startup that used ICD 10 codes for ten years, and he got to have a love/hate relationship with these codes. His favorite ICD-10 codes: • • Z63.1: Problems in relationship with in-laws. • • •
  • 111. Using Big Data from the Google Cloud CHAPTER 3 463 print ("Records Returned: ", df.shape ) print () print ("First 3 Records") print (df.head(3)) LIKE '554 %' Running the program gets these results: Records Returned: (286, 5) First 3 Records provider_city provider_state drg_definition average_total_payments average_medicare_payments 0 ABINGTON PA 554 - BONE DISEASES & ARTHROPATHIES W/O MCC 5443.67 3992.93 1 AKRON OH 554 - BONE DISEASES & ARTHROPATHIES W/O MCC 5581.00 4292.47 2 ALBANY NY 554 - BONE DISEASES & ARTHROPATHIES W/O MCC 7628.94 5137.31 MedicareQuery3.py import pandas as pd from google.cloud import bigquery # set up the query QUERY = """ SELECT provider_city, provider_state, drg_definition, average_total_payments, average_medicare_payments FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015` WHERE drg_definition LIKE '554 %' ORDER BY provider_city ASC LIMIT 1000 """ client = bigquery.Client.from_service_account_json( 'MedicareProject2-1223283ef413.json') query_job = client.query(QUERY) df = query_job.to_dataframe()
  • 112. 464 BOOK 5 print ("Records Returned: ", df.shape ) print () total_payment = df.average_total_payments.sum() medicare_payment = df.average_medicare_payments.sum() percent_paid = ((medicare_payment/total_payment))*100 print ("Medicare pays {:4.2f}% of Total for 554 DRG".format(percent_paid)) print ("Patient pays {:4.2f}% of Total for 554 DRG".format(100-percent_paid)) Records Returned: (286, 5) Medicare pays 77.06% of Total for 554 DRG Patient pays 22.94% of Total for 554 DRG MedicareQuery4.py: import pandas as pd from google.cloud import bigquery # set up the query QUERY = """ SELECT provider_city, provider_state, drg_definition, average_total_payments, average_medicare_payments FROM `bigquery-public-data.cms_medicare.inpatient_charges_2015` WHERE drg_definition LIKE '554 %' ORDER BY provider_city ASC LIMIT 1000 """ client = bigquery.Client.from_service_account_json( 'MedicareProject2-1223283ef413.json') query_job = client.query(QUERY) df = query_job.to_dataframe()
  • 113. Using Big Data from the Google Cloud CHAPTER 3 465 print ("Records Returned: ", df.shape ) print () # find the unique values of State states = df.provider_state.unique() states.sort() total_payment = df.average_total_payments.sum() medicare_payment = df.average_medicare_payments.sum() percent_paid = ((medicare_payment/total_payment))*100 print("Overall:") print ("Medicare pays {:4.2f}% of Total for 554 DRG".format(percent_paid)) print ("Patient pays {:4.2f}% of Total for 554 DRG".format(100-percent_paid)) print ("Per State:") # now iterate over states print(df.head(5)) state_percent = [] for current_state in states: state_df = df[df.provider_state == current_state] state_total_payment = state_df.average_total_payments.sum() state_medicare_payment = state_df.average_medicare_payments.sum() state_percent_paid = ((state_medicare_payment/state_total_payment))*100 state_percent.append(state_percent_paid) print ("{:s} Medicare pays {:4.2f}% of Total for 554 DRG".format (current_state,state_percent_paid)) Medicare Query4.py # we could graph this using MatPlotLib with the two lists # but we want to use DataFrames for this example data_array = {'State': states, 'Percent': state_percent}
  • 114. 466 BOOK 5 df_states = pd.DataFrame.from_dict(data_array) # Now back in dataframe land import matplotlib.pyplot as plt import seaborn as sb print (df_states) df_states.plot(kind='bar', x='State', y= 'Percent') plt.show() MedicareQuery4. py sudo apt-get install python3-seaborn hourly, believe it or not. Bar chart of Medicare % paid
  • 115. Using Big Data from the Google Cloud CHAPTER 3 467 measured by air quality: import pandas as pd from google.cloud import bigquery # sample query from: QUERY = """ SELECT location, city, country, value, timestamp FROM `bigquery-public-data.openaq.global_air_quality` WHERE pollutant = "pm10" AND timestamp > "2017-04-01" ORDER BY value DESC LIMIT 1000 """ client = bigquery.Client.from_service_account_json( 'MedicareProject2-1223283ef413.json') query_job = client.query(QUERY) df = query_job.to_dataframe() print (df.head(3)) location city country value timestamp 1 Bukhiin urguu Ulaanbaatar MN 1428.00 2019-01-21 17:00:00 00:00 2 Chaiten Norte Chaiten Norte CL 999.83 2018-04-24 11:00:00 00:00 mainly due to intense industrialization.
  • 118. Contents at a Glance Introduction to Physical Computing. . . . . . . . . . . . . 471 Physical Computing Is Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 What Is a Raspberry Pi? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Making Your Computer Do Things . . . . . . . . . . . . . . . . . . . . . . . . . . 474 Using Small Computers to Build Projects That Do and Sense Things. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 Computing in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Controlling the LED with Python on the Raspberry Pi . . . . . . . . . . 482 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 No Soldering! Grove Connectors for Building Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 So What Is a Grove Connector? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 Selecting Grove Base Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 The Four Types of Grove Connectors. . . . . . . . . . . . . . . . . . . . . . . . 492 The Four Types of Grove Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Using Grove Cables to Get Connected . . . . . . . . . . . . . . . . . . . . . . . 499 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 Understanding I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 A Fun Experiment for Measuring Oxygen and a Flame . . . . . . . . . 517 Building a Dashboard on Your Phone Using Blynk and Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 Making Things Move with Python. . . . . . . . . . . . . . . . 537 Exploring Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 Controlling Motors with a Computer . . . . . . . . . . . . . . . . . . . . . . . . 540
  • 119. CHAPTER 1 Introduction to Physical Computing 471 Introduction to Physical Computing W e have been talking about how to program in Python for the last several hundred pages in this book. It is now time to use our newly acquired Python skills to start doing things in the real world. We call this physical computing — making a computer interact with the world around you! about the hardware. That’s why this book is mostly focused on learning how to program computers with Python. But now it is time to learn how to make your computers do something with Python. In this chapter, we hook up various sensors and motors to a Raspberry Pi computer. to people, hooking up things incorrectly can burn out your computer or your sen- sors. For this reason, follow these two rules assiduously: » Rule 1: Turn all » Rule 2: IN THIS CHAPTER » Discovering how to use a Raspberry Pi » Understanding how to use small computers » Using a Raspberry Pi to sense the environment around you » Making your computer do physical things
  • 120. 472 BOOK 6 Talking to Hardware with Python Physical Computing Is Fun One reason that we want to you to learn about physical computing is that little embeddedsystems around you. And we mean everywhere. Go up to your kitchen. Look around. Your refrigerator has a computer, maybe two or three if it has a display. Your blender has a computer. Your oven has a computer. Your microwave has a computer. If you use Phillips Hue lights in your house, your light bulbs have a computer. Your car will have upwards of 20 computers in the vehicle. One more example. How about the lowly toaster? If you have a “Bagel Button” or a display on your toaster, you have a computer in there. Why? Why are there so all your gadgets using a computer than it is to design special hardware. Do you Most of these computers are much simpler, slower, and carrying much less RAM equal to one character in English. In most Asian countries, one character equals two bytes. So computers are everywhere. But the interesting thing is that all these little computers are doing physical computing. They are sensing and interacting with the environment. The refrigerator computer is checking to see whether it is at the right temperature, and if it is not, it turns on the cooling machinery, paying attention to what it is doing to minimize the amount of electricity it uses. The stove is updating your display on the front panel, monitoring the buttons and dials and controlling the temperature so you get a good lasagna for dinner. All of these interactions and controllers are called physical computing. What Is a Raspberry Pi? In this book, we could use one of these very small computers but the functionality
  • 121. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing since about 2012. It was created by the Raspberry Pi Foundation to teach basic science and engineering in schools around the world. It turned out to be wildly popular and has sold more than 19 million computers around the world. There a To demystify some of the technology that we deal with every day, let’s talk about the major blocks of hardware on this computer. Remember, your smartphone has computers inside that are very similar in terms of structure to the Raspberry Pi. » GPIO connector: » CPU/GPU: » USB: » Ethernet: » WiFi: » HDMI Out: » Audio jack: » Other ports: • Micro USB: • Camera CSI: • Display DSI:
  • 122. 474 BOOK 6 Talking to Hardware with Python Making Your Computer Do Things In order to get our computer to do and sense things apart from the computer or an actuator. A sensor is a small piece of electronics that can detect something actuator is a fancy word for a motor or cable that does things in the real world. In the remainder of this chapter, we are going to learn about the necessary ingre- is the physical computing version of “Hello World” that we all do when we are learning software. Blinking LED, here we come! grabbing a mouse, keyboard, and monitor to do the set-up for beginners, but Again, the best place to start is with www.raspberrypi.org. Using Small Computers to Build Projects That Do and Sense Things Earlier in this chapter, we talked about computers in the kitchen. All those com-
  • 123. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing controlled by a computer. after you get acquainted with the hardware, you are going to be able to design Then, in further chapters, we build more complex things that will be the launch- ing point to your own projects, all programmed in Python! One last remark before we move on. The software you will be writing in Python is the key to getting all these projects and computers working. Is the hardware think the software is the more important part, and the easier part to learn for beginners. Now before that statement unleashes a hundred nasty emails, let me humbly acknowledge that none, and we mean none, of this is possible if it wasn’t is a book on Python! WHAT ARE SOME OF THE OTHER SMALL COMPUTERS AVAILABLE? www.raspberrypi.org (continued)
  • 124. BOOK 6 Talking to Hardware with Python The Raspberry Pi: A Perfect Platform By now you have your Raspberry Pi computer set up and running on your www. raspberrypi.org computer to work with! The Raspberry Pi is the perfect platform to do physical computing with Python because it has a multiscreen environment, lots of RAM and storage to play with and all the tools to build the projects we want. We have been talking a lot about the computers in this chapter and not much about Python. Time to change that. (continued)
  • 125. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing 477 - into which we can plug a large number of sensors and controllers to do amazing things to expand your Raspberry Pi. GPIO pins An GPIO pin output pin “outputs” a 1 or a 0 from the computer to the pin. See next GPIO libraries There are a number of GPIO Python libraries that are usable for building projects. The one we use throughout the rest of this book is the gpiozero library that is installed on all Raspberry Pi desktop software releases. The library documenta- https://gpiozero. readthedocs.io/en/stable/. Now we are going to jump into the “Hello World” physical computing project with our Raspberry Pi.
  • 126. 478 BOOK 6 Talking to Hardware with Python The hardware for “Hello World” To do this project, we need some hardware. Because we are using Grove connec- hardware that we need for this project: » Pi2Grover: » Grove blue LED: Assembling the hardware physical computer based product. because of this, we’ll give you the step-by-step process:
  • 127. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing 479 1. Identify the Pi2Grover board from Figure 1-3 above. 2. Making sure you align the pins correctly gently press the Pi2Grover 1-6
  • 128. 480 BOOK 6 Talking to Hardware with Python 3. pins, making sure the pins are aligned. There will be no pins showing on Figure 1-7 Figure 1-8
  • 129. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing 481 5. If your blue LED is not plugged into the Grove blue LED board, then plug the board as in Figure 1-9. 6. Plug the other end of the Grove cable into the slot marked D12/D13 on
  • 130. 482 BOOK 6 Talking to Hardware with Python Controlling the LED with Python on the Raspberry Pi Now that we have the hardware all connected, we can apply the power to the Raspberry Pi. If all is well, then you will see your Grove blue LED light up, a blue The Grove blue LED lights up when we turn the Raspberry Pi power on because the GPIO pins on the Raspberry Pi power up as inputs. Because it is an input and and so the LED will turn on. When you turn your GPIO pin to an output in the code To get started, follow these steps: 1. Go to your keyboard and open up a terminal window. https://www.raspberrypi.org/ documentation/usage/terminal/
  • 131. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing 2. HelloWorld.py. from gpiozero import LED from time import sleep blue = LED(12) while True: blue.on() print( "LED On") sleep(1) blue.off() print( "LED Off") sleep(1) https://www.raspberrypi.org/ magpi/edit-text/ 3. Now the big moment. Start your program by running this on the command line your terminal window: sudo python3 HelloWorld.py appear on the screen in the terminal window: LED On LED Off LED On LED Off LED On LED Off LED On LED Off LED On LED Off LED On The keyword sudo stands for super user do. We use sudo in front of the python3 command in this type of code because some versions of the Raspberry Pi operating system restricts access to certain pins and functions from a regular user. By using sudo, we are running this as a super user. This means it will run no matter how the particular version of the operating system is set up. In the newer versions of the Raspberry Pi OS, you can just type python3 HelloWorld.py and it will work. If it doesn’t, go back to sudo python3 HelloWorld.py.
  • 132. 484 BOOK 6 Talking to Hardware with Python In the code, the following statement imports the function LED from the Python gpiozero library: from gpiozero import LED This statement imports the function sleep from the Python time library: from time import sleep blue = LED(12) Now you start the loop that will go on forever: while True: Turn the LED on: blue.on() print( "LED On") sleep(1) blue.off() print( "LED Off") sleep(1) Rinse and repeat. Wow, you have now entered the world of physical computing. Just wait until you
  • 133. Introduction to Physical Computing CHAPTER 1 Introduction to Physical Computing Because we have all this hardware set up, how about we do one more interesting Pulse-width modulation (PWM) is a technique by which you vary the amount of time a signal is at a 1 versus the amount of time the signal is at a 0. Because our LED a 0 then we can control the brightness to the human eye. This ratio is called the duty cycle time the signal is on will change the brightness of the LED. Enter this Python code into nano and save it as HelloWorld2.py: from gpiozero import PWMLED from time import sleep led = PWMLED(12) while True: led.value = 0 # off sleep(1) led.value = 0.5 # half brightness
  • 134. BOOK 6 Talking to Hardware with Python sleep(1) led.value = 1 # full brightness sleep(1) Now run the code: sudo python3 HelloWorld2.py You will see the brightness change every second. And one more thing, here is how to change your brightness in a continuous fashion: from gpiozero import PWMLED from signal import pause led = PWMLED(12) led.pulse() pause() With this code, we see a smooth continuous brightening and darkening of the LED. Boy, you accomplished a lot in this chapter. You have now started to see the possibilities of physical computing. And you have a blue LED! THE LED CHANGING IS NOT TOTALLY SMOOTH ps xaf
  • 135. CHAPTER 2 No Soldering! Grove Connectors for Building Things 487 No Soldering! Grove Connectors for Building Things O kay, okay. We all have been talking about Python for the past several hundred pages. Time to build something! But before we get to that, we need to talk about how to plug things together. Grove is a modular, standardized connecter prototyping system. Grove takes a building-block approach to assembling electronics. Compared to the jumper or solder-based system, it is easier to connect, experiment, and build, and it simpli- Some of the other prototype systems out there take the level down to building to build real systems. However, it requires some learning and expertise to hook things up. The Grove system consists of a base unit and various modules (with standardized connectors). IN THIS CHAPTER » Discovering how to plug hardware together » Avoiding the Box of Death! » Working with the four types of sensors » Understanding using Patch cables
  • 136. 488 BOOK 6 Talking to Hardware with Python The base unit, generally a microprocessor, allows for easy connection of any input or output from the Grove modules, and every Grove module typically addresses a single function, from a simple button to a more complex heart-rate sensor. You don’t need a base unit to connect up to Grove modules. You can use a cable (Grove-to-pin-header converter) to run from the pins on the Raspberry Pi or Arduino to the Grove connectors. See some examples of how to do this later in this chapter. So What Is a Grove Connector? Normally, when you’re wiring up a board, you have to pay attention. If you plug things in backwards or connect boards incorrectly, you can damage or destroy boards. All it takes is an incorrectly attached wire, and your board is gone forever. while taking no chances on hooking up power and ground incorrectly. A Grove connector is a four-pin standardized size connector used to plug into standardized connectors (common to all types of Grove connectors) are the key to making this system work. They are keyed so that you cannot plug them in backwards, and the four types of connectors (see “The Four Types of Grove Con- nectors,” later in this chapter) are all designed so that if you plug the wrong type of device into the wrong type of base unit, there is no problem. They aren’t destroyed; they just won’t work. This is a good thing, a very good thing. A Grove connector.
  • 137. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 489 avoid such situations by making sure everything we do is 5V! Selecting Grove Base Units A Grove base unit is a controller or shield to which you attach the Grove modules. sensors and output actuators of your system. For the Arduino We most talk about the Raspberry Pi in this book, but there are other computers out there too! Arduinos are one of the more popular ones. There are a number of good base unit shields available for the Arduino that provide a lot of Grove con- They are also available for the Arduino Mega, Due, and others. - nectors built right into the board so you don’t even need a base unit. The Arduino Uno Grove base board.
  • 138. 490 BOOK 6 Talking to Hardware with Python On the Raspberry Pi side, the pickings are much slimmer. The base unit devices available tend to be “too smart” and isolate you from the Raspberry Pi hardware and software. This is a huge problem when you want to connect to hardware using - ibility. You can still mask the complexity with software drivers. shifter (from the Raspberry Pi 3.3V to 5V for all the Grove sensors), and it does not The Arduino Mini Pro LB board with Grove.
  • 139. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things A TOAST OF WATER TO VOLTAGES V refers to voltage, which is similar to the water pressure in a pipe. The higher the pressure, the more water comes out. With voltage, the higher the voltage, the more current (like water) will come out of the pipe. If the water pressure is too high, it can break the pipe. Similarly, if the voltage is too high, you can break the computer input. Raspberry Pi’s are pretty particular about liking - thing, usually called ground. This is why grounds are so important to connect and to have a common ground so your voltages running around always know to what they are referenced. Not having a common ground in a system (thus confusing the voltages!) leads to very You can always trust your mother, but you can never trust your ground.” For those who may be interested in what the First Law of Shovic is, that one is a bit easier to understand. The First Law is “It works better if you plug it in!” The Pi2Grover board at work on the Raspberry Pi.
  • 140. BOOK 6 Talking to Hardware with Python The Four Types of Grove Connectors First of all, all Grove cables are physically identical and can be interchanged. The out power and ground by plugging in one type of Grove connector in the other. Although you do need to be careful and think about what you are doing, it is a lot less risky than soldering or using jumpers to wire up devices to your Pi or Arduino. ground. » Pin 1: Yellow (for example, SCL on I2C Grove connectors) » White (for example, SDA on I2C Grove connectors) » Pin 3: » Pin 4: Black (GND on all Grove connectors) I DON’T WANT TO USE A BASE UNIT! You do not have to have a hat or shield to use Grove with your Raspberry Pi or Arduino. All you need is to connect the I2C, digital, or analog inputs to the Grove devices by using a Grove-to-pin-header converter. cables.
  • 141. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 493 The Four Types of Grove Signals sensors and devices. It’s not hard, but pay attention. By using the wrong con- nector, you may not fry your board, but your project still may not work correctly! Many sensors only need one or two bits. A bit is the basis of all digital computer the bits are represented by voltage levels (see the discussion on Voltage earlier in “0” value. Computers often communicate with each other and with external devices by using digital bits. It turns out that there are two ways of getting information from bits. about later. A digital Grove connector consists of the standard four lines coming into the Grove use D0, but some (like the LED Bar Grove display) use both. Often base units will digital Grove connector. Examples of Grove digital modules are: Switch modules, the Fan module, and the the schematic for the LED Grove module. They range from the simple to the very complex. The Grove Digital Connector Pin Name Description Pin 1 - Yellow D0 Primary digital input/output Pin 2 - White D1 Secondary digital input/output Pin 4 - Black GND Ground
  • 142. 494 BOOK 6 Talking to Hardware with Python aren’t enough A Grove analog connector consists of the standard four lines coming into the Grove switch and of course, the voltage present across the green connector on the left A Grove analog simple voltage divider. The Grove Analog Connector Pin Name Description Pin 1 - Yellow A0 Primary analog input Pin 2 - White A1 Secondary analog input Pin 4 - Black GND Ground A simple digital Grove module with LED.
  • 143. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 495 Examples of Grove analog modules are: Potentiometer, voltage divider and a Grove air quality sensor. bit transmission Remember when we talked about digital signals? How you can convey informa- letter A. The speed at which the bit is sent is called a baud rate. (Baud comes from Emile Baudot, who was an inventor and scientist making great progress in the late uses the digital level and the timing of the signal to receive and transmit data. It called a serial interface) plug is labeled from the base unit’s point of view. In other The Grove UART Serial Connector Pin Name Description Pin 1 - Yellow RX Grove board’s) Pin 2 - White TX Grove board’s Pin 4 - Black GND Ground
  • 144. BOOK 6 Talking to Hardware with Python ANALOG VERSUS DIGITAL: THE DEBATE CONTINUES - by a digital port. Okay, okay. Enough about that. Let’s just treat signals for this book as digital or analog and leave it at that. Whew! An analog signal is used when it is important to know what voltage is present at the sig- is, it is important for us to know what the actual voltage number is. Later, we discuss how to read an analog voltage into a digital computer by converting the analog voltage into a digital number by the use of an ADC (analog-to-digital converter). Then our com- puter can tell whether the plant is dry or not! A Grove UART RFID reader.
  • 145. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 497 sense of the world plug and go! more! It not only calculates the visible sunlight strength, it also measures the can tell you whether you are going to get sunburned as well as if your plants are happy! You just have to love the things you can do these days with computers. clear top to let the light through to measure. the pin descriptions of the Grove connector. The Grove I2C sunlight sensor.
  • 146. 498 BOOK 6 Talking to Hardware with Python is the SDA signal. Power and ground are the same as the other connectors. This the 90s. Pin Name Description Pin 1 - Yellow SCL I2C clock Pin 2 - White SDA I2C data Pin 4 - Black GND Ground An I2C bus is often used to communicate with chips or sensors that are on the same licensing issues, sometimes the bus will be called TWI (two wire interface). SMBus, devel- I2C provides good support for slow, close peripheral devices that only need be addressed occasionally. For example, a temperate measuring device will generally only change very slowly and so is a good candidate for the use of I2C, whereas a camera will generate lots of data quickly and potentially changes often. I2C uses only two bidirectional open-drain lines, SCL (serial clock) and SDA (serial data). Kind of like two serial data lines next to each other. Open-drain means the I2C device pulses, very much like a dance between SDA and SCL. many devices on the same I2C bus, which is a very cool feature.
  • 147. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 499 Using Grove Cables to Get Connected the Raspberry Pi. These are easy. They come with a Grove connector on each end and are interchangeable. Grove cables also come as patch cables (between Grove and pins) and we talk about them next. Grove Patch Cables There always seems to be some kind of device or sensor that does not have Grove connectors and yet you want to use it in your system. The solution to this is to use a patch cable! It turns out there are easy ways of converting pin headers to Grove connectors using Grove adaptor cables. There are two types of Grove adaptor cables. One con- verts the Grove connector to female header pins The second type of Grove adaptor cables are Grove-connector-to-male-header- 20cm Grove cables.
  • 148. 500 BOOK 6 Talking to Hardware with Python The power of the patch cable is that you can connect to non-Grove sensors. Basically, you map the Grove connector to your pin headers. Be careful and make sure you check twice before applying power! How you map depends on what kind of a sensor you have and what the interface is. Grove connectors support four kinds of interfaces as we talk about earlier in this chapter. Grove female header cables. A close-up of a Grove female header cables.
  • 149. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things An example of the power of the patch! SunAirPlus, a solar power controller and data collector, is an example of convert- on the pin header that we often want to convert to Grove connectors. We connect adaptor board on the Raspberry Pi. Second example: The Adafruit Ultimate GPS - ing way: Grove male header cables.
  • 150. BOOK 6 Talking to Hardware with Python It’s time to start building! A Grove adaptor cable attached to Pi2Grover. The SunAirPlus board with the Grove female header patch cable.
  • 151. No Soldering! Grove Connectors for Building Things CHAPTER 2 No Soldering! Grove Connectors for Building Things 503 A close-up of the Adafruit GPS with a Grove patch cable.
  • 153. CHAPTER 3 Sensing the World with Python: The World of I2C 505 Sensing the World with Python: The World of I2C B efore we get into how to sense the world in Python, let’s go through a few of the hardware issues. You can skip all this and still use Python to talk to these devices, of course, but some background is a good thing to have You can always go back over it later when you have some experience with these devices. The available sensors for the Raspberry Pi and other small computers number in the thousands. From detecting people in front of your computer (PIR) to detecting a myriad of environmental conditions (temperature/humidity/air quality/and so on), there are many inexpensive ways to have your computer monitor the physical world. As always, the major thing you have to know about these sensors is how you can talk to them with a computer, which is commonly through the interface. The interface consists of two things: The hardware interface, which contains pins, types, and voltage levels, and the software interface, which is usually called a driver or an API (application programming interface). There are four major ways of getting data to your computer from your outside sensors: » » IN THIS CHAPTER » Discovering how to use I2C sensors » Sensing your environment with a Raspberry Pi » Collecting and saving data » Connecting Python to your smartphone
  • 154. 506 Talking to Hardware with Python » » In this book, we deal with sensors using digital inputs, analog inputs and I2C inter- faces. Why not SPI? Just for simplicity. Most SPI parts also have an I2C interface on the chip, and most small computer boards have an I2C interface built into the board. Understanding I2C address. For example, the address of the HDC1080 temperature and humidity sen- sor we use in this chapter has an address of 0x40. What does the “0x” mean in this address? It means that the number that follows is in hexadecimal notation, base 16 instead of base 10 (our normal numbering system). To understand this interface, let’s look at what an I2C bus is. An I2C bus is often used to communicate with chips or sensors that are on the same board or located - conductors). To get around licensing issues (that have largely gone away), often the bus will be called TWI (Two Wire Interface). SMBus, developed by Intel, is a policies and rules from SMBus, sometimes supporting both with minimal recon- I2C provides good support for slow, close peripheral devices that need be addressed only occasionally. For example, a temperature-measuring device will generally only change very slowly and so is a good candidate for the use of I2C, whereas a camera will generate lots of data quickly and potentially changes often. I2C uses only two bidirectional open-drain lines (open-drain means the device can pull a level down to ground, but cannot pull the line up to Vdd. Hence the name open-drain. Thus a requirement of I2C bus is that both lines are pulled up to Vdd. use in this book contains 10K Ohm pullup resistors so you should not have to worry about this. The two lines are SDA (serial data line) and the SCL (serial clock line). There are two types of devices you can connect to an I2C bus: Master devices and Slave devices. Typically, you have one Master device (The Raspberry Pi, in our case) When used on the Raspberry Pi, the Raspberry Pi acts as the Master and all other devices are connected as Slaves.
  • 155. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 507 The I2C protocol uses three types of messages: » » » Lucky for us, most of the complexity of dealing with the I2C bus is hidden by Python drivers and libraries. Exploring I2C on the Raspberry Pi - minal window, command line, and text editors. If you haven’t done that yet, refer To use the I2C bus on the Raspberry Pi, you need to make sure that it is enabled in the operating system. Here is a good tutorial from Adafrui9t on how to do just that: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio- setup/configuring-i2c. Did you do it right? The easy way to check for this is to type the following command in your terminal window: I2cdetect -y 1
  • 156. 508 Talking to Hardware with Python If it returns: -bash: i2cdetect: command not found On the other hand, if it returns: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- the I2C bus. In our next section, we are going to add a simple one. Talking to I2C devices with Python In order to talk to an I2C device, you should have one on the bus. A good one to start get one of these inexpensive sensors on store.switchdoc.com or on amazon.com. HDC1080
  • 157. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 509 1. Shut down your Raspberry Pi. When the yellow LED has stopped blinking, unplug the power from your Raspberry Pi. 2. Plug a Grove cable into the HDC1080. (See Figure 3-3.) sudo halt THE TEXAS INSTRUMENTS HDC1080 TEMPERATURE AND HUMIDITY SENSOR
  • 158. 510 Talking to Hardware with Python 3. Plug the other end of the Grove cable into one of the Grove connectors marked I2C on the Pi2Grover that plugged on top of your Raspberry Pi. (See Figure 3-4.)
  • 159. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 511 4. Power up the Raspberry Pi and open a terminal window. 5. Type into the terminal sudo i2cdetect -y 1 and you will be rewarded with this: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- from this sensor. Reading temperature and humidity from an I2C device using Python The use of Python libraries are key to being productive in writing Python applica- To read the temperature and humidity, follow these steps: 1. First, create a directory in your main directory: cd mkdir I2CTemperature cd I2CTemperature I2CTemperature 2. Before looking at the Python code for reading your temperature, install the library on our Raspberry Pi. You do this by “cloning” the library located at github.com by using the following command in your terminal window: git clone https://github.com/switchdoclabs/SDL_Pi_HDC1080_Python3.git
  • 160. 512 Talking to Hardware with Python git clone ls pi@RPi3-60:~/I2CTemperature $ ls SDL_Pi_HDC1080_Python3 pi@RPi3-60:~/I2CTemperature $ 3. temperature Test.py and enter the following code: import sys sys.path.append('./SDL_Pi_HDC1080_Python3') import time import SDL_Pi_HDC1080 # Main Program print print ("") print ("Read Temperature and Humidity from HDC1080 using I2C bus ") print ("") hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080() while True: print ("-----------------") print ("Temperature = %3.1f C" % hdc1080.readTemperature()) print ("Humidity = %3.1f %%" % hdc1080.readHumidity()) print ("-----------------") time.sleep(3.0)
  • 161. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 4. Run the code by typing: sudo python3 temperatureTest.py Read Temperature and Humidity from HDC1080 using I2C bus ----------------- Temperature = 24.2 C Humidity = 32.9 % ----------------- ----------------- Temperature = 24.2 C Humidity = 32.9 % ----------------- ----------------- Temperature = 24.2 C Humidity = 32.9 % ----------------- GITHUB, A REPOSITORY FOR GOOD THINGS
  • 162. 514 Talking to Hardware with Python Try this experiment. Blow on the HDC1080 sensor board and watch the humidity go up! You will see something like this: ----------------- Temperature = 24.2 C Humidity = 32.9 % ----------------- ----------------- Temperature = 24.1 C Humidity = 33.6 % ----------------- ----------------- Temperature = 24.1 C Humidity = 33.9 % ----------------- ----------------- Temperature = 24.1 C Humidity = 36.3 % ----------------- ----------------- Temperature = 24.1 C Humidity = 36.5 % ----------------- ----------------- Breaking down the program sys library: import sys The next line tells Python to search the SDL_Pi_HDC1080_Python3 directory below sys.path.append('./SDL_Pi_HDC1080_Python3') More imports: import time import SDL_Pi_HDC1080 # Main Program print print ("")
  • 163. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 515 print ("Read Temperature and Humidity from HDC1080 using I2C bus ") print ("") hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080() These statements read the temperature and humidity and print them out to the hidden by use of the HDC1080 library: while True: print ("-----------------") print ("Temperature = %3.1f C" % hdc1080.readTemperature()) print ("Humidity = %3.1f %%" % hdc1080.readHumidity()) Sleep for three seconds and then repeat: print ("-----------------") time.sleep(3.0) turning on a red LED if it gets too hot, or turning on a blue LED if it gets to cold. You could even tweet your temperature and humidity by using the https:// python-twitter.readthedocs.io Python library. LOOKING AT AN I2C DRIVER (continued)
  • 164. 516 Talking to Hardware with Python def readTemperature(self): s = [HDC1080_TEMPERATURE_REGISTER] # temp s2 = bytearray( s ) HDC1080_fw.write( s2 ) time.sleep(0.0625) # From the data sheet #read 2 byte temperature data data = HDC1080_fr.read(2) buf = array.array('B', data) # Convert the data temp = (buf[0] * 256) buf[1] cTemp = (temp / 65536.0) * 165.0 - 40 return cTemp def readTemperature(self): s = [HDC1080_TEMPERATURE_REGISTER] # temp s2 = bytearray( s ) HDC1080_fw.write( s2 ) (continued)
  • 165. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 517 time.sleep(0.0625) # From the data sheet #read 2 byte temperature data data = HDC1080_fr.read(2) buf = array.array('B', data) # Convert the data temp = (buf[0] * 256) buf[1] cTemp = (temp / 65536.0) * 165.0 – 40 return cTemp A Fun Experiment for Measuring Oxygen and a Flame under a more or less sealed glass jar with a lit candle. The idea is to measure the oxygen in the glass jar and watch the level go down as the candle consumes the 14.7 percent oxygen.
  • 166. 518 Talking to Hardware with Python by using MatPlotLib. You could also easily read this data into an Excel spreadsheet and graph it using Excel. MatPlotLib is a Python library for making publication quality plots using methods Then we lit the candle and watched the data on the browser window connected to the Raspberry Pi. What we need to do this experiment: » Analog-to-digital converter: » Grove oxygen sensor: » A candle: » A large glass bowl: Analog-to-digital converters (ADC) digital signal (16 bits, in this case) for a computer to read. When you have the digital number in the computer, you can scale it back to volts - ing volts. the Raspberry Pi. four-channel, 16-bit analog-to-digital converter available on store.switchdoc. wanted to use a 16-bit ADC converter for greater accuracy and the fact it has four channels instead of just one channel.
  • 167. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 519 The Grove oxygen sensor 2 ) is a sensor to test the oxygen concentration in the air. values proportional to the concentration of oxygen. You can interpret these num- bers by referring to the oxygen concentration linear characteristic graph. in a permissible error range, it does not represent the exact oxygen gas concen- tration. The detection of certain components in the air usually requires a more precise and costly instrument, which cannot be done with a single gas sensor.
  • 168. 520 Talking to Hardware with Python Hooking up the oxygen experiment Raspberry Pi. Follow these steps to set up the oxygen sensor: 1. Disconnect the power from the Raspberry Pi. 2. Plug a Grove cable into the Grove oxygen sensor and then into the Grove connector marked A1 on the Grove four-channel, 16-bit ADC board. 3. Plug another Grove cable into the Grove connector marked I2C on the Grove four-channel, 16-bit ADC board. Plug the other end of that Grove cable into one of the connectors marked I2C on the Pi2Grover board plugged into the Raspberry Pi. (See Figure 3-7.) 4. Apply the power to the Raspberry Pi. 5. Run the command i2cdetect -y 1 inside a terminal window. You should see this output: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  • 169. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 521 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- First make a new directory for the program: cd mkdir oxygenProject cd oxygenProject git clone https://github.com/switchdoclabs/SDL_Pi_Grove4Ch16BitADC senseOxygen.py in your terminal window using nano: import time, sys sys.path.append('./SDL_Pi_Grove4Ch16BitADC/SDL_Adafruit_ADS1x15') import SDL_Adafruit_ADS1x15 ADS1115 = 0x01 # 16-bit ADC # Select the gain gain = 6144 # /- 6.144V # Select the sample rate sps = 250 # 250 samples per second # Initialize the ADC using the default mode (use default I2C address) adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115) dataFile = open("oxygenData.csv",'w') totalSeconds = 0 while (1): # Read oxygen channel in single-ended mode using the settings above
  • 170. 522 Talking to Hardware with Python print ("--------------------") voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000 rawCh1 = adc.readRaw(1, gain, sps) # O2 Sensor sensorVoltage = voltsCh1 *(5.0/6.144) AMP = 121 K_O2 = 7.43 sensorVoltage = sensorVoltage/AMP*10000.0 Value_O2 = sensorVoltage/K_O2 - 1.05 print ("Channel 1 =%.6fV raw=0x%4X O2 Percent=%.2f" % (voltsCh1, rawCh1, Value_O2 )) print ("--------------------") dataFile.write("%d,%.2fn" % (totalSeconds, Value_O2)) totalSeconds = totalSeconds 1 dataFile.flush() time.sleep(1.0) When you are done using the oxygen sensor, make sure you put it back in the included capped container and seal the top. Humidity will destroy the sensor over time. (We have destroyed these sensors in the past.) When you run the program, here are the results: -------------------- Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05 -------------------- -------------------- Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05 -------------------- -------------------- Channel 1 =2.436375V raw=0x32C1 O2 Percent= 22.05 -------------------- -------------------- Channel 1 =2.436375V raw=0x32C2 O2 Percent= 22.05 -------------------- -------------------- Channel 1 =2.436187V raw=0x32C1 O2 Percent= 22.05 -------------------- Breaking down the code In these statements, we set the parameters for the ADC module:
  • 171. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C import time, sys sys.path.append('./SDL_Pi_Grove4Ch16BitADC/SDL_Adafruit_ADS1x15') import SDL_Adafruit_ADS1x15 Normal Imports. Notice the path goes to the subdirectory in the your directory. ADS1115 = 0x01 # 16-bit ADC # Select the gain gain = 6144 # /- 6.144V # Select the sample rate sps = 250 # 250 samples per second other method: # Initialize the ADC using the default mode (use default I2C address) adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115) dataFile = open("oxygenData.csv",'w') totalSeconds = 0 while (1): # Read oxygen channel in single-ended mode using the settings above print ("--------------------") voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000 rawCh1 = adc.readRaw(1, gain, sps) from the voltage from the ADC: # O2 Sensor sensorVoltage = voltsCh1 *(5.0/6.144) AMP = 121 K_O2 = 7.43 sensorVoltage = sensorVoltage/AMP*10000.0 Value_O2 = sensorVoltage/K_O2 - 1.05
  • 172. 524 Talking to Hardware with Python print ("Channel 1 =%.6fV raw=0x%4X O2 Percent=%.2f" % (voltsCh1, rawCh1, Value_O2 )) print ("--------------------") dataFile.write("%d,%.2fn" % (totalSeconds, Value_O2)) - ally terminate this program with a Ctrl-C: totalSeconds = totalSeconds 1 dataFile.flush() time.sleep(1.0) Looking at the numbers, we determined that we started with about 21 percent oxygen and the candle went out at about 15.8 percent oxygen, a reduction of about
  • 173. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 525 One more thing to note: Look at the graph right after the candle went out. You can see that the seal wasn’t perfect as the oxygen started to creep up. Building a Dashboard on Your Phone Using Blynk and Python When you drive a car, all the information about how fast you are going, how much fuel remains, and other car information is on your dashboard. We’re going to show you how construct a simple dashboard so you can view your project data on your smartphone. To illustrate how to do this, we’ll use the free app Blynk (free for small dashboards; they charge you a bit for more energy to build more controls). This app is available on the various app stores for both Android and iPhones. We’ll use the iPhone to show the usage, but it is pretty much identical for Android phones. HDC1080 temperature and humidity sensor redux Earlier in this chapter, you built a temperature and humidity sensing project using
  • 174. 526 Talking to Hardware with Python us what our dashboard will look like. OTHER DASHBOARDS • • • • • •
  • 175. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 527 How to add the Blynk dashboard First, we show you how to set up the Blynk app. This is done on an iPhone, but it is very similar to using it on an Android phone. And the Python is identical in both cases! 1. Install the Blynk app on your mobile phone. (See Figure 3-11.) 2. Open the Blynk app and create an account. (See Figure 3-12.) 3. Click the button to scan a QR (see Figure 3-13). 4. Scan the QR code shown in Figure 3-14. 5. You will now see the MyTemperature app on your screen. (See Figure 3-15.)
  • 176. 528 Talking to Hardware with Python
  • 177. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C 529 6. Click the middle of the project to select the project. Then click the indicated button to go to project settings. AUTH TOKEN temperatureTest.py the software to support the Blynk app.
  • 178. Talking to Hardware with Python
  • 179. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C software for the Blynk app To modify the software to support the Blynk app, follow these steps: 1. Create a directory in your main directory by entering the following: cd mkdir myTemperature cd myTemperature myTemperature 2. Before looking at the Python code for reading and then “Blynking” your temperature, install the library on the Raspberry Pi. You do this by “cloning” the library located up at github.com by using the following command in your terminal window: git clone https://github.com/switchdoclabs/SDL_Pi_HDC1080_Python3.git 3. myTemperature.py using nano or your favorite editor. #!/usr/bin/env python3 #imports import sys sys.path.append('./SDL_Pi_HDC1080_Python3') import time import SDL_Pi_HDC1080 import requests import json BLYNK_URL = 'http://blynk-cloud.com/' BLYNK_AUTH = 'xxxx' # Main Program print print ("")
  • 180. Talking to Hardware with Python print ("Read Temperature and Humidity from HDC1080 using I2C bus and send to Blynk ") print ("") hdc1080 = SDL_Pi_HDC1080.SDL_Pi_HDC1080() def blynkUpdate(temperature, humidity): print ("Updating Blynk") try: put_header={"Content-Type": "application/json"} val = temperature put_body = json.dumps(["{0:0.1f}".format(val)]) r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V0', data=put_body, headers=put_header) put_header={"Content-Type": "application/json"} val = humidity put_body = json.dumps(["{0:0.1f}".format(val)]) r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V1', data=put_body, headers=put_header) put_header={"Content-Type": "application/json"} val = time.strftime("%Y-%m-%d %H:%M:%S") put_body = json.dumps([val]) r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V2', data=put_body, headers=put_header) return 1 except Exception as e: print ("exception in updateBlynk") print (e) return 0 while True: temperature = hdc1080.readTemperature() humidity = hdc1080.readHumidity() print ("-----------------") print ("Temperature = %3.1f C" % hdc1080.readTemperature())
  • 181. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C print ("Humidity = %3.1f %%" % hdc1080.readHumidity()) print ("-----------------") blynkUpdate(temperature, humidity) time.sleep(3.0) 4. The last thing you need to do before you run the code is to replace the 'xxxx' with your Blynk authorization code, which will look something like this: 445730794c1c4c8ea7852a31555f44444. BLYNK_AUTH = 'xxxx' BLYNK_AUTH = '445730794c1c4c8ea7852a31555f44444' Breaking down the code This code is very similar to the HDC1080 code from earlier in this chapter with the exception of the blynkUpdate code. def blynkUpdate(temperature, humidity): print ("Updating Blynk") try: Why do we have a ’try’ here? Because sometimes the requests library will throw an error if the Internet is being funky. http header for the requests library: put_header={"Content-Type": "application/json"}
  • 182. Talking to Hardware with Python The following code sets the number of digits to the right of the decimal point to 1 so we won’t have long numbers of relatively meaningless digits because of the accuracy of the HDC1080: val = temperature put_body = json.dumps(["{0:0.1f}".format(val)]) The following code does the actual transfer of the data to the Blynk server in the form of an http request: r = requests.put(BLYNK_URL BLYNK_AUTH '/update/V0', data=put_body, headers=put_header) out if you have set the Blynk authentication code incorrectly: except Exception as e: print ("exception in updateBlynk") print (e) return 0 Sudo python3 myTemperature.py You will see this type of output on your terminal screen: ----------------- Temperature = 22.6 C Humidity = 36.8 % ----------------- THE REQUESTS LIBRARY http http
  • 183. Sensing the World with Python: The World of I2C CHAPTER 3 Sensing the World with Python: The World of I2C Updating Blynk ----------------- Temperature = 22.5 C Humidity = 36.8 % ----------------- Updating Blynk Hit the Run key at the top-right of your Blynk app on the phone, and then watch your data start to come in. If you don’t get data in a few seconds, check your authentication code and make sure you have started your app by hitting the Start button in the upper-right corner of the app. -
  • 184. Talking to Hardware with Python Where to Go from Here In this chapter, you have learned a lot about how to connect to the real world Python on your Raspberry Pi. We suggest these other interesting things to do, building upon your new expertise: » » » » »
  • 185. CHAPTER 4 Making Things Move with Python 537 Making Things Move with Python Making things move around with Python is undeniably cool. With motors, physical computing goes to a whole new level. Robots, microwaves, refrigerators, and electric cars all use electric motors to move around, blow air, pump coolant, and take you 60mph wherever you want to go. Electric motors are everywhere! At its simplest, an electric motor is a machine that converts electrical energy into mechanical energy. In this chapter, we talk about DC (direct current) motors. (AC), on the other hand, is what you get out of your house outlets. Interestingly, electric motors consume more than half of the electric energy pro- duced in the United States. IN THIS CHAPTER » How to make things move with Python » Understanding DC motors and software » Using a servo motor » Making a stepper motor step
  • 186. 538 BOOK 6 Talking to Hardware with Python Exploring Electric Motors An electric motor is all about magnetism. All motors use magnets to create motion. All magnets have a north and a south pole. North to north and south to south repel use this fact to create motion. We are all familiar with permanent magnets, like the ones you use to hang things on the front of your refrigerator. However, you can also create magnets by running a current around a coiled wire, which creates you can create force, which then becomes motion. There are many ways to build motors, but this is the fundamental basis of all of them. In this chapter, we are going to talk about three common types of motors used in small projects and robots. They are: » Small DC motors » Servo motors » Stepper motors Small DC motors A DC motor has two wires, power and ground. When you supply power (putting power and ground wires, and the motor will spin in the opposite direction. You control the speed of a DC motor by using pulse width modulation (PWM), a tech- wheels. Sometimes you will put an “encoder” on the motor shaft so you read into a computer how far the shaft has turned, giving the computer some feedback that can be useful. Use a DC motor anytime you want something to be spun at a RPM (revolutions per
  • 187. Making Things Move with Python CHAPTER 4 Making Things Move with Python Servo motors Servo motors are a generally a combination of three things: a DC motor, a simple is a variable resistor) that will give position feedback like the “encoder” in the DC - torque rating of that servo motor. range of motion. a servo motor is often the answer. Use a servo motor for fast, high torque and for pretty accurate rotation to a spe- Stepper motors A stepper motor shaft. Whereas a servo motor uses a DC motor, a stepper motor uses multiple- toothed electromagnets surrounding a central-toothed shaft. A DC motor on a small robot.
  • 188. BOOK 6 Talking to Hardware with Python that will sequence the electromagnets surrounding that central shaft to make the central shaft turn in “steps,” hence the name stepper motor. The design of a stepper motor provides a steady holding torque even when not powered up. Contrast that to the servo motor, which has to be powered up to supply torque. As long as the load is within the limits of the servo motor torque, then there are no positional errors. Stepper motors are for slow, precise rotation. They are superior to servo motors in motors. Stepper motors also don’t require a feedback system to determine where Controlling Motors with a Computer through all three types of motors and show you how to control them with Python. Python and DC Motors are dozens of robot controllers and motor controller boards that will work for Sun-tracking solar panels using a stepper motor.
  • 189. Making Things Move with Python CHAPTER 4 Making Things Move with Python our projects from the last chapter), which gives us control over two motors, their individual direction and their individual speed. Pretty cool. Here is the parts list: » Pi2Grover Grove interface board: Try store.switchdoc.com or Amazon.com. » Grove I2C motor drive: Available at www.seeedstudio.com or Amazon.com (comes with a Grove cable). » Two small DC motors: Try https://www.adafruit.com/product/711 or Amazon.com. of doing things. Grove I2C motor drive motors, so we will just use the Raspberry Pi power supply. If you are using bigger The Grove I2C motor drive.
  • 190. BOOK 6 Talking to Hardware with Python motors, which gets us up and running quickly. There are a couple of interesting things about this diagram: First of all, there is another computer on this board! It is an Atmega 8L and is another small computer boards for little computers have little computers on them. Computers are every- where! You can see the two motor connections on the left side of the board and also what the LEDs mean in the middle of the board. drive on the www.seeedstudio.com product page. You could change the program- ming if you want or at least understand how you can make a little computer look before you hook this up: 1. Loosen the set of two screw terminals on the end of the and insert the bare end of the wires on the motors (see Figure 4-5) into the holes and tighten the screws. (See Figure 4-6.) Annotated diagram of the I2C motor drive board.
  • 191. Making Things Move with Python CHAPTER 4 Making Things Move with Python Note that it really doesn’t matter which color goes in which hole with a DC motor. It will just rotate in the opposite direction. Just match them both as in shows the installed motor on your Grove I2C motor drive board. 2. Plug a Grove cable into the Grove connector on the Grove I2 motor drive (called “Output for external MCU selector” in the diagram). It comes that way wired if you don’t have that jumper installed. The Adafruit DC motor. The wires in the I2C motor drive screw terminals.
  • 192. BOOK 6 Talking to Hardware with Python If your board and motor don’t respond after hookup, try hitting the Reset button Motors installed on the motor drive. The DC motor setup.
  • 193. Making Things Move with Python CHAPTER 4 Making Things Move with Python Now, let’s power the Raspberry Pi up and start writing some Python! After pow- not, shut the Pi down again and check your wiring. Python DC motor software DC motors are often used for robot wheels, and so the words forward and backward should start to give you some ideas for later in the book when we are building a robot car. The use of Python libraries are key to being productive in writing Python applica- tions. We will be using the SDL_Pi_HDC1080_Python3 To set up the software, follow these steps: 1. Create a directory in your main directory by entering: cd mkdir dcMotor cd dcMotor Now you are in the dcMotor directory. 2. Before looking at the Python code for running your motors, install the library on the Raspberry Pi. You do this by “cloning” the library located up at github.com by using the following command in your terminal window: git clone https://github.com/switchdoclabs/SDL_Pi_GroveI2CMotorDrive.git The git clone clones the git repository located at the address and copies it to your Raspberry Pi. If you enter ls following output: pi@RPi3-60:~/dcMotor $ ls SDL_Pi_GroveI2CMotorDrive pi@RPi3-60:~/I2CTemperature $
  • 194. BOOK 6 Talking to Hardware with Python 3. dcmotorTest.py and enter the following code: import sys sys.path.append("./SDL_Pi_GroveI2CMotorDrive") import SDL_Pi_GroveI2CMotorDrive import time #"0b1010" defines the output polarity #"10" means the M is "positive" while the M- is "negative" MOTOR_FORWARD = 0b1010 MOTOR_BACKWARD = 0b0101 try: m= SDL_Pi_GroveI2CMotorDrive.motor_drive() #FORWARD print("Forward") #defines the speed of motor 1 and motor 2;) m.MotorSpeedSetAB(100,100) m.MotorDirectionSet(MOTOR_FORWARD) time.sleep(2) #BACK print("Back") m.MotorSpeedSetAB(100,100) #0b0101 Rotating in the opposite direction m.MotorDirectionSet(MOTOR_BACKWARD) time.sleep(2) #STOP print("Stop") m.MotorSpeedSetAB(0,0) time.sleep(1) #Increase speed for i in range (100): print("Speed:",i) m.MotorSpeedSetAB(i,i) time.sleep(.02)
  • 195. Making Things Move with Python CHAPTER 4 Making Things Move with Python print("Stop") m.MotorSpeedSetAB(0,0) except IOError: print("Unable to find the I2C motor drive") print("Hit Reset Button on I2C Motor Drive and Try Again") speed, stops the motors, and then runs them backward at a slow speed ramping up to full speed, and then stops the motors entirely. The key aspects of this software are calls to the SDL_Pi_GroveI2CMotorDrive library. The library supports the following functions: » MotorSpeedSetAB( MotorSpeedA, MotorSpeedB): Motor speed for the A motor (M1) and the B motor (M2). Range 0–100. » MotorDirectionSet(Direction): Forward or backward —constants set in the program. MOTOR_FORWARD = 0b1010, MOTOR_BACKWARD = 0b0101 SDL_Pi_GroveI2CMotorDrive library is that it uses smbus. In the library, you send the com- block write byte, and then the arguments. Here is the call for setting the motor direction: #Set motor direction def MotorDirectionSet(self,Direction): bus.write_i2c_block_data(self.I2CMotorDriveAdd, self.DirectionSet, [Direction,0]) time.sleep(.02) Time to run the DC motors now. Type this into your terminal window: sudo python3 testMotor.py You will be rewarded by seeing the LEDs change and seeing your motors go through a sequence that you have programmed in Python. You should be able to All these motors take power from the Raspberry Pi when running, so disconnect
  • 196. BOOK 6 Talking to Hardware with Python Python and running a servo motor inside, but it also has a controller circuit that allows us to position the DC wait there for further orders. You control servo motors by using PWM (pulse width modulation). Although you can buy boards that will do PWM (and support bigger servo motors!) under control of your computer, for this small servo we will be using the built-in PWM capabil- Here is the parts list: » Pi2Grover Grove interface board: Look for it at store.switchdoc.com or amazon.com. » SG90 micro servo motor: are inexpensive so you may end up buying two or more for under $10. » A package of Grove male patch cables: Grove-connector-to-male-pin cables: Available at store.switchdoc.com or amazon.com. three wires are: » » » and then you can plug it into your Raspberry Pi.
  • 197. Making Things Move with Python CHAPTER 4 Making Things Move with Python The SG90 micro servo with wires. HOW MUCH CURRENT CAN THE RASPBERRY PI SUPPLY TO THE 5V PINS? you have connected to the Raspberry Pi 3 and what kind of a USB power supply you Grove male-pin-to Grove-connector patch cable.
  • 198. 550 BOOK 6 Talking to Hardware with Python Now let’s connect the wires and make a servo motor rock! 1. Shut down your Raspberry Pi and remove power. 2. Plug the Grove patch cable into your SB90 servo motor following this wire chart in Table 4-1. (See Figure 4-11.) Check your wiring carefully. You can damage your Pi and motor if you reverse these wires. 3. Plug the end of the Grove cable in the Pi2Grover Grove connector marked D4/D5. 4. Put a piece of electrical tape or blue tape over the white exposed pin on the Grove patch cable to keep it from shorting anything out. Also put one of the supplied rocker arms on the servo motor gear so we can see more easily its range of motion. (See Figure 4-12.) Now let’s look at the Python software. Servo Motor to Patch Cable Wiring SG90 Servo Grove Patch Cable Function Power Black wire Ground Servo motor correctly wired to patch cable.
  • 199. Making Things Move with Python CHAPTER 4 Making Things Move with Python Python servo software We are not going to use a higher-level servo library (and there are many available for the Raspberry Pi in Python); instead, we are going to show you how to control RPi GPIO built- are using a library (RPi.GPIO), but we’re not adding layers of API (application programming interface) calls like we normally would 1. Create a directory in your main directory by entering: cd mkdir Servo cd Servo 2. servoTest.py and enter the following Python code: import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) ServoPin = 4 GPIO.setup(ServoPin, GPIO.OUT) p = GPIO.PWM(ServoPin, 50) Fully connected Pi and servo motor.
  • 200. BOOK 6 Talking to Hardware with Python p.start(7.5) try: while True: p.ChangeDutyCycle(7.5) # turn towards 90 degree print ("90 degrees") time.sleep(1) # sleep 1 second print ("0 degrees") p.ChangeDutyCycle(2.5) # turn towards 0 degree time.sleep(1) # sleep 1 second print ("180 degrees") p.ChangeDutyCycle(12.5) # turn towards 180 degree time.sleep(1) # sleep 1 second except KeyboardInterrupt: p.stop() GPIO.cleanup() Breaking down the code board: import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) ServoPin = 4 GPIO.setup(ServoPin, GPIO.OUT) This command sets an object p to the ServoPin 50Hz is a good number for this type of servo motor: p = GPIO.PWM(ServoPin, 50) The following line starts the servo motor at 7.5 percent duty cycle. Remember how PWM works on a servo? It goes from one end of the servo turn to the by going from
  • 201. Making Things Move with Python CHAPTER 4 Making Things Move with Python 553 the servo type. We just looked at the numbers and empirically determined these p.start(7.5) Now we move the servo through its entire range: try: while True: p.ChangeDutyCycle(7.5) # turn towards 90 degree print ("90 degrees") time.sleep(1) # sleep 1 second print ("0 degrees") p.ChangeDutyCycle(2.5) # turn towards 0 degree time.sleep(1) # sleep 1 second print ("180 degrees") p.ChangeDutyCycle(12.5) # turn towards 180 degree time.sleep(1) # sleep 1 second this so when your control-c out of the program, the program will shut down the - pins directly. except KeyboardInterrupt: p.stop() GPIO.cleanup() Now it is time to run the program. Type the following into a terminal window: sudo python3 servoTest.py You should be rewarded by your screen printing out the following liens and your - ent angles and sequences. You can’t hurt the servo by trying things. 90 degrees 0 degrees 180 degrees 90 degrees 0 degrees 180 degrees 90 degrees 0 degrees
  • 202. BOOK 6 Talking to Hardware with Python Now we have a servo motor working on our Raspberry Pi. Remember at the begin- airplane or a rudder on an RC boat? Watching the servo motor go thorough its programmed sequence should spark your thoughts of what to do with a servo motor. You can see why you use a DC motor for wheels and a servo to do things in a non-rotating manner. line to our last major motor, a stepper motor. Python and making a stepper motor step used for accurate positioning of items with a digital interface. You can accurately - nitely needs what is called positional feedback. A stepper motor gets around this by accurately moving from one “step” to another under command of software. The motor is constructed to use two motor coils to be implementing this “stepping” sequence in the Python software controlling the stepper motor. A stepper motor typically has two coils used to move the motor from one step to step. This pattern of steps will be very obvious in our Python software. A diagram of a stepper motor.
  • 203. Making Things Move with Python CHAPTER 4 Making Things Move with Python 555 project. Forward Stepping the Stepper Coil_A_1 (Pin 12) Coil_A_2 (Pin 20) Coil_B_1 (Pin 13) Coil_B_2 (Pin 21) 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 Backward Stepping the Stepper Coil_A_1 (Pin 12) Coil_A_2 (Pin 20) Coil_B_1 (Pin 13) Coil_B_2 (Pin 21) 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 Logic analyzer showing the motor stepping
  • 204. 556 BOOK 6 Talking to Hardware with Python Here is the parts list: » Pi2Grover Grove interface board: Try store.switchdoc.com or amazon.com. » 28BYJ-48 ULN2003 5 V stepper motor: Look for these at eBay.com or https://amzn.to/2BuNDVl as the ones at the preceding Amazon.com link). » A package of Grove female patch cables, Grove-connector-to-female- pins: Available at store.switchdoc.com or amazon.com. FEEDBACK: WHAT A USEFUL THING! Feedback occurs when you route the output of a system back into the inputs of a sys- and article and ask for comments. People supply comments (hopefully nice ones) and you change the article based on some of those comments. That is feedback! You use feedback in electrical circuits to achieve a better and more accurate position- on the feedback. we think of positive feedback as being good comments or at least constructive criti- article is. - get larger. Ever hear a speaker wail when you put a microphone too close the speaker? That’s positive feedback.
  • 205. Making Things Move with Python CHAPTER 4 Making Things Move with Python 557 The 28BYJ-48 stepper motor and UNL2003 driver board. A Grove- connector-to- female-pin-header patch cable.
  • 206. 558 BOOK 6 Talking to Hardware with Python 1. Shut down your Raspberry Pi and remove power. 2. Take a Grove female patch cord and connect it to the UNL2003 driver board, as in the wire chart in Table 4-4. Note we put a wire tie on the cable to keep things neat and tidy. Look very carefully at your red and black wire on the Grove patch cord to make First Grove Female Patch Cord to UNL2003 Driver Board Grove Patch Cable UNL2003 Driver Board Function IN1 Coil A_1 IN2 Coil B_1 Power Black wire Ground Close-up of power connections on the UNL2003 driver board.
  • 207. Making Things Move with Python CHAPTER 4 Making Things Move with Python 3. Take a second Grove female patch cord and connect it as in the wire chart in Table 4-5. Use a wire tie or a piece of tape to keep the unused red and black wires up and 4. Plug your 28BYJ-4 stepper motor cable into the UNL2003 driver board connector. It is keyed and only goes in one way. (See Figure 4-20.) Second Grove Female Patch Cord to UNL2003 Driver Board Grove Patch Cable UNL2003 Driver Board Function IN3 Coil A_2 IN4 Coil B_3 No Connect Black wire No Connect Second Grove patch cable attached.
  • 208. 560 BOOK 6 Talking to Hardware with Python 5. the UNL2003 driver board) into the Pi2Grover Grove connector marked D12/13 and the second Grove patch cable (the one that only has the yellow and white wires connected) into the Pi2Grove Grove connector marked D20/21. 6. Put a cardboard arrow on your stepper motor shaft so you can really see it move. (See Figure 4-22.) Stepper motor and driver board connected. All patch wires installed on UNL2003 driver board.
  • 209. Making Things Move with Python CHAPTER 4 Making Things Move with Python Fully wired Raspberry Pi and stepper motor project. ready to step.
  • 210. BOOK 6 Talking to Hardware with Python Python stepper software Similar to what we did with the servo motor, we are not going to use a higher level all work with all stepper motors!), and instead we are going to show you how to 1. Create a directory in your main directory by entering: cd mkdir Stepper cd Servo 2. stepperTest.py and enter the following Python code: import sys import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) coil_A_1_pin = 12 coil_B_1_pin = 13 coil_A_2_pin = 20 coil_B_2_pin = 21 GPIO.setup(coil_A_1_pin, GPIO.OUT) GPIO.setup(coil_A_2_pin, GPIO.OUT) GPIO.setup(coil_B_1_pin, GPIO.OUT) GPIO.setup(coil_B_2_pin, GPIO.OUT) def forward(delay, steps): for i in range(0, steps): setStep(1, 0, 1, 0) time.sleep(delay) setStep(0, 1, 1, 0) time.sleep(delay) setStep(0, 1, 0, 1) time.sleep(delay) setStep(1, 0, 0, 1) time.sleep(delay)
  • 211. Making Things Move with Python CHAPTER 4 Making Things Move with Python 563 def backwards(delay, steps): for i in range(0, steps): setStep(1, 0, 0, 1) time.sleep(delay) setStep(0, 1, 0, 1) time.sleep(delay) setStep(0, 1, 1, 0) time.sleep(delay) setStep(1, 0, 1, 0) time.sleep(delay) def setStep(w1, w2, w3, w4): GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) while True: try: # Delay between steps (milliseconds) delay = 10 # How many Steps forward steps = 50 forward(int(delay) / 1000.0, int(steps)) # How many Steps backwards steps = 50 backwards(int(delay) / 1000.0, int(steps)) except KeyboardInterrupt: # shut off all coils setStep(0,0,0,0) sys.exit() Breaking down the code The stepperTest.py outputs when you hit Ctrl-C to stop the program.
  • 212. BOOK 6 Talking to Hardware with Python Time to run! Power up your Pi and open a terminal window. Note that all four of You will see the stepper motor turn 50 steps to the left and then 50 steps to the right. Try changing those variables in the program above to move your stepper motor to other positions. Do you see how these motors can be used in 3D print- ers and robots to accurately position printing heads, bed height, and robot arms? The Raspberry Pi running the stepper motor.
  • 214. Contents at a Glance Introduction to Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . 567 A Robot Is Not Always like a Human. . . . . . . . . . . . . . . . . . . . . . . . . 567 Not Every Robot Has Arms or Wheels . . . . . . . . . . . . . . . . . . . . . . . 568 Understanding the Main Parts of a Robot. . . . . . . . . . . . . . . . . . . . 572 Programming Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 Building Your First Python Robot . . . . . . . . . . . . . . . . 575 Introducing the Mars Rover PiCar-B . . . . . . . . . . . . . . . . . . . . . . . . . 575 Assembling the Robot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 Programming Your Robot Rover in Python . . . . . 595 Building a Simple High-Level Python Interface . . . . . . . . . . . . . . . . 595 Making a Single Move with Python. . . . . . . . . . . . . . . . . . . . . . . . . . 597 Functions of the RobotInterface Class . . . . . . . . . . . . . . . . . . . . . . . 598 Coordinating Motor Movements with Sensors. . . . . . . . . . . . . . . . 610 Making a Python Brain for Our Robot . . . . . . . . . . . . . . . . . . . . . . . 613 . . . . . . . . . 623 This Chapter’s Project: Going to the Dogs . . . . . . . . . . . . . . . . . . . . 624 Setting Up the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624 Machine Learning Using TensorFlow . . . . . . . . . . . . . . . . . . . . . . . . 625 Testing the Trained Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 Taking Cats and Dogs to Our Robot . . . . . . . . . . . . . . . . . . . . . . . . . 640 Other Things You Can Do with AI Techniques and the Robot . . . 645 AI and the Future of Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
  • 215. CHAPTER 1 Introduction to Robotics 567 Introduction to Robotics R obots. That’s a name that has been bandied about for a hundred years. It comes from a Czech word, robota, which means “involuntary labor.” It was - robot A Robot Is Not Always like a Human » Robots have only two features, a computer and an actuator. » Robots are dumb; they are not people. IN THIS CHAPTER » Understanding what a robot is » Types of robots » Knowing the parts of a robot
  • 216. 568 BOOK 7 Building Robots with Python Not Every Robot Has Arms or Wheels Cobots cooperative robots). - - -
  • 217. Introduction to Robotics CHAPTER 1 Introduction to Robotics The Wilkinson Bread-Making Robot www.wilkinsonbaking. com - the consumer. Inputs for the BWM Robot Driving System.
  • 218. BOOK 7 Building Robots with Python - https://youtu.be/zVL8760H768 robotics class, and after three months and thousands of lines of code, they suc- customer’s table. A robot making bread.
  • 219. Introduction to Robotics CHAPTER 1 Introduction to Robotics because he wants a connected toaster. https://www.youtube.com/ watch?v=Z7h8-f-k8C8. Baxter
  • 220. BOOK 7 Building Robots with Python - Understanding the Main Parts of a Robot » Computers » Motors and actuators » Communications » Sensors Computers The Toasteroid Internet- connected toaster.
  • 221. Introduction to Robotics CHAPTER 1 Introduction to Robotics embedded systems. They may what to do. Motors and actuators Actuator motor Communications devices. Sensors - drivers. -
  • 222. BOOK 7 Building Robots with Python Programming Robots
  • 223. CHAPTER 2 Building Your First Python Robot 575 Building Your First Python Robot I n this chapter, we open up a robot to take a look inside, and we show you how to talk to all the parts with Python. We tell you how to program a robot after is inexpensive compared to buying a prebuilt one. Second, by building a robot you get to know how a robot works and how you can use Python to control it. We have chosen a robot that is based on our friend the Raspberry Pi. You can get robots that are based on many other computers, including the Arduino, but with ligence that you can on the Raspberry Pi. After all, this is Python All-In-One For Dummies. Wouldn’t you like to be able to use Introducing the Mars Rover PiCar-B When we were deciding which robot to build in this book, we looked at and and each of them had some drawbacks. However, after careful consideration, IN THIS CHAPTER » Building a robot » Understanding the components » Learning to program the components
  • 224. 576 BOOK 7 Building Robots with Python » The assembly manual was clear with lots of pictures and diagrams. » The supplied software was compatible with Python 3 (and the Stretch version of the Raspberry Pi operating system). » The PiCar-B required no soldering. » It had a reasonable price and good availability. A radio control car can be considered a robot too. Why did we choose this is because you can easily get inside the software and add more software to make own Python software inside the robot than we are in playing with the joystick. What you need for the build There are three things you need in order to build the robot used in this chapter, in The assembled PiCar-B robot.
  • 225. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot 577 » Raspberry Pi 3B+: Yes, you could get by with a smaller Raspberry Pi (like the Raspberry Pi Zero), but we recommend you get the fast one so you can do more sophisticated processing onboard the robot. The price you pay for a faster Pi (like the 3B ) is in power consumption and battery life. For our Among other places, you can buy the Raspberry Pi 3B at • Amazon.com (make sure you buy the 3B ) • Newark.com • Adafruit.com • Shop.switchdoc.com » Adeept Raspberry Pi PiCar-B: The Adeept Raspberry Pi PiCar-B is not quite as available as the Raspberry Pi. When you buy this, make sure you are buying the PiCar-B and not Mars Rover to the name of this product in their catalog, so look for the “Adeept Mars Rover PiCar-B.” You can buy the PiCar-B at these places: • Amazon.com (https://amzn.to/2B7mtop) • eBay.com • Adeept.com • Shop.switchdoc.com » 18650 LiPo batteries: The PiCar-B requires two 18650 3.7V LiPo 5000mAh removing the batteries) and supplying power for the Raspberry Pi from the micro USB plug, which then powers both the robot and the Raspberry Pi. The power for both the robot and the Raspberry Pi are connected together. The package we chose had two sets of batteries and an included wall charger. You can buy these kinds of batteries all over the place, including • Amazon.com (https://amzn.to/2TgPsx1) • Many, many other places. Understanding the robot components on the mechanical structure of the robots but rather on each of the active compo nents. We’ll also talk about the Python software used to communicate with each device used in the Python system test software later on in this chapter and in our
  • 226. 578 BOOK 7 Building Robots with Python We will be giving small Python code snippets to show you how each of the sensors Tests on Your Rover in Python” section, later in this chapter. Controller board This motor controller is designed to interface the Raspberry Pi to the sensors and vide power to the main drive motor. The rest of the board is used to connect up the Servo motors The PiCar-B motor controller board.
  • 227. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot 579 milliamp is » Yellow: PWM control signal » Red: Power (5V, in our case) » Brown: Ground range of motion. The SG90 micro servo motor.
  • 228. BOOK 7 Building Robots with Python To control a servo motor, we just have to set the value to the position we want the print ("-------------------") print ("Servo Test - Head Left") print ("-------------------") pwm.set_pwm(HEAD_TURN_SERVO, 0, calValues.look_left_max) time.sleep(1.0) calValues.look_ left_max in this chapter. Drive motor computer how far the shaft has turned, giving the computer some feedback that can be useful. direction. The intricacies of controlling this motor are well hidden from the user. Here is the motor.motor_left(MOTOR_START, forward,left_spd*spd_ad) motor.motor_right(MOTOR_START,backward,right_spd*spd_ad) Why are we turning both a left and right motor on when there is only one drive causes the robot to move backward, you just move the motor to the other motor
  • 229. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot RGB LED print () print ("-------------------") print ("Left Front LED Test - Red ") print ("-------------------") led.side_on(led.left_R) time.sleep(1.0) led.side_off(led.left_R) The main drive motor.
  • 230. BOOK 7 Building Robots with Python Pixel RGB programmable LEDs lights, if one goes out, then all the rest of the string goes too. That is because they are controlled by a single serial data stream that is sent through all the lights by pretty special software on the Raspberry Pi to make it work. print () print ("-------------------") print ("12 RGB Pixel LED Test - On ") print ("-------------------") rainbowCycle(strip, wait_ms=20, iterations=3) many projects. A single RGB LED.
  • 231. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot The 12 programmable RGB LEDs. PIXEL RGB STRINGS ON THE RASPBERRY PI The Raspberry Pi has a complex, multifaceted operating system based on Linux. It is a multitasking preemptive operating system, which means virtually any task (and all user tasks) can be interrupted (meaning stopped) and thus our serial stream to the Pixel LEDs stopped and corrupted to some degree. The library we are using solves the real-time control problem by using the PWM and DMA hardware on the Raspberry Pi’s processor. The PWM (pulse-width modulation) sequence of bytes to the PWM module, the Pixel data signal can be generated without being interrupted by the Raspberry Pi’s operating system. Because the Arduino type of processors don’t really have an operating system, it is pretty easy to generate these signals on an Arduino compared to an Raspberry Pi. Processors like the ESP8266 and the ESP32 do have tasks running in the background (like WiFi) and so require special drivers to compensate for that to avoid data corruption , the LEDs do not always work well with the older and smaller Raspberry Pis (A , 3B, Pi Zero, or Pi Zero W, for example).
  • 232. BOOK 7 Building Robots with Python Pi camera The Pi camera talks to the Raspberry Pi via a parallel data ribbon cable directly into the Raspberry Pi board. print () print ("-------------------") print ("Open Video Window") print ("-------------------") camera.resolution = (1024, 768) camera.start_preview(fullscreen=False,window= (100,100,256,192)) time.sleep(20) camera.preview.window=(200,200,256,192) time.sleep(2) Raspberry Pi camera and cable.
  • 233. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot 585 camera.preview.window=(0,0,512,384) time.sleep(2) camera.close() Ultrasonic sensor ultra.checkdisk() calls the software that sets which we convert to centimeters. print () print ("-------------------") print ("Ultrasonic Distance Test") print ("-------------------") An ultrasonic distance sensor.
  • 234. 586 BOOK 7 Building Robots with Python average_dist = 0.0 for i in range(0,10): distance = ultra.checkdist() average_dist = average_distance distance print ("Distance = {:6.3f}cm ".format( distance*100)) time.sleep(1.0) average_distance = average_distance / 10 print ("average_dist={:6.3f}cm".format(average_dist*100)) Assembling the Robot An example of the assembly manual diagrams.
  • 235. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot 587 testing. Note: starts installing all the Adeept software on the Raspberry Pi and you will want to test the robot using the software in this book to better understand the parts of the robot before continuing. So, go build your robot and then meet us back here to start testing your new robot. shows the assembled robot. » There is a power switch on the left side of the motor drive board (viewed power from the batteries. » Make sure you use the longer of the supplied ribbon cables for the Pi camera. The short one will not quite reach. Change it before you install the camera into the robot. » plastic wire ties to hold things in place, allowing for room for the servos and The assembled PiCar-B showing wiring.
  • 236. 588 BOOK 7 Building Robots with Python » Pay close attention to the orientation of the plastic parts and servos during assembly. Almost all of them have an asymmetrical top and bottom and need to be oriented correctly to be assembled. » Calibrating your servos Now that you have assembled the robot, it is time to start testing things. The assembly, they will not necessarily be completely in the right place. The calibrateServo.py program runs each of the three servos from one end to and center of each servo from the display on the terminal window. Then you place these values in the calValues.py program for the rest of the programs to access. The values in the calValues.py are right for our robot and will probably be pretty close for yours, but you should run the program to be sure. The calibrateServo #!/usr/bin/python3 # calibrate servos PROPERLY TURNING OFF YOUR RASPBERRY PI However, like many other computers, just pulling the plug on a Raspberry Pi can have dire consequences, in this case, corrupting the SDCard that the Raspberry Pi uses for program and data storage. Before you shut down the Raspberry Pi, enter the following in a terminal window: sudo halt. This safely shuts down the Raspberry Pi. When you run this command, after a bit, you’ll see the “ACT” light (the green one) blink 10 times (at 0.5 second intervals). When it stops the plug. The red power LED will remain on as long as there is power applied to the Raspberry Pi.
  • 237. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot 589 import time import Adafruit_PCA9685 import calValues #import the settings for servos pwm = Adafruit_PCA9685.PCA9685() pwm.set_pwm_freq(60) #servo mapping # pmw 0 head tilt HEAD_TILT_SERVO = 0 # pwm 1 head turn HEAD_TURN_SERVO = 1 # pwm 2 wheels turn WHEELS_TURN_SERVO = 2 if __name__ == '__main__': print("--------------------") print("calibrate wheel turn") print("--------------------") for i in range(calValues.turn_right_max, calValues.turn_left_max,10): pwm.set_pwm(WHEELS_TURN_SERVO,0, i) print("servoValue = ", i) time.sleep(0.5) print("--------------------") print("calibrate head turn") print("--------------------") for i in range(calValues.look_right_max, calValues.look_left_max,10): pwm.set_pwm(HEAD_TURN_SERVO,0, i) print("servoValue = ", i) time.sleep(0.5) print("--------------------") print("calibrate head up/down") print("--------------------")
  • 238. BOOK 7 Building Robots with Python for i in range(calValues.look_up_max, calValues.look_down_max,10): pwm.set_pwm(HEAD_TILT_SERVO,0, i) print("servoValue = ", i) time.sleep(0.5) The code is pretty straight forward, but let me talk about one of the servo program loops. for i in range(calValues.look_right_max, calValues.look_left_max,10): pwm.set_pwm(HEAD_TURN_SERVO,0, i) print("servoValue = ", i) time.sleep(0.5) This loop steps through the servo range as given in calValues.py from the right you can add those values to calValues.py. The calValues.py You replace the values in this program with your own values from calibrate Servos.py # Servo calibration values # head look_up_max = 150 look_down_max = 420 look_tilt_middle = 330 # head turn look_right_max = 200 look_left_max = 450 look_turn_middle = 310 # wheels turn_right_max = 180 turn_left_max = 460 turn_middle = 320 # turn_speed look_turn_speed = 5 # motor speed left_spd = 100 #Speed of the car right_spd = 100 #Speed of the car
  • 239. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot Running tests on your rover in Python installed the Adeept software on your Raspberry Pi, you have to disable the auto startup of their software. To do so, change the following line in the ~/.config/autostart/car.desktop Exec=sudo python3 /home/Adeept_PiCar-B/server/server.py To #Exec=sudo python3 /home/Adeept_PiCar-B/server/server.py sudo reboot. https://youtu.be/UvxRBJ-tFw8. That is what you will be running very shortly. Installing software for the CarPi-B Python test www.dummies.com Go into the Testing directory and then complete these instructions, which are 1. Install some developer libraries, which allow us to compile the software. This is installed using the normal Raspberry Pi installer, as follows: sudo apt-get install build-essential python3-dev git scons swig 2. Download the neopixel code from github using the clone command, which copies all the source code to your local computer: git clone https://github.com/jgarff/rpi_ws281x.git
  • 240. BOOK 7 Building Robots with Python 3. Change to that directory and run scons to compile the software: cd rpi_ws281x scons 4. Change to the python directory and install the Python module from there: cd python 5. sudo python3 setup.py install The PiCar-B Python test code the Testing directory. www.dummies.com sudo python3 PiCar-B-Test.py https://youtu.be/UvxRBJ-tFw8. Pi camera video testing
  • 241. Building Your First Python Robot CHAPTER 2 Building Your First Python Robot PiCar-B-Video-Test.py #!/usr/bin/python3 DEBUG = True VIDEOTEST = True # runs through a video tests for the PiCar-B import RPi.GPIO as GPIO import motor import ultra import socket import time import threading import turn import led import os import picamera from picamera.array import PiRGBArray import cv2 Setting the VNC viewer option.
  • 242. BOOK 7 Building Robots with Python import calValues if __name__ == '__main__': camera = picamera.PiCamera() #Camera initialization camera.resolution = (640, 480) camera.framerate = 7 rawCapture = PiRGBArray(camera, size=(640, 480)) try: print ("-------------------") print ("-------------------") print (" PiCar2- Video Test") print (" Must be run from a GUI") print ("-------------------") print ("-------------------") print () print () if (VIDEOTEST): print () print ("-------------------") print ("Open Video Window") print ("-------------------") camera.resolution = (1024, 768) camera.start_preview(fullscreen=False, window=(100,100,256,192)) time.sleep(20) camera.preview.window=(200,200,256,192) time.sleep(2) camera.preview.window=(0,0,512,384) time.sleep(2) camera.close() except KeyboardInterrupt: destroy() then closes the window. Now you are done building and testing your robot. Next, we are going to add some Python brains and have some fun with the robot.
  • 243. CHAPTER 3 Programming Your Robot Rover in Python 595 Programming Your Robot Rover in Python O kay, let’s review where you are now. You have a basic understanding of robots and (more importantly) of the major components of robots and how they work. You understand that these components can be controlled with Python and that they can work together to accomplish robotic tasks. That’s really a lot of information. Next, we show you how to string these things together to make a very, very simple “robotic brain” to make our robot move by itself. This won’t quite be a fully self-driving car, but after doing this you will have some sense of how those cars are programmed. Building a Simple High-Level Python Interface more complicated programs while hiding the complexity of the robot hardware. Our high-level interface is a python RobotInterface.py. The code length is beyond what we want to show in this book, so let us describe a couple of functions and then document the rest. IN THIS CHAPTER » Learning how to move and sense with your robot » Understanding autonomous vehicles » Playing and using the Adeept software
  • 244. 596 BOOK 7 Building Robots with Python The motorForward function The motorForward function is typical of the motor functions located within the RobotInterface class: def motorForward(self, speed, delay): motor.motor_left(self.MOTOR_START, self.forward,speed) motor.motor_right(self.MOTOR_START, self.backward,speed) time.sleep(delay) motor.motor_left(self.MOTOR_STOP, self.forward,speed) motor.motor_right(self.MOTOR_STOP, self.backward,speed) This function drives the robot forward for the number of seconds passed into the function in the delay argument. This means that when you call this function, you must use an actual number in the delay argument. It basically starts the motors running forward, waits a delay and then shuts The wheelsLeft function def wheelsLeft(self): pwm.set_pwm(self.WHEELS_TURN_SERVO, 0, calValues.turn_left_max) time.sleep(0.05) The wheelsLeft function sets the WHEELS_TURN_SERVO to the leftmost position of the wheels and then delays 50ms. Why the delay? You will see these delays scat- tered through the RobotInterface commands back-to-back from exceeding the current capacity of the power sup- ply. By delaying the next servo command 50ms, the high current transient caused - mand is executed. The wheelsPercent function This function allows the user to set the servo to a percent of the total range of the servo motor. It goes from the full left (0) to full right (100) for the wheels; if you have an asymmetric range of motion of your servo. If you do, then use the wheelsMiddle() function to center your wheels.
  • 245. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 597 This code calculates the total range of motion of the servo motor and then multi- plies it by the percentage requested. It then sets the servo motor to the requested range: def wheelsPercent(self,percent): adder = (calValues.turn_left_max – calValues.turn_right_max)*(percent/100.0) pwm.set_pwm(self.WHEELS_TURN_SERVO, 0, int(calValues.turn_right_max adder)) time.sleep(0.05) Making a Single Move with Python First of all, go to this book’s support page at www.dummies.com (refer to the Intro- In the following code, we move the robot a short distance ahead with a motor Forward command and then back to its original position with a motorBackward() command. Hopefully, you’re starting to see the magic of this approach. The “Single Move” code: #!/usr/bin/python3 # Robot Interface Test import RobotInterface import time RI = RobotInterface.RobotInterface() print ("Short Move Test") RI.wheelsMiddle() RI.motorForward(100,1.0) time.sleep(1.0) RI.motorBackward(100,1.0) First, we import the RobotInterface class library and also the time library (for sleep()). Note the simplicity of this. The complexity underlying robot interface libraries are hidden by this class: import RobotInterface import time
  • 246. 598 BOOK 7 Building Robots with Python Then we initialize the RobotInterface module and assign the module to the vari- able RI: RI = RobotInterface.RobotInterface() print ("Short Move Test") We center the wheels with the wheelsMiddle() function command: RI.wheelsMiddle() Now comes the good part. We drive the robot forward for one second, pause a second, and then run it backwards for one second to the robot’s original position: RI.motorForward(100,1.0) time.sleep(1.0) RI.motorBackward(100,1.0) singleMove.py. Here is a video of what you should see when you run this code on your robot in a terminal window: https://youtu.be/UT0PG7z2ccE. to document the RobotInterface class functions. Functions of the RobotInterface Class In this section, we document the functions of the RobotInterface class. We show robot software! The RobotInterface class is derived from both original software by the author and also from internal drivers from the PiCar-B Adeept software. Front LED functions The following functions control the two LEDs on the front of the robot. set_Front_LED_On() This function sets the front LED to On: set_Front_LED_On(colorLED)
  • 247. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 599 Remember that these two front LEDs are tricolor with red, green, and blue LEDs, each of which is individually controllable. The parameter, colorLED, controls the side of the robot to turn on and the color to turn on. You set the color and select the side by using the following constants from the RobotInterface class: RobotInterface.left_R RobotInterface.left_G RobotInterface.left_B RobotInterface.right_R RobotInterface.right_G RobotInterface.right_B For example, RobotInterface.left_R turns on the red LED on the Robot Left side. Robot Left refers to the left side of the robot as viewed from the rear of the robot. You can make multiple calls to this program to turn on all three of the LEDs. Turning on an already on LED does not hurt anything and is ignored. A more sophisticated driver could be written driving the LEDs GPIOs with PWM (pulse-width modulation) allowing even greater color mixing. Note that unless of the LEDs when using this technique because of the Raspberry Pi multitasking operating system. You could, however, write drivers for the PCA9685 servo driver problem. This function sets the Front LED to Off: set_Front_LED_Off(colorLED) Remember that these two front LEDs are tricolor with red, green, and blue LEDs, each of which is individually controllable. The parameter, colorLED, controls the side of the robot to turn on and the color to turn on. You set the color and select the side by using the following constants from the RobotInterface class: RobotInterface.left_R RobotInterface.left_G RobotInterface.left_B RobotInterface.right_R RobotInterface.right_G RobotInterface.right_B
  • 248. 600 BOOK 7 Building Robots with Python For example, RobotInterface.left_R turns on the red LED on the Robot Left side. Robot Left refers to the left side of the robot as viewed from the rear of the robot. You can make multiple calls to this program to turn on all three of the LEDs. Turning on an already on LED does not hurt anything and is ignored. Pixel strip functions There are 12 LEDs on the robot strung together as a single 12 LED strip. These RGB LEDs are called Pixels and are controlled by a single serial line that runs through all 12 of the LEDs. They are controlled by a fairly sophisticated and touchy serial sequence of precisely timed pulses from the Raspberry Pi. The Raspberry Pi (again because of the operating system) cannot generate these pulses accurately enough using Python and GPIO signals. Therefore a complex driver using the DMA (direct signals. Our RobotInterface software hides all this complexity from the user. rainbowCycle() This call starts a rainbow cycle that uses all 12 of the Pixel LEDs and runs through many colors: rainbowCycle(wait_ms = 20, iterations = 3) The parameter wait_ms sets the delay (in milliseconds) between each color change. It defaults to 20ms. Interations sets the number of full color cycles to colorWipe() This function sets all 12 Pixel LEDs to the same color: colorWipe(color) For example, colorWipe(color(0,0,0)) sets all the Pixels to Off. This is a handy way to set all 12 pixels to the same color. The parameter, color be used using the color() function. (See the color() function later in this chapter.) theaterChaseRainbow() This function starts a 40-second-long pattern of chasing LEDs on all 12 of the LEDs: theaterChaseRainbow(wait_ms = 50)
  • 249. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 601 The wait_ms parameter sets the delay between each of the movements in mil- liseconds. It defaults to 50 milliseconds. setPixelColor() setPixelColor(pixel, color, brightness) The pixel The color color() function. The brightness all the pixel string. Color() This function is a helper function that converts the R, G and B values into a single 24 bit integer used by the internal Pixel driver: Color(red, green, blue, white = 0) The parameters red, green, and blue (fully on). The white=0 parameter is for RGBW Pixel LEDs. The Pixels on the robot are RGB LEDs. allLEDSOff() Ultrasonic distance sensor function The ultrasonic distance sensor functions by sending out a pulse of high frequency sound and then counting the time before it bounces back to the receiver. Because we know the speed of sound, we can calculate the distance in front of the sensor. It’s not a perfect method (we would rather be using a laser!), but it is pretty good, and it makes for a good starting distance sensor.
  • 250. 602 BOOK 7 Building Robots with Python fetchUltraDistance() This function does an immediate measurement from the ultrasonic distance sen- sor in the head of the robot and returns the distance in centimeters (cm): fetchUltraDistance() Main motor functions The main motor on our robot is what drives the back wheels and makes our robot move. The motor functions are used to tell how fast and how long to run the main motor. motorForward() This function drives the robot forward at speed for the delay number of seconds motorForward(speed, delay) The parameter speed sets the duty cycle of the PWM GPIO pin driving the inter- The parameter delay tells the driver how long to run the motor in seconds. motorBackward() This function drives the robot backwards at speed for the delay number of sec- motorBackward(speed, delay) The parameter speed sets the duty cycle of the PWM GPIO pin driving the inter- The parameter delay tells the driver how long to run the motor in seconds. stopMotor() This function immediately stops the main motor: stopMotor()
  • 251. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python This is really only useful if, for example, you were driving the motor in another thread. You can think of a thread as another program running at the same time as your main program. This is a sophisticated programming technique that has some Servo functions This group of functions control the three servos on the robot: head-turning, head-tilting, and front wheels. headTurnLeft() This function turns the robot head all the way to the left: headTurnLeft() calValues.py this minibook for information on the calibration values and how to set them using the calibrateServos.py program. headTurnRight() This function turns the robot head all the way to the right: headTurnRight() calValues.py this minibook for information on the calibration values and how to set them using the calibrateServos.py program. headTurnMiddle() This function turns the robot head towards the front: headTurnMiddle() calValues.py - book for information on the calibration values and how to set them using the calibrateServos.py program.
  • 252. 604 BOOK 7 Building Robots with Python headTurnPercent() This function turns the head from 0 (all the way to the left) to 100 (all the way to the right): headTurnPercent(percent) This is useful for more precisely aiming the head. Again, “all the way to the left” calValues.py set them using the calibrateServos.py program. The parameter percent left to right. Note that the value 50 may not be quite in the middle because your servos may not be set exactly in the middle of their range. headTiltDown() This function tilts the robot head all the way down: headTiltDown() calValues.py minibook for information on the calibration values and how to set them using the calibrateServos.py program. headTiltUp() This function tilts the robot head all the way up: headTiltUp() calValues.py minibook for information on the calibration values and how to set them using the calibrateServos.py program. headTiltMiddle() This function tilts the robot head towards the front: headTiltMiddle() calValues.py - book for information on the calibration values and how to set them using the calibrateServos.py program.
  • 253. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 605 headTiltPercent() This function turns the head from 0 (all the way down) to 100 (all the way to the up): headTiltPercent(percent) This is useful for more precisely aiming the head. Again, “all the way down and calValues.py minibook for information on the calibration values and how to set them using the calibrateServos.py program. The parameter percent down to up. Note that the value 50 may not be quite in the middle because your servos may not be set exactly in the middle of their range as set by the servo calibration process and because of the way your robot was physically built. wheelsLeft() This function turns the robot front wheels all the way to the left: wheelsLeft() calValues.py this minibook for information on the calibration values and how to set them using the calibrateServos.py program. wheelsRight() This function turns the robot front wheels all the way to the right: wheelsRight() calValues.py this minibook for information on the calibration values and how to set them using the calibrateServos.py program. wheelsMiddle() This function turns the robot front wheels to the middle: wheelsMiddle() calValues.py for information on the calibration values and how to set them using the calibra- teServos.py program.
  • 254. 606 BOOK 7 Building Robots with Python wheelsPercent() This function turns the head from 0 (all the way to the left) to 100 (all the way to the right): wheelsPercent(percent) This is useful for more precisely setting the direction of the robot front wheels. calValues.py calibration values and how to set them using the calibrateServos.py program. The parameter percent has values 0-100 and represents the linear percent from down to up. Note that the value 50 may not be quite in the middle because of how your servos may not be set exactly in the middle of their range as set by the servo calibration process and how your robot was physically built. General servo function We have included general functions to control all the servos at once. Calling this function moves all servos to the center position. centerAllServos() This function puts all the servos on the robot to the center of their ranges as calValues.py centerAllServos() The Python Robot Interface Test let’s run the system test using the RobotInterface Python class. This program is useful for two reasons. First, it tests all our functions in the RobotInterface class. Second, it shows how to use each of the functions in a Python program. The code for RITest.py: #!/usr/bin/python3 # Robot Interface Test import RobotInterface
  • 255. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 607 import time RI = RobotInterface.RobotInterface() print ("Robot Interface Test") print ("LED tests") RI.set_Front_LED_On(RI.left_R) time.sleep(0.1) RI.set_Front_LED_On(RI.left_G) time.sleep(0.1) RI.set_Front_LED_On(RI.left_B) time.sleep(1.0) RI.set_Front_LED_On(RI.right_R) time.sleep(0.1) RI.set_Front_LED_On(RI.right_G) time.sleep(0.1) RI.set_Front_LED_On(RI.right_B) time.sleep(1.0) RI.set_Front_LED_Off(RI.left_R) time.sleep(0.1) RI.set_Front_LED_Off(RI.left_G) time.sleep(0.1) RI.set_Front_LED_Off(RI.left_B) time.sleep(1.0) RI.set_Front_LED_Off(RI.right_R) time.sleep(0.1) RI.set_Front_LED_Off(RI.right_G) time.sleep(0.1) RI.set_Front_LED_Off(RI.right_B) time.sleep(1.0) RI.rainbowCycle(20, 1) time.sleep(0.5) # Runs for 40 seconds #RI.theaterChaseRainbow(50) #time.sleep(0.5) print ("RI.Color(0,0,0)=", RI.Color(0,0,0)) RI.colorWipe(RI.Color(0,0,0)) time.sleep(1.0) for pixel in range (0,12): RI.setPixelColor(pixel,RI.Color(100,200,50),50) time.sleep(0.5)
  • 256. 608 BOOK 7 Building Robots with Python print ("Servo Tests") RI.headTurnLeft() time.sleep(1.0) RI.headTurnRight() time.sleep(1.0) RI.headTurnMiddle() time.sleep(1.0) RI.headTiltDown() time.sleep(1.0) RI.headTiltUp() time.sleep(1.0) RI.headTiltMiddle() time.sleep(1.0) RI.wheelsLeft() time.sleep(1.0) RI.wheelsRight() time.sleep(1.0) RI.wheelsMiddle() time.sleep(1.0) print("servo scan tests") for percent in range (0,100): RI.headTurnPercent(percent) for percent in range (0,100): RI.headTiltPercent(percent) for percent in range (0,100): RI.wheelsPercent(percent) print("motor test") RI.motorForward(100,1.0) time.sleep(1.0) RI.motorBackward(100,1.0) print("ultrasonic test") print ("distance in cm=", RI.fetchUltraDistance()) print("general function test") RI.allLEDSOff() RI.centerAllServos() Note: We have commented out the test code for RI.theaterChaseRainbow() because it runs for 40 seconds.
  • 257. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 609 Run the program by typing sudo python3 RITest.py into a terminal window. Note that you have to use sudo because the Pixel LEDs require root permission (granted by sudo) to correctly run. Robot Interface Test LED tests RI.Color(0,0,0)= 0 Servo Tests servo scan tests motor test ROS: THE ROBOT OPERATING SYSTEM We have written a fairly simple interface class for the PiCar-B robot. This allows us to control the robot from a Python program. If we had more room in this book (actually another whole book could be written about the use of ROS for a robot like ours), we would connect up our robot to the ROS (Robot Operating System). ROS is a system spe- the Robot Operating System, it really isn’t an operating system. ROS is what is called middleware. Middleware is software that is designed to manage the complexity of writing software in a complex and heterogenous (meaning lots of dif- robots in a very similar manner. ROS operates using what is called a publish-subscribe system. It works like a newspaper. A newspaper publishes stories, but only the people that subscribe to the newspaper see those stories. You might have a subscriber that only wants a subscription to the comics. Or the front page. ROS works like that. A robot like ours may publish the current value of the ultrasonic sensor or the current camera image (or even a video stream) and other computers or robots on the network could subscribe to the video stream and see what your robot is seeing. And your robot could subscribe to other sensors (such as a temperate sensor located in the middle of the room) or even look at what the other robots are seeing. The power of this technique is that now you can make your robot part of an ecosystem con- sisting of computers, sensors, and even people making use of your data and contribut- ing information to your robot. We could build a ROS interface on our robot and then we could control it remotely and feed sensor data to other computers. In many ways, ROS really rocks. Find out more about ROS at http://www.ros.org/.
  • 258. 610 BOOK 7 Building Robots with Python ultrasonic test distance in cm= 16.87312126159668 general function test Here’s a link to the video of the RobotInterface class test: https://youtu. be/1vi-UGao0oI Coordinating Motor Movements with Sensors The ability to modify and coordinate motor movements with sensor movements is key to movement in the environment. Sensors give information to be acted upon as well as feedback from our motions. Think of the act of catching a baseball with a glove. Your sensors? Eyes and the sense of touch. Your eyes see the ball and then move your hand and arm to intercept the ball. That’s coordinating your movement with a sensor. The feedback? Knowing you have caught the ball in your mitt by the feeling of it hitting your gloved hands. Of course, you are also updating your internal learning system to become better at catching the ball. PiCar-B has two sensors that read information from the outside world. The ultra- sonic sensor can detect what is in front of the robot while the camera can photo- to remember, however, is that robot vision is hard. Very hard. We touch upon - ple of how to do this using machine learning. For our example, we will focus on the simpler sensor, the ultrasonic distance sensor. Here is an example of code that will move the robot forward or backwards depend- ing on the distance from the object in front of the robot. Here is the Python code for simpleFeedback.py: #!/usr/bin/python3 # Robot Interface Test import RobotInterface import time DEBUG = True
  • 259. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 611 RI = RobotInterface.RobotInterface() print ("Simple Feedback Test") RI.centerAllServos() RI.allLEDSOff() # Ignore distances greater than one meter DISTANCE_TO_IGNORE = 1000.0 # Close to 10cm with short moves DISTANCE_TO_MOVE_TO = 10.0 # How many times before the robot gives up REPEAT_MOVE = 10 def bothFrontLEDSOn(color): RI.allLEDSOff() if (color == "RED"): RI.set_Front_LED_On(RI.right_R) RI.set_Front_LED_On(RI.left_R) return if (color == "GREEN"): RI.set_Front_LED_On(RI.right_G) RI.set_Front_LED_On(RI.left_G) return if (color == "BLUE"): RI.set_Front_LED_On(RI.right_B) RI.set_Front_LED_On(RI.left_B) return try: Quit = False moveCount = 0 bothFrontLEDSOn("BLUE") while (Quit == False): current_distance = RI.fetchUltraDistance() if (current_distance >= DISTANCE_TO_IGNORE): bothFrontLEDSOn("BLUE") if (DEBUG): print("distance too far ={:6.2f}cm" .format(current_distance)) else: if (current_distance <= 10.0): # reset moveCount # the Robot is close enough
  • 260. 612 BOOK 7 Building Robots with Python bothFrontLEDSOn("GREEN") moveCount = 0 if (DEBUG): print("distance close enough ={:6.2f}cm" .format(current_distance)) time.sleep(5.0) # back up and do it again RI.motorBackward(100,1.0) else: if (DEBUG): print("moving forward ={:6.2f}cm" .format(current_distance)) # Short step forward bothFrontLEDSOn("RED") RI.motorForward(90,0.50) moveCount = moveCount 1 # Now check for stopping our program time.sleep(1.0) if (moveCount > REPEAT_MOVE): Quit = True except KeyboardInterrupt: print("program interrupted") print ("program finished") see if it is less than one meter (1000cm) from the wall. If it is, it slowly starts to advance towards the wall in short little steps. When it is closer than 10cm to the It also gives up if it takes more than 10 moves to get to the wall, if somehow we have moved further than 1000cm away from the wall, or if the user has hit Ctrl-C to interrupt the program. Note how we use the LEDs to give feedback to surrounding people as to what the robot is doing. This sort of visual feedback is an important part of making The main structure of the program is contained within a Python while loop. As long as we haven’t interrupted the program (or one of the other quit criteria hasn’t
  • 261. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python Copy the code into simpleFeedback.py and give it a try by executing sudo python3 simpleFeedback.py. Here are the printed results: Simple Feedback Test moving forward = 55.67cm moving forward = 44.48cm moving forward = 34.22cm moving forward = 26.50cm moving forward = 17.53cm distance close enough = 9.67cm moving forward = 66.64cm moving forward = 54.25cm moving forward = 43.55cm moving forward = 36.27cm moving forward = 28.44cm moving forward = 21.08cm moving forward = 13.55cm distance close enough = 6.30cm moving forward = 64.51cm moving forward = 52.89cm moving forward = 43.75cm moving forward = 33.95cm moving forward = 26.79cm ^Cprogram interrupted program finished And you can see the feedback video here: https://youtu.be/mzZIMxch5k4. results. Making a Python Brain for Our Robot Now we are going to create a simple self-driving car. In a sense, we are going to apply the results above to create an autonomous vehicle that is not very smart but illustrates the use of feedback in decision-making. This Python brain we are writing is nothing more than a combination of our code for sensing the wall (from earlier in this chapter) and for generating a random walk based on the information. After running the code for a while, we saw where the robot would get stuck and added code to detect back out of stuck positions.
  • 262. 614 BOOK 7 Building Robots with Python Note: Make sure you have fully charged up batteries to run this code. When the bigger batteries. The “Robot Brain” code: #!/usr/bin/python3 # Robot Brsin import RobotInterface import time from random import randint DEBUG = True RI = RobotInterface.RobotInterface() print ("Simple Robot Brain") RI.centerAllServos() RI.allLEDSOff() # Close to 20cm CLOSE_DISTANCE = 20.0 # How many times before the robot gives up REPEAT_TURN = 10 def bothFrontLEDSOn(color): RI.allLEDSOff() if (color == "RED"): RI.set_Front_LED_On(RI.right_R) RI.set_Front_LED_On(RI.left_R) return if (color == "GREEN"): RI.set_Front_LED_On(RI.right_G) RI.set_Front_LED_On(RI.left_G) return if (color == "BLUE"): RI.set_Front_LED_On(RI.right_B) RI.set_Front_LED_On(RI.left_B) return STUCKBAND = 2.0 # check for stuck car by distance not changing def checkForStuckCar(cd,p1,p2):
  • 263. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 615 if (abs(p1-cd) < STUCKBAND): if (abs(p2-cd) < STUCKBAND): return True return False try: Quit = False turnCount = 0 bothFrontLEDSOn("BLUE") previous2distance = 0 previous1distance = 0 while (Quit == False): current_distance = RI.fetchUltraDistance() if (current_distance >= CLOSE_DISTANCE ): bothFrontLEDSOn("BLUE") if (DEBUG): print("Continue straight ={:6.2f}cm" .format(current_distance)) if (current_distance > 300): # verify distance current_distance = RI.fetchUltraDistance() if (current_distance > 300): # move faster RI.motorForward(90,1.0) else: RI.motorForward(90,0.50) turnCount = 0 else: if (DEBUG): print("distance close enough so turn ={:6.2f}cm" .format(current_distance)) bothFrontLEDSOn("RED") # now determine which way to turn # turn = 0 turn left # turn = 1 turn right turn = randint(0,1) if (turn == 0): # turn left # we turn the wheels right since # we are backing up RI.wheelsRight() else:
  • 264. 616 BOOK 7 Building Robots with Python # turn right # we turn the wheels left since # we are backing up RI.wheelsLeft() time.sleep(0.5) RI.motorBackward(100,1.00) time.sleep(0.5) RI.wheelsMiddle() turnCount = turnCount 1 print("Turn Count =", turnCount) # check for stuck car if (checkForStuckCar(current_distance, previous1distance, previous2distance)): # we are stuck. Try back up and try Random turn bothFrontLEDSOn("RED") if (DEBUG): print("Stuck - Recovering ={:6.2f}cm" .format(current_distance)) RI.wheelsMiddle() RI.motorBackward(100,1.00) # now determine which way to turn # turn = 0 turn left # turn = 1 turn right turn = randint(0,1) if (turn == 0): # turn left # we turn the wheels right since # we are backing up RI.wheelsRight() else: # turn right # we turn the wheels left since # we are backing up RI.wheelsLeft() time.sleep(0.5) RI.motorBackward(100,2.00) time.sleep(0.5) RI.wheelsMiddle() # load state for distances previous2distance = previous1distance previous1distance = current_distance
  • 265. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 617 # Now check for stopping our program time.sleep(0.1) if (turnCount > REPEAT_TURN-1): bothFrontLEDSOn("RED") if (DEBUG): print("too many turns in a row") Quit = True except KeyboardInterrupt: print("program interrupted") print ("program finished") This seems to be a much more complex program than our ultrasonic sensor pro- gram earlier in this chapter, but it is really not. We took the same structure of the program (the while loop) and added several features. First, we added a clause to speed up the car when we were far away from an if (current_distance >= CLOSE_DISTANCE ): bothFrontLEDSOn("BLUE") if (DEBUG): print("Continue straight ={:6.2f}cm" .format(current_distance)) if (current_distance > 300): # verify distance current_distance = RI.fetchUltraDistance() if (current_distance > 300): # move faster RI.motorForward(90,1.0) else: RI.motorForward(90,0.50) turnCount = 0 We continued to move in short little hops as the robot gets closer to the wall. When the robot gets within about 10cm of the wall, the robot decides to turn its front wheels in a random direction and backs up to try a new direction: if (DEBUG): print("distance close enough so turn ={:6.2f}cm" .format(current_distance)) bothFrontLEDSOn("RED") # now determine which way to turn # turn = 0 turn left
  • 266. 618 BOOK 7 Building Robots with Python # turn = 1 turn right turn = randint(0,1) if (turn == 0): # turn left # we turn the wheels right since # we are backing up RI.wheelsRight() else: # turn right # we turn the wheels left since # we are backing up RI.wheelsLeft() time.sleep(0.5) RI.motorBackward(100,1.00) time.sleep(0.5) RI.wheelsMiddle() turnCount = turnCount 1 print("Turn Count =", turnCount) We ran the robot for quite a while with just this logic, and we would see it get stuck if part of the robot was blocked, but the ultrasonic sensor was still picking up greater than 10cm distance. - ings, and if you had three readings /- 2.0cm, then the robot would decide it was stuck and back up, turn randomly, and proceed again to wandering. Worked like a champ: if (checkForStuckCar(current_distance, previous1distance, previous2distance)): # we are stuck. Try back up and try Random turn bothFrontLEDSOn("RED") if (DEBUG): print("Stuck - Recovering ={:6.2f}cm" .format(current_distance)) RI.wheelsMiddle() RI.motorBackward(100,1.00) # now determine which way to turn # turn = 0 turn left # turn = 1 turn right turn = randint(0,1)
  • 267. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 619 if (turn == 0): # turn left # we turn the wheels right since # we are backing up RI.wheelsRight() else: # turn right # we turn the wheels left since # we are backing up RI.wheelsLeft() time.sleep(0.5) RI.motorBackward(100,2.00) time.sleep(0.5) RI.wheelsMiddle() We set the robot down in a room that has furniture and a complex set of walls and let it loose. Here are the results from the console: Simple Robot Brain Continue straight =115.44cm Continue straight =108.21cm Continue straight =101.67cm Continue straight = 95.67cm Continue straight = 88.13cm Continue straight = 79.85cm Continue straight = 70.58cm Continue straight = 63.89cm Continue straight = 54.36cm Continue straight = 44.65cm Continue straight = 36.88cm Continue straight = 28.32cm Continue straight = 21.10cm distance close enough so turn = 11.33cm Turn Count = 1 Continue straight = 33.75cm Continue straight = 25.12cm distance close enough so turn = 18.20cm Turn Count = 1 Continue straight = 40.51cm Continue straight = 33.45cm Continue straight = 24.73cm distance close enough so turn = 14.83cm Turn Count = 1 Continue straight = 35.72cm Continue straight = 26.13cm distance close enough so turn = 18.56cm
  • 268. 620 BOOK 7 Building Robots with Python Turn Count = 1 Continue straight = 43.63cm Continue straight = 37.74cm Continue straight = 27.33cm Continue straight = 84.01cm way out and then continue on. in the video here: https://youtu.be/U7_FJzRbsRw. A Better Robot Brain Architecture If you look at the robotBrain.py software from an software architectural perspec- tive, one thing jumps out. The main part of the program is a single while loop that polls the sensor (the ultrasonic sensor) and then does one thing at a time (moves, turns, and so on) and then polls it again. This leads to the somewhat jerky behavior of the robot (move a little, sense, move a little, sense, and so on). Although this is the simplest architecture we could use for our example, there are better, albeit more complicated, ways of doing this that are beyond the scope of our project today. These better architectures are based on what are called threads. You can think of threads as separate programs that run at the same time and communicate to each other by using things called semaphores and data queues. Semaphores and data queues are simply methods by which a thread can communicate with other threads in a safe manner. Because both threads are running at the same time, you have to be careful how they talk and exchange information. This is not compli- cated, if you follow the rules. A better architecture for our robot brain would be like this: » Motor thread: This thread controls the motors. It makes them run on command and can stop the motors anytime. » Sensor thread: This thread reads the ultrasonic sensor (and any other sensors you may have) periodically so you always have the current distance available. » Head thread: This thread controls the head servos using commands from the Command thread. » Command thread: This is the brains of software. It takes current information from the Sensor thread and sends commands to the motors and head to their respective thread.
  • 269. Programming Your Robot Rover in Python CHAPTER 3 Programming Your Robot Rover in Python 621 This architecture leads to a much smoother operation of the robot. You can have the motors running while you are taking sensor values and sending commands simultaneously. This is the architecture that is used in the Adeept software server.py Overview of the Included Adeept Software server model in which the client is a control panel on another computer and the - trol the robot remotely and has a lot of interesting features, such as object track- ing using OpenCV and a radarlike ultrasonic mapping capability. You can also see the video coming from the robot and use that to navigate manually. It is pretty complicated to install, however, so pay close attention to the instructions. - ming. The software is all open source, so you can look inside to see how they are doing things. Especially check out server.py under the server directory and look at the way they use threading to get smooth motion out of the robot. Adeept remote control software.
  • 270. 622 BOOK 7 Building Robots with Python Where to Go from Here? You now have a small robot that can display quite complex behavior based on the ultrasonic sensor built in. You can add a lot to this robot in terms of adding sen- Light conditions?) Refer back to Book 6 and combine some of the motors and sen- sors you used there with this robot. Because we choose the PiCar-B, you can plug the Pi2Grover on top of the motor controller so you can use all the Grove devices you have accumulated. The sky is the limit!
  • 271. CHAPTER 4 623 —DICTIONARY.COM S o, AI is meant to replace people? Well, not really. Modern AI looks to enhance machine intelligence at certain tasks that are normally done by people. Even saying the words “machine intelligence” is somewhat of a misnomer because it is hard to claim that machines have intelligence at all, at least as we think of it in people. Instead of the philosophical debate, let’s focus on how to use some modern AI techniques in a real robot example. For a better overview of AI and some of the philosophy involved, check out Book 4. So, what AI technique can we use in our robotic car? Turns out there is a Pi camera on the car, and computer vision is really hard, so let’s do something with that. » » »
  • 272. 624 BOOK 7 Making robots see is easy, but making them understand what they are seeing is exceptionally hard. If you want to study up on computer vision using Python, check out by Matthew Rever. In this chapter, we show you how to build a machine-learning neural network and train it to recognize cats versus dogs. (This is a skill all robots should have.) We will train the network using a 1,000-image subset of the 25,000 element data- base of pictures of cats and dogs from the Kaggle cats and dog database using TensorFlow. TensorFlow is a Python package that is also designed to support neural networks major respect: TensorFlow is designed for use in machine learning and AI appli- cations, and so it has libraries and functions designed for those applications. If you need to, refer back to Book 4 as it has extensive information and examples about using TensorFlow in Python and on the Raspberry Pi. link: https://www.tensorflow.org/install/pip. Download TensorFlow and install according to the directions. Download the truncated list of the Cats and Dogs database here: https:// github.com/switchdoclabs/CatsAndDogsTruncatedData. It is about 65mb and is included with our software at dummies.com. For more experimentation, download the full Cats and Dogs dataset from this link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data. Another source of the full data is: https://www.microsoft.com/en-us/download/ details.aspx?id=54765. Unzip the main folder and then unzip the test.zip and the train.zip subfolders. Test train contains our training data that we will use to train our neural network.
  • 273. CHAPTER 4 625 Run the following command in your program directory to download the data: git clone https://github.com/switchdoclabs/CatsAndDogsTruncatedData.git Now that we have our data ready, let’s go train that network. Our goal in this section is to fully train our machine learning neural network on trained neural network so we can actually use it on our robot. Then the real fun will begin! When you run the following program, if you see ImportError: No module named 'seaborn', type sudo pip3 install seaborn. We starting using a pretty simple two-layer neural network for our cats and dogs machine learning network. There are many more complex networks available and may give better results, but we were hoping this would be good enough for our needs. There is also the option of using a much larger dataset (our training dataset has 1,000 cats and 1,000 dogs but over 25,000 images are available in the full dataset). Using a simple two-layer neural network on the cats and dog dataset did not really work very well because we achieved only about a 51 percent detection rate (50 per- cent is as good as just guessing randomly) so we needed to go to a more complex neural network that works better on complex images. You can use CNN (convolutional neural networks) in place of simple neural net- works, data augmentation (increasing the training samples by rotating, shifting, and zooming that pictures) and a variety of other techniques that are beyond the scope of this book. We are going to use a pretty standard six-layer CNN instead of our simple neural network. We changed the model layers in our program to use the following six-level convo- lutional layer model. You just have to love how easy Keras and TensorFlow makes it to dramatically change the neural network.
  • 274. 626 BOOK 7 You can see the complexity of our new network by looking at the model.summary() results: _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 150, 150, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 75, 75, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 75, 75, 32) 9248 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 37, 37, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 37, 37, 64) 18496 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 18, 18, 64) 0 _________________________________________________________________ dropout (Dropout) (None, 18, 18, 64) 0 _________________________________________________________________ flatten (Flatten) (None, 20736) 0 _________________________________________________________________ dense (Dense) (None, 64) 1327168 _________________________________________________________________ dropout_1 (Dropout) (None, 64) 0 _________________________________________________________________ dense_1 (Dense) (None, 2) 130 ================================================================= Total params: 1,355,938 Trainable params: 1,355,938 Non-trainable params: 0 CNNs work by scanning images and analyzing them chunk by chunk, say at a 5x5 window that moves by a stride length of two pixels each time until it spans the entire message. It’s like looking at an image using a microscope; you see only a small part of the picture at any one time, but eventually you see the whole picture. Every time we loop through the data is called an epoch. Going to a CNN network on a Raspberry Pi increased the single epoch time to 1,000 seconds from the 10-seconds epoch we had on the simple two-layer network. And it has a CPU utilization of 352 percent, which means it is using 3.5 cores on the Raspberry Pi, a machine that only has a total of 4. This amounts to a pretty high utilization of the Raspberry Pi 3B . The little board is using almost 3.8W up from about 1.5W normally.
  • 275. CHAPTER 4 627 #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.layers import * # load data img_width = 150 img_height = 150 train_data_dir = 'data/train' valid_data_dir = 'data/validation' datagen = ImageDataGenerator(rescale = 1./255) train_generator = datagen.flow_from_directory( directory=train_data_dir, target_size=(img_width,img_height), classes=['dogs','cats'], class_mode='binary', batch_size=16) validation_generator = datagen.flow_from_directory(directory=valid_data_dir, target_size=(img_width,img_height), classes=['dogs','cats'], class_mode='binary', batch_size=32) # build model model = tf.keras.Sequential() model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3), padding='same', activation='relu'))
  • 276. 628 BOOK 7 model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(32, (3, 3), padding='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(2, activation='softmax')) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) print (model.summary()) # train model print('starting training....') history = model.fit_generator(generator=train_generator, steps_per_epoch=2048 // 16,epochs=20, validation_data=validation_generator,validation_steps=832//16) print('training finished!!') # save coefficients model.save("CatsVersusDogs.trained") # Get training and test loss histories training_loss = history.history['loss'] accuracy = history.history['acc'] # Create count of the number of epochs epoch_count = range(1, len(training_loss) 1) # Visualize loss history plt.figure(0) plt.plot(epoch_count, training_loss, 'r--') plt.plot(epoch_count, accuracy, 'b--') plt.legend(['Training Loss', 'Accuracy']) plt.xlabel('Epoch') plt.ylabel('History') plt.grid(True) plt.show(block=True);
  • 277. CHAPTER 4 629 CatsVersusDogs.py. The code has 93 lines. It’s pretty amazing what we can do with so few lines of code. If you want to learn more about neural networks and machine learning, refer back to Book 4. First, we import all the libraries: #import libraries import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import seaborn as sns import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.examples.tutorials.mnist import input_data from PIL import Image from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.layers import * Next, we massage the data into tensors (matrices) for training through our neural network. The cat and dog images are stored under separate directories under data: # load data img_width = 150 img_height = 150 train_data_dir = 'data/train' valid_data_dir = 'data/validation' datagen = ImageDataGenerator(rescale = 1./255) train_generator = datagen.flow_from_directory( directory=train_data_dir, target_size=(img_width,img_height), classes=['dogs','cats'], class_mode='binary', batch_size=16)
  • 278. 630 BOOK 7 validation_generator = datagen.flow_from_directory(directory=valid_data_dir, target_size=(img_width,img_height), classes=['dogs','cats'], class_mode='binary', batch_size=32) Now we build the neural network that forms the basis of the machine-learning model. It is a six-layer neural network: # build model model = tf.keras.Sequential() activation function. This is quite common: model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3), padding='same', activation='relu')) This next layer, MaxPooling2D model.add(MaxPooling2D(pool_size=(2, 2))) Next, another two layers of convolutional neural networks are added, each fol- lowed by a pooling layer: model.add(Conv2D(32, (3, 3), padding='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2))) machine matches the data too closely such that the network won’t match any input units to zero. We are removing 25 percent of the input units in this layer: model.add(Dropout(0.25))
  • 279. CHAPTER 4 631 connected 64-neuron layer: model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(2, activation='softmax')) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) print (model.summary()) # train model : print('starting training....') history = model.fit_generator(generator=train_generator, steps_per_epoch=2048 // 16,epochs=20, validation_data=validation_generator,validation_steps=832//16) print('training finished!!') per run! # save coefficients model.save("CatsVersusDogs.trained") Finally, we generate a graph using MatPlotLib that shows how the accuracy improves with each epoch: # Get training and test loss histories training_loss = history.history['loss'] accuracy = history.history['acc']
  • 280. 632 BOOK 7 # Create count of the number of epochs epoch_count = range(1, len(training_loss) 1) # Visualize loss history plt.figure(0) plt.plot(epoch_count, training_loss, 'r--') plt.plot(epoch_count, accuracy, 'b--') plt.legend(['Training Loss', 'Accuracy']) plt.xlabel('Epoch') plt.ylabel('History') plt.grid(True) plt.show(block=True); Install the h5py library before running this program. Otherwise the save state- ment will not work. sudo apt-get install python-h5py Showtime! Run the following command in your terminal window: python3 CatsVersusDogs.py to generate 20 epochs of training and save the results into our CatsVersusDogs.training last epoch is as follows: Epoch 20/20 128/128 [==============================] - 894s 7s/step - loss: 0.0996 - acc: 0.9609 - val_loss: 1.1069 - val_acc: 0.7356 training finished!! You can safely ignore warnings from TensorFlow, such as: /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5 return f(*args, **kwds) /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412 return f(*args, **kwds)
  • 281. CHAPTER 4 633 By the shape of the curve, we might have been able to get a little better by running more epochs, but this is good enough for our example. Time to do a test on an external cat/dog image. We’re going to use the trained data from our neural network training session above to do a couple of predictions on new pictures to the neural network. Cats and dogs recognition accuracy per epoch.
  • 282. 634 BOOK 7 #import libraries import numpy as np import tensorflow as tf from tensorflow.python.framework import ops from PIL import Image Panther the Cat on salmon. Winston the Dog.
  • 283. CHAPTER 4 635 print("import complete") # load model img_width = 150 img_height = 150 class_names = ["Dog", "Cat"] model = tf.keras.models.load_model( "CatsVersusDogs.trained",compile=True) print (model.summary()) # do cat single image imageName = "Cat150x150.jpeg" testImg = Image.open(imageName) testImg.load() data = np.asarray( testImg, dtype="float" ) data = np.expand_dims(data, axis=0) singlePrediction = model.predict(data, steps=1) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print(NumberElement) print(Element) print(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") # do dog single image imageName = "Dog150x150.JPG" testImg = Image.open(imageName) testImg.load() data = np.asarray( testImg, dtype="float" ) data = np.expand_dims(data, axis=0) singlePrediction = model.predict(data, steps=1) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print(NumberElement) print(Element) print(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level")
  • 284. 636 BOOK 7 This code uses the training that we generated by training with the cats and dogs dataset earlier in this chapter. First, we import our libraries: #import libraries import numpy as np import tensorflow as tf from tensorflow.python.framework import ops from PIL import Image print("import complete") # load model img_width = 150 img_height = 150 class_names = ["Dog", "Cat"] Here is the place where we load the training data that we generated earlier for the neural network machine learning model: model = tf.keras.models.load_model( "CatsVersusDogs.trained",compile=True) print (model.summary()) Now, we test a single cat image: # do cat single image imageName = "Cat150x150.jpeg" testImg = Image.open(imageName) testImg.load() Convert to a NumPy tensor: data = np.asarray( testImg, dtype="float" ) Expand the dimension, since this function looks for an array of images: data = np.expand_dims(data, axis=0)
  • 285. CHAPTER 4 637 Now, we do the predication based on our image: singlePrediction = model.predict(data, steps=1) We print out the raw data: NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print(NumberElement) print(Element) print(singlePrediction) Interpret the prediction: print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") Next, we do the same with a single dog image: # do dog single image imageName = "Dog150x150.JPG" testImg = Image.open(imageName) testImg.load() data = np.asarray( testImg, dtype="float" ) data = np.expand_dims(data, axis=0) singlePrediction = model.predict(data, steps=1) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print(NumberElement) print(Element) print(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) print (str(int(Element*100)) "% Confidence Level") Save the code into singleTestImage.py and run using sudo python3 singleTest Image.py.
  • 286. 638 BOOK 7 Here are the results: import complete WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually. _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 150, 150, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 75, 75, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 75, 75, 32) 9248 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 37, 37, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 37, 37, 64) 18496 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 18, 18, 64) 0 _________________________________________________________________ dropout (Dropout) (None, 18, 18, 64) 0 _________________________________________________________________ flatten (Flatten) (None, 20736) 0 _________________________________________________________________ dense (Dense) (None, 64) 1327168 _________________________________________________________________ dropout_1 (Dropout) (None, 64) 0 _________________________________________________________________ dense_1 (Dense) (None, 2) 130 ================================================================= Total params: 1,355,938 Trainable params: 1,355,938 Non-trainable params: 0 _________________________________________________________________ None 1 1.0 [[ 0. 1.]] Our Network has concluded that the file 'Cat150x150.jpeg' is a Cat 100% Confidence Level 0 1.0 [[ 1. 0.]] Our Network has concluded that the file 'Dog150x150.JPG' is a Dog 100% Confidence Level
  • 287. CHAPTER 4 639 99.99 percent or something like that and is just rounded to 100 percent by the formatting. Now that we have the trained model and have tested it with some real images, it is time to put it into our robot and use the Pi camera for some dog and cat investigation. A real limitation of the way we built this neural network is that it really is only looking at images of cats and dogs and determining whether the image is a cat or as either a cat or dog. If we were test of the network using the much maligned dress picture from Book 4. And, as expected, the network got it wrong: Our Network has concluded that the file 'Dress150x150.JPG' is a Cat There is a lot more to the practice of teaching a machine to learn in a general manner. If you get an error such as: undefined symbol: cblas_sgemm from your import numpy, try running your program with sudo. A picture of a dress?
  • 288. 640 BOOK 7 Time to add a new experience to last chapter’s robot. We are going to install the trained cats and dogs neural network on the PiCar-B robot and use the onboard LEDs to display whether the onboard Pi camera is looking at a cat or a dog. when the ultrasonic sensor changes, such as when a dog or cat may walk in front of the robot. It has the robot staring at the screen (triggering the ultrasonic sensor) showing a PowerPoint presentation of various cats and dogs. #!/usr/bin/python3 #using a neural network with the Robot #import libraries import numpy as np import tensorflow as tf Robot vision neural network test setup.
  • 289. CHAPTER 4 641 from tensorflow.python.framework import ops from PIL import Image import RobotInterface import time import picamera print("import complete") RI = RobotInterface.RobotInterface() # load neural network model img_width = 150 img_height = 150 class_names = ["Dog", "Cat"] model = tf.keras.models.load_model("CatsVersusDogs.trained",compile=True) RI.centerAllServos() RI.allLEDSOff() # Ignore distances greater than one meter DISTANCE_TO_IGNORE = 1000.0 # How many times before the robot gives up DETECT_DISTANCE = 60 def bothFrontLEDSOn(color): RI.allLEDSOff() if (color == "RED"): RI.set_Front_LED_On(RI.right_R) RI.set_Front_LED_On(RI.left_R) return if (color == "GREEN"): RI.set_Front_LED_On(RI.right_G) RI.set_Front_LED_On(RI.left_G) return if (color == "BLUE"): RI.set_Front_LED_On(RI.right_B) RI.set_Front_LED_On(RI.left_B) return
  • 290. 642 BOOK 7 def checkImageForCat(testImg): # check dog single image data = np.asarray( testImg, dtype="float" ) data = np.expand_dims(data, axis=0) singlePrediction = model.predict(data, steps=1) print ("single Prediction =", singlePrediction) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) return class_names[NumberElement] try: print("starting sensing") Quit = False trigger_count = 0 bothFrontLEDSOn("RED") #RI.headTiltPercent(70) camera = picamera.PiCamera() camera.resolution = (1024, 1024) camera.start_preview(fullscreen=False, window=(150,150,100,100)) # Camera warm-up time time.sleep(2) while (Quit == False): current_distance = RI.fetchUltraDistance() print ("current_distance = ", current_distance) if (current_distance < DETECT_DISTANCE): trigger_count = trigger_count 1 print("classifying image") camera.capture('FrontView.jpg') imageName = "FrontView.jpg" testImg = Image.open(imageName) new_image = testImg.resize((150, 150)) new_image.save("FrontView150x150.jpg")
  • 291. CHAPTER 4 643 if (checkImageForCat(new_image) == "Cat"): bothFrontLEDSOn("GREEN") else: bothFrontLEDSOn("BLUE") time.sleep(2.0) bothFrontLEDSOn("RED") time.sleep(7.0) except KeyboardInterrupt: print("program interrupted") print ("program finished") Most of the preceding code is pretty straightforward and similar to the robot brain software earlier in the chapter. One part does deserve to be talked about, however, def checkImageForCat(testImg): # check dog single image data = np.asarray( testImg, dtype="float" ) data = np.expand_dims(data, axis=0) singlePrediction = model.predict(data, steps=1) print ("single Prediction =", singlePrediction) NumberElement = singlePrediction.argmax() Element = np.amax(singlePrediction) print ("Our Network has concluded that the file '" imageName "' is a " class_names[NumberElement]) return class_names[NumberElement] This function takes the incoming test vision (taken from the Pi camera and then robotVision.py sudo python3 robotVision.py
  • 292. 644 BOOK 7 really should run this program on the Raspberry Pi GUI so you can see the small camera preview on the screen. current_distance = 20.05481719970703 classifying image single Prediction = [[ 0. 1.]] Our Network has concluded that the file 'FrontView.jpg' is a Cat 100.00% Confidence Level current_distance = 20.038604736328125 classifying image single Prediction = [[ 1. 0.]] Our Network has concluded that the file 'FrontView.jpg' is a Dog 100.00% Confidence Level current_distance = 19.977807998657227 Overall, the results were pretty good. We found variations in recognition due to lighting, which wasn’t a big surprise. However, the network consistently identi- out what is wrong with a neural network machine learning model like this is there is no way to really know how the machine made its decision. Looking at the net- work you can look at 1.3 parameters and weights and you can’t tell where it is going wrong. After a while, you get a feel for what works and what doesn’t wrong, you are just out of luck. The cat who is apparently a dog.
  • 293. CHAPTER 4 645 We have a nefarious plan here for a variant of this neural network in the future. We’re going to use it on a new project, the Raspberry Pi based MouseAir, that will launch toy mice when the camera spots a cat but not when it spots a dog. You can be fun. As part of an advanced Python brain on the PiCar-B, there are a number of things you could do with neural networks and other AI techniques. Here are a few examples: Retrain our Cat/Dog network to focus only on cats; it lumps everything else into the “Not Cat” category. You can kind of do that with the current network because this training set tends to somewhat classify anything that doesn’t look like a cat as a dog. It doesn’t do that all the time though. Remember the dress picture in MouseAir, an AI mouse-launching cat toy.
  • 294. 646 BOOK 7 Take the streaming video from our Raspberry Pi and grab a frame once in a while and check with a neural network (trained on Santa images) to determine whether Santa is here for Christmas. Impossible! No, wait. It’s been done in a very cool series of articles here: https://www.pyimagesearch.com/2017/12/18/ keras-deep-learning-raspberry-pi/. You can program the Raspberry Pi using a library called OpenCV (Open-Source Computer Vision) to detect a ball in your camera stream and give you the change the tilt to look for it too. A version of this project is part of the Adeept soft- server.py under the server directory. You can use the power of the Amazon Alexa program to order your robot to do things. If you’d like to see how to connect up your Raspberry Pi to Alexa to do just that kind of project, check out the ebook by John Shovic. becomes less and less expensive and the software techniques more easily accessi- ble, there will be more and more uses for robots, not only manufacturing but also in the home and on the road. We expect to see new AI in many new consumer products in the coming years. The only question we have is “How long before we have AI in my toaster?” We’ll buy one when they come out.
  • 295. Index 647 Index Special Characters - operator, 70 / (division), 117 // / operator, 70 (backslash), 68 n, 95 + operator, 70 = assignment operator, 152 = operator, 80 == operator, 71, 80, 126 != operator, 71, 126 # sign, 195 $, number containing, 65 % operator, 70 %% directive, 110 %20 ASCII code, 329 %a, %b %d %Y format string, 111 %A, %B %d at %I:%M%p format string, 114 %A %B %d is day number %j of %Y format string, 111 %A directive, 110 %a directive, 110 %b directive, 110 %B directive, 110 %c directive, 110 %c format string, 114 %d directive, 110 %f directive, 110 %H directive, 110 %H:%M:%S and %f microseconds format string, 113 %I directive, 110 %I:%M %p format string, 113 %I:%M %p on %b %d format string, 114 %I:%M %p on %m/%d/%y format string, 114 %j directive, 110 %M directive, 110 %m directive, 110 %m/%d/%y at %H:%M%p format string, 114 %m/%d/%y at %I:%M %p format string, 114 %m-%d-%y format string, 111 %m/%d/%Y format string, 160 %p directive, 110 %S directive, 110 %U directive, 110 %w directive, 110 %W directive, 110 %X directive, 110 %x directive, 110 %x format string, 111 %X format string, 113 %x format string, 114 %Y directive, 110 %y directive, 110 %Z directive, 110 %z directive, 110 * operator, 70 ** operator, 70 *args, 204 @ decorator, 231 @staticmethod decorator, 231 < operator, 71, 126 <= operator, 71, 126 <article>...</article> tags, 333 > operator, 71, 126 >= operator, 71, 126 Numbers 3.3V base units, 497 3.3V I2C Grove module, 489 3D printers, 540 5cm-long Grove cables, 492
  • 296. 648 Python All-in-One For Dummies 5V base units, 497 5V I2C Grove connector, 489 5V pins, 549 5V stepper motor, 556 12C device building dashboard on phone, 525–536 adding Blynk dashboard, 527–530 breaking down code, 533–535 building on experience, 536 HDC1080 temperature and humidity, 525–526 temperatureTest.py software, 531–533 analog-to-digital converter (ADC), 518–519 breaking down code, 522–525 Grove gas sensor (O2), 519 oxygen experiment, 520–522 understanding, 506–517 breaking down program, 514–517 on Raspberry Pi, 507–508 reading temperature and humidity from, 511–514 talking to, 508–511 28BYJ-48 ULN2003 5 V stepper motor, 556, 557 125KHz RFID reader, 495 200 HTTP Status code, 326 400 HTTP Status code, 326 403 HTTP Status code, 326 404 HTTP Status code, 326 A a: (Append) mode, 269, 280 A0 signal line, 494 A1 signal line, 494 A12 chip, 360 abs() function, 86, 87 abstraction, 453 AC (alternating current), 537 .activate() method, 227 activation function, 369, 400 actuators, 474, 568, 573 Adafruit DC motor, 543 Adafruit Ultimate GPS, 501–503 ADAM method, 387, 390, 401 adaptor cables, 499, 502 ADC (analog digital converter), 476, 496, 522–523 .add() method, 166 Adeept Mars Rover PiCar-B assembling, 586–594 calibrating servos, 588–590 installing software for test, 591–592 Pi camera video testing, 592–594 running tests in Python, 591 test code, 592 components of, 577–586 controller board, 578 drive motor, 580–581 Pi camera, 584–585 Pixel RGB programmable LEDs, 582–583 RGB LED, 581–582 servo motors, 578–580 ultrasonic sensor, 585–586 materials for, 576–577 Adeept software disabling auto startup, 591 overview, 621 current limitations of, 363 Google Cloud Platform and, 452 NumPy library and, 438 in robotics, 623–646 future of, 646 machine-learning neural network, 624–633 Python Mars Rover PiCar-B with, 640–645 techniques of, 356–363 machine learning, 359–360 neural networks, 356–359 TensorFlow, 361–363 in Cloud, 420–422 Amazon Web Services (AWS), 421 Google cloud, 421 IBM cloud, 422 Microsoft Azure, 422
  • 297. Index 649 on graphics cards, 423–424 project inspiration, 424–425 Raspberry Pi adding to, 417–419 limitations with, 415–417 AI Accelerator, 416 AI compute sticks, 418–419 AI Winter, 358 Alexa. See Amazon Alexa algorithms, 361 alias name, 59 alignment, formatting, 96–97 allLEDSOff() function, 601 alphabetize() function, 202 alphabetizing lists, 159–161 alternating current (AC), 537 Amazon Alexa, 570 data science and, 431 neural network for controls with, 646 Amazon AWS Cloud, 570 Amazon Web Services (AWS), 421 America/Adak time zone, 119 America/Anchorage time zone, 119 America/Chicago time zone, 119 America/Denver time zone, 119 America/Detroit time zone, 119 America/Indiana/Indianapolis time zone, 119 America/Indiana/Knox time zone, 119 America/Los_Angeles time zone, 119 American Standard Code for Information Interchange (ASCII), 104, 105, 207 America/New_York time zone, 119 America/Phoenix time zone, 119 Anaconda development environment, 13–17 Anaconda Navigator home page, 17 latest version of, 57 opening in Mac, 16 opening in Windows, 16 pip (Pip Installs Packages) in, 345 analog, 494–495, 496 analog digital converter (ADC), 476, 496, 522–523 analyzing data, 434, 461–464 and operator, 71, 126 anomalies, 433 anonymous functions, 206–212 API (application programming interface), 382, 452, 505, 574 opening, 62 typed into VS Code, 76 Append (a:) mode, 269, 280 .append() method, 151, 163 Apple smartphones, 360 application programming interface (API), 382, 452, 505, 574 applications, building data types, 64–69 numbers, 65–66 True/false Booleans, 68–69 words (strings), 66–68 with operators, 69–72 arithmetic operators, 69–70 Boolean operators, 71–72 comparison operators, 70–71 putting code together, 82 typing and using comments, 63–64 using variables, 72–77 creating in code, 74 creating names for, 73–74 manipulating, 75–76 running app in VS Code, 76–77 saving work, 76 .archive() method, 216 Arduino Mini Pro LB board, 490, 498 Arduino Raspberry Pi. See Raspberry Pi Arduino Uno Grove base board, 489 arguments of functions, 86 passing information to functions, 204–205
  • 298. 650 Python All-in-One For Dummies arithmetic operators, 69–70 array JSON data conversion, 307 arrays. See lists arrow module, 123 current limitations of, 363 Google Cloud Platform and, 452 NumPy library and, 438 in robotics, 623–646 future of, 646 machine-learning neural network, 624–633 Python Mars Rover PiCar-B with, 640–645 techniques of, 356–363 machine learning, 359–360 neural networks, 356–359 TensorFlow, 361–363 in Cloud, 420–422 Amazon Web Services (AWS), 421 Google cloud, 421 IBM cloud, 422 Microsoft Azure, 422 on graphics cards, 423–424 project inspiration, 424–425 Raspberry Pi adding to, 417–419 limitations with, 415–417 Massaron), 413 ASCII (American Standard Code for Information Interchange), 104, 105, 207 assignment operator, 74 associative arrays. See data dictionaries Atmega 8L computer, 542 attributes changing values of, 222 general discussion of, 214 of objects in classes, 219–222 audio jack, 473 authentication token (AUTH TOKEN), 529, 530 Auto Save, 41 average_covered_ charges, inpatient_ charges_2015 Dataset, 458 average_medicare_ payments, inpatient_ charges_2015 Dataset, 458 average_total_payments, inpatient_ charges_2015 Dataset, 458 aware datetime, 120 AWS (Amazon Web Services), 421 Azure, 422–423 B b: (Binary) mode, 270 backpropagation, 367, 368, 390 backslash (), 68 backward stepping, 555 backwardPropagate function, 376–377 Base 2 system, 98 Base 8 system, 98 Base 16 system, 98 base classes, 236, 261 base units 3.3V base units, 497 5V base units, 497 Grove connectors, 489–492 Pi2Grover, 478, 490–491 Raspberry Pi, 490–491 batteries 18650 LiPo, 577 Raspberry Pi and life of, 577 self-driving car robot and, 614 baud rate, 495 Baxter robot, 361–362, 570–571 BeautifulSoup object, 332 biases, 368 bidirectional open-drain lines, 506 big data analytics, 432 data science project, 440–450 breaking down code, 443–444
  • 299. Index 651 choosing data, 440–443 heat plots with pandas, 448–450 Google Cloud Platform, 452–467 managing volume, variety, and velocity, 432 MatPlotLib library, 439–440 NumPy library, 438–439 pandas library, 439 public datasets, 453 variety, 431 velocity, 431 visualizing data with MatPlotLib, 444–448 diamond clarity versus carat size, 445–446 diamonds in each clarity type, 446–447 diamonds in each color type, 447–448 volume, 430–431 BigQuery cloud computer security, 453–454 importing, 460 Medicare database, 454–466 analyzing, 461–464 big-data code, 457–459 breaking down code, 460–461 setting up project, 454–457 visualizing data, 465–466 OpenAQ database, 466–467 signing up for, 454 bin() function, 87, 98 b: (Binary) mode, 270 compressed, 268 documents, 268 executable, 268 fonts, 268 images, 268 reading and copying, 283–286 and text, 267–269 binary numbers, 98–99 binary_accuracy values, 387, 388 binarycopy.py birth_year variable, 291 bit banging, 498 bits, 493 block write command, 547 blocks, hardware, 473 BlueTooth-enabled robots, 571, 573 Blynk app, building dashboard on phone using breaking down code, 533–535 building on expertise, 536 HDC1080 temperature and humidity, 525–526 BMW X3s, 568 BOM (Byte Order Mark), 289 Booleans, 68–69 converting to, 293 operators, 71–72, 126 values, 85, 186 break statements force stopping for loops with, 138–139 while loops and, 144–146 brightness parameter, 601 built-in functions, 208, 343 descrip function, 444 Math Module, 89–90 for numbers, 87 built-in methods, 103 Byte Order Mark (BOM), 289 C cables 5cm-long Grove cables, 492 adaptor cables, 499, 502 connecting with Adafruit Ultimate GPS, 501–503 Grove patch cable, 499–501 female header cables, 500 female patch cables, 556, 557 male header cables, 501 male patch cables, 548 male-pin-to Grove-connector patch cable, 549 patch cables, 503, 550 calibrateServo.py program, 588, 603 calibrating servos, 588–590, 603
  • 300. 652 Python All-in-One For Dummies calling functions, 194 calValues.py program, 588 Camel caps, 74 camera CSI, 473 cameras, Pi, 584–585, 592–594 candles, 518 Capek, Karel, 567 capitalize method, 342 capturing data, 433 carat, diamond database, 440 cats, machine learning neural network for recognizing, 645 centerAllServos() function, 606 central processing units (CPU), 360, 415, 473 CES (Consumer Electronic Show), 571 charts, heat plots with pandas, 448 cheat sheets, 34 chips, 371 Chollet, Francois, 413 chr() function, 142 chunk variable, 285 clarity, diamond database, 441 class help, 33 class methods, 230–232 class variables, 228–230 classes creating, 216–217 creating objects from, 217–218 empty classes error, 217 as Errors in Python, 261 general discussion of, 213–216 giving attributes to objects in, 218–224 changing values of, 222 creating objects, 219–222 giving methods to, 224–233 calling class method by class name, 227–228 passing parameters to methods, 226–227 using class methods, 230–232 using class variables, 228–230 using static methods, 232–233 inheritance, 234–246 adding extra parameters from subclasses, 239–241 calling base class method, 242 creating base classes, 236–237 overriding default value from subclasses, 239 using same name twice, 243–246 clear() method, 156, 163, 181, 184 clients, 324 clone command, 591 .close() method, 270, 272 clothes classifying, 395 creating network for detecting, 397–409 breaking down code, 399–401 CNN model code, 406–409 evaluation results, 402–403 external pictures, 403–405 Fashion-MNISTl database, 398 testing, 398–399, 405–406, 409 training, 402–403 learning more about, 413 420–422 Amazon Web Services (AWS), 421 Google cloud, 421 IBM cloud, 422 Microsoft Azure, 422 CNN (convolutional neural networks), 406–409, 625, 626 cobots (cooperative robots), 568, 570 code breaking down, 443–444, 460–461 calibrateServo, 588 creating data dictionary, 171 creating variables in, 74 debugging, 42–45 drive motor, 580 folders for, 37–39
  • 301. Index 653 putting together, 82 reading, 54–55 running in VS Code, 41–42 saving, 41 servo motor, 552–554 stepper motor step, 563–564 Style Guide for, 50–51 for web scraping, 331, 335 writing, 40–41, 45–48 code blocks, 80 code editors, 12 colons, in data dictionary code, 171 color, diamond database, 440 color() function, 600 Color() function, 601 color parameter, 601 color schemes choosing, 29 for comments, 64 colorWipe() function, 600 combining lists, 153–154 comma, number containing, 65 converting to Boolean, 293 to date, 292–293 to integers, 291 to objects and dictionaries, 295–302 strings, 290–291 importing to dictionaries, 299–302 to objects, 296–299 in Microsoft Excel, 286 opening, 288–290 reading into lists, 300–301 saving scraped data to, 336–338 storing information in, 518 in text editor, 287 Command Prompt, 42 command thread, 620 commands entering in Interactive Mode, 30–31 git clone, 512, 545 import, 346 model.fit, 389 module.compile, 390 pip, 57 print(pi), 346, 348 ps xaf, 486 python, 30 sudo, 609 sudo halt, 509, 588 sudo reboot, 591 commas, separating multiple values in functions with, 199 comments on functions, 195 typing and using, 63–64 using color for, 64 communicating results, 434–435 communications, for robotics, 573 comparison operators (relational operators), 70–71 != is not equal to, 126 < is less than, 126 <= is less than or equal to, 126 == is equal to, 126 > is greater than, 126 >= is greater than or equal to, 126 complex() function, 100 complex numbers, 66, 99–100 Computer Vision Projects with OpenCV and Python 3 (Rever), 624 computers building projects, 474–476 electric motors controlling with, 540–564 Python and DC motors, 540–547 Python and making stepper motor step, 554–564 Python and running servo motor, 548–551
  • 302. 654 Python All-in-One For Dummies computers (continued) physical computing, 471–486 controlling LED, 482–484 in everyday life, 472 making computers do things, 474 Pulse-width modulation (PWM), 485–486 Raspberry Pi, 476–482 assembling hardware, 478–482 GPIO pins, 477 GPIO Python libraries, 477 hardware for “Hello World,” 478 using small computers to build projects, 474–476 robotics and, 568, 572–573 SBC (single board computer), 473 small systems, 475 VNC (virtual network computer), 396 concatenating numbers to strings, 197 strings, 101–102 connectors. See also Grove connectors digital connectors, 493 GND connector, 498 GPIO connector, 473 SCL connector, 498 SDA connector, 498 standardized connectors, 487–488 VCC connector, 498 consumer behavior, big data and, 430 Consumer Electronic Show (CES), 571 content variable, 276, 333 context manager, 270–271 contextual coding, 270–271 continue statements with for loops, 140 with while loops, 143–144 control circuits, 578 controller boards, Python Mars Rover PiCar-B, 578 converting to Boolean, 293 to date, 292–293 to integers, 291 to objects and dictionaries, 295–302 strings, 290–291 data to JSON format, 304 Excel dates to JSON dates, 309–310 Excel spreadsheet, 305 convolutional neural networks (CNN), 406–409, 625, 626 cooperative robots (cobots), 568, 570 copy() method, 162, 163, 181 copying data dictionaries, 182 lists, 162 core language, 56 correlation heat plots, 450 .count() method, 157, 163, 164 CPU (central processing units), 360, 415, 473 crashing, keeping app from, 253–255 Create (x:) mode, 269 CSS, 8 converting to Boolean, 293 to date, 292–293 to integers, 291 to objects and dictionaries, 295–302 strings, 290–291 importing to dictionaries, 299–302 to objects, 296–299 in Microsoft Excel, 286 opening, 288–290 reading into lists, 300–301 saving scraped data to, 336–338 storing information in, 518 in text editor, 287 csv writer, 337 curly braces, 54 cut, diamond database, 440
  • 303. Index 655 D D0 signal line, 493 D1 signal line, 493 D4/5 Grove connector, 552 dashboards, building on phones breaking down code, 533–535 building on experience, 536 HDC1080 temperature and humidity, 525–526 how to add Blynk dashboard, 527–530 data analyzing, 432 from Medicare database, 461–464 with pandas library, 439 choosing for data science project, 440–443 converted to JSON format, 304 downloading from Kaggle, 440 dumping to JSON data, 318–321 loading in TensorFlow language, 384 metadata, 440 misleading online, 433 removing from dictionaries, 317–318 saving scraped, 335–338 visualizing with MatPlotLib, 444–448, 465–466 diamond clarity versus carat size, 445–446 diamonds in each clarity type, 446–447 diamonds in each color type, 447–448 data dictionaries copying, 182 creating, 171–179 accessing, 172–174 adding items, 177–179 changing items, 177–179 changing value of keys, 177 with get() method, 176 getting length of, 174–175 verifying key existence, 175 deleting data from, 182–185 looping through, 179–181 methods for, 181–182 multiple key dictionaries, 186–192 nesting, 190–192 using fromkeys() method, 188–190 using setdefault() methods, 188–190 data massaging, pandas library and, 439 data queues, 620 data science big data, 430–432 managing volume, variety, and velocity, 432 variety, 431 velocity, 431 volume, 430–431 data analytics and, 432 Google Cloud Platform and, 452 projects using libraries, 440–450 breaking down code, 443–444 choosing data, 440–443 heat plots with pandas, 448–450 steps to, 433–435 analyzing data, 434 capturing data, 433 communicating results, 434–435 maintaining data, 435 processing data, 433–434 visualizing data with MatPlotLib, 444–448 diamond clarity versus carat size, 445–446 diamonds in each clarity type, 446–447 diamonds in each color type, 447–448 data types audio recordings, 431 correlations between, 434 numbers, 65–66 photos, 431 tabular, 439 time-series, 439 True/false Booleans, 68–69 words (strings), 66–68 DataFrames from pandas library, 439 datasets, large, downloading, 451 date directives, 110 date format strings, 111, 113
  • 304. 656 Python All-in-One For Dummies date() method, 160 dates converting, 292–293 displaying in lists, 160 formatting strings for, 107–112 datetime format strings, 114 datetime module, 108, 160, 292, 309, 313 datetime .utcfromtimestamp() method, 313 datetime.date data type, 108, 109 datetime.datetime data type, 108 datetime.now() data type, 131 datetime.time data type, 108, 112 dateutil module, 121 DC (direct current) motors, 538–547, 578, 580 Grove I2C motor drive, 542–545 Python DC motor software, 545–547 dcMotor directory, 545 dcmotorTest.py Debug pane, 44 debugging built-in VS Code editor, 43–45 code, 42–43 decimal point, 65 decision trees, 360 decorators, 231 deep learning, 359 Deep Learning with Python (Chollet), 413 def keyword creating functions with, 194 custom functions and, 208 default values, 198, 222–224 del() command, 156 del keyword, 182–183 delay argument, 596 delay parameter, 602 depth, diamond database, 441 derivatives, 371 descrip function, 444 deserialization, 306 development workspace, 34–37 diamonds creating data science project, 440–450 breaking down code, 443–444 choosing data, 440–443 heat plots with pandas, 448–450 visualizing data with MatPlotLib, 444–448 diamond clarity versus carat size, 445–446 diamonds in each clarity type, 446–447 diamonds in each color type, 447–448 Dickinson, John, 357, 360 dict data conversion, 307 __dict__ method, 245 dictionaries changing value of key, 318 converting, 295–302 importing, 299–302 one value at time, 312, 315 in pandas DataFrames, 439 removing data from, 317–318 digital Grove digital, 493–495 vs.analog, 496 digital analog input, 505 digital bits, 493 digital combined messages, 507 digital connectors, 493 digital humidity sensor, 509 digital I2C, 506 digital input, 505 digital signal processor (DSP), 417 digital single message, 507 Digital SPI (serial peripheral interface), 506 dir() function, 340–341, 343, 346, 347 direct current (DC) motors, 538–547, 578, 580 Grove I2C motor drive, 542–545 Python DC motor software, 545–547 direct memory access (DMA), 583, 600 directional controls for robots, with GPIO lines, 580 directives, date and time, 110 display DSI, 473 division (/), 117
  • 305. Index 657 DMA (direct memory access), 583, 600 docs.python.org, 343 docstrings, 63–64, 195 double-quotation marks, 67–68 drg_definition, inpatient_charges_2015 Dataset, 458 drive motors, Python Mars Rover PiCar-B, 580–581 driver boards, 560 drivers, 505 DSP (digital signal processor), 417 dumps() method, 318–319 dunder init method, 217 dunder named items, 341 duplicates, 433 duty cycle, 485 E e constant, 90 Easter egg, 49–50 edge computing, 417 editing hello.py editors, 11–12 electric motors controlling with computers, 540–564 Grove I2C motor drive, 542–545 making stepper motor step, 554–564, 562–563, 563–564 running a servo motor, 548–551, 551–552, 552–554 using Python and DC motors, 540–547 exploring, 538–540 DC motors, 538–539 servo motor, 539 stepper motor, 539–540 elif statements, 131–133 ellipsis, in data dictionary code, 171 else keyword, using with errors, 255–258 else statements, adding to if statements, 130–131 embedded systems, 472, 573 EmptyFileError exception, 261 encoders, 580 encrypted data, 324 engineer neurons, 358–359 entropy, 401 enumerate() function, 280 epochs, 368, 378–380, 626, 631 error messages, 81–82 HTTP error, 329 import error, 375 OS Error, 313 error package, 327 errors concatenating numbers, 197 exceptions, 247–250 from functions without parameters, 197 keeping app from crashing, 253–255 programming, 253 raising own errors, 259–263 replacing error message code, 251 specifying exceptions, 252–253 syntax error for non-default argument, 199 using else keyword, 255–257 using try . . . except . . . else . . . finally, 257–258 escape characters, 68 ESP32 boards, 476 ESP8266 boards, 476, 498 Etc/UCT timezone, 119 Etc/UTC timezone, 119 Ethernet, 473 evolutionary algorithms, 360–361 evolutionary computing, 359 Excel reader (xlrd), 309 Excel spreadsheets converting to JSON, 304–305 data in, 304 dates in, 309–310 web scraped data, 337
  • 306. 658 Python All-in-One For Dummies except keyword, 251, 253, 257–258 exceptions, 247–250, 251 eXclusive OR gate (XOR gate), 370, 371 exponents, 70 extend() function, 153, 163 converting strings, 290–291 converting to Boolean, 293 converting to date, 292–293 converting to integers, 291 importing to dictionaries, 299–302 importing to objects, 296–299 opening, 288–290 with readline(), 279–280 with readlines(), 277–279 using seek(), 283 using tell(), 281–283 F Facebook dataset volume and, 430 as high velocity dataset, 431 False data conversion, 307 false JSON data conversion, 307 Fan module, 493 fashion database, 397 Standards and Technology) database, 395, 398–399, 402–403 feedback, 368, 556 feedForward function, 376–377 feed-forward input, 367 female header cables, 500 female patch cables, 556, 557 fetchUltraDistance() function, 602 FileNotFoundError message, 250, 261 See also b: (Binary) mode, 270 compressed, 268 documents, 268 executable, 268 fonts, 268 images, 268 reading and copying, 283–286 and text, 267–269 closing, 269–275 editing, 268 looping through, 281–283 opening, 269–275 overwriting, 280–281 finally keyword, 257–258 find_all method, 333 Firebase data, 320, 321 First Grove Female Patch Cord, 558 first_name variable, 290 float data type, 288 float() function, 87 FLOAT type, inpatient_charges_2015 Dataset, 458 (//), 70, 117 FMTensorFlowPlot.py FMTensorFlow.py for loops, operations with, 134–141 with continue statements, 140 force stopping with break statements, 138–139 nesting loops, 140–141 through list, 137–138 through numbers in ranges, 134–136 through strings, 136–137
  • 307. Index 659 format() function, 87 format strings (f-strings) in data dictionaries, 192 for dates, 111 expression part of, 91 formatting percent numbers, 93–94 literal part of, 91 making multiline, 95 showing dollar amounts, 92–93 width and alignment, formatting, 96–97 formatting numbers with f-strings, 91–92 making multiline format strings, 95 percent numbers, 93–94 showing dollar amounts, 92–93 width and alignment, 96–97 Forta, Ben, 459 fossil fuels, robotics and, 569 fragment, of URL, 324 from . command, 347 from statement, 348 fromkeys() method, 181, 188–190 f-strings (format strings) in data dictionaries, 192 for dates, 111 expression part of, 91 formatting percent numbers, 93–94 literal part of, 91 making multiline, 95 showing dollar amounts, 92–93 width and alignment, formatting, 96–97 full_name variable, 290 functions abs(), 86, 87 activation, 369, 400 allLEDSOff(), 601 alphabetize(), 202 anonymous, 206–212 backwardPropagate, 376–377 bin(), 87, 98 built-in, 208, 343 descrip function, 444 Math Module, 89–90 for numbers, 87 calculating numbers with, 86–90 centerAllServos(), 606 chr(), 142 color(), 600 Color(), 601 colorWipe(), 600 commenting on, 195 complex(), 100 creating, 194–195 descrip, 444 dir(), 340–341, 343, 346, 347 enumerate(), 280 extend(), 153, 163 feedForward, 376–377 fetchUltraDistance(), 602 float(), 87 format(), 87 headTiltDown(), 604 headTiltMiddle(), 604 headTiltPercent(), 605 headTiltUp(), 604 headTurnLeft(), 603 headTurnMiddle(), 603 headTurnPercent(), 604 headTurnRight(), 603 help(), 243, 341–343, 346, 347 hex(), 87, 98 int(), 87, 144, 291 lambdas. See anonymous functions len(), 102, 150 for data dictionaries, 174 transform and, 207 loss, 369–370, 387, 401 math module, 88–90
  • 308. 660 Python All-in-One For Dummies functions (continued) math.acos(), 89 math.atan(), 89 math.atan2(), 89 math.ceil(), 89 math.cos(), 89 math.degrees(), 89 math.e, 89 math.exp(), 90 math.factorial(), 90 math.floor(), 90 math.isnan(), 90 math.log(), 90 math.log2(), 90 math.pi, 90 math.pow(), 90 math.radians(), 90 math.sin(), 90 math.sqrt(), 89, 90 math.tan(), 90 math.tau(), 90 max(), 87 mean_squared_error, 387 min(), 87 model.evaluate, 401 model.predict, 389 motorBackward(), 602 MotorDirectionSet, 547 motorForward(), 596, 602 MotorSpeedSetAB, 547 neuron activation, 630 newline="," 308 newline='', 337 oct(), 87, 98 ord(), 104 passing information to, 196–205 arguments, 204–205 multiple values, 199–200 multiple values in lists, 202–203 optional parameters with defaults, 198 using keyword arguments (kwargs), 200–201 print(), 274 adding spaces in, 197 for data dictionaries, 172 rainbowCycle(), 600 range(), in for loop, 134–136 returning values from, 205–206 round(), 86–87, 388 set_Front_LED_Off(), 599–600 set_Front_LED_On(), 598–599 setPixelColor(), 601 sigmoid, 369, 377 sleep(), 597 softmax, 400 sparse categorical crossentropy, 401 stopMotor(), 602–603 str(), 87, 158 theaterChaseRainbow(), 600–601 type(), 87, 340 ultra.checkdisk(), 585 wheelsLeft(), 596, 605 wheelsMiddle(), 596, 598, 605 wheelsPercent, 596–597 wheelsPercent(), 606 wheelsRight(), 605 G Gartner analysis company, 430 gearing sets, 578 general purpose input-output (GPIO), 477, 482, 486, 540, 548, 555, 562–563, 578 .get() method, 175, 176, 181, 334 gettz, 121 Git, 18 git clone command, 512, 545 GitHub website, 363, 513 GMT (Greenwich Mean Time), 119 GND connector, 498 GND signal line, 493, 495 Google Brain Groups, 363 Google Chrome Extensions, 325, 326
  • 309. Index 661 Google Cloud Platform, 421 BigQuery, 452–467 cloud computer security, 453–454 Medicare database, 454–466 OpenAQ database, 466–467 signing up for, 454 Google Edge TPU accelerator, 419 Google Firebase Realtime Database data in, 305 Google trend searches, 9 google.cloud library importing, 460 installing, 459 GPIO (general purpose input-output), 477, 482, 486, 540, 548, 555, 562–563, 578 GPIO connector, 473 GPIO function, 551 GPIO lines LEDs controlled by, 581 on PiCar-B robots, 580 GPIO Python libraries, 477 gpiozero library, 477 GPU (graphics processing unit), 360, 415, 423–424 graphical user interface (GUI), 392, 396, 476 graphics cards, 423–424 graphics processing unit (GPU), 360, 415, 423–424 graphics processor, 417 graphs bar graphs, 447, 466 live graphs, 440 with MatPlotLib, 440 green errors, 81–82 Greenwich Mean Time (GMT), 119 ground, 491 Grove 12C, 497–498 Grove analog, 494–495 Grove blue LED, 478, 482 Grove cables, 479. See also cables 5cm-long, 492 adaptor cables, 499, 502 female patch cables, 556, 557 male patch cables, 548 male-pin-to Grove-connector patch cable, 549 patch cables, 499–501 plugged into Grove blue LED board, 481 Grove connectors, 487–503. See also connectors connecting with Grove cables, 499–503 Adafruit Ultimate GPS, 501–503 Grove patch cable, 499–501 D4/5 Grove connector, 552 selecting base units, 489–492 Arduinos, 489–490 Pi2Grover, 490–491 signals, 493–498 Grove 12C, 497–498 Grove analog, 494–495 Grove digital, 493–494 Grove UART, 495–496 types of, 492 Grove digital, 493–494 Grove female patch cables, 556, 557 Grove Female Patch Cord, 558, 559 Grove four-channel 16-bit ADC, 519 Grove gas sensor (O2), 519 Grove hardware, 478 Grove I2C motor drive, 541, 542–545 Grove male patch cables, 548 Grove male-pin-to Grove-connector patch cable, 549 Grove oxygen sensor, 518, 519–520 Grove patch cable, 499–501 Grove UART, 495–496 Grove-connector-to-female-pins, 556, 557 Grove-connector-to-male-header pins, 499 Grove-to-pin-header converter, 488 GUI (graphical user interface), 392, 396, 476 H h5py library, 632 happy_pickle_copy.png, 286 happy_pickle.jpg, 273
  • 310. 662 Python All-in-One For Dummies hardware assembling, 478–482 for “Hello World,” 478 major blocks of, 473 hardware interface, 505 HDC1000 compatible temperature and humidity sensor, 509 HDC1080 sensor board, 514 HDC1080 temperature and humidity sensor, 508–509, 525–526 HDMI Out, 473 head thread, 620 header, 287 headTiltDown() function, 604 headTiltMiddle() function, 604 headTiltPercent() function, 605 headTiltUp() function, 604 headTurnLeft() function, 603 headTurnMiddle() function, 603 headTurnPercent() function, 604 headTurnRight() function, 603 heat plots correlation heat plots, 450 pandas library, 448–450 “Hello World” physical computing project, 474, 478–482 hello.py comment in, 64 editing in VS code, 62 help searching topics online, 33–34 using in Interactive Mode, 31–33 help comment, 31 help() function, 243, 341–343, 346, 347 hex() function, 87, 98 hexadecimal numbers, 98–99 history variable, 387–388, 409 hits object, 311 hook up, 471 hospital_referral_ region_description, inpatient_charges_2015 Dataset, 458 hot-plugging, 509 HTML, 8, 453 html5lib, 332 HTTP (Hypertext Transfer Protocol), 324 HTTP headers, 325–327 HTTPResponse object, 328 Hubble Space Telescope, 371 humidity HDC1080, 525–526 reading from 12C device, 511–514 sensors, 508–509 Hypertext Transfer Protocol (HTTP), 324 hyphen, number containing, 65 I I2C bus, 498 I2C compass and accelerometer, 536 I2C controller board, 540–541, 578 I2C motor drive, 541, 542 I2CTemperature directory, 511 IAM (identity and access management), 453 IBM cloud, 422 ICD codes, 462 IDE (integrated development environment), 476 identity and access management (IAM), 453 if statements checking list contents for items with, 150 operations with, 126–134 adding else to, 130–131 elif, 131–133 ternary operations, 133–134 proving false example, 127 proving true example, 127 imaginary numbers, 99 import command, 346 import modulename syntax, 58 import random, 56 import statement, 400 importing BigQuery, 460 to dictionaries, 299–302 to objects, 296–299
  • 311. Index 663 google.cloud library, 460 modules, 58 in keyword, 175 inconsistent data, 433 indent= option, 318 indentations, 54–55, 80 for comments, 134 errors and, 256 four lines of code example, 129 with functions, 194 with if statements, 128 nesting loops and, 140 index counter, diamond database, 440 .index() method, 158, 163 indexes changing items in lists with, 153 “index out of range” error, 149 in pandas DataFrames, 439 infrared (IR) components, 497 init method, 217 __init__ method, 217 insert() method, 152, 163 installing Anaconda, 13–17 google.cloud library, 459 h5py library, 632 HDC1080 I2C sensor, 509 libraries, 396 MatPlotLib, 409, 444 modules, 57 NumPy library, 375, 441 pandas library, 441 seaborn library, 449, 466 software for CarPi-B Python test, 591–592 TensorFlow language library, 382 VS Code, 13–17 instance methods, 230 instance term, 339 instances. See objects int and float data conversion, 307 int() function, 87, 144, 291 INTEGER type, inpatient_charges_2015 Dataset, 458 integers, 66, 85, 186, 291 integrated development environment (IDE), 476 Intel Modvidius Neural Compute Stick (NCS), 418–419 Intel Movidius chips, 422, 423 interactive mode cheat sheets for, 34 entering commands, 30–31 exiting interactive help, 33 going to Python Interpreter, 30 opening Terminal, 28 searching help topics online, 33–34 using built-in help, 31–33 interations, 600 interface, Python, 505 building, 595–597 motorForward function, 596 wheelsLeft function, 596 wheelsPercent function, 596–597 Inter-IC device bus, 498 Internet, interacting with HTTP headers, 325–327 posting to Web with Python, 328–330 scraping Web with Python, 330–338 parsing part of pages, 333 storing parsed content, 333–335 URLs, 324–328 Internet of Things (IOT), 430 devices, 420 robotics and, 568 IR (infrared) components, 497 is not operator, 71 is operator, 71 .items() method, 180, 181 iterables, 162, 188, 202
  • 312. 664 Python All-in-One For Dummies J JavaScript, 8, 54–55, 133 JSON (JavaScript Object Notation), 303–321, 454 dumping Python data to, 318–321 Excel spreadsheet converting to, 305 changing JSON data, 316–317 converting Excel dates to JSON dates, 309–310 loading keyed JSON from string, 315–316 loading unkeyed JSON, 314–315 removing data from dictionaries, 317–318 organizing, 303–306 removing data from dictionaries, 317–318 saving scraped data to, 335–336 serialization, 306–307 web scraping data in, 336 json module, 307, 335 json.dump() method, 307, 318 json.dumps() method, 307, 318 json.load() method, 307 jumpers, 543 Jupyter Notebook, 21–25 creating, 24 launching from home page, 22 opening page, 23 running code in cell, 24 string operators in, 104 writing code in, 45–48 K k variable, 316–317 Kaggle online community Cats and Dogs database, 624 diamonds database project from, 440–450 Keras open source neural-network library, 383–384, 390 key= expression, 207 loading from string, 315–316 looping through, 310–312 keys, data dictionary changing value of, 177 removing, 184–185 verifying, 175 keys() method, 181 keyword help, 32–33 kit-based robots, 575 kwargs (keyword arguments), 200–201 L L289P controller board, 578 lambda functions. See anonymous functions large number, containing decimal point, 65 last_name variable, 290 layers, neural-network, 367–368, 384 LED, 482–484, 493 dimming, 583 functions, 598–600 set_Front_LED_Off() function, 599–600 set_Front_LED_On() function, 598–599 Pixel RGB programmable, 582–583 pulse width modulation and, 579 RGB, 581–582 len() function, 102, 150 for data dictionaries, 174 transform and, 207 lengths of data dictionaries, 174–175 of lists, 151 of strings, 102 of tuples, 164 libraries, 339–343 built-in functions, 343 data science project using, 440–450 breaking down code, 443–444 choosing data, 440–443 heat plots with pandas, 448–450 visualizing data with MatPlotLib, 444–448
  • 313. Index 665 GPIO Python libraries, 477 gpiozero library, 477 importing, 443 installing, 396 installing TensorFlow language in, 382 MatPlotLib, 439–440, 518 NumPy, 370, 371–373, 375, 386, 438–439 open source neural-network library, 383–384, 390 pandas, 439 Python3 library, 382 requests library, 533–534 RPi GPIO library, 551 SDL_Pi_GroveI2CMotorDriver library, 547 SDL_Pi_HDC1080_Python3 library, 545 SMBUS library, 517 smbus library, 547 sys library, 514 using dir() function, 340–341 using help() function, 341–343 lights dimming, 583 functions, 598–600 set_Front_LED_Off() function, 599–600 set_Front_LED_On() function, 598–599 Pixel RGB programmable, 582–583 pulse width modulation and, 579 RGB, 581–582 line (of code), 78 linear regressions, 434 link: variable, 333 Linting:Pep8Enabled, 82 Linux, 475, 583 LiPo batteries, 577, 578 list, tuple data conversion, 307 list object, 308 lists, 147–163. See also data dictionaries; sets adding items to end of, 151–152 alphabetizing, 159–161 changing items in, 153 checking contents for items, 150–151 clearing out, 156–157 combining, 153–154 copying, 162 counting item appearance in, 157–158 getting length of, 151 inserting items into, 152–153 looping through, 150 looping with for, 137–138 NumPy library and, 438 referencing items by position number, 148–149 removing items from, 154–156 reversing, 161–162 sorting, 159–161 tuples, 163–165 as values in data dictionary keys, 170 working with in functions, 202 .ljust() method, 212 load() method, 314 loading unkeyed JSON, 314–315 local maxima, 394 local scope, 196 logic analyzer, 555 logical operators, 126 logistic regressions, 434 with readline(), 279–280 with readlines(), 277–279 using seek(), 283 using tell(), 281–283 loops with for, 134–141 with continue, 140 force stopping loops, 138–139 nesting loops, 140–141 through list, 137–138 through numbers in ranges, 134–136 through strings, 136–137
  • 314. 666 Python All-in-One For Dummies loops (continued) pandas library and, 439 through data dictionaries, 179–181 through lists, 150 with while, 141–146 breaking with break, 144–146 with continue, 143–144 loss function, 369–370, 387, 401 M Mac computers creating folders for code, 37 development environments for, 35 opening Anaconda Navigator in, 16 machine learning, 359–360. See also neural networks classifying clothes with, 395 creating network for detecting clothes types breaking down code, 399–401 CNN model code, 406–409 Fashion-MNIST database, 398 test results, 405–406, 409 testing external pictures, 403–405 testing network, 398–399 training and evaluation results, 402–403 training network for, 398 detecting clothes types, 397–409 learning more about, 413 looking for solutions, 394–395 setting up software environment, 396–397 using TensorFlow, 395–396 409–413 machine learning accelerators, 419 Machine Learning For Dummies (Mueller and Massaron), 368, 413 magic methods, 341 magnets, 538 maintaining data, 435 male header cables, 501 male patch cables, 548 margins, 434 Markdown language, 23, 47–48 Mars Rover PiCar-B robot assembling, 586–594 calibrating servos, 588–590 installing software for test, 591–592 Pi camera video testing, 592–594 running tests in Python, 591 test code, 592 Cat/Not Cat recognition, 645 cats and dogs neural network on, 640–646 code, 640–643 results, 643–645 components of, 577–586 controller board, 578 drive motor, 580–581 Pi camera, 584–585 Pixel RGB programmable LEDs, 582–583 RGB LED, 581–582 servo motors, 578–580 ultrasonic sensor, 585–586 controls with Alexa, 646 Follow ball, 646 materials for, 576–577 programming for coordinating motor movements with sensors, 610–613 front LED functions, 598–600 high-level Python interface, 595–597 main motor functions, 602–603 Pixel strip functions, 600–601 Python Robot Interface Test, 606–610 self-driving, 613–622 servo functions, 603–606 “Single Move” code, 597–598 ultrasonic distance sensor function, 601–602 Santa/Not Santa, 646 marshalling format, 303 massaging data, into matrices, 629 Massaron, Luca, 368 Material Color Theme, 29 Material Icon Theme, 29
  • 315. Index 667 math module, 346–347, 348 math module functions, 88–90 math.acos() function, 89 math.atan() function, 89 math.atan2() function, 89 math.ceil() function, 89 math.cos() function, 89 math.degrees() function, 89 math.e function, 89 math.exp() function, 90 math.factorial() function, 90 math.floor() function, 90 math.isnan() function, 90 math.log() function, 90 math.log2() function, 90 math.pi function, 90 math.pow() function, 90 math.radians() function, 90 math.sin() function, 90 math.sqrt() function, 89, 90 math.tan() function, 90 math.tau() function, 90 MatLab library, 439 MatPlotLib, 371, 396, 409–413, 439–440, 518 displaying big data with, 435 visualizing data with, 444–448, 465–466 diamond clarity versus carat size, 445–446 diamonds in each clarity type, 446–447 diamonds in each color type, 447–448 matrices, 363, 423 converting image to, 636 for neural networks, 626 NumPy library and, 438 pandas library and, 439 max() function, 87 max(s) operator, 103 mdy(any_date), 349 mean_squared_error function, 387 analog-to-digital converter (ADC), 518–519 breaking down code, 522–525 Grove gas sensor (O2), 519 oxygen experiment, 520–522 Medicare database, on BigQuery analyzing, 461–464 big-data code, 457–459 breaking down code, 460–461 setting up project, 454–457 visualizing data, 465–466 metadata, 440 Metcalf, Robert, 358 methods .activate(), 227 ADAM, 387, 390, 401 .add(), 166 .append(), 151, 163 .archive(), 216 built-in, 103 capitalize, 342 class, 230–232 clear(), 156, 163, 181, 184 .close(), 270, 272 copy(), 162, 163, 181 .count(), 157, 163, 164 for data dictionaries, 181–182 date(), 160 datetime .utcfromtimestamp(), 313 __dict__, 245 dumps(), 318–319 dunder init, 217 find_all, 333 fromkeys(), 181, 188–190 general discussion of, 214 .get(), 175, 176, 181, 334 giving to classes, 224–233 calling class method by class name, 227–228 passing parameters to methods, 226–227 using class methods, 230–232 using class variables, 228–230 using static methods, 232–233 .index(), 158, 163 init, 217 __init__, 217 insert(), 152, 163
  • 316. 668 Python All-in-One For Dummies methods (continued) instance, 230 .items(), 180, 181 json.dump(), 307, 318 json.dumps(), 307, 318 json.load(), 307 keys(), 181 for lists, 163 .ljust(), 212 load(), 314 magic, 341 manipulating strings with, 105–107 now(), 113 open, 270, 307 pop(), 155, 163, 181, 184–185 popitem(), 181, 185 read(), 276 read([size]), 276 readline(), 276, 277, 279–280, 282 readlines(), 276, 277–279 remove(), 154, 163 resolution order, 245 reverse(), 161, 163 rjust(), 212 s.capitalize(), 106 s.count(x,[y.z]), 106 seek(), 283 setdefault(), 182, 188–190 s.find(x,[y.z]), 106 showexpiry(), 242 s.index(x,[y.z]), 106 s.isalpha(), 106 s.isdecimal(), 106 s.islower(), 106 s.isnumeric(), 106 s.isprintable(), 106 s.istitle(), 106 s.isupper(), 106 s.lower(), 106 s.lstrip(), 106 sort(), 159, 163, 202, 207 .sort(key=lambda s:s.lower()), 159 sort(reverse=True), 202 s.replace(x,y), 106 s.rfind(x,[y,z]), 106 s.rindex(), 106 s.rstrip(), 106 s.strip(), 106 s.swapcase(), 106 static s, 232–233 s.title(), 106 strip(), 294 s.upper(), 106 tell(), 281–283 today(), 108 update(), 166, 177–178, 182 urlopen, 332 .values(), 180, 182 micro servo motors, 579 micro USB, 473 Microsoft Azure, 422–423 Microsoft Excel spreadsheets converting to JSON, 304–305 data in, 304 dates in, 309–310 web scraped data, 337 middleware, 609 milliamps, 579 min() function, 87 Mini Pro LB board, 490 min(s) operator, 103 Minsky, Marvin, 357, 365 and Technology) database, 395 mobile graphics processor, 417 model.add statement, 386–387 model.evaluate function, 401 model.fit command, 389 model.predict function, 389 models CNN model code, 406–409 compiling, 384 evaluating, 388–390
  • 317. Index 669 modes Append (a:), 269, 280 Binary (b:), 270 Create (x:), 269 interactive, 27–34 cheat sheets for, 34 entering commands, 30–31 exiting interactive help, 33 going to Python Interpreter, 30 opening Terminal, 28 searching help topics online, 33–34 using built-in help, 31–33 r: (Read), 269 r+: (Read/Write), 269 Read (r:), 269 Read/Write (r+:), 269 t: (Text), 270 Text (t:), 270 w: (Write), 269 wb, 285 Write (w:), 269 x: (Create), 269 and Technology (MNIST) database, 395, 398–399, 402–403 modular programming, 343 module.compile command, 390 modules, 345–352 3.3V I2C Grove, 489 arrow, 123 datetime, 108, 160, 292, 309, 313 dateutil, 121 Fan, 493 installing, 57 json, 307, 335 making, 348–352 math, 346–347, 348 request, 332 Switch s, 493 syntax for importing, 58 using, 56–59 using alias with, 59 modulus, 70 motor thread, 620 motorBackward() function, 602 MotorDirectionSet function, 547 motorForward() function, 596, 602 motors. See also servo motors main motor functions, 602–603 motorBackward() function, 602 motorForward() function, 602 stopMotor() function, 602–603 for robotics, 573 MotorSpeedSetAB function, 547 MouseAir robot, 645 Mueller, Paul, 368, 413 multiline comments, 63–64 multiple key dictionaries, 186–192 nesting, 190–192 using fromkeys() method, 188–190 using setdefault() methods, 188–190 multiple-toothed electromagnets, 539 MyTemperature app, 526, 529, 530 N n * s operator, 103 naïve datetime, 120 names, variables, 73–74 names.txt naming convention, 80 National Oceanic and Atmospheric Agency (NOAA) database, 453 NCS (Intel Modvidius Neural Compute Stick), 418–419 negative feedback, 556 negative numbers, 65 nesting loops with for, 140–141 multiple key dictionaries, 190–192 Network News Transfer Protocol (NNTP), 112
  • 318. 670 Python All-in-One For Dummies networking, 415 neural networks building in Python, 370–382 code, 370–378 installing TensorFlow Python library, 382 running neural-network code, 378–381 using TensorFlow for same neural network, 381–382 building in TensorFlow, 383–392 breaking down code, 386–388 changing to three-layer neural network in TensorFlow/Keras, 390–392 compiling models, 384 evaluating models, 388–390 loading data, 384 creating machine learning, 624–633 with Python Mars Rover PiCar-B, 640–645 setting up, 624–625 testing, 633–639 using TensorFlow, 625–633 machine learning recognition Cat/Not Cat, 645 controls with Alexa, 646 Follow ball, 646 Santa/Not Santa, 646 understanding, 366–370 activation function, 369 layers of, 367–368 loss function, 369–370 weights and biases, 368 Neural_Network class, 375 neural-network code, 370–374, 378–381 neural-network model and layers, 384 neuron activation function, 630 neurons, 358–359, 366 New Horizons space probe, 371 new_dict string, 318 newline="" function, 308 newline='' function, 337 nicknames, 59 NNTP (Network News Transfer Protocol), 112 No such file or directory message, 250 NOAA (National Oceanic and Atmospheric Agency) database, 453 None data conversion, 307 not operator, 71, 126 now() method, 113 null JSON data conversion, 307 num variable, 72 number JSON data conversion, 307 numbers, 64–66 binary, 98–99 built-in functions for, 87 calculating with functions, 86–90 complex, 99–100 formatting, 91–97 with f-strings, 91–92 making multiline format strings, 95 percent numbers, 93–94 showing dollar amounts, 92–93 width and alignment, 96–97 hexadecimal, 98–99 looping with for, 134–136 octal, 98–99 quotation marks and, 148 NumPy library, 370, 371–373, 386, 437, 438–439, 636 O O2 (Grove gas sensor), 519 object JSON data conversion, 307 object term, 339 object-oriented programming (OOP), 53, 213, 216 objects converting, 295–302 creating from classes, 217–218, 219–222
  • 319. Index 671 as exceptions, 261 general discussion of, 213 importing, 296–299 tuples versus, 222 oct() function, 87, 98 octal numbers, 98–99 Olson Database, 119 online resources buying LiPo batteries, 577 buying PiCar-B, 577 buying Raspberry Pi 3B , 577 Cats and Dogs database, 624 Google cloud, 454 Kaggle online community, 440 NumPy library tutorials, 438 Python code feedback video, 613 Python exceptions list, 259 Python test on PiCar-B robot video, 591 “Robot Brain” code video, 620 Robot Operating System (ROS), 609 RobotInterface class test video, 609 Santa/Not Santa video, 646 Single Move code on robot video, 598 SQL tutorials, 459 support page for this book, 592 TensorFlow download link, 624 Wilkinson Baking, 569 OOP (object-oriented programming), 53, 213, 216 Open Editors bar, 38 open method, 270, 307 open source neural-network library, 383–384 OpenAQ database, 466–467 OpenCV (Open-Source Computer Vision), 584, 621, 646 open-drain lines, 498, 506 opening Terminal in interactive mode, 28 URLs from Python, 327–328 Open-Source Computer Vision (OpenCV), 584, 621, 646 operating system command prompt, 28 operations with if statements, 126–134 adding else to, 130–131 elif, 131–133 ternary operations, 133–134 with for loops, 134–141 with continue, 140 force stopping with break statements, 138–139 nesting loops, 140–141 through list, 137–138 through numbers in ranges, 134–136 through strings, 136–137 main operators, 125–126 with while loops, 141–146 breaking with break, 144–146 with continue, 143–144 operators -, 70 !=, 71, 126 %, 70 *, 70 **, 70 /, 70 //, 70, 117 +, 70 <, 71, 126 <=, 71, 126 =, 80, 152 ==, 71, 80 >, 71, 126 >=, 71, 126 and, 71, 126 arithmetic s, 69–70 assignment, 74 comparison (relational), 70–71, 125 != is not equal to, 126 < is less than, 126
  • 320. 672 Python All-in-One For Dummies operators (continued) <= is less than or equal to, 126 == is equal to, 126 > is greater than, 126 >= is greater than or equal to, 126 (//), 117 is, 71 is not, 71 logical, 126 max(s), 103 min(s), 103 n * s, 103 not, 71, 126 or, 71, 126 s * n, 103 s[i], 103 s[i:j], 103 s[i:j:k], 103 s.count(x), 103 sequence s for strings, 103 s.index(x[, i[, j]]), 103 string, 102–105 x in s, 103 x not in s, 103 or operator, 71, 126 ord() function, 104 OS Error, 313 outliers, 433 analog-to-digital converter (ADC), 518–519 breaking down code, 522–525 Grove gas sensor (O2), 519 oxygen experiment, 520–522 P Pacific/Honolulu time zone, 119 Pacific/Pago_Pago time zone, 119 packages, 12, 343–345 pages, parsing part of, 333 pandas DataFrames, 439 2D data, 441 setting up SQL query, 460 pandas library, 437, 439 heat plots with, 448–450 using BigQuery with, 453 parameters, 86 = value argument, 201 brightness, 601 color, 601 delay, 602 names, 196 optional, 198 passing to methods, 226–227 percent, 604, 605, 606 pixel, 601 speed, 602 validation_data, 387 Verbose, 389 wait_ms, 600, 601 parentheses with constants in functions, 90 number containing, 65 for tuples, 164 ParkMyRide application, 421 parse package, 327 parsing parts of pages, 333 storing content, 333–335 pass keyword, 217, 237 patch cables, 499–501, 503, 550 people object, 308 PEP (Python enhancement proposals), 50 PEP 8, 50 errors, 81–82 workspace settings enabled with, 52 PEP 20, 50 percent parameter, 604, 605, 606 persisted data, 224 physical computing, 471–486 controlling LED, 482–484 in everyday life, 472
  • 321. Index 673 making computers do things, 474 Pulse-width modulation (PWM), 485–486 Raspberry Pi, 476–482 assembling hardware, 478–482 GPIO pins, 477 GPIO Python libraries, 477 hardware for “Hello World,” 478 using small computers to build projects, 474–476 Pi camera, 623 Python Mars Rover PiCar-B, 584–585 video testing, 592–594 pi constant, 90 Pi2Grover, 502 base unit, 478, 490–491 board, 480, 552 Grove interface board, 541, 548, 556 PiCar-B robot assembling, 586–594 calibrating servos, 588–590 installing software for test, 591–592 Pi camera video testing, 592–594 running tests in Python, 591 test code, 592 components of, 577–586 controller board, 578 drive motor, 580–581 Pi camera, 584–585 Pixel RGB programmable LEDs, 582–583 RGB LED, 581–582 servo motors, 578–580 ultrasonic sensor, 585–586 materials for, 576–577 PiCar-B-Video-Test.py software, 593 pin headers, 500 pip (Pip Installs Packages), 344 pip commands, 57 PIR detectors, 536 pixel parameter, 601 Pixel RGB programmable LEDs, 582–583 functions allLEDSOff() function, 601 Color() function, 601 colorWipe() function, 600 rainbowCycle() function, 600 setPixelColor() function, 601 theaterChaseRainbow() function, 600–601 root permission for, 609 pointers moving with seek(), 283 using tell(), 281–283 pop() method, 155, 163, 181, 184–185 popitem() method, 181, 185 populating attributes, 218 position numbers, referencing items in list by, 148–149 positive feedback, 556 Positive number, 72 potentiometers, 539, 578 power consumption, Raspberry Pi and, 577 PowerShell task-based command-line shell, 42 prebuilt robots, 575 Preceptrons, 358 price, diamond database, 441 print() function, 274 adding spaces in, 197 for data dictionaries, 172 print statement, 278, 280 print(pi) command, 346, 348 processing data, 433–434 project inspiration, 424–425 property of objects. See attributes provider_city, inpatient_charges_2015 Dataset, 457 provider_id column, inpatient_charges_2015 Dataset, 457 provider_name, inpatient_charges_2015 Dataset, 457 provider_state, inpatient_charges_2015 Dataset, 458 provider_street_ address, inpatient_ charges_2015 Dataset, 457
  • 322. 674 Python All-in-One For Dummies provider_zipcode, inpatient_charges_2015 Dataset, 458 ps xaf command, 486 pseudo-random number generator, 371 publish-subscribe system, 609 pulse width modulation (PWM), 485–486, 538, 539, 548, 551, 579, 580, 583, 599 punctuation in Python colons, 171 commas, 199 parentheses with constants in functions, 90 for tuples, 164 PyLab package, 439 Pylint tool, 30, 51 workspace settings enabled with, 52 Python to access Web, 323 building dashboard on phone using, 525–536 breaking down code, 533–535 building on expertise, 536 HDC1080 temperature and humidity, 525–526 how to add the Blynk dashboard, 527–530 Blynk app, 531–533 choosing interpreter, 19 choosing version of, 9–11 DC motor software, 545–547 elements object-oriented programming (OOP), 53 use of indentations, 54–55 using modules, 56–59 Zen of Python, 49–53 exiting out of, 21 interpreters, 11–12 choosing, 19 going to, 30 objects, 70 path, 36 popularity of, 8–9 servo software, 551–552 stepper software, 562–563 tools used with, 11–17 writing in VS Code, 17–21 Python 3 workspace, 38 Python Coding Style Guidelines, 50–51 python command, 30 Python enhancement proposals (PEP), 50 Python Mars Rover PiCar-B robot, 575–594 assembling, 586–594 calibrating servos, 588–590 installing software for test, 591–592 Pi camera video testing, 592–594 running tests in Python, 591 test code, 592 Cat/Not Cat recognition, 645 cats and dogs neural network on, 640–646 code, 640–643 results, 643–645 components of, 577–586 controller board, 578 drive motor, 580–581 Pi camera, 584–585 Pixel RGB programmable LEDs, 582–583 RGB LED, 581–582 servo motors, 578–580 ultrasonic sensor, 585–586 controls with Alexa, 646 Follow ball, 646 materials for, 576–577 programming for coordinating motor movements with sensors, 610–613 front LED functions, 598–600 high-level Python interface, 595–597 main motor functions, 602–603 Pixel strip functions, 600–601 Python Robot Interface Test, 606–610 self-driving, 613–622
  • 323. Index 675 servo functions, 603–606 “Single Move” code, 597–598 ultrasonic distance sensor function, 601–602 Santa/Not Santa, 646 Python Nano editor, 441 Python Package Index, 344 Python Robot Interface Test, 606–610 Python3 library, 382 Q quotation marks, 67–68 quotes.txt, 270–272 R r: (Read) mode, 269 R library, 434 r+: (Read/Write) mode, 269 radio control cars, 576 rainbowCycle() function, 600 raise error statement, 259 raising errors, 259–263 raising exceptions, 249 RAM (Random Access Memory) , 415, 472, 567 range() function, in for loop, 134–136 Raspberry Pi, 474, 476–482, 489–490, 575 A , 583 3B, 583 3B , 409 building robots with, 577 CNN network on, 626 12C device on, 507–508 adding hardware AI to, 417–419 assembling hardware, 478–482 base unit, 490–491 bits on, 498 building robots with, 575 cameras, 584 CNN network on, 626 GPIO pins, 474, 477, 555 GPIO Python libraries, 477 GUI, 441, 643 hardware for “Hello World,” 478 installing google.cloud library, 459 installing MatPlotLib on, 444 installing NumPy on, 375, 441 installing pandas on, 441 installing seaborn library, 449 limitations with, 415–417 MouseAir robot, 645 Pixel RGB pulses with, 600 Pixel RGB strings on, 583 running headless, 396 shutting down, 509 stepper motor project, 561–564 Stretch version, 576 supplying current to 5V pins, 549 using for physical computing, 476–482 Zero, 577, 583 Zero W, 583 Raspberry Pi/ADC/oxygen sensor hookup, 520 rb (read binary), 273 RC airplanes/boats, 539 Read (r:) mode, 269 read binary (rb), 273 read() loop, 283 read() method, 276 read([size]) method, 276 reading code, 54–55 temperature and humidity, 511–514 readline() method, 276, 277, 279–280, 282 readlines() method, 276, 277–279 Read/Write (r+:) mode, 269 red errors, 81–82 Reed-Solomon error-correction algorithm, 371 regular expressions, 295 reinforcement learning algorithm, 394–395
  • 324. 676 Python All-in-One For Dummies relational operators, 70–71 != is not equal to, 126 < is less than, 126 <= is less than or equal to, 126 == is equal to, 126 > is greater than, 126 >= is greater than or equal to, 126 remove() method, 154, 163 removing items from lists, 154–156 Representational State Transfer (REST), 452 request module, 332 request package, 327 requests library, 533–534 Reset button, 544 resources buying LiPo batteries, 577 buying PiCar-B, 577 buying Raspberry Pi 3B , 577 Cats and Dogs database, 624 Google cloud, 454 Kaggle online community, 440 NumPy library tutorials, 438 Python code feedback video, 613 Python exceptions list, 259 Python test on PiCar-B robot video, 591 “Robot Brain” code video, 620 Robot Operating System (ROS), 609 RobotInterface class test video, 609 Santa/Not Santa video, 646 Single Move code on robot video, 598 SQL tutorials, 459 support page for this book, 592 TensorFlow download link, 624 Wilkinson Baking, 569 response package, 327 REST (Representational State Transfer), 452 Rethink Robotics, 570 returned values, from functions, 205–206 reverse() method, 161, 163 reversing lists, 161–162 revolutions per minute (RPM), 538 RGB LED, Python Mars Rover PiCar-B, 581–582 ribbon cables, 587 RITest.py code, 606–608 rjust() method, 212 “Robot Brain” code, 614–617 Robot Operating System (ROS), 570, 609 robotBrain.py software, 620 robotics future of, 646 machine learning neural network, 624–633 Python Mars Rover PiCar-B with, 640–645 general discussion of, 567–568 main parts of communications, 573 computers, 572–573 motors and actuators, 573 sensors, 573 programming, 574 Python Mars Rover PiCar-B, 575–594 assembling, 586–594 components of, 577–586 materials for, 576–577 programming, 595–622 types of, 568–572 Wilkinson Bread-Making Robot, 569–570 RobotInterface class functions, 598–610 front LED functions, 598–600 set_Front_LED_Off() function, 599–600 set_Front_LED_On() function, 598–599 main motor functions, 602–603 motorBackward() function, 602 motorForward() function, 602 stopMotor() function, 602–603 Pixel strip functions, 600–601 allLEDSOff() function, 601 Color() function, 601 colorWipe() function, 600
  • 325. Index 677 rainbowCycle() function, 600 setPixelColor() function, 601 theaterChaseRainbow() function, 600–601 Python Robot Interface Test, 606–610 servo functions, 603–606 centerAllServos() function, 606 headTiltDown() function, 604 headTiltMiddle() function, 604 headTiltPercent() function, 605 headTiltUp() function, 604 headTurnLeft() function, 603 headTurnMiddle() function, 603 headTurnPercent() function, 604 headTurnRight() function, 603 wheelsLeft() function, 605 wheelsMiddle() function, 605 wheelsPercent() function, 606 wheelsRight() function, 605 ultrasonic distance sensor function, 601–602 RobotInterface.py, 595 robotparser package, 327 ROS (Robot Operating System), 570, 609 round() function, 86–87, 388 RPi GPIO library, 551 RPM (revolutions per minute), 538 Run Python File option, 76 Run-As-Administrator, 14–15 RX signal line, 495 S s * n operator, 103 s[i] operator, 103 s[i:j] operator, 103 s[i:j:k] operator, 103 Samsung smartphones, 360 Santa, machine learning neural network for recognizing, 646 saving code, 41 current settings as workspace settings, 36 scraped data, 335–338 work, 76 SBC (single board computer), 473 SC-90 9g micro servo motors, 579 scalar value, 65 scalars, 382 s.capitalize() method, 106 scatter plots, 445 SCL (serial clock line), 498, 506 s.count(x,[y.z]) method, 106 s.count(x) operator, 103 scraped data scraper.py program, 338 scraping. See web scraping screw terminals, 543 scripting language, 42 SD cards, 588 SDA (serial data line), 498, 506 SDA connector, 498 SDL_Pi_GroveI2CMotorDriver library, 547 SDL_Pi_HDC1080_Python3 directory, 514, 516 SDL_Pi_HDC1080_Python3 library, 545 seaborn library, 437 generating heat plots with, 448–450 installing, 466 Second Grove Female Patch Cord, 559 Secure SHell (SSH), 474 security, cloud computer, 453–454 seek() method, 283 self keyword, 226, 229 self-driving programming for Python Mars Rover PiCar-B, 613–622 Adeept software overview, 621 using threads, 620–621 semaphores, 620 senseOxygen.py sensor thread, 620
  • 326. 678 Python All-in-One For Dummies sensors, 474 coordinating motor movements with, 610–613 distance function, 601–602 Python Mars Rover PiCar-B, 585–586 for robotics, 573 ultrasonic, 585–586, 601–602 sequence operators for strings, 103 serial clock line (SCL), 498, 506 serial data line (SDA), 498, 506 serial dates, 306, 309 serial interface, 495 serial peripheral interface (Digital SPI), 506 serial signal, 495 serialization, 306–307 servers, 324 servo motors, 539, 548–551 breaking down code, 552–554 calibrating, 588–590 functions, 603–606 centerAllServos(), 606 headTiltDown(), 604 headTiltMiddle(), 604 headTiltPercent(), 605 headTiltUp(), 604 headTurnLeft(), 603 headTurnMiddle(), 603 headTurnPercent(), 604 headTurnRight(), 603 wheelsLeft(), 605 wheelsMiddle(), 605 wheelsPercent(), 606 wheelsRight(), 605 Python Mars Rover PiCar-B, 578–580 Python servo software, 551–552 SC-90 9g micro, 579 SG90 micro, 579 set_Front_LED_Off() function, 599–600 set_Front_LED_On() function, 598–599 setdefault() method, 182, 188–190 setPixelColor() function, 601 sets, 165–167, 170 settings saving as workspace settings, 36 VS Code editor, 36 s.find(x,[y.z]) method, 106 SG90 micro servo motors, 548, 549, 579 Shovic, John, 357, 462 showexpiry() method, 242 shutting down Raspberry Pi, 509 sigmoid function, 369, 377 signals, 493–498 Grove 12C, 497–498 Grove analog, 494–495 Grove digital, 493–494 Grove UART, 495–496 simpleFeedback.py code, 610–612 s.index(x,[y.z]) method, 106 s.index(x[, i[, j]]) operator, 103 single board computer (SBC), 473 “Single Move” code, 597–598 singleMove.py, 598 s.isalpha() method, 106 s.isdecimal() method, 106 s.islower() method, 106 s.isnumeric() method, 106 s.isprintable() method, 106 s.istitle() method, 106 s.isupper() method, 106 SKU (Stock Keeping Unit), 188 slave, 506–507 sleep() function, 597 slices, 417 s.lower() method, 106 s.lstrip() method, 106 Smart City application, 421 SMBus, 498, 506, 517 smbus library, 547 The Society of Mind (Minsky), 357 softmax function, 400 software environment, 396–397 sort() method, 159, 163, 202, 207 sorting lists, 159–161 .sort(key=lambda s:s.lower()) method, 159
  • 327. Index 679 sort(reverse=True) method, 202 source code, 268, 330 spaces, number containing, 65 sparse categorical crossentropy function, 401 special characters, 281 special variables, 341 speed, of robots, controlling with GPIO lines, 580 speed parameter, 602 SQL (Structured Query Language), 459 SQL All In One For Dummies 3rd Edition (Tayor), 459 SQL For Dummies (Taylor), 459 SQL in 10 Minutes (Forta), 459 s.replace(x,y) method, 106 s.rfind(x,[y,z]) method, 106 s.rindex() method, 106 s.rstrip() method, 106 SSH (Secure SHell), 474 s.strip() method, 106 s.swapcase() method, 106 standardized connectors, 487–488 statements, 78 static methods, 232–233 statistical analysis, 360 stepper motors, 539–540, 554–564 breaking down code, 563–564 and driver boards, 560 project, 561 Python stepper software, 562–563 stepperTest.py code, 563 s.title() method, 106 Stock Keeping Unit (SKU), 188 Stop button on Jupyter Notebook, 142–143 stopMotor() function, 602–603 storage, 415 on cloud, 420–422 parsed content, 333–335 str class, 340, 342 str data conversion, 307 str() function, 87, 158 str object, 105 Stretch version, Raspberry Pi, 576 strftime, 111 string concatenation, 101 string JSON data conversion, 307 STRING type, inpatient_charges_2015 Dataset, 457–458 strings, 66 concatenating, 101–102 converting, 290–291 dates, working with, 107–112 keyed JSON loading from, 315–316 in lists, 148 looping with for, 136–137 manipulating with methods, 105–107 passing for functions, 197 Pixel string, 582 sequence operators for, 103 string operators, 102–105 times, formatting for, 112–123 calculating timespans, 114–118 time zones, 118–123 as values in data dictionary keys, 170 strip() method, 294 Structured Query Language (SQL), 459 Style Guidelines, 50–51 subclasses, 235, 237–238 sub-dictionaries, 312 sudo command, 609 sudo halt command, 509, 588 sudo keyword, 483 sudo python3 RITest.py, 609 sudo reboot command, 591 Sum Squared Loss, 375, 380 SumSquaredLossList.csv SunAirPlus board, 501, 502 sunlight sensor, 497 sun-tracking solar panels, 540 supervised learning algorithm, 394 s.upper() method, 106 Switch modules, 493 SwitchDoc Labs HDC1080, 509
  • 328. 680 Python All-in-One For Dummies synapses, 368 syntax to create variable, 74 for importing modules, 58 rule, 80 SyntaxError, 79 sys library, 514 T t: (Text) mode, 270 table, diamond database, 441 tabular data, 303–304, 439 tau constant, 90 Taylor, Allen G., 459 TCP/IP networks, 573 telecommunications network, 358 tell() method, 281–283 temperature HDC1080, 508–509, 525–526 MyTemperature app, 526, 529, 530 reading from 12C device, 511–514 temperatureTest.py, 529, 531–533 tensor processing unit (TPU), 419, 421 TensorBoard, 392 TensorFlow language, 361–363 building neural-network in, 383–392 breaking down code, 386–388 changing to three-layer neural network, 390–392 compiling models, 384 evaluating models, 388–390 loading data, 384 installing in library, 382 for machine learning, 395–396 code, 627–629 examining code, 629–632 results, 632–633 machine learning neural network in robotics, 625–633 or same neural network, 381–382 TensorFlowKeras.py tensors, 363, 371, 381–382, 423. See also matrices Terminal, 275 opening in Interactive Mode, 28 Run Python File option in, 76 in VS Code, 20 ternary operations, operations with if statements, 133–134 testing for detecting clothes, 405–406, 409 external clothes pictures, 403–405 for Mars Rover PiCar-B, 591 networks, 398–399 trained networks, 633–639 code, 634–635 explaining code, 636–637 results, 637–639 Text (t:) mode, 270 text editor, 287 adding in Jupyter Notebook, 47 editing, 268 and numbers, 64–65 text: variable, 334 theaterChaseRainbow() function, 600–601 This %A %B %d format string, 111 threads, 620–621 three-input XOR gate, 370 three-layer neural network, 384 threshold, 359 throwing exceptions. See raising exceptions time directives, 110 time library, 597 time zones, 118–123 current date and time for multiple, 121–122 map of, 118 Olson Database, 119 scheduled event in multiple, 121–122
  • 329. Index 681 timedelta object, 114, 120 times, formatting strings for, 112–123 calculating timespans, 114–118 time zones, 118–123 time-series data types, pandas library and, 439 timespans, calculating, 114–118 timestamps, 107 to_curr(any_num, len), 349 to_date(any_str), 349 Toasteroid robot, 571 today() method, 108 torque rating, 539 total_discharges, inpatient_charges_2015 Dataset, 458 TPU (tensor processing unit), 419, 421 trailing whitespace, 81–82 training networks, 398, 402–403 for detecting cats and dogs, 631 code, 634–635 explaining code, 636–637 results, 637–639 trainingEpochs variable, 378 triple quotation marks, 95 True data conversion, 307 true JSON data conversion, 307 True/false Booleans, 340 building applications, 71–72 building data types, 68–69 converting, 293 Truth Table, 370 try keyword preventing crashes with, 256 using with errors, 251, 257–258 tuples, 163–165 error messages in, 165 inside functions, 204 objects versus, 222 as values in data dictionary keys, 170 TWI (Two Wire Interface), 498, 506 two-layer neural networks, 366, 625 TX signal line, 495 type() function, 87, 340 type term, 339 U UART, 495–496 UART RFID reader, 496 UART serial interface, 501 Ubuntu system, 375 ULN2003 motor drive chip, 557 ultra.checkdisk() function, 585 ultrasonic mapping, 621 ultraviolet (UV) components, 497 274, 275 Universal Product Code (UPC), 188 universal serial bus (USB) ports, 473 Universal Time Coordinated (UTC), 119 unkeyed JSON, 314–315 UNL2003 driver board, 558, 559, 560 Uno Grove base board, 489 unsupervised learning algorithm, 394 UPC (Universal Product Code), 188 update() method, 166, 177–178, 182 url: variable, 334 urllib package, 327 urlopen method, 332 URLs, 324–325 USB (universal serial bus) ports, 473 user agents, 324 users, 324 UTC (Universal Time Coordinated), 119 274, 275 UV (ultraviolet) components, 497 V v object, 311 v variable, 316–317 validation_data parameter, 387
  • 330. 682 Python All-in-One For Dummies values, 74 changing attributes of, 222 to data dictionary keys, 169–170 functions with multiple, 199–203 .values() method, 180, 182 variables, 72–77 chunk, 285 class, 228–230 content, 276, 333 creating in code, 74 creating names for, 73–74 first_name, 290 full_name, 290 history, 387–388, 409 inside functions, 196 k, 316–317 last_name, 290 link:, 333 manipulating, 75–76 num, 72 running app in VS Code, 76–77 saving work, 76 special, 341 text:, 334 trainingEpochs, 378 url:, 334 using syntax to create, 74 v, 316–317 variety, 431, 432 VCC connector, 498 VCC signal line, 493, 495 vectors, 382 velocity, 431, 432 Verbose parameter, 389 video adding in Jupyter Notebook, 47 Pi camera testing, 592–594 Videocore-IV mobile graphics processor, 417 Virtual Network Computing (VNC), 396, 445, 585 Voice Time (Shovic), 646 voltage, 491 dividers, 494 of SG90 micro servo motors, 579 volume, 430–432 VS Code editor, 12–13 built-in debugger, 43–45 exiting out of, 21 extensions, 17 installing, 13–17 Python 3 workspace in, 38 running apps in, 76–77 running code in, 41–42 settings, 36 Terminal in, 20 using alphabetize function in, 202–203 welcome screen, 17 VS Code IntelliSense, 195 .vscode icon, 38 W w: (Write) mode, 269 wafer level chip scale package (WLCSP), 509 wait_ms parameter, 600, 601 Watson Personality Insights, 422 wb mode, 285 Web, 316–317, 323–327 HTTP headers, 325–327 scraping, 323–327 understanding URLs, 324–325 web scraping, 330–338 parsing part of pages on Web, 333 storing parsed content on Web, 333–335 web services, 452–453 weights, 366, 368 weightsLayer1.txt weightsLayer2.txt
  • 331. Index 683 wheelsLeft function, 596 wheelsLeft() function, 605 wheelsMiddle() function, 596, 598, 605 wheelsPercent function, 596–597 wheelsPercent() function, 606 wheelsRight() function, 605 whence value, 283 while loops, 280 operations with, 141–146 breaking with break, 144–146 with continue, 143–144 removing items from lists with, 155 “Robot Brain” program with, 620 whole number, 65, 66 width, formatting, 96–97 WiFi, 473, 573 Wilkinson Bread-Making Robot, 569–570 Windows computers creating folders for code, 37 development environments for, 35 opening Anaconda Navigator in, 16 WLCSP (wafer level chip scale package), 509 words (strings), 66–68 words, number containing, 65 workspace creating, 34–37 on Python 3, 38 settings, 36, 52 Write (w:) mode, 269 X x: (Create) mode, 269 x, diamond database depth, 441 length, 441 x in s operator, 103 x not in s operator, 103 XBee wireless sockets, 495 xlrd (Excel reader), 309 XOR gate (eXclusive OR gate), 370, 371 Y y, diamond database, 441 Z Zen of Python principles, 49–53, 348 Zulu time, 119
  • 333. About the Author John Shovic has been working with software and electronics since he talked his school into letting him use their IBM 1130 computer for the whole summer of 1973. has founded multiple companies: Advance Hardware Architectures; TriGeo Net- over 70 invited talks and has published over 50 papers on a variety of topics on - bunch of students that are as excited about technology and computers as he is. Alan Simpson - - tinue to get rave reviews from his many students and followers. Author’s Acknowledgments John Shovic: curled up on my lap. Alan Simpson: - - -
  • 334. Dedication John Shovic: making sure my socks match in the morning. Thank you! Alan Simpson: Publisher’s Acknowledgments Executive Editor: Project Editor: Copy Editor: Technical Editor: Production Editor: Vasanth Koilraj Cover Image: