我正在做一个实时面罩识别系统,在两个或更多人脸(这次是 3 个,因为它收到 3 个输入)出现在框架中之后,这个错误会停止进程。
错误 -
Traceback (most recent call last):
File "C:\Users\___\Desktop\MD\test_mask_video.py", line 99, in <module>
(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
File "C:\Users\___\Desktop\MD\test_mask_video.py", line 68, in detect_and_predict_mask
preds = maskNet.predict(faces)
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\engine\training.py", line 1621, in predict_function *
return step_function(self, iterator)
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\engine\training.py", line 1611, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\engine\training.py", line 1604, in run_step **
outputs = model.predict_step(data)
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\engine\training.py", line 1572, in predict_step
return self(x, training=False)
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\___\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\engine\input_spec.py", line 199, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "model_1" expects 1 input(s), but it received 3 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 224, 224, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 224, 224, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 224, 224, 3) dtype=float32>]
我的代码 -
confidence_arg = 0.5
def detect_and_predict_mask(frame, faceNet, maskNet):
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
(104.0, 177.0, 123.0))
faceNet.setInput(blob)
detections = faceNet.forward()
faces = []
locs = []
preds = []
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > confidence_arg:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
(startX, startY) = (max(0, startX), max(0, startY))
(endX, endY) = (min(w - 1, endX), min(h - 1, endY))
face = frame[startY:endY, startX:endX]
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
face = np.expand_dims(face, axis=0)
faces.append(face)
locs.append((startX, startY, endX, endY))
if len(faces) > 0:
#for x in faces:
preds = maskNet.predict(faces)
return (locs, preds)
print("Loading face detector model...")
prototxtPath = 'face_detector/deploy.prototxt'
weightsPath = 'face_detector/res10_300x300_ssd_iter_140000.caffemodel'
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
maskNet = load_model('model')
print("Starting video stream...")
vs = VideoStream(src=0).start()
while True:
frame = vs.read()
frame = imutils.resize(frame, width=800, height=320)
(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
print(locs, preds)
for (box, pred) in zip(locs, preds):
(startX, startY, endX, endY) = box
(noproperMask, mask, withoutMask) = pred
if (mask>withoutMask and mask>noproperMask):
label = "Mask"
color = (0, 255, 0)
elif (withoutMask>mask and withoutMask>noproperMask):
label = "No Mask"
color = (0, 0, 255)
cv2.putText(frame, label, (startX, startY - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
cv2.imshow("Mask Detection System", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
vs.stop()
cv2.destroyAllWindows()
我试图将面具识别作为一个for
循环,但它一次只能识别一张脸,并且不想再识别一秒或更多的脸。
我尝试过的代码(第 36 行)-
for x in faces:
preds = maskNet.predict(x)
任何帮助将不胜感激!