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)