befor you beginn download on https://github.com/opencv/opencv/tree/master/data/haarcascades this files:
haarcascade_frontalface_default.xml
haarcascade_eye.xml
haarcascade_smile.xml
and put it in a directory called cascade_classifier
Next copy from site the 3 images and take it in a directory with name images
Once you have downloaded all the files, install the following with pip:
pip install numpy
pip install pandas
pip install matplotlib
pip install jupyter
pip install pillow
pip install opencv-python
pip install sklearn
import numpy as np
import cv2
Face Detection
img = cv2.imread('./images/0021.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Load cascassde classifire - frontalface
face_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_frontalface_default.xml')
# apply cascade classfier to an image
faces, num_detection = face_cascade.detectMultiScale2(img)
print(faces)
[[114 40 196 196]]
print(num_detection)
[89]
next var:
pt1 = (114,40)
pt2= (114+196, 40+196)
draw the rectangle
cv2.rectangle(img, pt1, pt2, (0,255,0))
cv2.imshow('face detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
draw the circle
cx = 114 + 196 //2
cy = 40 + 196 //2
r = 196 //2
cv2.circle(img,(cx,cy),r, (0,255,255) )
cv2.imshow('face detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Multiple Face Detection
img = cv2.imread('./images/friends.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# step 1: apply cascade classfier to an image
faces, num_detection = face_cascade.detectMultiScale2(img, minNeighbors=8)
#step2: run a loop
for x,y,w,h in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0))
# step 3: display the image
cv2.imshow('face detection rectangle', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Faces & Eyes Dection
img = cv2.imread('./images/friends.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
face_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_eye.xml')
# step 1: Face detection
faces, num_detection = face_cascade.detectMultiScale2(img, minNeighbors=8)
for x,y,w,h in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0))
# setp 2: croping the face
face_roi = img[y:y+h, x:x+h] # croping the image
# step 3: apply cascade classfier (eye)
eyes, num_detection_eyes = faces, num_detection = eye_cascade.detectMultiScale2(face_roi)
for ex, ey, ew, eh in eyes:
cx = x + ex + ew //2
cy = y + ey + eh //2
r = eh //2
cv2.circle(img,(cx,cy),r, (0,255,255),2)
# step 4:
cv2.imshow('face eye detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Faces, Eyes, Smiles Detection
img = cv2.imread('./images/friends.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
face_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('./cascade_classifier/haarcascade_smile.xml')
faces, num_detection = face_cascade.detectMultiScale2(img, minNeighbors=8)
for x,y,w,h in faces:
face_roi = img[y:y+h, x:x+h].copy() # croping the image
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0))
# step 2: apply cascade classfier (eye)
eyes, num_detection_eyes = eye_cascade.detectMultiScale2(face_roi)
for ex, ey, ew, eh in eyes:
cx = x + ex + ew //2
cy = y + ey + eh //2
r = eh //2
cv2.circle(img,(cx,cy),r, (0,255,255),2)
# step 3: smile detection
smiles, num_detection_smile = smile_cascade.detectMultiScale2(face_roi)
for sx, sy, sw, sh in smiles:
cv2.rectangle(img,(x+sx,y+sy),(x+sx+sw, y+sy+sh),(255,0,0),2)
# step 4:
cv2.imshow('face eye detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Congratulations @aborowczak1972! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 200 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out our last posts:
Support the HiveBuzz project. Vote for our proposal!