-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1330 from knorth55/check-room-light
add check_room_light.py to check if room light is on
- Loading branch information
Showing
7 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
std_msgs/Header header | ||
bool light_on | ||
# relative luminance (Digital ITU BT.601) | ||
# relative luminance is not illuminance and not in Lux unit | ||
float32 relative_luminance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
jsk_robot_common/jsk_robot_startup/scripts/check_room_light.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env python | ||
|
||
import cv2 | ||
from cv_bridge import CvBridge | ||
import numpy as np | ||
import rospy | ||
|
||
from jsk_topic_tools import ConnectionBasedTransport | ||
|
||
from sensor_msgs.msg import CompressedImage | ||
from sensor_msgs.msg import Image | ||
|
||
from jsk_robot_startup.msg import RoomLight | ||
|
||
|
||
class CheckRoomLightNode(ConnectionBasedTransport): | ||
def __init__(self): | ||
super(CheckRoomLightNode, self).__init__() | ||
self.bridge = CvBridge() | ||
self.pub = self.advertise('~output', RoomLight, queue_size=1) | ||
self.luminance_threshold = rospy.get_param('~luminance_threshold', 50) | ||
self.transport_hint = rospy.get_param('~image_transport', 'raw') | ||
rospy.loginfo("Using transport {}".format(self.transport_hint)) | ||
|
||
def subscribe(self): | ||
if self.transport_hint == 'compressed': | ||
self.sub = rospy.Subscriber( | ||
'{}/compressed'.format(rospy.resolve_name('~input')), | ||
CompressedImage, self._image_cb, queue_size=1, buff_size=2**26) | ||
else: | ||
self.sub = rospy.Subscriber( | ||
'~input', Image, self._image_cb, queue_size=1, buff_size=2**26) | ||
|
||
def unsubscribe(self): | ||
self.sub.unregister() | ||
|
||
def _image_cb(self, msg): | ||
if self.transport_hint == 'compressed': | ||
encoding = msg.format.split(';')[0] | ||
else: | ||
encoding = msg.encoding | ||
if encoding == 'mono8': | ||
if self.transport_hint == 'compressed': | ||
np_arr = np.fromstring(msg.data, np.uint8) | ||
img = cv2.imdecode(np_arr, cv2.IMREAD_GRAYSCALE) | ||
else: | ||
img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='mono8') | ||
lum_img = img | ||
else: | ||
if self.transport_hint == 'compressed': | ||
np_arr = np.fromstring(msg.data, np.uint8) | ||
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) | ||
img = img[:, :, ::-1] | ||
else: | ||
img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8') | ||
# relative luminance calculation with RGB from Digital ITU BT.601 | ||
# relative luminance is not illuminance and not in Lux unit | ||
# lum = 0. 299 * R + 0.587 * G + 0.114 * B | ||
lum_img = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] \ | ||
+ 0.114 * img[:, :, 2] | ||
luminance = np.mean(lum_img) | ||
light_msg = RoomLight(header=msg.header) | ||
light_msg.light_on = luminance > self.luminance_threshold | ||
light_msg.relative_luminance = luminance | ||
self.pub.publish(light_msg) | ||
|
||
|
||
if __name__ == '__main__': | ||
rospy.init_node('check_room_light') | ||
app = CheckRoomLightNode() | ||
rospy.spin() |