Summary
Running openface detect-video results in an OpenCV error:
Error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'imread'
> Expected 'filename' to be a str or path-like object
This happens because process_video() passes a numpy frame to FaceDetector.get_face(), but get_face() and detect_faces() assume the input is a file path, which leads to cv2.imread() being called on a numpy array.
How to Reproduce
openface detect-video path/to/video.mp4 --device cpu
Proposed Fix
Modify preprocess_image() in face_detection.py to allow either a file path or a numpy frame:
def preprocess_image(self, image_path, resize: float = 1.0):
if isinstance(image_path, str):
img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR)
else:
# assume numpy BGR array from cv2.VideoCapture
img_raw = image_path
img = np.float32(img_raw)
if resize != 1:
img = cv2.resize(img, None, fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR)
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0).to(self.device)
return img, img_raw
Summary
Running
openface detect-videoresults in an OpenCV error:This happens because
process_video()passes a numpy frame toFaceDetector.get_face(), butget_face()anddetect_faces()assume the input is a file path, which leads tocv2.imread()being called on a numpy array.How to Reproduce
openface detect-video path/to/video.mp4 --device cpuProposed Fix
Modify
preprocess_image()inface_detection.pyto allow either a file path or a numpy frame: