maskdetection
maskdetection copied to clipboard
Importing trained model directly using python opencv
Python: 3.6.9 opencv-python==4.2.0.34
Rather than trying to build this project with so little documentation, I decided to directly import the model in cv2 python but no success so far.
import cv2
proto_path = 'deploy.prototxt'
model_path = 'face_mask.caffemodel'
model = cv2.dnn.readNetFromCaffe(proto_path, model_path)
image_file = 'sample_images/0000.jpg'
img = cv2.imread(image_file)
image_blob = cv2.dnn.blobFromImage(img)
model.setInput(image_blob)
prediction = model.forward()
Error:
cv2.error: OpenCV(4.2.0) /io/opencv/modules/dnn/src/dnn.cpp:3084: error: (-215:Assertion failed) total(os[i]) > 0 in function 'getLayerShapesRecursively'
Did anyone had any success doing this or what else can be done to use the pre-trained model?
Try
image_blob = cv2.dnn.blobFromImage(img, 1, (224, 224), (127.5, 127.5, 127.5))
deploy.prototxt indicates that the input should be a 224 x 224 image, and I got the 127.5 from here: https://github.com/didi/maskdetection/blob/master/lib/src/FaceMaskDetector.cpp#L72