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()
Related
I'm trying to detect faces from images using Haar Cascades in opencv and getting Syntax Error while running the below snippet.
import cv2
import os
imagePath = os.path.abspath("C:\\Users\\rohit\\Desktop\\Project\\1.jpg")
cascPath = os.path.abspath("C:\\Users\\rohit\\Desktop\\Project\\haarcascade_frontalface_default.xml")
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
filename = 'myconvertedimage.jpg'
cv2.imwrite(filename, image)
u missed the closing bracket in linegray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY
I am new to opencv and am trying to detect a face in an image however I am having problems with this line of code
faces = cv2.CascadeClassifier.detectMultiScale(gray_img, scaleFactor=1.05, minNeighbors=5, minSize=(1,1))
I get this error
TypeError: descriptor 'detectMultiScale' requires a 'cv2.CascadeClassifier' object but received a 'numpy.ndarray'
I have tried doing research on this error however nothing seems to tell me why this is happening and how to fix it.
full code
import cv2
img = cv2.imread("C:\\Users\\Astroid\\Desktop\\.py pics\\pic.jpg")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier('C:\\Users\\Astroid\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
minSize=(10, 10)
flags=cv2.CASCADE_SCALE_IMAGE
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("pic", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
If i'm not so wrong, i guess you have not mentioned the CascadeClassifier in your code.
e.g.
# Classifier you want to use
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
...
...
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
...
Here's a code that executes:
import cv2
# don't add 1 in arg
img = cv2.imread("your path to the image")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier('your Path to
haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
minSize=(10, 10)
flags=cv2.CASCADE_SCALE_IMAGE
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("pic", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
so after all that, I tweaked with the code a bit and I honestly have no idea what was wrong but here is the final working code.
import cv2
img = cv2.imread("C:\\Users\\Astroid\\Desktop\\.py pics\\image.png")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade =
cv2.CascadeClassifier('C:\\Users\\Astroid\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
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?
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)
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
frame,
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)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1)==ord('e'):
break
code gives this error, please help me
Traceback (most recent call last): File
"C:\Users\yavuz\Desktop\sor\sor2.pyw", line 15, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) error: ........\opencv\modules\imgproc\src\color.cpp:3737: error: (-215)
scn == 3 || scn == 4 in function cv::cvtColor
You need to check if "ret == True' before going on with cvtColor. The code should look like this:
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
frame,
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)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1)==ord('e'):
break