I'm trying to detect a face and then crop it to use it in a face recognition algorithm. Here is my code.
import numpy as np
import cv2
import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('xD.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]
print x
print y
print w
print h
img.crop((x,y,w,h))
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
When I print (x,y,w,h), it gives precise coordinates, but when I crop it, it gives me this error.
img.crop((x,y,w,h))
AttributeError: 'numpy.ndarray' object has no attribute 'crop'
import numpy as np
import cv2
from PIL import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('xD.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]
cropped = img[y:y+h, x:x+w]
cv2.imwrite("thumbnail.png", cropped)
cv2.imshow("cropped", cropped)
cv2.waitKey(0)
Related
I want to check if a face has been detected.
I have the variable face_detect and when a face is detected I want to turn this variable to True however I don't know how to check for a detected face. I tried using faces.size() to check if it was greater than zero but it said
AttributeError: 'tuple' object has no attribute 'size'
So I don't know why that is not working.
import cv2
import numpy as np
import winsound
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
face_detect = False
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]
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I've slightly modified your code to update the face_detect variable to True whenever a face is detected.
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
face_detect = False
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) > 0:
face_detect = True
else:
face_detect = False
print(face_detect)
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]
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I'm currently trying to run a simple python program to detect face and eyes.
I downloaded the appropriate classifiers and they're in the same directory. I even checked my camera settings and it's set to on with permission to python. Any ideas how to solve this?
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.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)
for (x,y,w,h) in faces:
cv2.rectange(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,ehh) 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()
Why does this OpenCV program give the error:
My camera settings show that python is using it:
Thank you!
!_src.empty() means you have an empty frame
You can check the frame to make sure you actually get the image:
ret, img = cap.read()
if img is not None:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else: < implement how you want to deal with empty frame >
I've been trying to load the .xml file on cascadeclassifier but it wont work.
Solutions on internet include using \ or / in file path but that does not work for me either
#creating a cascade classifier object
fc = 'C:/Users/abc/Desktop/cv/opencv/data/haarcascades/haarcascade_frontalcatface_default.xml'
face_cascade = cv2.CascadeClassifier(fc)
img1 = cv2.imread("images.jpg")
grayImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(grayImg, scaleFactor=1.05, minNeighbors = 5)
for x,y,w,h in faces:
img1 = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 3)
resizedd = cv2.resize(img1, (int(img1.shape[1]*7), int(img1.shape[0]*7)))
cv2.imshow("gray", resizedd)
cv2.waitKey(0)
cv2.destroyAllWindows()
also tried:
#creating a cascade classifier object
face_cascade = cv2.CascadeClassifier('C:/Users/abc/Desktop/cv/opencv/data/haarcascades/haarcascade_frontalcatface_default.xml')
img1 = cv2.imread("images.jpg")
grayImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(grayImg, scaleFactor=1.05, minNeighbors = 5)
for x,y,w,h in faces:
img1 = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 3)
resizedd = cv2.resize(img1, (int(img1.shape[1]*7), int(img1.shape[0]*7)))
cv2.imshow("gray", resizedd)
cv2.waitKey(0)
cv2.destroyAllWindows()
How can I save only the eye image after detection with opencv and Python?
This is the code that I have tried:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\\Users\MJ-INFO\Anaconda3\pkgs\libopencv-3.4.1-h875b8b8_3\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\Users\MJ-INFO\Anaconda3\pkgs\libopencv-3.4.1-h875b8b8_3\Library\etc\haarcascades\haarcascade_eye.xml')
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2, 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:
crop_img = roi_color[ey: ey + eh, ex: ex + ew]
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
But it is not working?
If I get your question correctly, you want to save crop_img in the loop? So, simply add cv::imwrite(...) after defining crop_img. Since you can have more than one eye, you need to think of naming your files.
I am a beginner to opencv and I have tried to crop a single face from a picture for my project but couldn't crop all the faces from the picture.
What can be done to detect all the faces and crop them to move to a folder?
taking images from the input folder and posting the cropped image to the output folder.
import numpy as np
import cv2
import os, os.path
#multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('faces.xml')
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('eye.xml')
DIR = 'input'
numPics = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
for pic in range(1, (numPics+1)):
img = cv2.imread('input/'+str(pic)+'.jpg')
height = img.shape[0]
width = img.shape[1]
size = height * width
if size > (500^2):
r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
img = img2
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
eyesn = 0
for (x,y,w,h) in faces:
imgCrop = img[y:y+h,x:x+w]
#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)
eyesn = eyesn +1
if eyesn >= 2:
cv2.imwrite("output/crop"+str(pic)+".jpg", imgCrop)
#cv2.imshow('img',imgCrop)
print("Image"+str(pic)+" has been processed and cropped")
k = cv2.waitKey(30) & 0xff
if k == 27:
break
#cap.release()
print("All images have been processed!!!")
cv2.destroyAllWindows()
cv2.destroyAllWindows()
How to crop all faces from a picture to save in a folder?
#### the counter
cnt = 0
for pic in range(1, (numPics+1)):
img = cv2.imread('input/'+str(pic)+'.jpg')
height = img.shape[0]
width = img.shape[1]
size = height * width
if size > (500^2):
r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
img = img2
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
eyesn = 0
imgCrop = img[y:y+h,x:x+w]
#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)
eyesn = eyesn +1
if eyesn >= 2:
#### increase the counter and save
cnt +=1
cv2.imwrite("output/crop{}_{}.jpg".format(pic, cnt), imgCrop)
#cv2.imshow('img',imgCrop)
print("Image"+str(pic)+" has been processed and cropped")
k = cv2.waitKey(100) & 0xff
if k == 27:
break
#cap.release()
print("All images have been processed!!!")
cv2.destroyAllWindows()
cv2.destroyAllWindows()
You said you are tried for single face. Have you succeed in that? What is the error you are getting after executing the given code?