我有一个96x96像素的图像,以numpy数组方式存储。但问题是我不知道如何让OpenCV加载这个ndarray并检测人脸。
img = cv2.imread(X)
这行代码无法正常工作,我遇到了以下错误
TypeError: expected string or Unicode object, numpy.ndarray found
X 是输入图像数组(灰度)
我有一个96x96像素的图像,以numpy数组方式存储。但问题是我不知道如何让OpenCV加载这个ndarray并检测人脸。
img = cv2.imread(X)
这行代码无法正常工作,我遇到了以下错误
TypeError: expected string or Unicode object, numpy.ndarray found
X 是输入图像数组(灰度)
好的,现在它可以使用了
img = X[k].reshape(96,96)
但是我 OpenCV 输出的图像全都是黑色的。按要求,这是代码片段。
import numpy as np
import cv2, cv
import numpy as np
import pandas as pd
import pylab as pl
from skimage import transform
from numpy import ravel
import pylab as pl
from sklearn import linear_model, cross_validation
from sklearn.svm import SVR
from sklearn.decomposition import PCA
from sklearn.neural_network import BernoulliRBM
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
df = pd.read_csv('/users/prabhubalakrishnan/Desktop/training.csv', header=0)
x = df['Image'][:5].values
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
X = []
for k in xrange(len(x)):
img = np.fromstring(x[k], dtype = np.uint8, sep=' ', count=96*96)
X.append ( ravel(transform.resize (img.reshape(96,96) , (96,96))) )
'''
for k in xrange(len(X)):
pl.imshow(X[k].reshape(96,96),cmap=pl.cm.gray)
pl.show()
'''
X = np.array(X)
X = X.astype('uint8')
print 'X:', X.shape, X.dtype
pl.ion()
for k in xrange(len(X)):
img = X[k].reshape(96,96)
faces = face_cascade.detectMultiScale(img, 1.03, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]
print 'Image',img
print 'Faces',faces
cv2.namedWindow("img", cv2.CV_WINDOW_AUTOSIZE)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()