opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

Segmentation Fault (core dumped) in face recognition pipeline

Open sergi0g opened this issue 1 year ago • 3 comments

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

  • example code:
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)
  • Ubuntu 18.04
  • aarch64 (I am using a jetson nano)
  • OpenCV (contrib) 4.6.0.66
Issue submission checklist
  • [X] This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • [X] I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
  • [ ] The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
  • [X] I'm using the latest version of opencv-python

sergi0g avatar Sep 09 '22 13:09 sergi0g

Could you provide small reproducer with few lines to highlight the issue with all input data. Images and models from samples may be used.

asmorkalov avatar Sep 12 '22 08:09 asmorkalov

After some debugging I have come to the conclusion that the code that makes the program stop is

model.predict()

I can use all other opencv functions perfectly well. It must be a problem with the contrib package and not the standard one. I am using a photo of me saved in pgm format by opencv. I don't think this is an opencv bug, because I have used the same code successfully in the past. I am trying this code on a machine with a clean install and the only other things I installed are

  • ZED SDK for the camera
  • ROS Melodic
  • A few other Python packages I need
  • TensorRT for object detection Do you think that all these packages may have conflicting dependencies? (By the way, I had these problems before TensorRT installation).

sergi0g avatar Sep 13 '22 04:09 sergi0g

Could you prepare small independent reproducer for the issue and create dedicated bug in opencv_contrib repo. Looks like it's not relevant to Python bindings, but algorithm bug.

asmorkalov avatar Feb 21 '23 11:02 asmorkalov