Displaying and accessing camera pixels

I’ve been trying to get this to work all night but I’m not got very far. I’d be very grateful for any help!

I want to display the output from a webcam on the screen, but I would also like to be able to read the values of individual camera pixels in order to do some rudimentary image processing.

I’m able to do this first bit, i.e. get the webcam image displayed on the screen by doing this:

if WebcamVideo.getNumOptions() > 0:
    opt = WebcamVideo.getOption(8)
    self.cursor = opt.open()
    self.cameratexture = Texture()
    cm = CardMaker("plane")
    cm.setFrame(-0.5, 0.5, -0.5, 0.5)
    plane = render2d.attachNewNode(cm.generate())
    scaleX = float(self.cursor.sizeX())/float(self.cameratexture.getXSize())
    scaleY = float(self.cursor.sizeY()) / float(self.cameratexture.getYSize())
    plane.setTexScale(TextureStage.getDefault(),Vec2(scaleX, scaleY))
    self.peeker = self.cameratexture.peek()

However when I try to get access to the camera pixels by doing this:


I get the following error:

AssertionError: do_has_ram_mipmap_image(n)

If anyone could give me some ideas on how to both display the webcam output on screen and also be able to query the values of pixels in the texture I’d be extremely grateful!

Hmm, I believe the RAM image should be available. But perhaps it’s just not present the first frame. In that case, you should check before you attempt to store the image:

if not self.cameratexture.hasRamImage():
   print "skipping"
   print "saving"

Then, it will skip the first frame, but correctly save the image on subsequent frames.

Note that saving the image to a PNMImage will be kind of slow.