ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Face detection with usb_cam

asked 2020-04-09 22:33:15 -0600

jesusy gravatar image

updated 2020-04-09 22:39:13 -0600

Hello, I'm following this video on How to use ros usb cam for face detection. In the las part, when I run:

rosrun detect_face ros_face_detection.py

And yes, I changed the location of the haar cascades files. It gives me this error:

yepez@user:~/catkin_ws$ rosrun detect_face ros_face_detection.py

[rosrun] Couldn't find executable named ros_face_detection.py below /home/yepez/catkin_w/src/RobotOperatingSystem/detect_face
[rosrun] Found the following, but they're either not files
[rosrun] or not executable:
[rosrun]   /home/yepez/catkin_ws/src/RobotOperatingSystem/detect_face/src/ros_face_detection.py`

This is the ros_face_detection.py file:

 -----#!/usr/bin/env python

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

   bridge = CvBridge()
   face_cascade = cv2.CascadeClassifier('/home/yepez/catkin_ws/src/RobotOperatingSystem/detect_face/src/haarcascade_frontalface_default.xml')
   eye_cascade = cv2.CascadeClassifier('/home/yepez/catkin_ws/src/RobotOperatingSystem/detect_face/src/haarcascade_eye.xml')

  def image_callback(ros_image):
  print ('got an image')
  global bridge
  #convert ros_image into an opencv-compatible image
  try:
      img = bridge.imgmsg_to_cv2(ros_image, "bgr8")
  except CvBridgeError as e:
      print(e)
  #from now on, you can work exactly like with opencv

  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

  faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  for (x,y,w,h) in faces:
      img = 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)
  font = cv2.FONT_HERSHEY_SIMPLEX
  cv2.putText(img,'Face Detection with ROS & OpenCV!',(15,450), font, 1,(255,255,255),2,cv2.LINE_AA)
  cv2.imshow('img',img)
  cv2.waitKey(3)


 def main(args):
  rospy.init_node('image_converter', anonymous=True)
  #for turtlebot3 waffle
  #image_topic="/camera/rgb/image_raw/compressed"
  #for usb cam
  #image_topic="/usb_cam/image_raw"
  image_sub = rospy.Subscriber("/usb_cam/image_raw",Image, image_callback)
  try:
   rospy.spin()
  except KeyboardInterrupt:
   print("Shutting down")
  cv2.destroyAllWindows()

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

Thanks in advance for the help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-04-09 23:42:17 -0600

Did you chmod +x the file to make it an executable?

Also roscd detect_face and make sure that the file exists in the package in that directory to be found. If you messed up install rules it could not be copied. Please add cmakelists. But I'm pretty sure its an executable issue.

edit flag offensive delete link more

Comments

Thank you, I had not done chmod +x the file, beginner mistake.

jesusy gravatar image jesusy  ( 2020-04-10 00:44:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-09 22:33:15 -0600

Seen: 523 times

Last updated: Apr 09 '20