@Gaurav_Gola, I got the same error as you when implementing @neoniv’s code. For a separate project, I integrated OpenCV with Panda3D. I was able to set the Texture in Panda3D to my webcam video using OpenCV and Panda3D together. Make sure you have OpenCV installed and try running the following code. It works for me! I’m using Ubuntu 18.04.
import cv2
from panda3d.core import Texture, CardMaker
from direct.showbase.ShowBase import ShowBase
# open webcam
cap = cv2.VideoCapture(0)
# Check if the webcam is opened correctly
if not cap.isOpened():
raise IOError("Cannot open webcam")
# use opencv to read from webcam
success = False
while not success:
success, frame = cap.read()
h, w, _ = frame.shape # accessing the width and height of the frame
# setup panda3d scripting env (render, taskMgr, camera etc)
base = ShowBase()
# set up a texture for (h by w) rgb image
tex = Texture()
tex.setup2dTexture(w, h, Texture.T_unsigned_byte,
Texture.F_rgb)
# set up a card to apply the numpy texture
cm = CardMaker('card')
card = render.attachNewNode(cm.generate())
WIDTHRATIO = 1
HEIGHTRATIO = h/w
CAMDISTANCE = 1.5
DEPTH = 1
# card is square, rescale to the original image aspect ratio
card.setScale(WIDTHRATIO, DEPTH, HEIGHTRATIO)
# bring it to center, put it in front of camera
card.setPos(-WIDTHRATIO/2, CAMDISTANCE, -HEIGHTRATIO/2)
def updateTex(task):
success, frame = cap.read()
if success:
# positive y goes down in openCV, so we must flip the y coordinates
flipped_frame = cv2.flip(frame, -1)
# overwriting the memory with new frame
tex.setRamImage(flipped_frame)
card.setTexture(tex) # now apply it to the card
return task.cont
taskMgr.add(updateTex, 'video frame update')
base.run()
cap.release()