Page 1 of 1

mmal tutorial SyntaxError

Posted: Thu Mar 08, 2018 5:58 am
by townkat
hi all,
i have a pi 3 and a camera v2.1 working fine with standard camera = picamera.PiCamera() object.

I am experimenting now with mmal but i get "SyntaxError: invalid syntax"
when i try:
encoder.inputs[0].enable(lamdba port, buf: True)
which is first line from example from: ... -mmal-tour
16.1.8. File Input (JPEG encoding)

i can get the output.tell() 921600 result , from 16.1.6. File Output (RGB capture) as a checkpoint

i also did apt update/upgrade rpi-update

1. please help as i am really interested into seeing this hardware jpg encoding working :)

(2. also why isn't jpg encoding available with standard picamera object? i think it deserve its place in standard library as it should be really useful..)

3. and not to open another topic just for this i'l also add a slightly unrelated question: are there any chances to get full 8mp resolution h264 encoding even if it is low fps in the future ?
(even 1fps would be better than nothing, even if extrapolating would estimate a 7.5fps maybe?:))
i think it would be really cool don't you think? ..not to talk about h265.. :)

thank you all

Re: mmal tutorial SyntaxError

Posted: Fri Mar 09, 2018 5:30 am
by townkat
1. I answered myself, typo in documentation: lamdba=lambda .

I now have other questions, i'll just add another one..:

When capturing with mmal if i do not do a time.sleep(x) before connecting the callback function and capturing, the camera object become corrupted and cannot be used untill reboot. How does the picamera object do this without useless waitings ? i am thinking at the threading events like in callback function, but how? as here there is no calling ..

Also how to connect mmal splitter(or decoder)? if i try i get: 'property' object not suscriptable...If this would be solved i think the complexity of capturing directly with mmalcamera is not much bigger with higher posibilities..

Here is my code if anyone needs a sample of capturing with mmalcamera object. I also have it working with jpg encoder, and maybe also with resizer (not sure, working on it..), if anyone need them just let me know.

Code: Select all

from io import BytesIO
from picamera import mmal, mmalobj as mo

camera = mo.MMALCamera()

camera.outputs[2].format = mmal.MMAL_ENCODING_RGB24
camera.outputs[2].framesize = (640, 480)

from io import BytesIO
rgbfile = BytesIO()

from threading import Event
finished = Event()

def savetorgb(port, buf):
    if (buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END):
        return True
    return False

time.sleep(1) #without this the picamera object become unusable untill reboot
camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = True
if (not finished.wait(10)):
camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = False
print('rgbfile size is ' + str(rgbfile.tell()))

Re: mmal tutorial SyntaxError

Posted: Sun Mar 11, 2018 3:27 am
by townkat
Just a small update, i managed to get all the objects i was interested in (camera, encoder, splitter, resizer etc.) connected and working.
The fix to the delay needing was simply to change the port number to "1" ex.: camera.outputs[1].params[picamera.mmal.MMAL_PARAMETER_CAPTURE] = True
, then it works without the time.sleep(1) line,

A new problem appeared thought, it only works up to 1920x1080 resolution, if i try to set higher, even in the simple example in the above post then there will be no output, output file object 0 bytes.
Now i am studying picamera.picamera object source as it manage to work at higher than 1920x1080 resolution..but i am not very optimistic unfortunately.. (already almost 2 days into this problem..)
..i just wish the documentation would help me more..

If anyone wonders, my purpose which picamera object was not able to satisfy is to capture exactly the same picture at multiple resolution (1 full sensor resolution, and another smaller thumb, but may be bigger than exif limits), from video port (so not post processing). Also the thumb should be a separate file.

Also as i am sure others will get into this python error and i found nothing on google about it, the error: 'property' object not subscriptable , was caused by forgetting to use ( ) parenthesis.
for ex: encoder = picamera.mmalobj.MMALImageEncoder
instead of
encoder = picamera.mmalobj.MMALImageEncoder()
then encoder.inputs[0] = ... would return 'property' object not subscriptable..
i am sure there will be others.. :)

Re: mmal tutorial SyntaxError

Posted: Thu Jan 02, 2020 9:26 am
by DJG66
Bro, can you pst your code for image capture with encoding?
I am unable to connect the encoder

Re: mmal tutorial SyntaxError

Posted: Fri Jan 03, 2020 1:27 am
by DougieLawson
Don't hold your breath waiting. The OP hasn't logged on to the forum since Sun 15 Apr 2018 @ 18:08.

Re: mmal tutorial SyntaxError

Posted: Sat Mar 21, 2020 12:15 am
by townkat
Just to make Mr. Dougie wrong, or actually he was right, you could't hold your breat till now.
Just kiddin', it's a pleasure and honor to be colegue with you here.

I cannot test it now, it's from a test file i kept from when i was working on it, it should work.

Code: Select all

import time, picamera
import threading, datetime, os
import timeit 
import io

#from picamera import picamera.mmal, mmalobj as mo

camera = picamera.mmalobj.MMALCamera()

cc = camera.control.params[picamera.mmal.MMAL_PARAMETER_CAMERA_CONFIG]
cc.max_stills_w = 3280
cc.max_stills_h = 2464
cc.stills_yuv422 = 0
cc.one_shot_stills = 1
cc.max_preview_video_w = 3280
cc.max_preview_video_h = 2464
cc.num_preview_video_frames = max(3, 30 // 10)
cc.stills_capture_circular_buffer_height = 0
cc.fast_preview_resume = 0
cc.use_stc_timestamp = picamera.mmal.MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
camera.control.params[picamera.mmal.MMAL_PARAMETER_CAMERA_CONFIG] = cc

#camera.outputs[1].format = picamera.mmal.MMAL_ENCODING_RGB24
camera.outputs[1].framesize = (3280, 2464)
camera.outputs[1].framerate = -1

splitter = picamera.mmalobj.MMALSplitter()

resizer = picamera.mmalobj.MMALResizer()
resizer.outputs[0].format = picamera.mmal.MMAL_ENCODING_RGB24
resizer.outputs[0].framesize = (320, 240)

encoder = picamera.mmalobj.MMALImageEncoder()
encoder.outputs[0].format = picamera.mmal.MMAL_ENCODING_JPEG
encoder.outputs[0].params[picamera.mmal.MMAL_PARAMETER_JPEG_Q_FACTOR] = 20

nullsink = picamera.mmalobj.MMALNullSink()

def savetojpg(port, buf):
    if buf.flags & picamera.mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END:
        return True
    return False

fileo = io.BytesIO()
finished = threading.Event()

camera.outputs[1].params[picamera.mmal.MMAL_PARAMETER_CAPTURE] = True
if not finished.wait(10):
    raise Exception('capture timed out')
camera.outputs[1].params[picamera.mmal.MMAL_PARAMETER_CAPTURE] = False
print('capture time '+ str(end-start))
with open('out.jpg', 'wb+') as file: 

Re: mmal tutorial SyntaxError

Posted: Sat Mar 21, 2020 10:02 am
by DougieLawson
Welcome back. You got a bunch of guys about to turn blue.

With apologies to Charlie Duke, Apollo 11 Capcom.

Re: mmal tutorial SyntaxError

Posted: Sat Mar 21, 2020 2:04 pm
by townkat
DougieLawson wrote: Welcome back. You got a bunch of guys about to turn blue.

With apologies to Charlie Duke, Apollo 11 Capcom.
:D :D