I wrote a program for face recognition, which was supposed to show a rectangle around any detected faces.
The program started running and once it detected my face (but it did not recognize it), the program exited saying "Segmentation fault (core dumped)". I have also tried with simple examples from the internet with haar cascade classifiers and an EigenFaceRecognizer. I have also had this problem when trying to load a model created by the LBPHFaceRecognizer.
import os
import cv2
import numpy
def read_images(path, image_size):
names = []
training_images, training_labels = [], []
label = 0
for dirname, subdirnames, filenames in os.walk(path):
for subdirname in subdirnames:
names.append(subdirname)
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
img = cv2.imread(os.path.join(subject_path, filename),
cv2.IMREAD_GRAYSCALE)
if img is None:
# The file cannot be loaded as an image.
# Skip it.
continue
img = cv2.resize(img, image_size)
training_images.append(img)
training_labels.append(label)
label += 1
training_images = numpy.asarray(training_images, numpy.uint8)
training_labels = numpy.asarray(training_labels, numpy.int32)
return names, training_images, training_labels
path_to_training_images = './people'
training_image_size = (200, 200)
names, training_images, training_labels = read_images(
path_to_training_images, training_image_size)
model = cv2.face.EigenFaceRecognizer_create()
model.train(training_images, training_labels)
model.save("myModel")
face_cascade = cv2.CascadeClassifier(
'./cascades/haarcascade_frontalface_default.xml')
camera = cv2.VideoCapture(0)
while (cv2.waitKey(1) == -1):
success, frame = camera.read()
if success:
faces = face_cascade.detectMultiScale(frame, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
roi_gray = gray[x:x+w, y:y+h]
if roi_gray.size == 0:
# The ROI is empty. Maybe the face is at the image edge.
# Skip it.
continue
roi_gray = cv2.resize(roi_gray, training_image_size)
label, confidence = model.predict(roi_gray)
text = '%s, confidence=%.2f' % (names[label], confidence)
cv2.putText(frame, text, (x, y - 20),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow('Face Recognition', frame)
Expected behaviour
I wrote a program for face recognition, which was supposed to show a rectangle around any detected faces.
Actual behaviour
The program started running and once it detected my face (but it did not recognize it), the program exited saying "Segmentation fault (core dumped)". I have also tried with simple examples from the internet with haar cascade classifiers and an EigenFaceRecognizer. I have also had this problem when trying to load a model created by the LBPHFaceRecognizer.
Steps to reproduce
Issue submission checklist
opencv-python