Reading video data from a webcam

Using a live camera feed opens new and interesting possibilities for video games: Motion controlled games require players to get off the couch and use their whole bodies. Or why not add a video chat feature to enhance the presentation of player-to-player communication? Seeing your opponents face could even become a gameplay feature, for instance, an online poker game could greatly benefit from this. What about a game where players are taking turns in making stupid faces while their opponent tries not to smile or laugh at it? Detection of facial expressions works quite well and in the case of the last example, will detect even the slightest smirk!

Developing game ideas for camera-based video games is very interesting and great fun, especially when your game involves doing funny faces or flapping your arms frantically. But behind the pleasure of these games, there's always a technical side to it, which you will get to know over the course of this recipe. After you worked your way through it, you will be able to set up Panda3D to use a live video stream from a webcam and display it in your application.

Getting ready

To be able to work through this recipe, you need two things:

  • First, a new project skeleton as described in Chapter 1.
  • Second, you need to have a webcam connected to your computer.

How to do it...

Panda3D can read a video stream from a webcam if you follow these steps:

  1. Open Application.py and replace its contents with the code that follows:
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import *
    from panda3d.vision import *
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    for i in range(WebcamVideo.getNumOptions()):
    print WebcamVideo.getOption(i)
    if WebcamVideo.getNumOptions() > 0:
    opt = WebcamVideo.getOption(0)
    self.cursor = opt.open()
    self.tex = Texture()
    self.cursor.setupTexture(self.tex)
    cm = CardMaker("plane")
    cm.setFrame(-1, 1, -1, 1)
    plane = render2d.attachNewNode(cm.generate())
    plane.setTexture(self.tex)
    scaleX = float(self.cursor.sizeX()) / float(self.tex.getXSize())
    video datareading, from webcamscaleY = float(self.cursor.sizeY()) / float(self.tex.getYSize())
    plane.setTexScale(TextureStage.getDefault(), Vec2(scaleX, scaleY))
    taskMgr.add(self.update, "update video")
    def update(self, task):
    if self.cursor.ready():
    self.cursor.fetchIntoTexture(0, self.tex, 0)
    return task.cont
    
  2. Start the application. Your webcam should turn on and the live video feed should be displayed in the program window:
How to do it...

How it works...

If you already happened to have read the recipe on recording and using audio data found in this chapter, the code might seem very familiar—you choose an option, which is a combination of input device and recording resolution, and open a cursor to it that allows controlling and accessing the input data.

Additionally, you need to provide and setup a Texture object that will hold the recorded video images. Because Panda3D tries to use textures with edge lengths that are a power of two, you need to rescale the texture coordinates of the object the texture will be applied to. If you do not do this, you will see a stretched image that is surrounded by black bars, because your 640x480 video stream, for example, will be fitted into a texture that is 1024x512.

There's more...

This is a very basic sample. If you need to process the image data or do some analysis on it to detect motion, faces or colors, you should always think about using shaders, as they are the most efficient tool for the job. Take a look at Chapter 4, Scene Effects and Shaders if you want to know how to write and apply shader programs!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset