Python rospy Script doesn't end running
Hey there,
i already talked to some guys in #python irc channel. My aim was to close a while loop with ctrl-c so i used a try - exception statement. The problem is that the while loop doesnt really close. If i put a print "whatever" in the exception statement it doesnt get printed, though the code in the while loop doesnt get executed anymore. People said that my code itself seems correct. So i used the faulthandler to get deeper into that problem and it seems that it has smth to do with ROS. But im not into programming that deeply to understand what is going wrong here or maybe its just a simple mistake i make...
Anyways, here is my code:
#!/usr/bin/env python
import time
import sys
#Ueye Imports
import ids
#ROS Imports
import rospy
from sensor_msgs.msg import Image
#OpenCV Imports
import cv2
from cv_bridge import CvBridge, CvBridgeError
import faulthandler
import signal
faulthandler.enable()
faulthandler.register(signal.SIGUSR1)
# +++++++++++++++ ROS Node Einstellugen +++++++++++++++
# Diese Node agiert unter dem Namen "NeedleDetector"
rospy.init_node('VideoPublisher', anonymous=True)
# und published folgende Topics:
VideoRaw = rospy.Publisher('Kamera_Rohbild', Image, queue_size=10)
EdgesRaw = rospy.Publisher('Kamera_Kantenbild', Image, queue_size=10)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++
cam = cv2.VideoCapture('output.avi')
# ++++++++++++++++ Einstellungen Bildverarbeitung +++++++++++++++
#Einstellungen fuer den Gaussschen Weichzeichner
gaussian_blur_ksize = (5, 5)
gaussian_blur_sigmaX = 4
#Die Threshold Werte fuer den Canny Operator
threshold1 = 55
threshold2 = 75
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
br = CvBridge()
try:
while cam.isOpened():
# Naechstes Bild abfragen
meta, frame = cam.read()
# Anwendung des Gausschen Weichzeichners um das Rauschen etwas zu unterdruecken
frame_gaus = cv2.GaussianBlur(frame, gaussian_blur_ksize, gaussian_blur_sigmaX)
# ueberfuehren in ein Graustufen Bild fuer den Canny Algorithmus
frame_gray = cv2.cvtColor(frame_gaus, cv2.COLOR_BGR2GRAY)
# ueberfuehren in das Kantenbild
frame_edges = cv2.Canny(frame_gray, 55, 75)
# umwandeln der Numpy Arrays in die ROS Nachricht
frame_msg = br.cv2_to_imgmsg(frame, "rgb8")
frame_msg_edges = br.cv2_to_imgmsg(frame_edges, "mono8")
# Nachrichten veroeffentlichen
VideoRaw.publish(frame_msg)
EdgesRaw.publish(frame_msg_edges)
time.sleep(0.1)
except KeyboardInterrupt:
print "1"
rospy.is_shutdown()
cam.close()
print "2"
And here is what the faulthandler said:
Thread 0x00007f8f02b25700:
File "/usr/lib/python2.7/threading.py", line 358 in wait
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/tcpros_pubsub.py", line 418 in _run
File "/usr/lib/python2.7/threading.py", line 763 in run
File "/usr/lib/python2.7/threading.py", line 810 in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 783 in __bootstrap
Thread 0x00007f8eea43b700:
File "/usr/lib/python2.7/threading.py", line 358 in wait
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/tcpros_pubsub.py", line 418 in _run
File "/usr/lib/python2.7/threading.py", line 763 in run
File "/usr/lib/python2.7/threading.py", line 810 in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 783 in __bootstrap
Thread 0x00007f8ee9c3a700:
File "/usr/lib/python2.7/threading.py", line 358 in wait
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/tcpros_pubsub.py", line 418 in _run
File "/usr/lib/python2.7/threading.py", line 763 in run
File "/usr/lib/python2.7/threading.py", line 810 in __bootstrap_inner
File ...