我将96x96像素图像作为numpy数组。问题是我不知道如何使opencv加载这个ndarray并检测面孔。
img = cv2.imread(X)
此行不起作用。我收到此错误
TypeError: expected string or Unicode object, numpy.ndarray found
X是输入图像阵列(灰度)
答案 0 :(得分:0)
好吧现在使用这个
img = X[k].reshape(96,96)
但是i 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()