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)
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.
Well, i'm using this code to take pictures, but i would like it to take the picture, then resize it in a determined size, for example, i need that data of the 10 pictures with the same size for training PCA face recognition and i need images with 112 x 92 pixels. Do anyone knows how to do that?
import cv2
import time
face_cascade = cv2.CascadeClassifier('/home/alvaro/Desktop/Pruebas Algoritmos/PCA/DetecRostro/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
num = 0
while num<1:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
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]
x = 0
y = 20
text_color = (0,255,0)
cv2.imwrite('opencv'+str(num)+'.jpg',frame)
num = num+1
cap.release()
cv2.destroyAllWindows()
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.
I have to detect faces using openCV and python. Then identify the position of the detected face if it is in the right, the left or the middle of the screen.
I already succeed to detect faces using the code below and still to know the position of the faces could someone please help me ?
import cv2
import sys
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(1)
while True:
#capture frame by frame
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors= 5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
#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)
cv2.imshow('video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
You could get the centre of the rectangle:
centre_x = x + w/2
centre_y = y + y/2
Then compare it with the size of the image. Assuming you have the image shape information:
height, width, channels = frame.shape #it could be gray.shape too
You can understand for example if the face is detected on the left side of the image by checking centre_x<width.
You have all the information to divide the image into a grid and understand where the rectangle places itself.