Good afternoon, I currently have some code which detects eyes and faces using haar cascades, I was curious to see if anybody knows how to get the program to recognize movement of the head e..g. nod or movement of the eye e.g. blink.
Here is what i currently have:
import cv2
import numpy as np
"""
Created on Mon Mar 2 11:38:49 2020
#author: bradl
"""
# Open Camera
camera = cv2.VideoCapture(0)
camera.set(10, 200)
face_cascade = cv2.CascadeClassifier('haarcascades/face.xml')
##smile = cv2.CascadeClassifier('haarcascades/smile.xml')
eye_cascade = cv2.CascadeClassifier('haarcascades/eye.xml')
while True:
ret, img = camera.read()
## converts to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## determines what a face is and how it is found
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
## Determines the starting and ending co-ordinates for a blue rectangle to be drawn around the face
cv2.rectangle (img, (x,y), (x+w, y+h), (255,0,0), 2)
## Declares the region of the image where the eyes will be
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
## Determines what an eye is based on the eye haar cascade xml file
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
##Draws green rectangles around the co-ordintates for eyes
cv2.rectangle(roi_color, (ex, ey),(ex+ew,ey+eh), (0,255,0),2)
##Displays camera
cv2.imshow('Image',img)
##Requires the user to press escape to exit the program
k = cv2.waitKey(40)
if k == 27:
break
Does anybody have any ideas to get the program to recognize head or eye movement?
There are a number of ways to detect eye blinking.
In the ROI of the eyes apply white color detection.
Draw contours of around this mask, if the area of the contour is above a threshold you can interpret the eye is open, whenever there is a sudden change in the area of the contour, that is the point of a blink.
This method would fail if you move towards and away from the camera.
Another way of doing this would be face landmark detection using a library like DLIB.
Related
I want to detect the whole head from chin to the top of the hair to calculate its size.
I have a simple face detection code with python/openCV :
import cv2
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
faceCascade = cv2.CascadeClassifier(os.path.join(dir_path, 'haarcascade_frontalface_default.xml'))
image_name = input()
image = cv2.imread(os.path.join(dir_path, image_name))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255), 3)
print('{} faces detected.'.format(len(faces)))
cv2.imshow('image', image)
cv2.waitKey()
cv2.imwrite(os.path.join(dir_path, image_name[:image_name.find('.') + 1] + '_resault.jpg'), image)
this detect the face from the chin to the top of the forehead like this:
the problem is I want to calculate the height of the face to the top of the hair like this:
how can I detect the whole head from the chin to the top of the hair ?
I am attempting to create a model that can recognize green circles or green rectangular arrows as shown in the "image".
I figured I would need to make my own classifier and I am using Cascade Trainer to accomplish this. (See repo for the classification images and .xml file.)
Complete code and supporting files here:
https://github.com/ThePieMonster/Computer-Vision
# Analyze Screenshots
print("Analyze Screenshots")
img = cv2.imread('Data/image.png')
classifier_path = 'Data/train/classifier/cascade.xml'
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print("- load classifier")
cascade = cv2.CascadeClassifier(classifier_path)
print("- detect objects")
objects = cascade.detectMultiScale(image=img_rgb, scaleFactor=1.10, minNeighbors=3)
print("- draw rectangle around objects")
for(x,y,w,h) in objects:
# cv2.rectangle(<image>, (x,y), (x+w,y+h), <rectangle rgb color>, <rectangle thickness>)
img = cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
print()
As you can see via the below image, I can't seem to get the classifier to recognize the green dots. There are about 41 green dots in the image and I am hoping to spot them all, currently I have a few red dots spotted and some random map squares. Any help is much appreciated!
I am trying to create a face detection system in python using openCV that counts the total number of faces.(also it is detecting the eyes of the person).
But i want it to give the exact number of faces it detected say, by the end of day in real time.
following is the code.. any suggestions please:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\Python27\Scripts\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\Python27\Scripts\haarcascade_eye.xml')
img = cv2.VideoCapture(0)
while(1):
_,f=img.read()
gray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
for (x,y,w,h) in faces:
cv2.rectangle(f,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = f[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('img',f)
if cv2.waitKey(25) == 27:
break
cv2.destroyAllWindows()
img.release()
I'm using a guide provided online with opencv2.4 that shows you how to detect faces with opencv2 and python. I followed the guide and understand what it says. However I can't seem to find the issue with my program because the video shows but now face is detected and the video is very clear. There are no errors. I ran in debug mode and the value faces remains a blank tuple so I'm assuming that means its not finding the face. What I don't understand is why and I think it has something to do with the hash table.
By hash table I mean the cascade xml file. I understand cascades are basically the guidelines for detecting the facial artifacts correct?
Links to the guides. The hash table i.e the xml file is on the github linked.
https://github.com/shantnu/FaceDetect/blob/master/haarcascade_frontalface_default.xml
https://realpython.com/blog/python/face-detection-in-python-using-a-webcam/
import cv2
import sys
import os
#cascPath = sys.argv[1]
cascPath = os.getcwd()+'facehash.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
print faceCascade
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
cv2.cv
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
You have a wrong path to your xml classifier. (I guess you've changed the name to get a shorter form).
Instead of your cascPath:
cascPath = os.getcwd()+'facehash.xml'
Try this:
cascPath = "{base_path}/folder_with_your_xml/haarcascade_frontalface_default.xml".format(
base_path=os.path.abspath(os.path.dirname(__file__)))
And now it should work as well.
I have started to learn OpenCV lib in Python. I want to write a code that will detect faces and eyes on the photo but also count number of detections.
I have wrote a picture that has 6 people on it.
You can download the image form here:
https://www.sendspace.com/file/cznbqa
I wrote a code that detect faces and eyes, but I do not know how to count number of detections.
Also, my code detect 4/8 faces and 1/12 eyes on the photo so...I probably have some issues in the code, If you can help me with that also, I would be very thankful.
# Standard imports
import cv2
import numpy as np
# Read image
my_image = cv2.imread("ljudi.jpg", 1)
# data for detecting faces and eyes
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread("ljudi.jpg", 1)
gray = cv2.cvtColor(img, 0)
faces = face_cascade.detectMultiScale(gray, 1.2, 4)
for (x,y,w,h) in faces:
#detect face with bule rectangle
#rec. start point (x,y), rec. end point (x+w, y+h), blue color(255,0,0), line width 2
face_rec = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 1)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 2)
for (ex,ey,ew,eh) in eyes:
eye_rec = cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), )
cv2.imshow('img', img)
cv2.waitKey(0)
From the OpenCV documentation:
https://www.docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale
Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
where a rectangle for the Python bindings simply seems to be of type list [x,y,w,h] (https://www.docs.opencv.org/2.4/modules/core/doc/basic_structures.html#rect).
To the number of faces / rectangles returned inside the list can easily be retrieved by getting the length of the list (i.e. number of items inside), which is done with the Python builtin function len(): https://docs.python.org/3/library/functions.html#len.