martk20
Posts: 36
Joined: Fri Jan 27, 2012 10:42 am

Changing camera zoom/crop dynamically

Wed Jan 16, 2019 6:41 pm

Is it possible to change the Pi camera zoom dynamically? I have written a python script which changes the following:-
camera.zoom = (a, b, c, d,); When I attempt to make a change to the zoom, 'a' for instance the camera module stops with lots of errors.
I am using the camera as a cctv using the pistreaming application which streams the video to a web server. I would like to be able to change the zoom from the receiving device. I can make the variable increment and decrement from the webpage on the receiving device.

ethanol100
Posts: 587
Joined: Wed Oct 02, 2013 12:28 pm

Re: Changing camera zoom/crop dynamically

Wed Jan 16, 2019 8:04 pm

Did you make sure that a+c and b+d are between 0 and 1?

What errors do you get?

martk20
Posts: 36
Joined: Fri Jan 27, 2012 10:42 am

Re: Changing camera zoom/crop dynamically

Wed Jan 16, 2019 9:40 pm

Thank you for your rapid response ethanol100. The variables are definitely limited to between 0 and 1.

Here is a copy from the script when it terminates. Frankly I do not understand them.
Traceback (most recent call last):
File "./server.py", line 237, in <module>
main()
File "./server.py", line 218, in main
camera.zoom = (a, b, c, d,);
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 3415, in _set_zoom
self._camera.control.params[mmal.MMAL_PARAMETER_INPUT_CROP] = mp
File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1490, in __setitem__
prefix="Failed to set parameter %d to %r" % (key, value))
File "/usr/lib/python3/dist-packages/picamera/exc.py", line 184, in mmal_check
raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to set parameter 65573 to <picamera.mmal.MMAL_PARAMETER_INPUT_CROP_T object at 0x7614f350>: Argument is invalid

ethanol100
Posts: 587
Joined: Wed Oct 02, 2013 12:28 pm

Re: Changing camera zoom/crop dynamically

Wed Jan 16, 2019 11:03 pm

Can you try this small example?

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.start_preview()
    camera.start_recording('my_video.h264')
    time.sleep(0.5)
    for x in range(0,50):
        camera.zoom=(x/100.,x/100.,0.5,0.5)
        time.sleep(0.1)
    for x in range(1,100):
        camera.zoom=(0,0,x/100.,x/100.)
        time.sleep(0.1)
    camera.stop_recording()
Does this create a recording panning and zooming?

Which version of picamera are you using? What type are a,b,c,d? I think they should be floats.

User avatar
HermannSW
Posts: 1657
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Changing camera zoom/crop dynamically

Thu Jan 17, 2019 12:54 am

ethanol100 wrote:
Wed Jan 16, 2019 11:03 pm
Can you try this small example?
thanks for that picamera script !!
I never before used picamera because of my interest in Raspberry camera very high framerate capturing (>200fps, up to 750/1007 fps for v1/v2 camera) and picamera topics discussed in this forum often handle of "picamera framerate is too low".
Very impressive script -- changed my view on picamera ...

I just did record execution of the picamera script. Because Pi 3B+ with v2 camera is not connected to HDMI monitor right now, I used UV4L WebbRTC recording feature the very first time. I had to click "start recordig", then executed the picamera script in ssh session, and finally pressed "stop recording" after script finished. Result is a (2588KB) webm video that runs happily with totem video player, or in the browser (I tested Chrome and Firefox):
https://stamm-wilbrandt.de/en/forum/9bd ... 9b04a.webm
or
https://www.youtube.com/watch?v=4QRnchB ... e=youtu.be
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

martk20
Posts: 36
Joined: Fri Jan 27, 2012 10:42 am

Re: Changing camera zoom/crop dynamically

Thu Jan 17, 2019 12:20 pm

Thanks again for your help ethanol100
I have tried the script you supplied and it works fine. I have also taken the little for loop into my server script and it works there too. Now I am confused.

I didn't write the server script that would be way beyond my ability. I have just hacked it to do as I require

ethanol100
Posts: 587
Joined: Wed Oct 02, 2013 12:28 pm

Re: Changing camera zoom/crop dynamically

Thu Jan 17, 2019 12:53 pm

I would try to print some information about a,b,c,d in your program, before setting the zoom. Something like:

Code: Select all

print("Type of a=",type(a), "contains the value", a)
print("Type of b=",type(b), "contains the value", b)
print("Type of c=",type(c), "contains the value", c)
print("Type of d=",type(c), "contains the value", d)
The output should look similar to:
Type of a= <class 'float'> contains the value 0.5

If its not float or the values are not what you expect, you know where to look next.

Return to “Camera board”