Reputation: 47
This is my first question here, so I hope I am asking it in the proper way.
I am running Python 3.6.3 (Anaconda 64-bit installation) on a laptop with Windows 10.
I have a main routine that, amongst other things, captures video via cv2.VideoCapture(). Another file stores a function to perform face detection. When I call the function from the main program I receive following error message: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
Here is a simplified version of the code:
Main program:
from facecounter import facecounter
import cv2
cap = cv2.VideoCapture(0)
x = 0
while x < 20:
ret, frame = cap.read()
print(type(frame))
output = facecounter(frame, ret)
cv2.imshow("output", output)
cv2.waitKey()
x += 1
cv2.destroyAllWindows()
cap.release()
Function stored in facecounter.py:
def facecounter(frame, ret):
import cv2
face_classifier = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml
if ret is True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
number_faces = len(faces)
return number_faces
I have search for this error and as far as I can see it is due to the lack of an appropriate image in numpy.array format fed to cv2.detectMultiScale(). So I tried to simplify even further the code to isolate the error:
Main routine:
from file import function
import cv2
cap = cv2.VideoCapture(0)
x = 0
while x<300:
ret, frame = cap.read()
output = file(ret, frame)
cv2.imshow("window", output)
print(output)
cv2.destroyAllWindows()
cap.release()
Function stored in file.py:
import cv2
def function(ret, frame):
output = frame
return output
When I run this simplified version of the code no error appears but, although for every iteration I get the correct array printed, the window created with cv2.imshow() shows a grey image.
I would really appreciate your help. Thanks a lot in advance!
Upvotes: 2
Views: 280
Reputation: 14530
You need to fix your simplified version.
function
not file
.cv2.waitKey(1)
.x
.Here is the fixed code
from file import function
import cv2
cap = cv2.VideoCapture(0)
x = 0
while x<300:
ret, frame = cap.read()
output = function(ret, frame)
cv2.imshow("window", output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print(output)
x += 1
cv2.destroyAllWindows()
cap.release()
Also it's not a good idea to name your python file file.py
, name it image_processing.py
or something that doesn't clash with names used by Python.
Upvotes: 1