|
10 | 10 | import diagnostic_updater |
11 | 11 | import diagnostic_msgs |
12 | 12 | from sensor_msgs.msg import Image |
| 13 | +import cv2 |
| 14 | +from cv_bridge import CvBridge |
| 15 | +from cv_bridge import CvBridgeError |
13 | 16 |
|
14 | 17 | from jsk_tools.sanity_lib import checkUSBExist |
15 | 18 |
|
16 | 19 |
|
17 | 20 | class CameraCheck(object): |
18 | 21 |
|
19 | | - def __init__(self): |
| 22 | + def __init__(self, device_path=None): |
| 23 | + self.bridge = CvBridge() |
20 | 24 | self.topic_names = rospy.get_param('~topic_names', []) |
21 | 25 | self.device_type = rospy.get_param("~device_type", 'usb') |
22 | 26 | self.device_path = rospy.get_param('~device_path', None) |
@@ -97,6 +101,23 @@ def check_topic(self, stat): |
97 | 101 | stat.add('topic {} {} Hz'.format(topic_name, topic_hz)) |
98 | 102 | return stat |
99 | 103 |
|
| 104 | + def is_image_valid(self, msg): |
| 105 | + try: |
| 106 | + cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8") |
| 107 | + sum_of_pixels = max(cv2.sumElems(cv_image)) |
| 108 | + except CvBridgeError as e: |
| 109 | + rospy.logerr( |
| 110 | + "[%s] failed to convert image to cv", |
| 111 | + self.__class__.__name__) |
| 112 | + return False |
| 113 | + rospy.loginfo( |
| 114 | + "[%s] sum of pixels is %d at %s", |
| 115 | + self.__class__.__name__, |
| 116 | + sum_of_pixels, msg.header.stamp.secs) |
| 117 | + if sum_of_pixels == 0: |
| 118 | + return False |
| 119 | + return True |
| 120 | + |
100 | 121 | def run(self): |
101 | 122 | while not rospy.is_shutdown(): |
102 | 123 | self.subscribe() |
|
0 commit comments