Sunday, May 3, 2020

Computer Vision 5- Objection detection of OpenCV

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




No comments:

Post a Comment