python Opencv error -215 scn ==3 || scn == 4 - python

I am using python 2.7 on windows 8.1 while working with python-tesseract-0.9-0.4.win32-py2.7 my previous code's weren't working properly like face detection.
The code below is to detect face in a pic. It works fine when I uninstall python-tesseract-0.9-0.4.win32-py2.7. But I need it for OCR.
what should I do.?
the error is : open cv error: (-215) scn == 3 || scn == 4 in function cvtColor
import cv2
import sys
imagepath = ('C:\Users\Default\Downloads\group1.jpg')
cascpath ('C:\opencv\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
caspatheye=('C:\opencv\opencv\sources\data\haarcascades\haarcascade_eye.xml')
faceCascade = cv2.CascadeClassifier(cascpath)
eye_cascade = cv2.CascadeClassifier(caspatheye)
img = cv2.imread(imagepath)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(15,15),
flags=cv2.CASCADE_SCALE_IMAGE
)
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]
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)
cv2.namedWindow("faces found", cv2.WINDOW_NORMAL)
cv2.imshow("faces found",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Related

How to Extract Faces from a Video Using Haarcascade?

I am using below script for haarcascade face detection.
My aim is to collect face shots on a video.
I would like to extract faces from a video.
Below script provides the detection of faces on a video.
How could I extract detected faces on a video?
import numpy as np
import cv2
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
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]
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)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()

OpenCV error (-215:Assertion failed) !empty() [duplicate]

This question already has answers here:
Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale
(9 answers)
Closed 9 months ago.
I have the following code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'opencv_haarcascade_eye.xml')
img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray)
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]
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)
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('frame',img)
cv2.destroyAllWindows()
Running it produces the following error:
eyes = eye_cascade.detectMultiScale(roi_gray)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
When I removed eyes = eye_cascade.detectMultiScale(roi_gray) part, the code works fine with fave detection but with eyes = eye_cascade.detectMultiScale(roi_gray) parth its showing error.
How to solve this issue?
The error is just in the process of loading of the classifier. It should be
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_eye.xml')
Instead of
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'opencv_haarcascade_eye.xml')
At least, this solution is shown here, and I tested it with OpenCV(4.5.1)
According to this topic about roi_gray explanation
for (x,y,w,h) in faces:
roi_gray=gray[y:y+h,x:x+w] #This particular code will return the cropped face from the image.
roi_color = img[y:y+h, x:x+w] #This particular code will return the details of the image that u will recive after getting the co-ordinates of the image.
Are you sure there is a face in the image? roy_gray returns cropped face from the image but if there isn't any maybe you will get an error I think.

Eyes recognition using Haar-classifier of a static photo

it is possible to use haarclassifier on a static photo instead of the live streaming of the webcam?
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
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,255,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,127,255),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I need to recognize just one or all eyes of the photo, without face.
Any suggest?
I edit from ur code
import cv2
#load haarcascade_eye
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# Load an color image
img = cv2.imread('Test_Image.jpg',cv2.IMREAD_COLOR)
#convert to gray scale to work with HAAR
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#read HAAR ade loop for find eye(s)
eyes = eye_cascade.detectMultiScale(gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
#show eye(s) rectangle in color image
cv2.imshow('img',img)
#press any key to close
cv2.waitKey(0)
cv2.destroyAllWindows()

error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

I am working on a project image processing and i get an error when run this code.
I use python and opencv in the project.The error was,
error: D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
and this is the code i used,
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('C:\\Users\\Hp\\Downloads\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(1)
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]
cv2.imshow('img',img)
k = cv2.waitkey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Please help i was stucked here for 5 hrs.
Thanks.
The img variable does not contain the correct dimensions. If you have only one camera in your system use cv2.VideoCapture(0) instead of cv2.VideoCapture(1).

OpenCV 3.0 Beta - Python - Face Detection via Webcam - code gives me error

I wrote and looked over this code multiple times and it seems right to me especially that it's very simple and straight forward (Took snippet out of opencv tutorial), however it is still giving me an error. I've checked the error and I understand that it's missing channels, however, I don't know how to fix it. Any help would be appreciated. I'm using Windows x64 and Spyder (from Anaconda distribution) as my IDE. Could it be hardware issue? (webcam)
#####################################################
# Face Detection (Video) #
#####################################################
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video = cv2.VideoCapture(0)
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
Here's the error I get:
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:5731:
error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
i guess, you wanted a 'continuous detection, not a 'single shot' one, right ?
you're just some small changes away:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
if face_cascade.empty(): raise Exception("your face_cascade is empty. are you sure, the path is correct ?")
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
if eye_cascade.empty(): raise Exception("your eye_cascade is empty. are you sure, the path is correct ?")
video = cv2.VideoCapture(0)
while(video.isOpened()):
ret, frame = video.read()
if frame not None:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()

Categories