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()
Related
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")
I am trying to run this OpenCV code to detect faces with my video camera. It's giving me this error whenever I run my code. The light on my video camera blinks but then shuts down with this error in the console box along with this one cv2.error: OpenCV(4.5.1) error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Here's the code
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
#THIS IS THE ERROR AREA
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Release the VideoCapture object
cap.release()
It seems your script is not able to find the haarcascade_frontalface_default.xml file properly because of relative path. Try to give an absolute path and check.
I am following an opencv tutorial here: https://www.youtube.com/watch?v=PmZ29Vta7Vc
At 11:36 when he runs his code, it works fine, but when I run my code, I get an error:
Traceback (most recent call last):
File "C:/Users/Joshua/OneDrive/VscodePrograms/MyPythonFolder/facialRec/main.py", line 11, in <module>
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback
Here is the code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('casades/data/haardcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while (True):
ret, frame = cap.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
cap.release()
cv2.destroyAllWindows()
Please help!
The problem is in the CascadeClassifier's path:
face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
I think now it should be working fine.
Here is my code :
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascase_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.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]
eye = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eye:
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()
cap.destroyAllWindows()
These are the errors I'm getting:
error: OpenCV(4.2.0)
C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182:
error: (-215:Assertion failed) !_src.empty() in function
'cv::cvtColor'
error Traceback (most recent call
last) in
3 while True:
4 ret, img = cap.read()
----> 5 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
6 faces = face_cascade.detectMultiScale(gray,1.3,5)
7 for (x,y,w,h) in faces:
error: OpenCV(4.2.0)
C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182:
error: (-215:Assertion failed) !_src.empty() in function
'cv::cvtColor'
in your code have 3 little errors:
1= if k == 27:``` for solve first that delete `` 2=cap.destroyAllWindows()for solve secound, typecv2.destroyAllWindows() 3=('haarcascase_frontalface_default.xml')for solve third ,just type thishaarcascade_frontalface_default.xml`
you type cascase but that is cascade.
There could be numerous reasons as to why it doesn't work (eg. some OS will require that you grant permission to your term to access the webcam), however there's a couple of issues with your code:
start of the while loop: you don't check the value of the parameter ret. It's good practice to check its value with a small check like if not ret: break to exit the loop. That won't help with your webcam not working, but that will prevent the error that you are seeing (retis False, so img is likely empty and had no data)
End of your code. destroyAllWindows is a method in the cv2 namespace, not a property of your capture. You should call cv2.destroyAllWindows() instead of cap.destroyAllWindows()
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.