Is it possible to record video while writing to the SPI bus? (OpenMV H7)

I am currently recording video with my OpenMV H7 and writing the video via SPI bus to another board. I was thinking I can speed up the process by performing SPI writes on each frame that I create for my mjpeg. Is this possible to do?

I wrote some code to get data to send over the SPI bus but text reads nothing:

    last_size = 0
    m = mjpeg.Mjpeg("example.mjpeg")
    for i in range(150):
        clock.tick()
        m.add_frame(sensor.snapshot())
        with open("example.mjpeg", "rb") as f:
            f.seek(last_size)
            text = f.read(m.size()-last_size)
            f.close()
            last_size = m.size()

    m.close(clock.fps())

The code here records video by adding frames to example.mjpeg and on each frame I attempt to open the file, read out the new frame data to text. My plan was to then send the frame data over SPI but to simplify the code here I did not include the SPI write part as I’m not able to read any data from example.mjpeg to send in the first place

Please put your code in code tags. I don’t understand what you posted.

Apologies, it looked more well formatted the way I had it up originally from my side

last_size = 0
m = mjpeg.Mjpeg(“example.mjpeg”)
for i in range(150):
  clock.tick()
  m.add_frame(sensor.snapshot())
  with open(“example.mjpeg”, “rb”) as f:
  f.seek(last_size)
  text = f.read(m.size()-last_size)
  f.close()
  last_size = m.size()
m.close(clock.fps())

You are opening a file you are currently writing too… that’s just not going to work in any way or form.

What do you want to do? Please explain clearly. Also, we have a firmware update coming which will massively boost the FPS on everything.

Yes that is correct. The crux of the issue is that it doesn’t work because I open the file to write with mjpeg.Mjpeg(“example.mjpeg”). This is an abstraction that prevents me from opening the same file to read from because I am forced to close the file with m.close(clock.fps()) first before I can begin reading. But if I were to close and reopen the mjpeg each time just so that I can do a file read on it then I would not be creating a single recorded video.

I would like to take sensor.snapshot() and write that directly over SPI but still keep all of the header info I presume is getting added via class mjpeg. Mjpeg ( filename [, width [, height ]]).

FPS is not the issue and I’m totally ok with taking a hit there to perform a SPI send on each frame. Is it possible to see the source code to the mjpeg class? I’m just looking to recreate the same mjpeg header (and tail? if it exists) and append the snapshots in the middle.

Current Issue:

  1. Open a file to write with mjpeg.Mjpeg(“example.mjpeg”)
  2. Append a new snapshot with m.add_frame(sensor.snapshot())
  3. At this point, attempt to start reading out from “example.mjpeg” but uh oh if I close the file with m.close(clock.fps()) I can start reading but there is no API listed in in the docs to reopen the file to keep writing more snapshots. The only thing I can do is overwrite my old mjpeg file

What I’d want to do:

  1. Open a file to write with the same mjpeg header used by mjpeg.Mjpeg(“example.mjpeg”)
  2. For each snapshot, write the snapshot data to example.mjpeg, close the file, reopen the file and read the contents to transfer over SPI. Then close it again to reopen for writing on the next snapshot.
  3. If there is one, append a tail indicating the end of the mjpeg after the final snapshot

I would then be able to tune the amount of data that I send via the SPI bus if the FPS hit is too great.

Hi, just use the .compress() method on the image. This produces a jpeg image. Write this via SPI and then save that to the MJPEG file.

Please do not try to open the MJPEG file while writing it.

img = sensor.snapshot().compress()

Then write the image to the mjpeg file and SPI bus. It will be a JPEG byte stream with the 0xFFD8 and 0xFFD9 markers.

Thanks for the prompt response Kwabena.

Apologies in advance for the following naive question but is consecutively appending jpeg images and then saving the file as an mjpeg all there is to the mjpeg format or do I need a specific mjpeg header followed by consecutive jpeg images?

Hi, this is what the MJPEG object does:

As you can see. Add frame literally just puts a JPEG image in the file with some padding. There’s really only one header for an MJPEG file. Otherwise each frame is literally a standard jpeg image.