python - 如何从相机调整阵列大小以适合人脸识别模型?
问题描述
好吧,长话短说,我做了一个人脸识别模型。现在,当我想从我的模型中进行预测时,我收到以下错误:
ValueError:检查输入时出错:预期 conv2d_3_input 有 4 个维度,但得到了形状为 (182、182、3) 的数组
我使用我的相机和面部检测工作并绘制了一个矩形,只是我的面部识别预测出错了。
我是机器学习的新手,我真的很清楚自己需要做什么,所以不要因为我的错误而评判我:/
我已经尝试了一些对某些人有用的解决方案,比如重塑等,但对我来说没有。
编辑:好的,到目前为止你们设法帮助我摆脱了数组错误,但是通过调整我的 roi_color 我收到新错误:OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src \resize.cpp:3363: 错误: (-215:Assertion failed) !dsize.empty() in function 'cv::hal::resize'
谢谢你的帮助,简
import numpy as np
import cv2
import random as rd
from pathlib import Path
import string
from keras.models import model_from_json
def Recog_face(x, y, roi_color, frame, recognizer_face):
predictions = recognizer_face.predict(roi_color)
color = (255, 255, 255)
stroke = 2
font = cv2.FONT_HERSHEY_SIMPLEX
return cv2.putText(frame, predictions, (x,y), font, 1, color, stroke, cv2.LINE_AA)
def Detect_face(x, y, frame):
color = (255, 0, 0)
stroke = 1
end_cord_x = x + w
end_cord_y = y + h
return cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recognizer_face = model_from_json(open("face_rec.json", "r").read())
recognizer_face.load_weights('face_rec.h5')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5,
minNeighbors=5)
for (x, y, w, h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
Detect_face(x, y, frame)
# Face recognizer
Recog_face(x, y, roi_color, frame, recognizer_face)
cv2.imshow('frame', frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier = Sequential()
# Convolution
classifier.add(Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu'))
# Maxpooling To reduce the number of nodes will get in flattening step
# Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# adding second Convolution layer
classifier.add(Convolution2D(32, 3, 3, activation='relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Flatten
classifier.add(Flatten())
# Full connection
classifier.add(Dense(output_dim = 128, activation = 'relu')) # 128 hidden nodes in the hidden layer
classifier.add(Dense(output_dim = 5, activation = 'softmax')) # softmax function cause multiple outcome
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
print(classifier.summary())
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('images/Training_set',
target_size=(64, 64),
batch_size=32,
color_mode='rgb',
class_mode='categorical')
test_set = test_datagen.flow_from_directory('images/Test_set',
target_size=(64, 64),
batch_size=32,
color_mode='rgb',
class_mode='categorical')
classifier.fit_generator(training_set,
steps_per_epoch=380,
epochs=2,
validation_data= test_set,
validation_steps=127)
fer_json = classifier.to_json()
with open("face_rec.json", "w") as json_file:
json_file.write(fer_json)
classifier.save_weights("face_rec.h5")
解决方案
推荐阅读
- java - 存在“Access-Control-Allow-Origin”标头
- vba - 替换所有工作表的同一列中的文本
- database - oracle数据库的性能
- java - Spring Boot项目jar命令行执行失败
- javascript - 从 reddit 评论中删除链接
- audio - WASAPI 渲染保存的音频文件
- google-apps-script - 使用 Google Sheet Add-on 时 Stackdriver 中“脚本延迟(xx 分钟)等待配额”的含义
- python - 在 anaconda 环境中使用 pyinstaller
- facebook-graph-api - App Insights API 已弃用并将停止工作 - 有哪些替代方案?
- java - 使用构建器模式时的 Lambda