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
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())
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 classmjpeg.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.
Open a file to write with mjpeg.Mjpeg(“example.mjpeg”)
Append a new snapshot with m.add_frame(sensor.snapshot())
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
Open a file to write with the same mjpeg header used by mjpeg.Mjpeg(“example.mjpeg”)
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.
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.
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?
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.