Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Load Single Channel Numpy Array Image with detectMultiScale

I have sequences of digits originally from Mnist data set that I've manipulated with OpenCV. They are saved in a pickle file. They are 1 channel images with shape (112, 112).

I want to run these through an OpenCV Cascade Classifier, similar to docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.html, but instead with these github.com/ankitdhall/Google-Street-View-House-Numbers-Digit-Localization.

Here is my attempt:

data = p.load_file('data/train_sequences00.pickle')
zero = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade0/cascade.xml')
one = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade1/cascade.xml')
two = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade2/cascade.xml')
three = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade3/cascade.xml')
four = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade4/cascade.xml')
five = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade5/cascade.xml')
six = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade6/cascade.xml')
seven = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade7/cascade.xml')
eight = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade8/cascade.xml')
nine = cv2.CascadeClassifier('data/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade9/cascade.xml')
gray = np.array(data['sequences'][0]).astype(np.float32)
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
#gray = cv2.cvtColor(img, cv2.CV_RGB2GRAY)

zeros = zero.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
ones = one.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
twos = two.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
threes = three.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
fours = four.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
fives = five.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
sixes = size.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
sevens = seven.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
eights = eight.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
nines = nine.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)

Unfortunately this just results in the following error:

Traceback (most recent call last):
  File "digit_cascade.py", line 22, in <module>
    zeros = zero.detectMultiScale(gray, 1.3, 5, scaleFactor=0.6)
SystemError: error return without exception set

Is anyone familiar with how to load raw numpy arrays into OpenCV detectMultiscale?

Here are some uncertainties I have:

  • The OpenCV docs docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale have two different signatures for detectMultiscale. What is the purpose of the first, with the arguments for rejectLevels and levelWeights?
  • My images are 112x112. Each digit within this image would be around 11x11 pixels. The cascade sizes are 20x30. What parameter should I/ do I need to set in order for the cascades to work properly?

(this is a repost from SO).