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

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.

Related

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

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 was making a camera and i kept getting this error in (python) i have given the code

from types import FrameType
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontal_default.xml")
bodie_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_fullbody.xml")
while True:
_, Frame = cap.read()
gray = cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, width, height) in faces:
cv2.rectangle(Frame, (x, y), (x + width, y + height), (255, 0 , 0), 3)
cv2.imshow("Camera", Frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Traceback (most recent call last):
File "c:\Users\RITESH BHAKTA\Desktop\py\secuirity.py", line 15, in <module>
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
You loaded an invalid path to the haarcascades xml file. Try to use an absolute path: C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml:
face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_eye.xml')
You can download the haarcascade files here: https://github.com/opencv/opencv/tree/master/data/haarcascades
Here is how to do it.
In line 4.
In "haarcascade_frontal_default.xml" change the frontal into frontalface
Here is an example:
Wrong:
cv2.data.haarcascades + "haarcascade_frontal_default.xml")
Correct:
cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

How to measure temperature with thermal camera using python?

I am trying to get temperature from Flir thermal camera with Python and OpenCV in real time, as shown in this video.
I am using the code below for face detection. It works for regular camera but it does not work with the thermal one, which gives me this error:
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
File "c:\Users\Render\Documents\BTK_Python\face_detection.py", line 18, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
import cv2
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(1)
while True:
_, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,255,0), 4)
cv2.imshow("banu-taner", img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.realase()

Error in opencv - python3 (detectMultiScale) Face recognition

I am trying to create a face detection program. When I execute the below code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('../../Practica - Proyectos/Practica -
OpenCV/cascades/data/haarcascade_frontalface_alt2.xml')
captura = cv2.VideoCapture(0)
while(True):
ret, frame = captura.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
print(x,y,w,h)
cv2.imshow('frame', frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
captura.release()
cv2.destroyAllWindows()
The following error occurs:
Traceback (most recent call last): File "c:\xampp\htdocs\Practica - Proyectos\Practica - OpenCV\facialRec.py", line 20, in faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) cv2.error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
I understand that there is a problem with the route but I can not guess why it does not execute. The code is supposed to execute and read the different points on my face.
Thank you very much for your time in advance.

AttributeError: 'module' object has no attribute 'createFisherFaceRecognizer'?

I'm using Python 2.7 and OpenCV 3.2 I work on Windows 64 bit.
I'm working on a face recognition code and I want to use the OpenCV FaceRecognizer
recognizer = cv2.face.createFisherFaceRecognizer()
but when i run the code i get this error,
AttributeError: 'module' object has no attribute 'createFisherFaceRecognizer'
I tried also to write it like this
recognizer = cv2.createFisherFaceRecognizer()
but still getting the same error
This is the code, the code is part of a kivy code
def FaceRecognizer():
recognizer = cv2.face.createFisherFaceRecognizer()
recognizer.load('Trainer/Trainer.yml')
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
while (True):
retval,image = cam.read() # reading image from cam
print np.shape(image)
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # converting image to gray image
faces = face_detector.detectMultiScale(gray_image,1.3,5)
''' detectMultiScale, detects objects of different sizes in the input image.
the detected objects are returned as a list of rectangles
'''
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf<50):
Id="Aladdin Hammodi"
else:
Id= "Unknown"
cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
cv2.imshow('frame',image)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
output = "Recognition is done"
return output

Categories

Resources