OpenCVTexture again

I read about the error, described by Thomas Egi (discourse.panda3d.org/viewtopic … 2433#52433), but I was able to get the picture from camera:

tex = OpenCVTexture()
tex.setTexturesPower2(0)
assert tex.fromCamera(0)
cm = CardMaker("background-card")

cm.setFrame(-1, 1, 1, -1)

card = render2d.attachNewNode(cm.generate())
img = PNMImage()
tex.store(img)
tex2 = Texture("test")
tex2.load(img)

card.setTexture(tex2)

The problem is that I get only one frame. The image is updated only when initializing the camera. Even if I try to get an image in a loop, it remains the same. Can I manually force the update texture?

Which version of Panda3D are you using? I think this was fixed in either 1.6.1 or 1.6.2, but it got horribly broken again in 1.7.0.

I’m looking into a way to fix this - I may actually deprecate OpenCVTexture in favour of WebcamVideo (which is Windows-only right now).

I use version 1.7.0 on Ubuntu 9.10 from repository.
So is there no way to force update the texture?
This code (modified Thomas’s sample) is work, but it’s some ugly

from pandac.PandaModules import *
loadPrcFileData("", "auto-flip 1") #usualy the drawn texture lags a bit behind the calculted positions. this is a try to reduce the lag.
loadPrcFileData("editor-startup", "show-frame-rate-meter #t")

from direct.directbase import DirectStart
from direct.task import Task
from time import sleep
from direct.actor import Actor


#------use OpenCVTexture under linux---------- use WebcamVideo under windows------------

tex = OpenCVTexture()
#---------------------

tex.setTexturesPower2(0)
#if you want to know what assert is doing.. ask pro-rsoft. all i know is, it prevents an assertion error :D

assert tex.fromCamera(0)

#create a card which shows the image captured by the webcam.

cm = CardMaker("background-card")

cm.setFrame(-1, 1, 1, -1)

card = render2d.attachNewNode(cm.generate())
img = PNMImage()
tex.store(img)
tex2 = Texture("test")
tex2.load(img)

card.setTexture(tex2)
card2 = card.copyTo(render)
card2.setTexture(tex)

#set the rendering order manually to render the card-with the webcam-image behind the scene.

base.cam.node().getDisplayRegion(0).setSort(20)


#load a model to visualize the tracking

stickfigure = Actor.Actor("stickfigure-artoolkit", {"stand": "stickfigure-artoolkit-stand","hang":"stickfigure-artoolkit-hang","hold":"stickfigure-artoolkit-hold"})
stickfigure.reparentTo(render)
stickfigure.loop("stand")
stickfigure.setScale(0.2)
stickfigure.setColor(0,0,0)

#initialize artoolkit, base.cam is our camera ,
#the camera_para.dat is the configuration file for your camera. this one comes with the artoolkit installation.
#last paremeter is the size of the pattern in panda-units.

ar = ARToolKit.make(base.cam, "./camera_para.dat", 1)

#attach the model to a pattern so it updates the model's position relative to the camera each time we call analyze()

ar.attachPattern("./4x4_48.patt", stickfigure)

#updating the models positions each frame. and play the appropriate animation.

def updatePatterns(task):

  ar.analyze(tex, False)
  myvec=stickfigure.getRelativeVector(render,(0,0,1))
  myvec.normalize()
  rot=myvec[2]
  if rot > .6:
    if stickfigure.getCurrentAnim() != "stand":
        stickfigure.loop("stand")
  elif rot < -.3:
    if stickfigure.getCurrentAnim() != "hang":
        stickfigure.loop("hang")
  else:
    if stickfigure.getCurrentAnim() != "hold":
        stickfigure.loop("hold")    
  #tex.fromCamera(0)  
  img = PNMImage()
  tex.store(img)
  tex2 = Texture("test")
  tex2.load(img)
  card.setTexture(tex2,1)

  print myvec
  return Task.cont
  
  
sleep(1) #some webcams are quite slow to start up so we add some safety

taskMgr.add(updatePatterns, "update-patterns",-100)



run()

i use “card2” to force to update the texture and “card” to view result.
Without hack with card i see white field and without hack with card2 - texture is not updated.