In [8]:
import cv2
import numpy as np
I. PART 1: OpenCV and object detection¶
I.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
cv2.imshow(‘frame’,frame)
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.
cv2.VideoCature() opens the default camera
The while loop runs forever (as long as cap.read() is True, which meas as long as the camera is filming).
Inside the loop, the video captures both gray and color videos and shows them.
if cvq2.waitKey(1) & 0xFF==ord(‘q’) means if you press q, then the video capture stops
I.2 Digit Recognition¶
In [3]:
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
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))
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
I.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()
Part II: RNN and text classification¶
In [ ]:
## II.1
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?
Type answers here
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.
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 [ ]: