python - X.shape[1] 大小不符合预期值
问题描述
我目前正在完成我的机器人学最后学位项目,我决定创建一个能够复制人类情感的开源机器人。机器人已全部设置好并准备好接收订单,但我仍在忙于编写代码。我目前的代码基于这种方法。这个想法是从低 FPS 视频源中提取 68 个面部标志(使用 RPi Camera V2),将这些标志提供给经过训练的 SVM 分类器,并让它根据检测到的表情(愤怒、厌恶、恐惧、快乐、悲伤、惊喜和中性)。我正在使用我使用 RPi 相机拍摄的一些照片来测试我的模型的功能,这就是我迄今为止在代码方面设法整合的内容:
# import the necessary packages
from imutils import face_utils
import dlib
import cv2
import numpy as np
import time
import argparse
import os
import sys
if sys.version_info >= (3, 0):
import _pickle as cPickle
else:
import cPickle
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from data_loader import load_data
from parameters import DATASET, TRAINING, HYPERPARAMS
def get_landmarks(image, rects):
if len(rects) > 1:
raise BaseException("TooManyFaces")
if len(rects) == 0:
raise BaseException("NoFaces")
return np.matrix([[p.x, p.y] for p in predictor(image, rects[0]).parts()])
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
print("Initializing variables...")
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
# path to pretrained model
path = "saved_model.bin"
# load pretrained model
print("Loading model...")
model = cPickle.load(open(path, 'rb'))
# initialize final image height & width
height = 48
width = 48
# initialize landmarks variable as empty array
landmarks = []
# load the input image and convert it to grayscale
print("Loading image...")
gray = cv2.imread("foo.jpg")
# detect faces in the grayscale image
print("Detecting faces in loaded image...")
rects = detector(gray, 0)
# loop over the face detections
print("Looping over detections...")
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
for (x, y) in shape:
cv2.circle(gray, (x, y), 2, (0, 255, 0), -1)
# show the output image with the face detections + facial landmarks
print("Storing saved image...")
cv2.imwrite("output.jpg", gray)
print("Image stored as /'output.jpg/'")
# arrange landmarks in array
print("Collecting and arranging landmarks...")
# scipy.misc.imsave('temp.jpg', image)
# image2 = cv2.imread('temp.jpg')
face_rects = [dlib.rectangle(left=1, top=1, right=47, bottom=47)]
landmarks = get_landmarks(gray, face_rects)
# load data
print("Loading collected data into predictor...")
print("Extracted landmarks: ", landmarks)
landmarks = np.array(landmarks.flatten())
# predict expression
print("Making prediction")
predicted = model.predict(landmarks)
但是,在运行代码之后,到目前为止一切似乎都很好:
Making prediction
Traceback (most recent call last):
File "face.py", line 97, in <module>
predicted = model.predict(landmarks)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 576, in predict
y = super(BaseSVC, self).predict(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 325, in predict
X = self._validate_for_predict(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 478, in _validate_for_predict
(n_features, self.shape_fit_[1]))
ValueError: X.shape[1] = 136 should be equal to 2728, the number of features at training time
我在这个网站上搜索了类似的问题,但由于如此特定的目的,我并没有完全找到我需要的东西。我从事设计和研究工作已经有一段时间了,但是找到使代码工作所需的所有片段已经花费了我最多的时间,我很想尽快完善这个概念,因为演示日期很快就要到了。非常欢迎任何和所有的贡献!
解决方案
解决了!原来我的模型是使用 HOG 特征和 Dlib 地标的组合进行训练的,但是我只是将地标提供给预测器,这导致了大小差异。
推荐阅读
- stopwatch - 我需要帮助为反应时间测试仪创建秒表
- .htaccess - .htaccess 出现 | 但它不存在
- racket - 用合约实现算术模 N
- c# - 为什么'value'可以用作变量,当它是C#中的关键字时?
- c - 将节点添加到结束函数有什么作用
- java - 如何使用 parseInt 将字符串转换为整数?
- angular - TS2339:“对象”类型上不存在属性“地图”
- python - Matplotlib,图例未出现在直方图中
- java - Spring Boot 应用程序无法在 Azure 上正常运行
- c# - 如何使用字符串在 UWP 的 AutoSuggestBox 中显示数据