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
Related
I am trying to create a face detection model using OpenCV, Python. This is the code:
import cv2
import sys
# Get user supplied values
imagePath = "elon_musk.jpg"
cascPath = "haarcascade_frontalface_default.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)
cv2.waitKey(0)
print("Found {0} faces!".format(len(faces)))
I am facing this error while running:
Traceback (most recent call last): File "[PATH]", line 21, in
<module>
flags = cv2.cv.CV_HAAR_SCALE_IMAGE AttributeError: module 'cv2' has no attribute 'cv'
Can anyone help?
I have been writing a face detection program in python using OpenCV. It is working fine, but the program is not terminating after closing the window in which faces are detected.
I have tried cv2.destroyAllWindows() and sys.exit(0) also, but nothing is working. I can't find any solution in the internet.
I am using python 3.8 and openCV 4.2.0.
Below is my code
import cv2
import sys
# Get user supplied values
imagePath = "../training_images/1/bla.jpg"
cascPath = "../HaarCascade/haarcascade_frontalface_alt.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.CASCADE_SCALE_IMAGE
)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
sys.exit(0)
Please help me.
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()
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()
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