Trying to blur just the face (python) - python

I am trying to blur just the face by using the following script, but end up blurring the entire image. Any suggestions on how to change the script such that it blurs only the faces?
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('beatles.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detecting faces
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]
# To show the detected faces
cv2.imshow('img',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
# Blurring of just the faces in a picture
skaldetlykkes = cv2.filter2D(img, -1, k)
cv2.imshow("Ladetskje", skaldetlykkes)
cv2.waitKey (5000)
cv2.destroyAllWindows()

The following code sample will apply your box filter only on the faces:
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Blur the faces, i.e. only the ROIs defined by faces
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
for (x, y, w, h) in faces:
img[y:y+h,x:x+w] = cv2.filter2D(img[y:y+h,x:x+w], -1, k)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()

this code sample
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()
from Face Detection should compute the rectangle area you want to blur.
Then you just need to apply an average function on the area :) (Blur = window average)

Related

Extract eyes from haarcascade

I know how to identify a face and the eyes, with opencv and haarcascade. But how can I crop or get only the eyes from webcam capture? Just one, or even the both, so I can use as a new frame in imshow...
See on this post:
How to crop an image in OpenCV using Python
import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
Your "img[y:y+h, x:x+w]" is just the output from your haarcascade. Maybe you recieve multiple output so you have to choose one.
After detect face you can do this.
eyes = eye_cascade.detectMultiScale(roi_gray)
for ex, ey, ew, eh in eyes:
roi = roi_color[ey:ey + ew, ex:ex + eh]
Then can show roi using cv2.imshow().
The haarcascade_eye.xml and haarcascade_frontalface_default.xml link file
import cv2
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#load image saved in the same directory
img = cv2.imread("your_image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
alt_gray = gray[y:y+h, x:x+w]
alt_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(alt_gray)
eye_crop = []
for f in eyes:
x, y, w, h = [ v for v in f ]
cv2.rectangle(alt_color, (x,y), (x+w, y+h), (255,0,0), 2)
# Define the region of interest in the image
eye_crop.append(alt_gray[y:y+h, x:x+w])
for eye in eye_crop:
cv2.imshow('eye',eye)
cv2.waitKey(0)

Is there anyway to take pictures of the face in a determined size?

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()

Haarcascades don't work in OpenCV

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.

Python OpenCV face detection

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()

Save the face detected in a png/jpg file

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).

Categories

Resources