When running face recognition OpenCV I get IOerror

710 Views Asked by At

This is the code im trying to run that I found on this video https://www.youtube.com/watch?v=Ax6P93r32KU

I have never used OpenCV, and I do not understand the error.

The code is designed to detect if someone from a live feed is wearing a mask or not.

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()

This is what happens in the terminal when I run the code:

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>

I have the model file in the same folder as the rest of the project.

2

There are 2 best solutions below

0
Mohit Verma On

Please try to install the exact same version of dependencies that are mention in 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
0
Jason Arvizu On

You have one too many quotation marks on your prototxtPath and also change the path of maskNet as below:

maskNet = load_model(r"C:\Users\Public\Desktop\Python_Work\Face-Mask-Detection-master\mask_detector.model")