Created
May 8, 2023 22:08
-
-
Save jarmitage/cac412398b8452a830254a2935270e7c to your computer and use it in GitHub Desktop.
Taichi OpenCV VideoCapture Webcam GGUI Window
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
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