0
$\begingroup$

I am currently working on a project on NiryoOne which requires facial detection of a person through NiryoOne camera (ELP SONY IMX).

The code that I am using works on my laptop running Ubuntu. However, I am currently getting an assertion error, which suggests that the file path has not been properly specified, but have tried various file path and none seem to be the solution. Not certain if I am missing some dependencies or packages on NiryoOne. Any help regarding the use of OpenCV would be greatly appreciated.

For both laptop and NiryoOne

  • Ubuntu: 16.04
  • Python: 2.7.13
  • OpenCV: 3.3.1-dev

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import os, sys
import cv2
import numpy as np
import rospy
from std_msgs.msg import Int32

Start = False

rospy.init_node('my_third_node')
pub = rospy.Publisher('camera',Int32,queue_size=10)

#initialise cascade type
face_cascade = cv2.CascadeClassifier('/haarcascade_frontalface_alt2.xml')

# Open the device at the ID 1 (USB)
cap = cv2.VideoCapture(0)

#To set the resolution
#remove cv.cv depeding on version
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

#REPLACE WITH VOICE LATER
while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grey for haarcascade as per documentation


    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #1.5, 5 standard values ; scaling factor increase for higher accuracy
    # x,y = x,y coordinates, h,w = height, weight unit pixel
    for (x, y, w, h) in faces:
        print(x,y,w,h)
        pub.publish(x)
        pub.publish(y)
        pub.publish(w)
        pub.publish(h)

        #roi = region of interest
        roi_gray = gray[y:y+h+35, x:x+w+20]
        #roi_color = frame[y:y+h+35, x:x+w+20]
        img_item = "saved-img.png"
        cv2.imwrite(img_item, roi_gray)


        #bounding rectangle
        stroke = 2
        end_cord_x = x + w
        end_cord_y = y + w
        #arg 1;2;3: frame to draw, start coord, end coord
        cv2.rectangle(frame, (x,y), (end_cord_x,end_cord_y), (255, 0, 0))
        # Start == True
        # if Start == True:
        #       cap.release()
        #       cv2.destroyAllWindows()
    cv2.imshow('frame', frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break


#Check whether user selected camera is opened successfully.
if not (cap.isOpened()):
    print('Could not open video device')

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Error

niryo@niryo-desktop:~/sepra6$ python camhaar.py
OpenCV Error: Assertion failed (!empty()) in detectMultiScale, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/objdetect/src/cascadedetect.cpp, line 1698
Traceback (most recent call last):
  File "camhaar.py", line 31, in <module>
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #1.5, 5 standard values ; scaling factor increase for higher accuracy
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/objdetect/src/cascadedetect.cpp:1698: error: (-215) !empty() in function detectMultiScale
$\endgroup$
1
  • $\begingroup$ Very unlikely that '/haarcascade_frontalface_alt2.xml' is the correct file path. That means it would be in your computer's root directory ('/'), where only Operating System files and folders should go. $\endgroup$
    – edwinem
    Jul 6 at 19:30

2 Answers 2

1
$\begingroup$

TL;DR

You are probably getting that error, because the file that you wish to use for your face_cascade is not loaded correctly. That is to say, the path that you passed to face_cascade() is incorrect.


Incorrect local path

The path you gave is set in this line:

face_cascade = cv2.CascadeClassifier('/haarcascade_frontalface_alt2.xml')

Check that the path to the file /haarcascade_frontalface_alt2.xml is actually correct. At a guess, assuming that you have supplied the file yourself, you may need to omit the preceding /, so that the line is just

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

Full path

Alternatively, if the XML files are not in your current directory then you will need to use the full path in order to be able to load them correctly.

Try

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')

Adding a loading check

Better still, use os.path.join(), like so:

face_cascade = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades, 'haarcascade_frontalface_alt2.xml'))

Now you can actually check to see if the cascade file has loaded because face_cascade.empty() will be False if it has loaded:

if face_cascade.empty()
   #print 'File not loaded. Game over!'
   # Exit the program
   sys.exit("Cascade file not loaded.") 
$\endgroup$
0
$\begingroup$

Without enough information it's hard to say more than you have a bug in your code at the line it points to.

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #1.5, 5 standard values ; scaling factor increase for higher accuracy

One of the arguments is failing the assertion that it's not empty.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.