Skip to content

Instantly share code, notes, and snippets.

@jarmitage
Created May 8, 2023 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarmitage/cac412398b8452a830254a2935270e7c to your computer and use it in GitHub Desktop.
Save jarmitage/cac412398b8452a830254a2935270e7c to your computer and use it in GitHub Desktop.
Taichi OpenCV VideoCapture Webcam GGUI Window
import taichi as ti
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
ti.init(arch=ti.vulkan)
x,y = 1920,1080
px = ti.Vector.field(3, dtype=ti.f32, shape=(x,y))
@ti.kernel
def frame2field(f: ti.types.ndarray(dtype=ti.f32, ndim=3)):
for i, j in ti.ndrange(x, y):
_i, _j = y-j,x-i
px[i,j] = [f[_i,_j,2]/255,f[_i,_j,1]/255,f[_i,_j,0]/255]
window = ti.ui.Window("cv", (x, y), fps_limit=120)
canvas = window.get_canvas()
while window.running:
ret, frame = cap.read()
frame2field(frame)
canvas.set_image(px)
window.show()
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment