In [1]:
!pip install opencv-python
Requirement already satisfied: opencv-python in d:\python\lib\site-packages (4.2.0.34)
Requirement already satisfied: numpy>=1.14.5 in d:\python\lib\site-packages (from opencv-python) (1.18.1)
In [8]:
import cv2
import numpy as np
Step 1: OpenCV and object detection¶
1.1 Video Capture¶
In [14]:
cap =cv2.VideoCapture(0)
while True: #means forever
ret,frame=cap.read() # Forever it returns the frame and ret which is false or true
gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#if you want to convert the color to black and white
cv2.imshow(‘frame’,frame) # colorful video
cv2.imshow(‘gray’,gray)# to show the gray video
if cvq2.waitKey(1) & 0xFF==ord(‘q’):# If q is pressed stop
break
cap.release()
cv2.destroyAllWindows()
Q1-Try to understand each line of the code and explain it in your report.¶
1. import cv2: open opencv package
2. cv2.VideoCature(0): From cv2, start to record the video, start capture the image from camera. 0 is number of camera you are using. 0 is default camera. cv2.VideoCature(0) opens the default camera to start capture image.
3. while True: When we open the camera, we just open the camera for a second and then close it. By this loop we are keep the camera open forever. (while 1 is equal to 1)
4. ret,frame=cap.read(): read what you’ve seen through camera and store it in frame. Two outputs, ret (return, shows everything is correct or not. -1 means things are not correct. Use ret to debug on our code.) and frame (the actual image recorded)
5. gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY): Creates some black and white images. (While reading the images, you can apply on some filters.)
6. cv2.imshow(‘frame’,frame) & cv2.imshow(‘gray’,gray): just showing the images (frame(colorful images) and gray (B&W images))
7. if cvq2.waitKey(1) & 0xFF==ord(‘q’): means if you press q, then the video capture stops (only works for windows not for MAC, need to click the window and press q.)
8. cap.release(): release the camera, python won’t use the camera anymore.
9. cv2.destroyAllWindows(): close the windows (the two) that are running now.
1.2 Digit Recognition¶
Q2- Use the code provided (mnist-cnn) to design a digit recognition but this time read the image of the number directly from your webcam. After the model was learnt, you can use following command to save the model for future uses:¶
model.save(‘address/to/file/my_model.h5’) This model is already trained and the result is saved as part of the lab package. To load the provided model you can use following command:
In [3]:
import cv2
import tensorflow
new_model = tensorflow.keras.models.load_model(‘my_model.h5’)
In [6]:
# Handwritten recognition using camera
import cv2
import numpy as np
def get_img_contour_thresh(img):
x, y, w, h = 0, 0, 300, 300 #create small rectangle green frame
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (35, 35), 0)
ret, thresh1 = cv2.threshold(blur, 70, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
thresh1 = thresh1[y:y + h, x:x + w]
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
return img, contours, thresh1
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
ret, img = cap.read()
ret
img, contours, thresh = get_img_contour_thresh(img)
ans1 = ”
if len(contours) > 0:
contour = max(contours, key=cv2.contourArea)
if cv2.contourArea(contour) > 2500:
# print(predict(w_from_model,b_from_model,contour))
x, y, w, h = cv2.boundingRect(contour)
# newImage = thresh[y – 15:y + h + 15, x – 15:x + w +15]
newImage = thresh[y:y + h, x:x + w]
newImage = cv2.resize(newImage, (28, 28)) #(28*28 green frame)
newImage = np.array(newImage)
newImage = newImage.flatten()
newImage = newImage.reshape(newImage.shape[0], 1)
newImage2 = newImage.flatten().reshape(1,28,28,1)
newImage2 = newImage2.astype(‘float32’)
newImage2 /= 255
result = new_model.predict(newImage2)
ans1= np.argmax(result)
#ans1 = Digit_Recognizer_LR.predict(w_LR, b_LR, newImage)
x, y, w, h = 0, 0, 300, 300
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(img, “Prediction : ” + str(ans1), (10, 320), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow(“Frame”, img)
cv2.imshow(“Contours”, thresh)
k = cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
# PRESS ESCAPE TO GET OUT OF THE CAMERA
1.3 Face Regonition¶
In [10]:
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
eye_cascade = cv2.CascadeClassifier(‘haarcascade_eye.xml’)
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow(‘Lab 3 Face recognition’,img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Step2: RNN and text classification¶
In [12]:
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
# fix random seed for reproducibility
np.random.seed(7)
In [13]:
# Just load 5000 cases
top_words = 5000
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz
17465344/17464789 [==============================] – 8s 0us/step
In [14]:
# truncate and pad input sequences
max_review_length = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
Q3- We need to do word embedding at this point. What is the meaning of word embedding in¶
context of NLP? Word embedding is the collective name for a set of language modeling and feature learning techniques in natural language processing where words or phrases from the vocabulary are mapped to vectors of real numbers. We view the raw symbols as points in a space of dimension equal to the vocabulary size. The word representations embed those points in a feature space of lower dimension. In the embedding space, words that frequently appear in similar contexts (or any pair of words sharing some “features” learned by the model) are close to each other.
In [15]:
# design model
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1, activation=’sigmoid’))
model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
print(model.summary())
hist= model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=20, batch_size=64)
Model: “sequential_1”
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 500, 32) 160000
_________________________________________________________________
lstm_1 (LSTM) (None, 100) 53200
_________________________________________________________________
dense_1 (Dense) (None, 1) 101
=================================================================
Total params: 213,301
Trainable params: 213,301
Non-trainable params: 0
_________________________________________________________________
None
C:\Users\aymon\Anaconda3\lib\site-packages\tensorflow_core\python\framework\indexed_slices.py:424: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
“Converting sparse IndexedSlices to a dense Tensor of unknown shape. ”
Train on 25000 samples, validate on 25000 samples
Epoch 1/20
25000/25000 [==============================] – 596s 24ms/step – loss: 0.4937 – accuracy: 0.7526 – val_loss: 0.3583 – val_accuracy: 0.8463
Epoch 2/20
25000/25000 [==============================] – 641s 26ms/step – loss: 0.2959 – accuracy: 0.8808 – val_loss: 0.3058 – val_accuracy: 0.8715
Epoch 3/20
25000/25000 [==============================] – 604s 24ms/step – loss: 0.2604 – accuracy: 0.8981 – val_loss: 0.3487 – val_accuracy: 0.8519
Epoch 4/20
25000/25000 [==============================] – 608s 24ms/step – loss: 0.2323 – accuracy: 0.9096 – val_loss: 0.3637 – val_accuracy: 0.8427
Epoch 5/20
25000/25000 [==============================] – 628s 25ms/step – loss: 0.2001 – accuracy: 0.9244 – val_loss: 0.3314 – val_accuracy: 0.8650
Epoch 6/20
25000/25000 [==============================] – 624s 25ms/step – loss: 0.2437 – accuracy: 0.9019 – val_loss: 0.3289 – val_accuracy: 0.8732
Epoch 7/20
25000/25000 [==============================] – 632s 25ms/step – loss: 0.1678 – accuracy: 0.9376 – val_loss: 0.3546 – val_accuracy: 0.8653
Epoch 8/20
25000/25000 [==============================] – 635s 25ms/step – loss: 0.1468 – accuracy: 0.9460 – val_loss: 0.3655 – val_accuracy: 0.8706
Epoch 9/20
25000/25000 [==============================] – 632s 25ms/step – loss: 0.1282 – accuracy: 0.9538 – val_loss: 0.4439 – val_accuracy: 0.8542
Epoch 10/20
25000/25000 [==============================] – 627s 25ms/step – loss: 0.1203 – accuracy: 0.9564 – val_loss: 0.4049 – val_accuracy: 0.8669
Epoch 11/20
25000/25000 [==============================] – 623s 25ms/step – loss: 0.0963 – accuracy: 0.9662 – val_loss: 0.4128 – val_accuracy: 0.8651
Epoch 12/20
25000/25000 [==============================] – 629s 25ms/step – loss: 0.0817 – accuracy: 0.9718 – val_loss: 0.5048 – val_accuracy: 0.8432
Epoch 13/20
25000/25000 [==============================] – 628s 25ms/step – loss: 0.1008 – accuracy: 0.9652 – val_loss: 0.4456 – val_accuracy: 0.8513
Epoch 14/20
25000/25000 [==============================] – 634s 25ms/step – loss: 0.0836 – accuracy: 0.9709 – val_loss: 0.5243 – val_accuracy: 0.8642
Epoch 15/20
25000/25000 [==============================] – 627s 25ms/step – loss: 0.0629 – accuracy: 0.9795 – val_loss: 0.5900 – val_accuracy: 0.8628
Epoch 16/20
25000/25000 [==============================] – 634s 25ms/step – loss: 0.0480 – accuracy: 0.9850 – val_loss: 0.6665 – val_accuracy: 0.8544
Epoch 17/20
25000/25000 [==============================] – 633s 25ms/step – loss: 0.0439 – accuracy: 0.9864 – val_loss: 0.6639 – val_accuracy: 0.8630
Epoch 18/20
25000/25000 [==============================] – 635s 25ms/step – loss: 0.0440 – accuracy: 0.9859 – val_loss: 0.6255 – val_accuracy: 0.8574
Epoch 19/20
25000/25000 [==============================] – 631s 25ms/step – loss: 0.0266 – accuracy: 0.9928 – val_loss: 0.7426 – val_accuracy: 0.8615
Epoch 20/20
25000/25000 [==============================] – 627s 25ms/step – loss: 0.0815 – accuracy: 0.9716 – val_loss: 0.5471 – val_accuracy: 0.8405
In [16]:
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print(“Accuracy: %.2f%%” % (scores[1]*100))
Accuracy: 84.05%
Q4- Draw the learning curves and describe them.¶
There is a overfitting problem. We have a model works perfectly on training data but does not work very well on test data.
In [22]:
import matplotlib.pyplot as plt
plt.style.use(‘ggplot’)
In [25]:
def draw_curves(history):
plt.style.use(‘ggplot’)
acc = history.history[‘accuracy’]
val_acc = history.history[‘val_accuracy’]
loss = history.history[‘loss’]
val_loss = history.history[‘val_loss’]
x = range(1, len(acc) + 1)
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(x, acc, ‘b’, label=’Training accuracy’)
plt.plot(x, val_acc, ‘r’, label=’Validation acc’)
plt.title(‘Training and validation accuracy’)
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(x, loss, ‘b’, label=’Training loss’)
plt.plot(x, val_loss, ‘r’, label=’Validation loss’)
plt.title(‘Training and validation loss’)
plt.legend()
plt.show()
plt.show()
draw_curves(hist)

Q5- Add a dropout to see how the model changes¶
In [1]:
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
# fix random seed for reproducibility
np.random.seed(7)
Using TensorFlow backend.
In [2]:
# Just load 5000 cases
top_words = 5000
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
In [3]:
# truncate and pad input sequences
max_review_length = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
In [4]:
# design model
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1,activation=’sigmoid’))
model.add(Dropout(0.2))
model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
print(model.summary())
hist= model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=20, batch_size=64)
—————————————————————————
TypeError Traceback (most recent call last)
4 embedding_vecor_length = 32
5 model = Sequential()
—-> 6 model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
7 model.add(LSTM(100))
8 model.add(Dense(1,activation=’sigmoid’))
D:\Python\lib\site-packages\tensorflow_core\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
–> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
D:\Python\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in add(self, layer)
159 raise TypeError(‘The added layer must be ‘
160 ‘an instance of class Layer. ‘
–> 161 ‘Found: ‘ + str(layer))
162
163 tf_utils.assert_no_legacy_layers([layer])
TypeError: The added layer must be an instance of class Layer. Found:
In [ ]:
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print(“Accuracy: %.2f%%” % (scores[1]*100))
In [ ]:
def draw_curves(history):
plt.style.use(‘ggplot’)
acc = history.history[‘accuracy’]
val_acc = history.history[‘val_accuracy’]
loss = history.history[‘loss’]
val_loss = history.history[‘val_loss’]
x = range(1, len(acc) + 1)
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(x, acc, ‘b’, label=’Training accuracy’)
plt.plot(x, val_acc, ‘r’, label=’Validation acc’)
plt.title(‘Training and validation accuracy’)
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(x, loss, ‘b’, label=’Training loss’)
plt.plot(x, val_loss, ‘r’, label=’Validation loss’)
plt.title(‘Training and validation loss’)
plt.legend()
plt.show()
plt.show()
draw_curves(hist)
Q6- Add the CNN layer and evaluate the model.¶
In [14]:
from tensorflow.keras.layers import Conv1D, MaxPooling1D
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset but only keep the top n words, zero the rest
top_words = 5000
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
# truncate and pad input sequences
max_review_length = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
# create the model
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(Conv1D(filters=32, kernel_size=3, padding=’same’, activation=’relu’))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(1, activation=’sigmoid’))
model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
print(model.summary())
model.fit(X_train, y_train, epochs=3, batch_size=64)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print(“Accuracy: %.2f%%” % (scores[1]*100))
—————————————————————————
NameError Traceback (most recent call last)
1 from tensorflow.keras.layers import Conv1D, MaxPooling1D
2 # fix random seed for reproducibility
—-> 3 numpy.random.seed(7)
4 # load the dataset but only keep the top n words, zero the rest
5 top_words = 5000
NameError: name ‘numpy’ is not defined
In [15]:
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
—————————————————————————
ModuleNotFoundError Traceback (most recent call last)
1 from __future__ import print_function
—-> 2 import torch
3 x = torch.rand(5, 3)
4 print(x)
ModuleNotFoundError: No module named ‘torch’
In [ ]:
In [ ]: