proba obslugi kamery w julia i python

This commit is contained in:
zymon 2024-01-24 09:34:24 +01:00
parent b6909997cd
commit e5dc2355c7
4 changed files with 144 additions and 11 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
.CondaPkg
Manifest.toml
*.png
*.jpg

79
cam_test.jl Normal file
View file

@ -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

60
cam_test_python.jl Normal file
View file

@ -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()

14
rt.jl
View file

@ -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);