Published: 26 Dec 2022 › Updated: 26 Dec 2022
Example for Face detection a Addition to the post Face recognition with OpenCV and the Viola-Jones algorithm
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()
Leave Example for Face detection a Addition to the post Face recognition with OpenCV and the Viola-Jones algorithm to:
Read more #python posts
Best Posts From aborowczak1972
We have not curated any of aborowczak1972's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From aborowczak1972
- Why is there an Anarchist Computer Club?
- Why is man intrinsically stupid and runs himself down
- Future thoughts how it could go on with CryptoCompany
- Example for Face detection a Addition to the post Face recognition with OpenCV and the Viola-Jones algorithm
- Face recognition with OpenCV and the Viola-Jones algorithm
- My attempt to integrate ChatGPT into Django
- Why the hell pizza- the miracle continues
- A Newbi in a new world