Skip to content

Commit f143ef6

Browse files
committed
[jsk_tools/camera_check.py] Add is_image_valid function to check camera is valid
1 parent 26c454d commit f143ef6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

jsk_tools/src/camera_check.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
import diagnostic_updater
1111
import diagnostic_msgs
1212
from sensor_msgs.msg import Image
13+
import cv2
14+
from cv_bridge import CvBridge
15+
from cv_bridge import CvBridgeError
1316

1417
from jsk_tools.sanity_lib import checkUSBExist
1518

1619

1720
class CameraCheck(object):
1821

19-
def __init__(self):
22+
def __init__(self, device_path=None):
23+
self.bridge = CvBridge()
2024
self.topic_names = rospy.get_param('~topic_names', [])
2125
self.device_type = rospy.get_param("~device_type", 'usb')
2226
self.device_path = rospy.get_param('~device_path', None)
@@ -97,6 +101,23 @@ def check_topic(self, stat):
97101
stat.add('topic {} {} Hz'.format(topic_name, topic_hz))
98102
return stat
99103

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+
100121
def run(self):
101122
while not rospy.is_shutdown():
102123
self.subscribe()

0 commit comments

Comments
 (0)