微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

运行人脸识别 OpenCV 时出现 IOerror

如何解决运行人脸识别 OpenCV 时出现 IOerror

这是我在此视频中找到的我尝试运行的代码 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,i,2]

    if confidence > 0.5:

        Box = detections[0,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))
        face = img_to_array(face)
        face = preprocess_input(face)

        faces.append(face)
        locs.append((startX,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,maskNet)

    for (Box,pred) in zip(locs,preds):
        # unpack the bounding Box and predictions
        (startX,endY) = Box
        (mask,withoutMask) = pred

        label = "Mask" if mask > withoutMask else "No Mask"
        color = (0,255,0) if label == "Mask" else (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,startY),(endX,endY),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>

我的模型文件与项目的其余部分位于同一文件夹中。

解决方法

请尝试安装 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。