Ask Your Question
0

Error in Assertion failed (!empty()) in detectMultiScale

asked 2019-02-28 04:22:00 -0600

loguna gravatar image

updated 2019-02-28 07:32:16 -0600

supra56 gravatar image

Hi,

I am using ROS + Open Cv and recently coded a code but receive this error:Error in Assertion failed (!empty()) in detectMultiScale I check my directory, the .xml file is indicated as it suppose to. Kindly do help me.

P.S. I did another test code with using Open Cv inputs (without ROS), it work perfectly.

Code:

#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import roslib
import sys
import rospy
import cv2 
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

class image_converter:

  def __init__(self):
    self.image_pub = rospy.Publisher("image_topic_2",Image)

    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber("/camera/rgb/image_raw",Image,self.callback)

  def callback(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
      cv_image2 =  self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print(e)

    (rows,cols,channels) = cv_image.shape
    if cols > 60 and rows > 60 :
      cv2.circle(cv_image, (50,50), 10, 255)
    cv2.imshow('cv_image',cv_image)
    cv2.waitKey(3)
    face_cascade = cv2.CascadeClassifier('compare/data/haarcascade_frontalface_alt.xml')
    test = face_cascade.load('compare/data/haarcascade_frontalface_alt.xml')
    print(test)
    gray = cv2.cvtColor(cv_image2, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
    cv2.imshow('gray',gray)
    for (x, y, w, h) in faces:
        #print(x,y,w,h)
        roi_gray = gray[y:y+h, x:x+w] #(ycord_start, ycord_end)
        roi_color = cv_image2[y:y+h, x:x+w]
        img_item = "my-image.png"
        cv2.imwrite(img_item, roi_color)

        color = (255, 0, 0) #BGR 0-255 
        stroke = 2
        end_cord_x = x + w
        end_cord_y = y + h
        cv2.rectangle(cv_image2, (x, y), (end_cord_x, end_cord_y), color, stroke)
        #subitems = smile_cascade.detectMultiScale(roi_gray)
        #for (ex,ey,ew,eh) in subitems:
        #   cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    # Display the resulting frame

    cv2.waitKey(3)


    try:
      self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
    except CvBridgeError as e:
      print(e)

def main(args):

  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)
edit retag flag offensive close merge delete

Comments

Error in Assertion failed (!empty()) in detectMultiScale: is when a system cannot find the .xml file in cv2.CascadeClassifier('compare/data/haarcascade_frontalface_alt.xml') right? But i have manually check and the file is in there.

loguna gravatar imageloguna ( 2019-02-28 04:46:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-28 07:47:17 -0600

supra56 gravatar image

updated 2019-02-28 07:53:08 -0600

Error in Assertion failed (!empty()) mean. the file is empty becasue you didn't add to data parameter.

def callback(self,data)

But how do you called function of callback This doesn't do anything.

if __name__ == '__main__':
    main(sys.argv)

You need filename like this:

if __name__ == '__main__':
    x= image_converter()
    filename = x.callback(filename)

I also noticed you will have to changed cv_image2 to cv_image. The cv_image2 doesn't draw circle.

edit flag offensive delete link more

Comments

Here is tutorial this and this this and this

supra56 gravatar imagesupra56 ( 2019-02-28 07:59:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-28 04:22:00 -0600

Seen: 1,449 times

Last updated: Feb 28 '19