Saturday, May 9, 2020

Deep Learning 3-Classification with Data Augmentation

May 09, 2020 0

Learn A-Z Deep Learning in 15 Days


In this Lecture, will learn about Classification with Data Augmentation

What is Data Augmentation?
Data augmentation is the process of increasing the amount and diversity of data. We do not collect new data, rather we transform the already present data. Data augmentation techniques such as Rotation, Shearing, Zooming, Cropping, Flipping, and Changing the brightness level are commonly used to train large neural networks.


Imp:-Use Kaggle Kernel to Run Code. Download the Cat Vs Dog dataset from the link below.

import cv2
import os 
import numpy as np


Unzip  the Dataset

!unzip ../input/train.zip

!unzip ../input/test1.zip


Import the Train 

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/train"
data = []
label = []

for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    category = filename.split('.')[0]
    if category == 'dog':
        label.append(1)
    else:
        label.append(0)

    data.append(image/255)


List to Array Conversion

data=np.array(data)
data=data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1)
label=np.array(label)
print(data.shape)
print(label.shape)


Train Test Split

from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(data, label, test_size=0.3, random_state=42) 


One hot encoding


from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train,num_classes=2)
y_val = np_utils.to_categorical(y_val,num_classes=2)


Data Augmentation


from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator( 
                                    rotation_range=20,
                                    width_shift_range=0.2,
                                    height_shift_range=0.2,
                                    brightness_range=(0.3, 0.7),
                                    zoom_range=[0.5, 1.5],
                                    horizontal_flip=True
                                  )

validation_datagen = ImageDataGenerator()
train_generator = train_datagen.flow(x_train, y_train, batch_size = 64)
validation_generator = validation_datagen.flow(x_val, y_val, batch_size= 64)

CNN Architecture

from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, Conv2D, MaxPooling2D, AveragePooling2D, Input, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers 


img_input  = Input(shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS))

x = Conv2D(32, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(64, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(128, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(2, activation='softmax', name='predictions')(x)

model = Model(img_input , x)
model.summary()
model.compile(loss='categorical_crossentropy',optimizer=optimizers.adam(lr=1e-6),metrics=['acc'])


Callbacks


from keras.callbacks import ModelCheckpoint, EarlyStopping

filepath = "./cp-{epoch:02d}.h5"

checkpoint = ModelCheckpoint(filepath,
                             monitor="val_loss",
                             mode="min",
                             save_best_only = True,
                             verbose=1)


earlystop = EarlyStopping(monitor = 'val_loss', 
                          min_delta = 0, 
                          patience =4,
                          verbose = 1,
                          restore_best_weights = True)

# put our call backs into a callback list
callbacks = [earlystop, checkpoint]


Start the Training

model.fit_generator(train_generator,steps_per_epoch = train_generator.n// train_generator.batch_size, epochs=50, validation_data = validation_generator, validation_steps = validation_generator.n// validation_generator.batch_size,  verbose=1, callbacks = callbacks)


Visualize Graph


### Graph Epoch vs acc AND Epoch vs Loss

import matplotlib.pyplot as plt

acc = model.history.history['acc']
val_acc = model.history.history['val_acc']
loss = model.history.history['loss']
val_loss = model.history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'blue', label='Training acc')
plt.plot(epochs, val_acc, 'red', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'blue', label='Training loss')
plt.plot(epochs, val_loss, 'red', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()



Import test dataset


IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/test1"
data = []


for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    data.append(image/255)


List to Array Conversion


data = np.array(data)
test_image = np.array(data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1))


Make the Prediction

predictions = model.predict(test_image)
results = np.argmax(predictions, axis = 1)


Convert the label into the category

key={0:'cat',1:'dog'}
label_prediction=[key[r] for r in results]


Display the Result

import matplotlib.image as img
import matplotlib.pyplot as plt

nb_rows = 3
nb_cols = 3
fig, axs = plt.subplots(nb_rows, nb_cols, figsize=(6, 6), dpi =100)

n = 0
for i in range(0, nb_rows):
    for j in range(0, nb_cols):
        axs[i,j].set_title(label_prediction[n])
        axs[i,j].imshow(data[n],cmap = "gray")
        n += 1  
        
plt.tight_layout()
plt.show()


Create the Dataframe


import pandas as pd

df=pd.DataFrame(data={'imagename':os.listdir(directory), 'predicted_labels': label_prediction})
df.head()


Save the dataframe in csv format


df.to_csv('submission_new_model.csv', index=False, header=True)



In the next blog, we will start  Deep Learning Classification with Transfer Learning.
https://sngurukuls247.blogspot.com/2020/05/deep-learning-4-classificationwithtrans.html

                                                                                                                                                    

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

Deep Learning 2-Classification with Custom CNN

May 09, 2020 0

Learn A-Z Deep Learning in 15 Days


In this Lecture, will learn about Classification with Custom CNN

Imp:-Use Kaggle Kernel Or Google Colab to Run Code. Download the Cat Vs Dog dataset from the link below.

import cv2
import os 
import numpy as np


Unzip  the Dataset

!unzip ../input/train.zip

!unzip ../input/test1.zip


Import the Train 

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/train"
data = []
label = []

for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    category = filename.split('.')[0]
    if category == 'dog':
        label.append(1)
    else:
        label.append(0)

    data.append(image/255)


List to Array Conversion

data=np.array(data)
data=data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1)
label=np.array(label)
print(data.shape)
print(label.shape)


Train Test Split

from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(data, label, test_size=0.3, random_state=42) 


One hot encoding

from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train,num_classes=2)
y_val = np_utils.to_categorical(y_val,num_classes=2)


Custom CNN Architecture

### 1st Network
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax')) 

model.summary()
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

OR

### 2nd Network
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, Conv2D, MaxPooling2D, AveragePooling2D, Input, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers 


img_input  = Input(shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS))

x = Conv2D(32, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(64, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(128, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(2, activation='softmax', name='predictions')(x)

model = Model(img_input , x)
model.summary()
model.compile(loss='categorical_crossentropy',optimizer=optimizers.adam(lr=1e-8),metrics=['acc'])


Callbacks

from keras.callbacks import ModelCheckpoint, EarlyStopping

filepath = "./cp-{epoch:02d}.h5"

checkpoint = ModelCheckpoint(filepath,
                             monitor="val_loss",
                             mode="min",
                             save_best_only = True,
                             verbose=1)


earlystop = EarlyStopping(monitor = 'val_loss', 
                          min_delta = 0, 
                          patience =4,
                          verbose = 1,
                          restore_best_weights = True)

# put our callbacks into a callback list
callbacks = [earlystop, checkpoint]


Start the Training

model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=50,batch_size=64, callbacks = callbacks)


Visualize Graph

### Graph Epoch vs acc AND Epoch vs Loss

import matplotlib.pyplot as plt

acc = model.history.history['acc']
val_acc = model.history.history['val_acc']
loss = model.history.history['loss']
val_loss = model.history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'blue', label='Training acc')
plt.plot(epochs, val_acc, 'red', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'blue', label='Training loss')
plt.plot(epochs, val_loss, 'red', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()



Import test dataset

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/test1"
data = []


for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    data.append(image/255)


List to Array Conversion

data = np.array(data)
test_image = np.array(data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1))


Make the Prediction

predictions = model.predict(test_image)
results = np.argmax(predictions, axis = 1)


Convert the label into the category

key={0:'cat',1:'dog'}
label_prediction=[key[r] for r in results]


Display the Result

import matplotlib.image as img
import matplotlib.pyplot as plt

nb_rows = 3
nb_cols = 3
fig, axs = plt.subplots(nb_rows, nb_cols, figsize=(6, 6), dpi =100)

n = 0
for i in range(0, nb_rows):
    for j in range(0, nb_cols):
        axs[i,j].set_title(label_prediction[n])
        axs[i,j].imshow(data[n],cmap = "gray")
        n += 1  
        
plt.tight_layout()
plt.show()


Create the Dataframe

import pandas as pd

df=pd.DataFrame(data={'imagename':os.listdir(directory), 'predicted_labels': label_prediction})
df.head()


Save the dataframe in csv format

df.to_csv('submission_new_model.csv', index=False, header=True)



In the next blog, we will start  Deep Learning Classification with Data Augmentation.
https://sngurukuls247.blogspot.com/2020/05/deep-learning-3-classification-with_9.html

                                                                                                                                                    

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

Deep Learning 1-Convolution Neural Network

May 09, 2020 0

Learn A-Z Deep Learning in 15 Days



Deep Learning

Deep Learning is a subfield of machine learning concerned that teaches computers to do what comes naturally to humans. The term “deep” usually refers to the number of hidden layers in the neural network. Deep learning models are trained by using large sets of labeled data and neural network architectures that learn features directly from the data without the need for manual feature extraction. One of the most popular types of deep neural networks is known as convolutional neural networks.

Convolutional Neural Network

CNNs, like neural networks, are made up of neurons with learnable weights and biases. Each neuron receives several inputs, takes a weighted sum over them, passes it through an activation function, and responds with an output.CNNs have wide applications in image and video recognition, recommender systems, and natural language processing.



Convolutional Neutral Network is made of Feature extraction layer  & Classification layer.



Feature Extraction

The main objective of convolution is to extract features such as edges, colors, corners from the input.


Layer performs a dot product between two matrices, where one matrix(known as filter/kernel)is the set of learnable parameters, and the other matrix is the restricted portion of the image.


Fig:-Maths Behind Convolution




Gif:- Input image & Filter Convolution 

ReLU(Rectified Linear Unit)

ReLU replaces all negative pixel values in the feature map by zero. The purpose of ReLU is to introduce non-linearity in our ConvNet.

Pooling Layer

The dimensionality of the feature map gets reduced keeping the important information. sometimes this spatial pooling is also called Downsampling or subsampling.

Fully Connected Layer
The Fully Connected layer is a traditional Multi-Layer Perceptron that uses a softmax activation function in the output layer. Fully Connected layer is to use these features for classifying the input image.


**Imp Rules
- Overfitting -val_Acc +val_Loss, +drop -network 
- Underfitting +val_Acc -val_loss, -drop +network +dataset  


Summary





In the next blog, we will start  Deep Learning Classification with Custom.
https://sngurukuls247.blogspot.com/2020/05/deep-learning-2-classification-with.html

                                                                                                                                                      

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

Sunday, May 3, 2020

Computer Vision 5- Objection detection of OpenCV

May 03, 2020 0

Learn A-Z Computer Vision In 15 Days 


In this lecture, we will Learn about  Objection detection of OpenCV.

Contour
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having the same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.

import cv2
import matplotlib.pyplot as plt
%matplotlib inline

image = cv2.imread("color shape.png")
image_grayscale = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

_, otsu_thresh = cv2.threshold(image_grayscale, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
plt.imshow(otsu_thresh, cmap='gray')

contours, hierarchy = cv2.findContours(otsu_thresh, cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image,contours,-1, (0,0,255),2)

# Red color (0,0,255) contour is drawn round the shape
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

Sorting Contours
import cv2
import matplotlib.pyplot as plt
%matplotlib inline

# Read the image  
image = cv2.imread("shapes.png")
orginal_image = image
image_grayscale = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_, otsu_thresh = cv2.threshold(image_grayscale, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# Display the image
plt.rc('xtick', labelsize=3) 
plt.rc('ytick', labelsize=3) 

fig,axes = plt.subplots(nrows= 1, ncols = 2,dpi=500 )

axes[0].set_title('Original Image')
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

axes[1].set_title('otsu_thresh')
axes[1].imshow(otsu_thresh, cmap='gray')

# Find the contours 
contours, hierarchy = cv2.findContours(otsu_thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)

import cv2
import numpy as np

# Function returns the areas of all contours as list
def get_contour_areas(contours):
    all_areas = []
    for cnt in contours:
        area = cv2.contourArea(cnt)
        all_areas.append(area)
    return all_areas


print("Contor Areas before sorting") 
print(get_contour_areas(contours))

# Sort contours in descending order(large to small)
sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)

print("Contor Areas after sorting") 
print(get_contour_areas(sorted_contours))

# Iterate over our contours and draw one at a time
for c in sorted_contours:
    cv2.drawContours(image, [c], -1, (0,255,255), 3)
    cv2.waitKey(0)
    cv2.imshow('Contours by area', image)
cv2.destroyAllWindows()
>>Contor Areas before sorting
[5343.5, 8553.0, 3966.0, 9417.0, 4907.0, 8736.5, 5491.0]
>>Contor Areas after sorting
[9417.0, 8736.5, 8553.0, 5491.0, 5343.5, 4907.0, 3966.0]


Contours Moment
import cv2
import matplotlib.pyplot as plt
%matplotlib inline

# Read the image  
image = cv2.imread("shapes.png")
orginal_image = image
image_grayscale = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_, otsu_thresh = cv2.threshold(image_grayscale, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# Display the image
plt.rc('xtick', labelsize=3) 
plt.rc('ytick', labelsize=3) 

fig,axes = plt.subplots(nrows= 1, ncols = 2,dpi=500 )

axes[0].set_title('Original Image')
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

axes[1].set_title('otsu_thresh')
axes[1].imshow(otsu_thresh, cmap='gray')

# Find the contours 
contours, hierarchy = cv2.findContours(otsu_thresh, cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
# Sort contours by area in descending order(large to small)
sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)

# Iterate over contours 
# draw one at a time  
# Label the shape in using sorted counters
for (i,c)  in enumerate(sorted_contours):
    cv2.drawContours(orginal_image, [c], -1, (0,0,255), 3)  
    
# calculate the contours moment    
    M = cv2.moments(c)
# calculate the x & y by using moment
    cx = int(M['m10'] / M['m00'])
    cy = int(M['m01'] / M['m00'])
    cv2.putText(orginal_image, str(i+1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    cv2.imshow('Moment', orginal_image)
    cv2.waitKey(0)

cv2.destroyAllWindows()

Convex hull
import cv2
import matplotlib.pyplot as plt
%matplotlib inline

image = cv2.imread('shapes.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Threshold the image
ret, thresh = cv2.threshold(gray, 176, 255, 0)

# Find contours 
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    
# Sort Contours by area and then remove the largest frame contour
n = len(contours) - 1

contours = sorted(contours, key=cv2.contourArea, reverse=False)[:n]

# Iterate through contours and draw the convex hull
for c in contours:
    hull = cv2.convexHull(c)
    cv2.drawContours(image, [hull], 0, (0, 255, 0), 2)

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

Edge Detection
import cv2
image =  cv2.imread("dog.jpg")
image = cv2.blur(image,ksize=(5,5))

# Detect the image edges
canny = cv2.Canny(image, 50, 120)


# Display the image
plt.rc('xtick', labelsize=3) 
plt.rc('ytick', labelsize=3) 

fig,axes = plt.subplots(nrows= 1, ncols = 2,dpi=500)

axes[0].set_title('Original Image')
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

axes[1].set_title('Canny Edges')
axes[1].imshow(canny, cmap='gray')


-Line Detection

HoughLines
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

image = cv2.imread('chess.png')

# Grayscale and Canny Edges extracted
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 170)

cv2.imshow("dd",edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Run HoughLines using a rho accuracy of 1 pixel
# theta accuracy of np.pi / 180 which is 1 degree
# Our line threshold is set to 240 (number of points on a line)

lines = cv2.HoughLines(edges, 2, np.pi / 180, 300)

# We iterate through each line and convert it to the format
# required by cv.lines (i.e. requiring endpoints)

for line in lines:
    rho,theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a * rho
    y0 = b * rho
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * (a))
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * (a))
    cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))


Probabilistic Hough line
import cv2
import numpy as np
image = cv2.imread('chess.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imshow('edges', edges)

lines = cv2.HoughLinesP(edges,1,np.pi/180,200,minLineLength=100,maxLineGap=10)

for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

-Circle Detection

Hough Circle
import cv2
import numpy as np

img = cv2.imread('color shape.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray,5)
cimg = cv2.cvtColor(blur,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50 ,param2= 30, minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

-Feature Detection

Sirf & Surf

- Sift & Surf feature detection are patented by their respective creators. In order to run, install the module below:
- pip install python=3.5
- pip install OpenCV-contrib-python==3.4.2.16


import cv2
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

# Create SIFT Object
image = cv2.imread("sbi debit card.jpeg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Initiate ORB detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with ORB
keypoints, descriptors = sift.detectAndCompute(gray,None)

print("Number of keypoints Detected: ", len(keypoints))

# Draw rich keypoints on input image
sift_kp = cv2.drawKeypoints(image, keypoints, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Display the image
plt.rc('xtick', labelsize=3) 
plt.rc('ytick', labelsize=3) 

fig,axes = plt.subplots(nrows= 1, ncols = 2,dpi=500 )

axes[0].set_title('Original Image')
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

axes[1].set_title('ORB feature')
axes[1].imshow(cv2.cvtColor(sift_kp, cv2.COLOR_BGR2RGB))

Oriented FAST and Rotated BRIEF (ORB)
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

image = cv2.imread("sbi debit card.jpeg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Initiate ORB detector
orb = cv2.ORB_create()

# find the keypoints and descriptors with ORB
keypoints, descriptors = orb.detectAndCompute(gray,None)

print("Number of keypoints Detected: ", len(keypoints))

# Draw rich keypoints on input image
orb_kp = cv2.drawKeypoints(image, keypoints, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Display the image
plt.rc('xtick', labelsize=3) 
plt.rc('ytick', labelsize=3) 

fig,axes = plt.subplots(nrows= 1, ncols = 2,dpi=500 )

axes[0].set_title('Original Image')
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

axes[1].set_title('ORB feature')

axes[1].imshow(cv2.cvtColor(orb_kp, cv2.COLOR_BGR2RGB))

Feature Mapping In Live Cam

-SIFT
import cv2
import numpy as np

def sift_detector(new_image, image_template):
    # Function that compares input image to template
    # It then returns the number of SIFT matches between them
    
    image1 = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)
    image2 = image_template
    
    # Create SIFT detector object
    sift = cv2.SIFT()

    # Obtain the keypoints and descriptors using SIFT
    keypoints_1, des1 = sift.detectAndCompute(image1, None)
    keypoints_2, des2 = sift.detectAndCompute(image2, None)

    # Create matcher 
    # Note we're no longer using Flannbased matching
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Do matching
    matches = bf.match(des1,des2)

    # Sort the matches based on distance.  Least distance
    # is better
    matches = sorted(matches, key=lambda val: val.distance)

    return len(matches)

cap = cv2.VideoCapture(0)

# Load our image template, this is our reference image
image_template = cv2.imread('sbi debit card.jpeg', 0) 

while True:

    # Get webcam images
    ret, frame = cap.read()
    
    # Get height and width of webcam frame
    height, width = frame.shape[:2]

    # Define ROI Box Dimensions (Note some of these things should be outside the loop)
    top_left_x = int(width / 3)
    top_left_y = int((height / 2) + (height / 4))
    bottom_right_x = int((width / 2) * 2)
    bottom_right_y = int((height / 2) - (height / 4))
    
    # Draw rectangular window for our region of interest
    cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)
    
    # Crop window of observation we defined above
    cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]

    
    # Get number of ORB matches 
    matches = ORB_detector(cropped, image_template)
    
    # Display status string showing the current no. of matches 
    output_string = "Matches = " + str(matches)
    cv2.putText(frame, output_string, (50,450), cv2.FONT_HERSHEY_COMPLEX, 2, (250,0,150), 2)

    # If matches exceed our threshold then object has been detected
    if matches > threshold:
        cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),
 (0,255,0), 3)
        cv2.putText(frame,'Object Found',(50,50), cv2.FONT_HERSHEY_COMPLEX, 2 ,(0,255,0), 2)
    
    cv2.imshow('Object Detector using ORB', frame)
    
    if cv2.waitKey(1) == 13: #13 is the Enter Key
        break

cap.release()
cv2.destroyAllWindows()

import cv2
import numpy as np

def ORB_detector(new_image, image_template):
    # Function that compares input image to template
    # It then returns the number of ORB matches between them
    
    image1 = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)

    # Create ORB detector with 1000 keypoints with a scaling pyramid factor of 1.2
    orb = cv2.ORB_create()

    # Detect keypoints of original image
    (kp1, des1) = orb.detectAndCompute(image1, None)

    # Detect keypoints of rotated image
    (kp2, des2) = orb.detectAndCompute(image_template, None)

    # Create matcher 
    # Note we're no longer using Flannbased matching
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Do matching
    matches = bf.match(des1,des2)

    # Sort the matches based on distance.  Least distance
    # is better
    matches = sorted(matches, key=lambda val: val.distance)

    return len(matches)

cap = cv2.VideoCapture(0)

# Load our image template, this is our reference image
image_template = cv2.imread('sbi debit card.jpeg', 0) 

while True:

    # Get webcam images
    ret, frame = cap.read()
    
    # Get height and width of webcam frame
    height, width = frame.shape[:2]

    # Define ROI Box Dimensions (Note some of these things should be outside the loop)
    top_left_x = int(width / 3)
    top_left_y = int((height / 2) + (height / 4))
    bottom_right_x = int((width / 2) * 2)
    bottom_right_y = int((height / 2) - (height / 4))
    
    # Draw rectangular window for our region of interest
    cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)
    
    # Crop window of observation we defined above
    cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]

    
    # Get number of ORB matches 
    matches = ORB_detector(cropped, image_template)
    
    # Display status string showing the current no. of matches 
    output_string = "Matches = " + str(matches)
    cv2.putText(frame, output_string, (50,450), cv2.FONT_HERSHEY_COMPLEX, 2, (250,0,150), 2)
    
    
    # Our threshold to indicate object deteciton
    # For new images or lightening conditions you may need to experiment a bit 
    # Note: The ORB detector to get the top 1000 matches, 350 is essentially a min 35% match
    threshold = 130
    
    # If matches exceed our threshold then object has been detected
    if matches > threshold:
        cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),
 (0,255,0), 3)
        cv2.putText(frame,'Object Found',(50,50), cv2.FONT_HERSHEY_COMPLEX, 2 ,(0,255,0), 2)
    
    cv2.imshow('Object Detector using ORB', frame)
    
    if cv2.waitKey(1) == 13: #13 is the Enter Key
        break

cap.release()
cv2.destroyAllWindows()

Watershed Algorithm

Watershed treats images as topographic surface where high intensity is hills and low intensity is valley. It  start filling every isolated valleys (local minima) with different colored water (labels). As the water rises, depending on the peaks (gradients) nearby, water from different valleys, obviously with different colors will start to merge. To avoid that, you build barriers in the locations where water merges. You continue the work of filling water and building barriers until all the peaks are under water. Then the barriers you created gives you the segmentation result.

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('water_coins.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)


# white noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# sure background(black) area
sure_bg = cv2.dilate(opening,kernel,iterations=3)

# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

# sure_fg consist small point
# Finding unknown region
sure_fg = np.uint8(sure_fg)

# unkown has substracted value sure_bg from the sure_fg
unknown = cv2.subtract(sure_bg,sure_fg)


 # Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)

# Add one to all labels so that sure background is not 0, but 1
markers = markers+1

# Now, mark the region of unknown with zero
markers[unknown==255] = 0

markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]

cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Watershed Custom
 import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

colors = [(255,0,0),(0,255,0),(0,0,0)]
# Numbers 0-9
n_markers = 3
# Default settings
current_marker = 1
marks_updated = False
size = 5

img = cv2.imread('water_coins.jpg')
img = cv2.resize(img,((img.shape[1]*750)//img.shape[0],750))
img_copy = np.copy(img)
marker_image = np.zeros(img.shape[:2],dtype=np.int32)
segments = np.zeros(img.shape,dtype=np.uint8)


def mouse_callback(event, x, y, flags, param):
    global marks_updated 

    if event == cv2.EVENT_LBUTTONDOWN:
        
        # TRACKING FOR MARKERS
        cv2.circle(marker_image, (x, y), size, (current_marker), -1)
        
        # DISPLAY ON USER IMAGE
        cv2.circle(img_copy, (x, y), size, colors[current_marker], -1)
        marks_updated = True



cv2.namedWindow('Img Image')
cv2.setMouseCallback('Img Image', mouse_callback)

while True:
    
    # Show the 2 windows
    cv2.imshow('WaterShed Segments', segments)
    cv2.imshow('Img Image', img_copy)
        
        
    # Close everything if Esc is pressed
    k = cv2.waitKey(1)
    
    if k == 27:
        break
        
    # Clear all colors and start over if 'c' is pressed
    elif   k == ord('c'):
        size = 5
        current_marker = 1
        img_copy = img.copy()
        marker_image = np.zeros(img.shape[0:2], dtype=np.int32)
        segments = np.zeros(img.shape,dtype=np.uint8)
        
    # If a number 1,2 is chosen index the color
    elif ( k==49 or k==50) and chr(k).isdigit():    
        # chr converts to printable digit
        current_marker  = int(chr(k))

    elif k == ord('+'):
        size = size + 2
        
    elif k == ord('-'):
        size = size - 2      
        size = abs(size)
        
    if marks_updated:
        
        marker_image_copy = marker_image.copy()
        cv2.watershed(img, marker_image_copy)
        
        segments = np.zeros(img.shape,dtype=np.uint8)
        
        for color_ind in range(1,3):
            segments[marker_image_copy == (color_ind)] = colors[color_ind]
        
        marks_updated = False
        
cv2.destroyAllWindows()

Haar cascade face detection
import cv2
import numpy as np

face_classifier=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_classifier=cv2.CascadeClassifier('haarcascade_eye.xml')


def face_dectector(grayscaletarget,frame):

    faces=face_classifier.detectMultiScale(frame,1.3,5)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(225,0,0),5) 
        roi_gray=grayscaletarget[y:y+h,x:x+w] 
        roi_color=frame[y:y+h,x:x+w]
        eye=eye_classifier.detectMultiScale(roi_gray,1.1,3)
        for (ex,ey,ew,eh) in eye:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(225,0,0),2)
            
    return frame

cap = cv2.VideoCapture(0)

while (cap.isOpened()):
    ret, frame=cap.read()
    if ret:
        grayscaletarget=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)  
        canvas=face_dectector(grayscaletarget,frame)
        cv2.imshow('Live face dectector',cv2.flip(canvas,1))

    if cv2.waitKey(1)==13:
        break
    

cap.release()
cv2.destroyAllWindows()

Dlib Face Detection
import imutils
import dlib
import cv2
from imutils import face_utils

cap = cv2.VideoCapture(0)
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

while (cap.isOpened()):
    ret, frame=cap.read()
    if not ret:
        break  

    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)  
    subjects= detect(gray, 1)    
    for subject in subjects: 
        shape = predict(gray, subject)
        shape = face_utils.shape_to_np(shape)
        (x, y, w, h) = face_utils.rect_to_bb(subject)

    cv2.imshow("Frame",frame)
    if cv2.waitKey(1)==13:
        break

cap.release()
cv2.destroyAllWindows()    


import imutils
import dlib
import cv2
from scipy.spatial import distance
from imutils import face_utils

def eye_aspect_ratio(eye):
    A = distance.euclidean(eye[1], eye[5])
    B = distance.euclidean(eye[2], eye[4])
    C = distance.euclidean(eye[0], eye[3])
    ear = (A + B) / (2.0 * C)
    return ear


flag=0
thresh = 0.2
frame_check = 10
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

cap=cv2.VideoCapture(0)



while (cap.isOpened()):
    
    ret, frame=cap.read()
    frame= cv2.flip(frame,1)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    subjects = detect(gray, 0)
    for subject in subjects:
        shape = predict(gray, subject)
        shape = face_utils.shape_to_np(shape)
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]
        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        ear = (leftEAR + rightEAR) / 2.0
        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (0, 225,255), 2)
        cv2.drawContours(frame, [rightEyeHull], -1, (0, 225, 255), 2)

        if ear < thresh:
            flag += 1
            if flag >= frame_check:
                cv2.putText(frame, "****************ALERT!****************", (10, 40),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
                cv2.putText(frame, "****************ALERT!****************", (10,450),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
                
        else:
            flag = 0

    cv2.imshow("Frame",frame)
    if cv2.waitKey(1)==13:
        break
    
cap.release()
cv2.destroyAllWindows()

Color Detection using kmeans
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('watch.jpeg')

if image.shape[1]>750:
    image = cv2.resize(image,((image.shape[1]*750)//image.shape[0],750))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# reshape the image to a 2D array of pixels and 3 color values (RGB)
pixel_values = image.reshape((-1, 3))
# convert to float
pixel_values = np.float32(pixel_values)


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)

# number of clusters (K)
k = 10

_, labels,_ = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

# flatten the labels array
labels = labels.flatten()

# disable only the cluster number 2 (turn the pixel into black)
masked_image = np.copy(image)
# convert to the shape of a vector of pixel values
masked_image = masked_image.reshape((-1, 3))
# color (i.e cluster) to disable

masked_image[labels == labels[-1]] = [0,0,0]
masked_image[labels != labels[-1]] = [255,255,255] 

# convert back to original shape
masked_image = masked_image.reshape(image.shape)

cv2.imshow("mask",cv2.cvtColor(masked_image, cv2.COLOR_RGB2BGR))
cv2.imshow("image",cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
cv2.waitKey(0)

cv2.destroyAllWindows()



                                                                                                                                                        

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Feel free contact me on-

Email - sn.gurukul24.7uk@gmail.com