Python AttributeError: module 'cv2' has no attribute 'cv' - python

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?

Related

AttributeError: 'VideoCapture' object has no attribute 'read'

I was trying to run a face detection code I copy pasted from https://www.thepythoncode.com/article/detect-faces-opencv-python on PyCharm IDE. I got an error saying:
"C:\Users\NH\PycharmProjects\Hello_World\venv\Scripts\python.exe"
"C:/Users/NH/Desktop/WELCOME!/Docs/PycharmProjects/pythonProject/FACEDETECT.py"
Traceback (most recent call last): File "C:/Users/NH/Desktop/WELCOME!/Docs/PycharmProjects/pythonProject/FACEDETECT.py",
> line 5, in
_, image = cap.read() AttributeError: 'VideoCapture' object has no attribute 'read'
Here's the code:
import cv2
cap = cv2.VideoCapture()
face_cascade = cv2.CascadeClassifier("haarcascade_fontalface_default.xml")
while True:
_, image = cap.read()
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(image_gray, 1.3, 5)
for x, y, width, height in faces:
cv2.rectangle(image, (x, y), (x + width, y + height), color=(255, 0, 0), thickness=2)
cv2.imshow("image", image)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
I tried the debugger on line 5 but I can't seem to figure out the problem.

Getting an error in an OpenCV project (Project to recognize the face of a person using LBPH)

I am making a project to recognize the person by his/her face using OpenCV library in Python.
My code is :-
# Import OpenCV and sys libraries
import cv2
import sys
import os
import numpy as np
subjects = ["", "Daksh", "Ishika", "Dashya", "Shivang"]
#Create the haar cascade
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def draw_rectangle(img, rect):
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
def draw_text(img, text, x, y):
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
def imgdetect(gray,image):
#Detect faces in the image
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
#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)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
return image
#for video capturing
vidCapture = cv2.VideoCapture(0)
while True:
# To capture video frame by frame
_, image = vidCapture.read()
#monochrome image capturing (color to gray)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
subjects = ["Daksh", "Ishika", "Shivang", "Dashya"]
canvas = imgdetect(gray, image)
gray, rect = detect_face(image)
label = face_recognizer.predict(gray)
label_text = subjects[label]
#draw a rectangle around face detected
draw_rectangle(canvas, rect)
#draw name of predicted person
draw_text(canvas, label_text, rect[0], rect[1]-5)
cv2.imshow("Attendence....", canvas)
#To break control by pressing 'q'
if cv2.waitKey(1) & 0xff == ord('q'):
break
#Release after processing is done
vidCapture.release()
cv2.destroyAllWindows()
But, I am getting the following error while proceeding with the code
---------------------------------------------------------------------------
error Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13428/371309763.py in <module>
40 canvas = imgdetect(gray, image)
41 img, rect = detect_face(image)
---> 42 label = face_recognizer.predict(img)
43 label_text = subjects[label]
44
error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix.cpp:250: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
I am unable to understand the error message.
Why am I getting this error and what can be the possible fixes for the error?

Syntax Error: "faces = faceCascade.detectMultiScale( ^ SyntaxError: invalid syntax "

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

OpenCV (Python) face detection program not terminating

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.

'detectMultiScale' requires a 'cv2.CascadeClassifier' object but received a 'numpy.ndarray'

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

Categories

Resources