99import eagerx .core .register as register
1010from eagerx .utils .utils import Msg
1111from eagerx .core .entities import EngineNode
12- from eagerx .core .specs import NodeSpec , ObjectSpec
12+ from eagerx .core .specs import NodeSpec
1313
1414
1515class CameraRender (EngineNode ):
@@ -22,6 +22,7 @@ def make(
2222 color : str = "cyan" ,
2323 shape : Optional [List [int ]] = None ,
2424 camera_idx : int = 0 ,
25+ always_render : bool = False ,
2526 ) -> NodeSpec :
2627 """CameraRender spec"""
2728 spec = cls .get_specification ()
@@ -33,18 +34,24 @@ def make(
3334 # Modify custom node params
3435 spec .config .shape = shape if isinstance (shape , list ) else [480 , 480 ]
3536 spec .config .camera_idx = camera_idx
37+ spec .config .always_render = always_render
3638
3739 # Set image space
3840 spec .outputs .image .space .update (low = 0 , high = 255 , shape = [spec .config .shape [0 ], spec .config .shape [1 ], 3 ])
3941 return spec
4042
41- def initialize (self , spec : NodeSpec , object_spec : ObjectSpec , simulator : Any ):
43+ def initialize (self , spec : NodeSpec , simulator : Any ):
44+ self .always_render = spec .config .always_render
4245 self .cam = None
4346 self .height , self .width = spec .config .shape
4447 self .camera_idx = spec .config .camera_idx
4548 self .render_toggle = False
4649 self .sub_toggle = self .backend .Subscriber ("%s/env/render/toggle" % self .ns , "bool" , self ._set_render_toggle )
4750
51+ # initialize camera if always rendering
52+ if self .always_render :
53+ self ._init_cam ()
54+
4855 @register .states ()
4956 def reset (self ):
5057 # This sensor is stateless (in contrast to e.g. a Kalman filter).
@@ -53,7 +60,7 @@ def reset(self):
5360 @register .inputs (tick = Space (shape = (), dtype = "int64" )) # Dummy, because a node must have at least one input.
5461 @register .outputs (image = Space (dtype = "uint8" ))
5562 def callback (self , t_n : float , tick : Optional [Msg ] = None ):
56- if self .render_toggle :
63+ if self .render_toggle or self . always_render :
5764 self .cam : cv2 .VideoCapture
5865 ret , cv_img = self .cam .read ()
5966 if ret :
@@ -69,17 +76,22 @@ def callback(self, t_n: float, tick: Optional[Msg] = None):
6976 img = np .zeros ((self .height , self .width , 3 ), dtype = "uint8" )
7077 return dict (image = img )
7178
72- def _set_render_toggle (self , msg ):
73- if msg :
74- self .backend .logdebug ("[%s] START RENDERING!" % self .name )
79+ def _init_cam (self ):
80+ if self .cam is None :
7581 self .cam = cv2 .VideoCapture (self .camera_idx )
7682 self .cam .set (cv2 .CAP_PROP_FRAME_WIDTH , self .width )
7783 self .cam .set (cv2 .CAP_PROP_FRAME_HEIGHT , self .height )
78- else :
79- self .backend .logdebug ("[%s] STOPPED RENDERING!" % self .name )
80- if self .cam is not None :
81- self .cam .release ()
82- self .cam = None
84+
85+ def _set_render_toggle (self , msg ):
86+ if not self .always_render :
87+ if msg :
88+ self .backend .logdebug ("[%s] START RENDERING!" % self .name )
89+ self ._init_cam ()
90+ else :
91+ self .backend .logdebug ("[%s] STOPPED RENDERING!" % self .name )
92+ if self .cam is not None :
93+ self .cam .release ()
94+ self .cam = None
8395 self .render_toggle = msg
8496
8597 def shutdown (self ):
0 commit comments