From e5dc2355c7334ee520f44db1b732e6f7d27b2780 Mon Sep 17 00:00:00 2001 From: zymon Date: Wed, 24 Jan 2024 09:34:24 +0100 Subject: [PATCH] proba obslugi kamery w julia i python --- .gitignore | 2 ++ cam_test.jl | 79 ++++++++++++++++++++++++++++++++++++++++++++++ cam_test_python.jl | 60 +++++++++++++++++++++++++++++++++++ rt.jl | 14 ++------ 4 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 cam_test.jl create mode 100644 cam_test_python.jl diff --git a/.gitignore b/.gitignore index 8741257..7541a91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .CondaPkg Manifest.toml +*.png +*.jpg diff --git a/cam_test.jl b/cam_test.jl new file mode 100644 index 0000000..e523a02 --- /dev/null +++ b/cam_test.jl @@ -0,0 +1,79 @@ +using CondaPkg +using PythonCall +using Images +using ImageView +import VideoIO +using GLMakie + + +## + +VideoIO.TestVideos.available() +videoFile = VideoIO.TestVideos.testvideo("black_hole") +VideoIO.playvideo(videoFile) +close(videoFile) + +## + +cam = VideoIO.opencamera("/dev/video0") +img = read(cam) +# @show VideoIO.framerate(cam) +# cam |> close + +try + img = read(cam) + obs_img = GLMakie.Observable(GLMakie.rotr90(img)) + scene = GLMakie.Scene(camera=GLMakie.campixel!, resolution=reverse(size(img))) + GLMakie.image!(scene, obs_img) + display(scene) + fps = VideoIO.framerate(cam) + while GLMakie.isopen(scene) + img = read(cam) + obs_img[] = GLMakie.rotr90(img) + sleep(1 / fps) + end +finally + close(cam) +end + +## + +cam = VideoIO.opencamera("/dev/video0") +imgstack = [] +secrec = 1 +frames = 30*secrec +framecounter = 0 +fps = VideoIO.framerate(cam) + +try + # read the first image in start + img = read(cam) + obs_img = GLMakie.Observable(GLMakie.rotr90(img)) + scene = GLMakie.Scene(camera=GLMakie.campixel!, size=reverse(size(img))) + # set the first image on screen of Makie window + GLMakie.image!(scene, obs_img) + # display the GLMakie scene + display(scene) + + # until the window is open + i = 0 + while GLMakie.isopen(scene) + # read from camera + img = read(cam) + # we can process our image here + # you should comment below 4 lines if don't wanna save + # img = process(img) + # push!(imgstack, img) + # framecounter = framecounter + 1 + # if(framecounter == frames) break end + + # update image in the window + # obs_img[] = GLMakie.rotr90(img) + # delay to achieve right framerate + # also when you don't delay, it ends up hanging everything as + # it's a while loop with no control and huge number of reads. + sleep(1e-6) + end +finally + close(cam) +end diff --git a/cam_test_python.jl b/cam_test_python.jl new file mode 100644 index 0000000..5561e1e --- /dev/null +++ b/cam_test_python.jl @@ -0,0 +1,60 @@ +using CondaPkg +using PythonCall +using Images +using ImageView +import VideoIO +using GLMakie + +cv2 = pyimport("cv2") + +### + +py2a(frame) = pyconvert(Array{Float32, 3}, frame) / 255; +py2a(frame) = PyArray(frame) +a2rgb(frame) = RGB{N0f8}.((@view frame[:, :, 3])/255 .* 1, (@view frame[:, :, 2])/255 .* 1, (@view frame[:, :, 1])/255 .* 1) +py2rgb(frame) = frame |> py2a |> a2rgb + +blueth(pixel, th) = pixel.b > th ? 1.0f0 : 0.0f0 +## + + +vid = cv2.VideoCapture(0) +vid.set(3,1920) +vid.set(4,1080) +_, img = vid.read() +a = img |> PyArray; +frame = img |> py2rgb |> GLMakie.rotr90; +image(frame) +vid.release() + +## + +vid = cv2.VideoCapture(0) +vid.set(3,1920) +vid.set(4,1080) +try + _, frame = vid.read() + frame = py2rgb(frame) + obs_img = GLMakie.Observable(GLMakie.rotr90(frame)) + scene = GLMakie.Scene(camera=GLMakie.campixel!, size=reverse(size(frame))) + GLMakie.image!(scene, obs_img) + display(scene) + while GLMakie.isopen(scene) + _, img = vid.read() + frame = img |> py2rgb |> GLMakie.rotr90 + obs_img[] = frame #blueth.(frame, 0.97) + sleep(1/9000) + end +finally + vid.release() +end + +## + +vid = cv2.VideoCapture(0) +for i = 1:100 + ret, frame = vid.read() + cv2.imshow("frame", frame) + sleep(1/60) +end +vid.release() diff --git a/rt.jl b/rt.jl index 4df0100..43aade5 100644 --- a/rt.jl +++ b/rt.jl @@ -24,21 +24,13 @@ push!(world, SimpleRayTracer.Sphere(Vec3(-4.0, 0.0, 0.0), 2.0, RGB(1, 0, 0))) push!(world, SimpleRayTracer.Sphere(Vec3( 4.0, 0.0, 0.0), 2.0, RGB(0, 1, 0))) push!(world, SimpleRayTracer.Sphere(Vec3( 0.0, 0.0, 3.0), 2.0, RGB(0, 0, 1))) - -camera = SimpleRayTracer.OrthogonalCamera( - Vec3(0.0, 0.0, -5.0), - (20.0, 10.0), - RotXYZ(0.0, 0.0, 0.0), -) - camera = SimpleRayTracer.PinHoleCamera( - Vec3(0.0, 1.0, -8.0), + Vec3(0.0, 4.0, -8.0), Vec3(0.0, 0.0, 0.0), Vec3(0.0, 1.0, 0.0), - 1.0, - (4.0, 2.0), + 0.005, + (0.02, 0.01), ) -camera.base resolution = (512, 256); img = SimpleRayTracer.render(world, camera, resolution);