I am trying to do some face detection here and using the standard cascades, which can be found here, the following code should draw rectangles over eyes and the face, either the cascades aren't working at all or something's wrong with my code that's supposed to draw over the frame. Also would it be possible to grab the video feed and not have it inverted?
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier(r'A:\scripts\opencvstuff\haarcascade_frontalcatface.xml')
eye_cascade = cv2.CascadeClassifier(r'A:\scripts\opencvstuff\haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[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',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I was able to get the code to work by changing the Face model from haarcascade_frontalcatface.xml to haarcascade_frontalface_default. Seems dumb but yeah that worked.
Related
I'm trying to build a face detector with webcam using opencv in Python.
When human face is detected, the system need to be able to capture only one photo of face per person and save it automatically (without pressing a key), until the person leaves the frame and another person enters and again his face detected and saved in an image file.
I'm using a while loop for the live video stream and the continous face detection. I tried to create an inner while loop for saving the face photo, but it takes multiple frames.
The code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(0)
while True:
ret, img=cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(150, 150), maxSize=(300, 300))
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
face_img_gray = gray[y:y+h, x:x+w]
face_img_color = img[y:y+h, x:x+w]
laplacian_var = cv2.Laplacian(face_img_gray, cv2.CV_64F).var()
#print(laplacian_var)
if laplacian_var > 140: # avoid blur frame capture
cv2.imwrite('Frame.jpg', face_img_color)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This code works corectly , But it's a very slow .
I changed the cv2.waitKey(1) number. But it still didn't change much
import cv2
import numpy as np
facexml = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyexml = cv2.CascadeClassifier("haarcascade_eye.xml")
cap = cv2.VideoCapture("my_video.avi")
while True:
_,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = facexml.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eyexml.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),1)
cv2.imshow("window",frame)
if cv2.waitKey(1) & 0XFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Simply a Harris cascade classifier is an old and slow algorithm for fast online face recognition in video. Try to read an OpenCV manual on Cascade Classifier and reduce the number of scales by setting equal maxSize and minSize or set larger scaleFactor to decrease total amount of images computed from original image by resizing.
I have code that detects a face. All I want to do is save the detected face as a jpg
Here is the code for my program:
import numpy as np
import cv2
detector= cv2.CascadeClassifier('haarcascade_fullbody.xml')
cap = cv2.VideoCapture(0)
while(True):
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
How do I save the detected face? Please help!
detectMultiScale method returns a list where each elements contains the coordinates and width and height of each face that is detected.
So you can use cv2.imwrite and array slicing:
count = 0
for (x,y,w,h) in faces:
face = img[y:y+h, x:x+w] #slice the face from the image
cv2.imwrite(str(count)+'.jpg', face) #save the image
count+=1
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
How can I check for the location of an eye for example.
Its in this function?
eyes = eye_cascade.detectMultiScale(gray, 1.4, 7)
How to read the input of this?
i would like to calculate in a face detection program if on the picture for example a nose or an eye is the place as should be, in a human.
OpenCV-Python Tutorials’s documentation has a good example for face detection.
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('sachin.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[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',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am trying to save the face and eye detected using opencv in a separate png/jpg file, what can I use to do it.(I would want the rectangle as well). The code is as below: (I do not have GUI enabled)
import numpy as np
import cv2
from matplotlib import pyplot as plt
face_cascade = cv2.CascadeClassifier('/Users/i855838/opencv/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/Users/i855838/opencv/data/haarcascades/haarcascade_eye.xml')
img = cv2.imread('/Users/i855838/Desktop/xfiles4.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[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',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Simply use cv2.imwrite. For example, to save to a file called 'face_color.png' you would do cv2.imwrite('face_color.png', roi_color).