I am learning OpenCV through Python v3.8.5 since last few days, got stuck in CascadeClassifier on Facedetection . Following the same code as the tutor : https://www.youtube.com/watch?v=LopYA64KmdE Timestamp: 08:40
I have the image and haarcascade_frontalface_default.xml files in the resources folder
Still, I don’t get any output for this below code. I tried changing images files too but still no output. When I tried printing it print till ‘foo1’. So I suspect the issue is around detectMultiScale() method.
Here is my code:
import cv2
face_cascade = cv2.CascadeClassifier('resources\haarcascade_frontalface_default.xml')
# img = cv2.imread('resources\\lena.jpg')
# img = cv2.imread('D:\photos\house\DSCF2736 copy.jpg')
img = cv2.imread('resources\\messi5.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print('foo1')
faces = face_cascade.detectMultiScale(gray,1.1,4)
print('foo2')
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+h,y+w),(0,255,0),2)
cv2.imshow('out',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am using Windows7 32 bit PC. Is this due to OS support issue? Please let me know the solution.
Thanks
check face_cascade.empty()
. is it empty?
there’s your error. the string is broken. either use an r-string or escape the backslash.
also make sure that file is actually there.
same goes for your other paths.
ALWAYS check that some operation has succeeded. LOOK at the returned values/objects. learn the basic usage of pdb, python’s debugger.
Thanks for your python debugger suggestion, will definitely try to use that…
face_cascade.empty() is False.
and I added double backslash, still no output…
does gray
contain image data? what is gray.shape
?
I tried 3 types of images
for messi5.jpg (current existing code): gray.shape is (342, 548)
for lena.jpg: gray.shape is (512, 512)
for DSCF2736 copy.jpg : gray.shape is (2592, 1456)
okay those should work…
say, what version of OpenCV do you use? is it a binary package, from where? or did you build it yourself?
here’s some minimal code that gives me results within a fraction of a second. I ran this interactively, didn’t need print
calls.
import cv2 as cv
cv.samples.findFile("lena.jpg")
# 'P:/opencv/build\\..//samples/data\\lena.jpg'
lena = cv.imread(cv.samples.findFile("lena.jpg"), cv.IMREAD_GRAYSCALE)
cv.samples.findFile("data/haarcascades/haarcascade_frontalface_alt.xml")
# 'P:/opencv/build\\..//data/haarcascades/haarcascade_frontalface_alt.xml'
cascade = cv.CascadeClassifier(cv.samples.findFile("data/haarcascades/haarcascade_frontalface_alt.xml"))
cascade.empty()
# False
cascade.detectMultiScale(lena)
# array([[213, 200, 178, 178]], dtype=int32)
cascade.detectMultiScale(lena, 1.1, 4)
# array([[213, 200, 178, 178]], dtype=int32)
I am using Opencv version v4.5.1
I installed it by pip install opencv-python. I am not sure about binary package
This is screenshot of the program you sent me and the output.
Program simply terminated its execution at cascade.detectMultiScale(lena)
To remind you once, I am using Windows7 32 bit PC. Is it because of that?
Thanks for your continuous help and support
it is a possibility. one of several. perhaps skvarg’s opencv-python package isn’t tested on win32. something may be broken.
things you could try:
-
look at previous releases of the skvarg package and try installing various earlier versions with pip: pip install opencv-python==4.5.1.48
(example)
-
NOT a solution: the official binary package for Windows from Releases - OpenCV appears to be 64 bit only
-
find a different package for python and try with that
– I don’t know of any other packages… except for some v3.4 package on condaforge.
-
build OpenCV yourself and see if that has better luck
-
translate python code into a C++ version, compile and run that, see if it also crashes
– trying this from C++ code makes debugging into OpenCV’s functions easier
I would recommend upgrading the OS. or try running a 64 bit VM: virtual machines - Can I run 64-bit VM guests on a 32-bit host? - Server Fault