我有最初来自Mnist数据集的数字序列,我已经用OpenCV操作了。它们保存在pickle文件中。它们是具有形状的单通道图像(112,112)。
我希望通过类似于the face detection tutorial的OpenCV级联分类器来运行这些,而不是使用这些Google-Street-View-House-Numbers-Digit-Localization cascades。
这是我的尝试:
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)
不幸的是,这只会导致以下错误:
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
是否有人熟悉如何将原始numpy数组加载到OpenCV detectMultiscale
?
以下是我的一些不确定因素:
detectMultiscale
签名。第一个的目的是什么,有rejectLevels
和levelWeights
的参数?系统信息:
cv2.__version__ # '2.4.11'
sys.version #'2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:43:17) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]'
答案 0 :(得分:1)
我可以直接从图像加载数据时使用相同的级联文件运行分类器:
import numpy as np
import cv2
workspace = '/path/to/directory'
# cascades
cascades = [
(cv2.CascadeClassifier(workspace + '/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade1/cascade.xml'), (255, 0, 0)),
(cv2.CascadeClassifier(workspace + '/Google-Street-View-House-Numbers-Digit-Localization/cascades/cascade6/cascade.xml'), (0, 255, 0))
]
# input
img = cv2.imread('/path/to/image/house-number-16.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect and mark
for cascade, color in cascades:
digits = cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in digits:
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
# show result
cv2.imshow('img', img)
cv2.waitKey()
结果(但无法检测到“1”):
我怀疑你的问题在于你从泡菜中保存图像的方式或加载它们的方式。
例如,默认情况下cv2.imread
将图像读取为整数numpy.ndarray
。当我将其更改为float(例如,除以255)时,我收到错误:
OpenCV错误:断言失败(scaleFactor&gt; 1&amp;&amp; image.depth()== CV_8U)在detectMultiScale文件中 /home/yohanesgultom/opencv/modules/objdetect/src/cascadedetect.cpp, 第1081行追溯(最近一次调用最后一次):文件“cascade.py”,行 21,在 digits = cascade.detectMultiScale(gray,1.3,5)cv2.error:/home/yohanesgultom/opencv/modules/objdetect/src/cascadedetect.cpp:1081: 错误:( - 215)scaleFactor&gt; 1&amp;&amp; image.depth()== CV_8U函数 detectMultiScale
我在Ubuntu上运行它:
>>> cv2.__version__
'2.4.13'
>>> sys.version
'2.7.10 (default, Oct 14 2015, 16:09:02) \n[GCC 5.2.1 20151010]'