In [1]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Input
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.keras import backend as K
import tensorflow.keras.utils
from tensorflow.keras.utils import plot_model
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import matplotlib.pyplot as plt
import sklearn.metrics
from IPython.display import Image, display
import numpy as np
import utils_keras
import utils_classification
import time
import os
from IPython.display import clear_output
from tensorflow.keras.models import Model
%matplotlib notebook
Using TensorFlow backend.
In [3]:
path_model_whole = '/home/sheila/datasets/cows/outputs/heatcows-VGG16-topmodel-1000-256_model_whole.h5'
model_whole = load_model(path_model_whole)
model_whole.summary()
# plot_model(model_whole, expand_nested=True)
WARNING: Logging before flag parsing goes to stderr.
W0822 15:11:31.883861 140307225327424 hdf5_format.py:177] No training configuration found in save file: the model was *not* compiled. Compile it manually.
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, None, None, 3)]   0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, None, None, 64)    1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, None, None, 64)    36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, None, None, 64)    0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, None, None, 128)   73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, None, None, 128)   147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, None, None, 128)   0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, None, None, 256)   295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, None, None, 256)   590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, None, None, 256)   590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, None, None, 256)   0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, None, None, 512)   1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, None, None, 512)   0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, None, None, 512)   0         
_________________________________________________________________
model (Model)                (None, 2)                 25345770  
=================================================================
Total params: 40,060,458
Trainable params: 40,060,458
Non-trainable params: 0
_________________________________________________________________
In [4]:
img_width, img_height = 224, 224
num_classes=2
In [84]:
img_width, img_height = 224, 224
path_query = '/home/sheila/datasets/cows/query/'
img = load_img(path_query+'heat5.jpg')
img = img.resize((img_width, img_height))
plt.imshow(img)
x = img_to_array(img)
print('input shape', x.shape)
x = np.expand_dims(x, axis=0)
print('Shape of x =>',x.shape)
input shape (224, 224, 3)
Shape of x => (1, 224, 224, 3)
In [85]:
class_names= ['nonheat', 'onheat']
In [86]:
test_datagen = ImageDataGenerator(rescale=1. / 255)
testing_generator = test_datagen.flow(x, batch_size=1, shuffle=False)
predictions = model_whole.predict_generator(testing_generator,steps=1)
print('Shape of predictions =',predictions.shape)
print('Predictions =',predictions)
predictions_top1 = utils_classification.invert_categorical(predictions)
print(predictions_top1)
predictions_top1 = predictions_top1[0]
print('The predicted class number of next image is {} -> {} '.format(
    predictions_top1 , class_names[predictions_top1] )  )

sorted_indices=np.argsort(-predictions).ravel()
Shape of predictions = (1, 2)
Predictions = [[2.0549185e-18 1.0000000e+00]]
[1]
The predicted class number of next image is 1 -> onheat 
In [113]:
import os
predictions = predictions.ravel()
for index in np.arange(2):
    #print(sorted_indices[index])
    print('Class => {0} -> \t\t{1:.2f}% '.format( class_names[sorted_indices[index]],
                                    predictions[sorted_indices[index]]*100 ))
    if(index==0 and  predictions[sorted_indices[index]] > 0.5 ):
        os.system("say '"+class_names[sorted_indices[index]]+"'")
Class => onheat -> 		100.00% 
Class => nonheat -> 		0.00% 
In [114]:
# Constant Interaction
#while True:
for i in np.arange(10):
    clear_output(wait=True)
    print('hola',i)
    time.sleep(0.5)
hola 9
In [115]:
test_datagen = ImageDataGenerator(rescale=1. / 255)
In [118]:
def predict_img():
    img = load_img(path_query + 'currentFrame.jpg')
    img = img.resize((img_width, img_height))
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)
    testing_generator = test_datagen.flow(x, batch_size=1, shuffle=False)
    predictions = model_whole.predict_generator(testing_generator,steps=1)

    sorted_indices=np.argsort(-predictions).ravel()
    predictions = predictions.ravel()
    for index in np.arange(num_classes):
        print('Class => {0} -> \t\t{1:.2f}% '.format( class_names[sorted_indices[index]],
                                        predictions[sorted_indices[index]]*100 ))
        #if(index==0 and  predictions[sorted_indices[index]] > 0.5 ):
            #if class_names[sorted_indices[index]]=='drinking': return
            #os.system("say '"+class_names[sorted_indices[index]]+"'")
In [121]:
import time
while True:
    clear_output(wait=True)
    try:
        predict_img()
    except IOError:
        pass
    time.sleep(2)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-121-24493d50b644> in <module>
      6     except IOError:
      7         pass
----> 8     time.sleep(2)

KeyboardInterrupt: 

Reading from IoT Device

In [130]:
import cv2 
#fx=0.5
#fy=0.5

def save_image():
    ret, frame = cap.read() # Capture frame-by-frame
    #cv2.imshow('frame',frame) # Display the resulting frame
    #frame = cv2.resize(frame, None, fx=fx, fy=fy)
    cv2.imwrite(path_query+'currentFrame.jpg', frame)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
    plt.imshow(frame)
    plt.show()
    type(frame) # --> already ndarray
    frame.shape #frame = frame[:224, :224]
    #return frame
In [126]:
#import cv2 
#cap = cv2.VideoCapture(0)

#while(True):
#    clear_output(wait=True)
#    save_image()
#    time.sleep(0.3)

# When everything done, release the capture
#cap.release()
#cv2.destroyAllWindows()

Constant Prediction

In [133]:
import cv2 
cap = cv2.VideoCapture(0)
while True:
    clear_output(wait=True)
    save_image()
    try:
        predict_img()
    except IOError:
        print('------------IO Error --------')
        pass
    time.sleep(1)
Class => onheat -> 		100.00% 
Class => nonheat -> 		0.00% 
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-133-6ab3ceb68ece> in <module>
      9         print('------------IO Error --------')
     10         pass
---> 11     time.sleep(1)

KeyboardInterrupt: 
In [ ]:
 
In [ ]: