townkat
Posts: 7
Joined: Thu Mar 08, 2018 5:35 am

mmal tutorial SyntaxError

Thu Mar 08, 2018 5:58 am

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:
https://picamera.readthedocs.io/en/rele ... -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

townkat
Posts: 7
Joined: Thu Mar 08, 2018 5:35 am

Re: mmal tutorial SyntaxError

Fri Mar 09, 2018 5:30 am

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)
camera.outputs[2].commit()

from io import BytesIO
rgbfile = BytesIO()

from threading import Event
finished = Event()
finished.clear()

def savetorgb(port, buf):
    rgbfile.write(buf.data)
    if (buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END):
        finished.set()
        return True
    return False

camera.outputs[2].enable(savetorgb)
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)):
    print('nope')
camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = False
camera.outputs[2].disable()
print('rgbfile size is ' + str(rgbfile.tell()))

townkat
Posts: 7
Joined: Thu Mar 08, 2018 5:35 am

Re: mmal tutorial SyntaxError

Sun Mar 11, 2018 3:27 am

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.. :)

DJG66
Posts: 1
Joined: Thu Jan 02, 2020 9:24 am

Re: mmal tutorial SyntaxError

Thu Jan 02, 2020 9:26 am

Bro, can you pst your code for image capture with encoding?
I am unable to connect the encoder

User avatar
DougieLawson
Posts: 37612
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: mmal tutorial SyntaxError

Fri Jan 03, 2020 1:27 am

Don't hold your breath waiting. The OP hasn't logged on to the forum since Sun 15 Apr 2018 @ 18:08.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

townkat
Posts: 7
Joined: Thu Mar 08, 2018 5:35 am

Re: mmal tutorial SyntaxError

Sat Mar 21, 2020 12:15 am

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.
mmaljpg.py

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
camera.outputs[1].commit()

splitter = picamera.mmalobj.MMALSplitter()
#splitter.inputs[0].connect(camera.outputs[1])

resizer = picamera.mmalobj.MMALResizer()
resizer.outputs[0].format = picamera.mmal.MMAL_ENCODING_RGB24
resizer.outputs[0].framesize = (320, 240)
#resizer.outputs[0].commit()
#resizer.inputs[0].connect(camera.outputs[1])

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
encoder.outputs[0].commit()
encoder.inputs[0].connect(camera.outputs[1])
encoder.inputs[0].enable()

nullsink = picamera.mmalobj.MMALNullSink()
nullsink.inputs[0].connect(camera.outputs[2])
nullsink.inputs[0].enable()
time.sleep(2)


def savetojpg(port, buf):
    fileo.write(buf.data)
    if buf.flags & picamera.mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END:
        finished.set()
        return True
    return False

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

start=timeit.default_timer()
encoder.outputs[0].enable(savetojpg)
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
encoder.outputs[0].disable()
end=timeit.default_timer()
print('capture time '+ str(end-start))

#fileo.seek(0)
with open('out.jpg', 'wb+') as file: 
    file.write(fileo.getvalue())
print(fileo.tell())

User avatar
DougieLawson
Posts: 37612
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: mmal tutorial SyntaxError

Sat Mar 21, 2020 10:02 am

Welcome back. You got a bunch of guys about to turn blue.


With apologies to Charlie Duke, Apollo 11 Capcom.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

townkat
Posts: 7
Joined: Thu Mar 08, 2018 5:35 am

Re: mmal tutorial SyntaxError

Sat Mar 21, 2020 2:04 pm

DougieLawson wrote: Welcome back. You got a bunch of guys about to turn blue.

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

Return to “Camera board”