0

I am getting Segmentation Fault when I am running below code. I have used cv2 haarcascade classifier.

I have already verified that Camera is integrated and working correctly and there are enough free resources to run this program.

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)
if not cap:
    print "!!! Failed VideoCapture: unable to open device 0"
    sys.exit(1)
while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(
        gray,     
        scaleFactor=1.3,
        minNeighbors=5,     
        minSize=(20, 20)
    )

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]  
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew,ey+eh), (0,255,0), 2)
    cv2.imshow('img',img)
    k=cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()
Pert8S
  • 582
  • 3
  • 6
  • 21
  • You have to supply additional debugging information. See [Fixing Segmentation faults in C++](https://stackoverflow.com/questions/3718998/fixing-segmentation-faults-in-c)! – Klaus D. Oct 06 '20 at 12:13
  • Starting program: /usr/bin/python faced.py [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1". [New Thread 0xab477460 (LWP 2611)] [New Thread 0xaac76460 (LWP 2612)] [New Thread 0xaa475460 (LWP 2613)] Thread 1 "python" received signal SIGSEGV, Segmentation fault. 0xb42e2c1c in cv::CascadeClassifier::empty() const () from /usr/local/lib/libopencv_objdetect.so.4.1 – Pert8S Oct 06 '20 at 13:23
  • Getting above result when I am using gdb Python @KlausD. – Pert8S Oct 06 '20 at 13:23
  • Are you sure haarcascade xml files are read correctly? I see no problem testing the code.They are located at [`data/haarcascades`](https://github.com/opencv/opencv/tree/master/data/haarcascades). – Burak Oct 10 '20 at 08:31

0 Answers0