Camera sourcing doesn't stop.

Hey all in my painting app there’s a option to take a picture using the back camera. Camera input requires about 30 fps itself but then after camera sourcing the camera source continues drawing input from the camera and I still remain at 30fps without even showing the camera. I tried cameraSource(0) which seemed to work before, but now it doesn’t… Does anyone know how to stop camera feed after it has started?

@Luatee I tried some camera code that I have. I added FPS to one of them and started the camera. My FPS remained at 60 FPS as I viewed the live images. I switched from back to front with no change in speed. I couldn’t find anyway to turn off the cameraSource either, but still at 60 FPS it didn’t seem to matter. Are you saying your FPS drops to 30 when you use your camera. Here’s the code I used to check FPS.


function setup()
    parameter.action("Front Camera",function() cameraSource(CAMERA_FRONT) end)
    parameter.action("Back Camera",function() cameraSource(CAMERA_BACK) end)
    parameter.action("Take picture",function() img=image(CAMERA) end)
    parameter.action("Clear",function() img=nil end)
    cameraSource(CAMERA_BACK)
    tot=0
    count=0
end

function draw()
    background(40, 40, 50)
    if img~=nil then
        sprite(img,WIDTH/2,HEIGHT/2)
    else
        sprite(CAMERA,WIDTH/2,HEIGHT/2)  
    end  
    fill(255)
    tot = tot + 1/DeltaTime
    count = count + 1
    text(string.format("%d",tot/count),WIDTH/2,HEIGHT-100)   
end

@Luatee, bit of an amateurish guess, but are you saving the camera image to a local image variable or a global? If local, then collectgarbage() when leaving the function might clear it?

@dave1707 I will get the code for you in a minute and put it up.
@time_trial I do need to make a garbage collection somewhere but it’s not related to this I don’t think. I’m thinking when the best time to collect garbage is.

Drawing (the slow down afaik)

if camalpha > 0 and tool == "camera" then
        camalpha = camalpha - 5
        pushStyle()
            fill(255,camalpha)
            rect(0,0,Width,Height)
            popStyle()
    elseif camalpha < 0 then
        camalpha = 0
    end
    
    if tool == "camera" and cam then
        pushStyle()
        spriteMode(CENTER)
        local w,h = spriteSize(CAMERA)
        sprite(CAMERA,screenpos.x,screenpos.y,(w/screensize)/1.4,(h/screensize)/1.4)
        popStyle()
    end
    if tool ~= "camera" and cam then
        cameraSource(0)
        cam = false
    end

It’s a bit odd to me for this to happen…
Edit: garbage collection does nothing, text tool seems to add to the slow down, the only tools I can identify with this problem.

@Luatee Did you run my example above to see what the FPS is for you. I’m not seeing what’s going on with your code to slow things down.

Me neither thats the problem I’m having! But the reason for the drop isn’t the camera actually now I’ve used your test, it’s the fact I’m using setContext instead of drawing straight, but I would rather use setContext as that’s what the engine for the drawing is based around, for everything.

Turns out putting the drawing part in its own function stopped the slow down… How does that work?

@Luatee: Is the original function very large?

Well the draw function is about 1000 lines?

@Luatee:
I was thinking there was maybe a register exhaustion issue, but looking at the Lua parser and VM source, that doesn’t seem possible (if you run out of registers/localvars you just get an error, I think). I suppose there could be caching issue, though that seems unlikely.

@daveedvdv I think a bit of it might have had to do with the lack of garbage collection which is now fixed, but it still occurred afterwards until I put it in a function.