Created
July 16, 2015 08:38
-
-
Save i-namekawa/fceb859e062f34299f38 to your computer and use it in GitHub Desktop.
Use Numpy array for panda3d texture, video player example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import os | |
import cv2 | |
import numpy as np | |
from panda3d.core import Texture, CardMaker | |
from direct.showbase.ShowBase import ShowBase | |
fp = 'path/to/video.avi' | |
# use opencv to read from avi | |
if os.path.exists(fp): | |
cap = cv2.VideoCapture(fp) | |
success, frame = cap.read() | |
h, w, depth = frame.shape # source is RGB video but will be used as gray scale image | |
else: # if no input, generate random noise image | |
h, w = 480, 640 | |
cap = None | |
# setup panda3d scripting env (render, taskMgr, camera etc) | |
base = ShowBase() | |
# set up a texture for (h by w) 8 bit gray scale image | |
tex = Texture() | |
tex.setup2dTexture(h, w, Texture.T_unsigned_byte, Texture.F_luminance) | |
# set up a card to apply the numpy texture | |
cm = CardMaker('card') | |
card = render.attachNewNode(cm.generate()) | |
card.setPos(-0.5, 1.5, -w/h/2) # bring it to center, put it in front of camera | |
card.setScale(1,1,w/h) # card is square, rescale to the original image aspect | |
def updateTex(task): | |
if cap: # video input | |
success, frame = cap.read() | |
else: # random noise | |
frame = np.random.randint(0,255,(h,w,3)).astype(np.uint8) | |
buf = frame[:,:,0].T.tostring() # slice RGB to gray scale, transpose 90 degree, convert to text buffer | |
tex.setRamImage(buf) # overwriting the memory with new buffer | |
card.setTexture(tex) # now apply it to the card | |
return task.cont | |
taskMgr.add(updateTex, 'video frame update') | |
base.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment