这是我在此视频中找到的我尝试运行的代码 https://www.youtube.com/watch?v=Ax6P93r32KU
<块引用>我从未使用过 OpenCV,我不明白其中的错误。
<块引用>该代码旨在检测来自实时供稿的人是否戴着口罩。
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os
def detect_and_predict_mask(frame, faceNet, maskNet):
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224),
(104.0, 177.0, 123.0))
faceNet.setInput(blob)
detections = faceNet.forward()
print(detections.shape)
faces = []
locs = []
preds = []
# loop over the detections
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
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)
faces.append(face)
locs.append((startX, startY, endX, endY))
if len(faces) > 0:
faces = np.array(faces, dtype="float32")
preds = maskNet.predict(faces, batch_size=32)
return (locs, preds)
prototxtPath = r""C:\Users\Public\Desktop\Python_Work\Face-Mask-Detection-master\face_detector\deploy.prototxt"
weightsPath = r"C:\Users\Public\Desktop\Python_Work\Face-Mask-Detection-master\face_detector\res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
maskNet = load_model("mask_detector.model")
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
for (box, pred) in zip(locs, preds):
# unpack the bounding box and predictions
(startX, startY, endX, endY) = box
(mask, withoutMask) = pred
label = "Mask" if mask > withoutMask else "No Mask"
color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
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("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.stop()
<块引用>
这是我运行代码时在终端中发生的事情:
PS C:\Users\Public\Desktop\Python_Work> &
C:/Users/rainb/AppData/Local/Programs/Python/Python38/python.exe
c:/Users/Public/Desktop/Python_Work/Face-Mask-Detection-master/detect_mask_video.py
2021-03-16 00:55:39.090747: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not
load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-03-16 00:55:39.099661: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart
dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
File "c:/Users/Public/Desktop/Python_Work/Face-Mask-Detection-master/detect_mask_video.py", line
80, in <module>
maskNet = load_model("mask_detector.model")
File "C:\Users\rainb\AppData\Local\Programs\Python\Python38\lib\site-
packages\tensorflow\python\keras\saving\save.py", line 186, in load_model
loader_impl.parse_saved_model(filepath)
File "C:\Users\rainb\AppData\Local\Programs\Python\Python38\lib\site-
packages\tensorflow\python\saved_model\loader_impl.py", line 110, in parse_saved_model
raise IOError("SavedModel file does not exist at: %s/{%s|%s}" %
OSError: SavedModel file does not exist at:
mask_detector.model/{saved_model.pbtxt|saved_model.pb}
PS C:\Users\Public\Desktop\Python_Work>
<块引用>
我的模型文件与项目的其余部分位于同一文件夹中。
答案 0 :(得分:0)
请尝试安装 requirements.txt 中提到的完全相同版本的依赖项
keras==2.3.1
imutils==0.5.3
numpy==1.18.2
opencv-python==4.2.0.*
matplotlib==3.2.1
argparse==1.1
scipy==1.4.1
scikit-learn==0.23.1
pillow==7.2.0
streamlit==0.65.2
playsound
pyobjc