Changeset 665 for trunk/software/rb/vision/gstcam.py
- Timestamp:
- 02/11/10 01:33:02 (2 years ago)
- Files:
-
- 1 modified
-
trunk/software/rb/vision/gstcam.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/rb/vision/gstcam.py
r629 r665 16 16 import time 17 17 import logging 18 from thread import start_new_thread 18 from rb.vision._camera import ipl_image_from_rgb_buffer, save_image 19 19 20 import gst 20 21 21 from rb.vision._camera import ipl_image_from_rgb_string, save_image22 22 23 23 … … 38 38 'appsink name=out max_buffers=1 drop=1' 39 39 ) % source 40 import pdb; pdb.set_trace()40 logger.info('Launching pipeline %r', launch_string) 41 41 self.pipeline = gst.parse_launch(launch_string) 42 42 self.appsink = self.pipeline.get_by_name('out') 43 assert self.appsink 43 44 44 45 bus = self.pipeline.get_bus() … … 46 47 bus.connect('message', self.message_cb) 47 48 48 s tart_new_thread(self._main, ())49 self.pipeline.set_state(gst.STATE_PLAYING) 49 50 50 def _main(self):51 '''52 Main loop: pull images from the buffer and dispatch them.53 '''54 self.pipeline.set_state(gst.STATE_PLAYING)51 def get_last_image(self): 52 buffer = self.appsink.get_last_buffer() 53 if buffer is None: 54 return None 55 return gst_buffer_to_ipl_image(buffer) 55 56 56 while self.running:57 # TODO: Detect EOS (end of stream)58 # TODO: Make this non-blocking59 buffer = self.appsink.emit('pull-buffer')60 if buffer is None:61 logger.error("Pulled None buffer")62 time.sleep(0.1)63 continue64 struct = buffer.caps[0]65 size = (struct['width'], struct['height'])66 image = ipl_image_from_rgb_string(buffer, size)67 if self.controller:68 self.controller.send('camera_image', (self.source, image))69 logger.debug("Captured image")70 else:71 fn = "cap%0.2f.png" % time.time()72 print "Got image; saving to", fn73 save_image(fn, image)74 75 76 57 def message_cb(self, bus, message): 77 58 if message.type == gst.MESSAGE_STATE_CHANGED: … … 82 63 self.pipeline.set_state(gst.STATE_NULL) # Stop playback 83 64 65 66 def gst_buffer_to_ipl_image(buffer): 67 struct = buffer.caps[0] 68 size = (struct['width'], struct['height']) 69 return ipl_image_from_rgb_buffer(buffer, size) 70 71 84 72 if __name__ == '__main__': 85 import sys, gtk 73 import sys, gtk, gobject 86 74 logging.basicConfig(level=logging.DEBUG) 87 75 try: … … 89 77 except IndexError: 90 78 source = 'v4l2src' 91 gtk.gdk.threads_init()92 79 cam = GstCamera(None, source) 80 81 def save_pic(): 82 ipl_image = cam.get_last_image() 83 if ipl_image is None: 84 print "Got None" 85 else: 86 fn = "cap%0.4f.png" % time.time() 87 print "Got image, saving to", fn 88 save_image(fn, ipl_image) 89 return True 90 91 gobject.timeout_add(1000, save_pic) 93 92 gtk.main() 94 93

