Render object in world via webcam

Hey,
I am new to game development and panda3d. I want to render object in real world by opening webcam
in realtime. I tried to open webcam following this discussion. But unable to figure out how to render object in live webcam feed. If someone guides me through the steps i need to look for will be very helpful.

This works for me, although you might not need OpenCV like I do:

import cv2
import panda3d
from direct.showbase.ShowBase import ShowBase
panda3d.core.load_prc_file_data("", "show-frame-rate-meter #t")
panda3d.core.load_prc_file_data("", "sync-video #f")

base = ShowBase()

# generate a frame geometry to apply the camera texture to
cardmaker = panda3d.core.CardMaker("frame")
cardmaker.set_frame(-base.win.get_x_size(), base.win.get_x_size(), -base.win.get_y_size(), base.win.get_y_size())
frame = panda3d.core.NodePath(cardmaker.generate())
frame.set_scale(frame.get_scale()/ base.win.get_y_size())
frame.set_r(180)
frame.flatten_light() # apply scale
frame.reparent_to(aspect2d)

cv_camera = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
cv_camera_frame_texture = panda3d.core.Texture()
cv_camera_frame_texture.setup_2d_texture(640, 480, panda3d.core.Texture.T_unsigned_byte, panda3d.core.Texture.F_rgb8)
frame.set_texture(cv_camera_frame_texture, 1)

def update_usb_camera_frame(task):
	success_state, image = cv_camera.read()
	image = cv2.flip(image, 1)
	cv_camera_frame_texture.set_ram_image(image)
	return task.again
	
base.task_mgr.do_method_later(1000/60*0.001, update_usb_camera_frame, "update_usb_camera_frame")

base.run()

1 Like

Thanks @bodaf62109 for the code.
Can you please provide code for rendering and 3D object in live cam.
TIA

With all due respect, that’s something you should learn from the documentation and do on your own.
If you have a specific question on how to do it on the way, please ask, but “please provide code for rendering and 3D object” is in the “please write the Panda3D code for me” territory.
OpenCV is not part of Panda3D and making them work together is a bit of an advanced topic, but your last question isn’t:

https://docs.panda3d.org/1.10/python/programming/scene-graph/index
https://docs.panda3d.org/1.10/python/programming/models-and-actors/loading-models
https://docs.panda3d.org/1.10/python/programming/rendering-process/controlling-render-order

1 Like