Skip to content

Segmentation Fault (core dumped) in face recognition pipeline #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 of 4 tasks
sergi0g opened this issue Sep 9, 2022 · 3 comments
Closed
3 of 4 tasks

Segmentation Fault (core dumped) in face recognition pipeline #728

sergi0g opened this issue Sep 9, 2022 · 3 comments

Comments

@sergi0g
Copy link

sergi0g commented Sep 9, 2022

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
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • 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")
  • I'm using the latest version of opencv-python
@asmorkalov
Copy link
Collaborator

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

@sergi0g
Copy link
Author

sergi0g commented Sep 13, 2022

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

@asmorkalov asmorkalov changed the title Segmentation Fault (core dumped) Segmentation Fault (core dumped) in face recognition pipeline Sep 30, 2022
@asmorkalov
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants