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

ROS Segmentation fault (core dumped)

asked 2018-04-04 09:26:12 -0600

SGpedro gravatar image

updated 2018-04-05 12:03:29 -0600

I've been developing a face detection app with ROS to run on a Drone. The face detection code is the code that can be find on google and works fine but when I mixed it with ROS, I've been struggling with an error: segmentation fault (core dumped). I've tried to find error like using the wrong type of variables but unsuccessfully. Here is the code:

    while not rospy.is_shutdown():
        # Capture frame-by-frame
        ret, frame =
        frame = imutils.resize(frame, width=600)

        #convert the frame (of the webcam) to gray)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        mask = cv2.erode(gray, None, iterations=2)
        mask = cv2.dilate(mask, None, iterations=2)

        # detecting the faces
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
            x_imagem = x + (w/2)
            y_imagem = y + (h/2)

  , (x+(w/2), y+(h/2)), 5, (0,0,255), -1)
            #600 x 450;

            cmd_vel_msg = Twist()
            if(x_imagem > 200 and x_imagem < 400):

            elif(x_imagem < 200):  #WHERE IT CRASH
                cmd_vel_msg.linear.x = 0.0
                cmd_vel_msg.linear.y = -0.3
                cmd_vel_msg.linear.z = 0.0

            elif(x_imagem > 400): #WHERE IT CRASH
                cmd_vel_msg.linear.x = 0.0
                cmd_vel_msg.linear.y = 0.3
                cmd_vel_msg.linear.z = 0.0

         # Display the resulting frame

        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):

# When everything is done, release the capture

The program is crashing everytime my face goes to the left or right of the windows. I marked where it's crashing. No ERRO MSG are appearing in the terminal.

Hope you can help me and could make it clzar this time! Thanks!

edit retag flag offensive close merge delete



A SEGFAULT is a C/C++ thing, while you show Python code. It's still possible to run into SEGFAULTs, but it's likely that would be where Python is interacting with 'native' (ie: compiled) code. It would probably help if you could identify where that is, or use gdb to find that spot.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-04 09:51:33 -0600 )edit

Note also: you have an infinite while loop in your program, which precludes the line rospy.spin() from ever executing. That is not necessarily a problem, but something to be aware of.

Finally: please check the indentation of your code. The formatting seems a little off.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-04 09:53:38 -0600 )edit

^ Instead of while True:, I will typically use while not rospy.is_shutdown():

(alternatively, instead of while bool_condition: -> while not rospy.is_shutdown() and bool_condition:)

Makes exiting rospy nodes a bit cleaner.

josephcoombe gravatar image josephcoombe  ( 2018-04-04 11:55:56 -0600 )edit

Hi! Thanks for the answers, gvdhoorn what do I need to identify to help you out? For the identation, I think it's because of the code block from the text.

SGpedro gravatar image SGpedro  ( 2018-04-04 12:30:47 -0600 )edit

gvdhoorn what do I need to identify to help you out?

it's not me per se, it's everyone that might be able to help you. Without an idea of where the SEGFAULT occurs, it's probably going to be hard to help you.

First: copy the exact error msg into your question.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-04 13:10:12 -0600 )edit

For the identation, I think it's because of the code block from the text.

probably, but that doesn't mean you can't fix it. Just make sure to keep a minimum of 4 spaces of indentation, as that is what Askbot uses to markup verbatim text.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-04 13:10:51 -0600 )edit

And do what @josephcoombe writes. while True is almost an anti-pattern in an event based system, especially in event consumers.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-04 13:11:54 -0600 )edit

Thanks for the feeback, I changed the while loop, add * where it was crashing* and just put the part that I think that matters.

SGpedro gravatar image SGpedro  ( 2018-04-05 11:52:13 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-04-08 19:17:05 -0600

SGpedro gravatar image

I finally make it works, what I did was:

  1. Instantiate the publisher and init_node inside the function that was called by the main
  2. Now my function main just call the function that recognize the face
  3. Delete rospy.spin()
  4. Create a new .py to work with the publishing, the one that was crashing is being used to just sent to the new one the value where the drone must go.

Thanks for the answers and the help!

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-04-04 09:26:12 -0600

Seen: 3,912 times

Last updated: Apr 08 '18