saving video on SD card

Discussion related to "under the hood" OpenMV topics.
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

saving video on SD card

Postby iScienceLuvr » Sat Apr 28, 2018 3:09 am

I am trying to save a video from the camera feed onto the SD card. Here is the code:

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
time = [0]
start = pyb.millis()
while(True):
    clock.tick()
    time.append(pyb.elapsed_millis(start))
    m.add_frame(sensor.snapshot())
    print(clock.fps())
    if pyb.elapsed_millis(start) > 100000:
        print('over')
        print(time)
        m.close(clock.fps())
        break
I am having two problems when using this code. First, I get a very low frame rate, about 10 FPS. Secondly, the program will only run for like 10 seconds and stop receiving data. It eventually prints out the error message: "Timeout error while getting board architecture!"

How can I resolve this problem?
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat Apr 28, 2018 1:55 pm

Hi:

1. In regards to the frame rate, please keep in mind the camera has to jpeg compress images twice to send to the disk and display on the IDE. So, if you click the disable frame buffer option your FPS will go up to about 20 FPS. If you want to save the camera effort then do: sensor.snapshot().compress() to jpeg compress the frame buffer just once.

2. "error message: "Timeout error while getting board architecture!"" -> That would be an IDE issue... which should not be possible for you to run the script because the IDE would have not enabled control of the camera yet.

...

I'm getting some weird issues here however related to the new firmware. I keep getting a disk I/O error. I think it's due to us upgrading the HAL to support the H7... man this stuff just keeps breaking... anyway, Ibrahim is debugging this.

...

Once other note - in your script you are endlessly appending time stamps to a list. You will run out of heap at some point and the system will fail.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sat Apr 28, 2018 4:11 pm

I am still a little confused on what you mean by it's an IDE error? What would I do to fix this error?
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat Apr 28, 2018 5:40 pm

Okay, I got this working with no errors with the latest firmware for the M7:

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
#time = [0]
start = pyb.millis()
while(True):
    clock.tick()
    #time.append(pyb.elapsed_millis(start))
    m.add_frame(sensor.snapshot())
    print(clock.fps())
    if pyb.elapsed_millis(start) > 100000:
        print('over')
        #print(time)
        m.close(clock.fps())
        break
Um, I haven't released the latest firmware with the IDE yet. But, It's available on our github right now. https://github.com/openmv/openmv/releases

Use the Tools->Run bootloader option and pass the camera the OPENMV3/firmware.bin file. You are allowed to run this option before connecting... so, don't hit that button.
Nyamekye,
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat Apr 28, 2018 5:41 pm

As for saving the time stamps, please do that in another file.

Note that you may see time stamps not printed in the terminal when you print them all in one go. This is because the USB serial text fifo only holds about 512 bytes and we don't block the camera when printing text, so, generally, text gets lost if you print too much at one time. The IDE polls the text buffer at 200 Hz so this is only an issue if you print out something very large.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sun Apr 29, 2018 6:03 pm

so is it impossible to get the timestamps? because if I do it in another file (i assume you mean another python program), then it won't be the timestamps for the same video...
Also, the program still did not work with the new firmware...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 6:09 pm

Hi, you can open two files at the same time. Just write the time stamps to the text file while also saving the video to a file. Then, close both at the same time.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sun Apr 29, 2018 6:52 pm

ok like using tradition open() function in python? also, the code still doesn't work... it sometimes abruptly stops in the middle, and openMV disconnects...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 8:12 pm

Yep, using the open method. Can you post your code?
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sun Apr 29, 2018 10:23 pm

Same code, but without the timestamps:

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
time = [0]
start = pyb.millis()
while(True):
    clock.tick()
    time.append(pyb.elapsed_millis(start))
    m.add_frame(sensor.snapshot())
    print(clock.fps())
    if pyb.elapsed_millis(start) > 10000:
        print('over')
        m.close(clock.fps())
        break
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 10:26 pm

Hi, did you update your OpenMV Cam firmware? Also, please try formatting the disk. I've got it running without errors for the full 100 seconds.

Also, what OS are you on?
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sun Apr 29, 2018 10:36 pm

Don't you mean 10 seconds?

Yes I am using the new firmware. I formatted and cleared out the SD card before using it with OpenMV but I don't think that's the problem because it's the camera that keeps disconnecting from the IDE. I don't think it's due to the cable because I have tried several already. I am using Windows 10.
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 10:46 pm

This works with the latest firmware:

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
start = pyb.millis()
with open("time_stamps.txt", "wb") as f:
    while(True):
        clock.tick()
        f.write("%d\n" % pyb.elapsed_millis(start))
        m.add_frame(sensor.snapshot())
        print(clock.fps())
        if pyb.elapsed_millis(start) > 10000:
            print('over')
            #print(time)
            m.close(clock.fps())
            break
There seems to be a problem with using a value of 100000 however. After about 20 seconds I get an EIO 5 error from the break line which doesn't make sense.
Nyamekye,
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 10:52 pm

Mmm, I can get it to run for 100 seconds on the H7 which records at about 27 FPS. On the M7 it only runs about 24 seconds before I get an EIO 5 error. I've asked Ibrahim to take a look.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sun Apr 29, 2018 11:04 pm

As a matter of fact, even the example programs "mjpeg.py" and "gif.py" has the same problem... So there might be some other problem...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sun Apr 29, 2018 11:22 pm

Hi, please verify that you've updated the firmware and reformated the SD card.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Mon Apr 30, 2018 12:00 am

I did update the firmware (it says 2.8.0). I reformatted and it ran fine, then I tried to run again and the problem happened again. I reformatted again, and then the problem still happened. So yeah, the problem's still there.
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Mon Apr 30, 2018 9:17 pm

Is there a solution to this problem?
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Mon Apr 30, 2018 10:12 pm

Hi, I'm fairly certain there's a bug in this now with the M7. It seems to be fine on the H7. But, not the M7. I don't know the plumbing of the HALs as well as Ibrahim however, so, I have to get him to fix it.

...

Sorry about this. We had all this stuff rather dialed in and bug free we we completed these features. But, trying to stay up to date with MicroPython has been a challenge. Every time we update our MP version stuff like this breaks. I've been able to keep the upper level vision code quite stable given this... but, lower level hardware drivers are a pain.

We'll issue you a patch firmware soon.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Mon Apr 30, 2018 10:35 pm

No worries... Please let me know when the updated firmware is released.
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Wed May 02, 2018 3:21 am

Hi, so, I've tried with the M7 with v2.7.0 and v2.8.0 and gotten no errors with this:

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
start = pyb.millis()
with open("time_stamps.txt", "wb") as f:
   while(True):
       clock.tick()
       f.write("%d\n" % pyb.elapsed_millis(start))
       m.add_frame(sensor.snapshot())
       print(clock.fps())
       if pyb.elapsed_millis(start) > 100000:
           print('over')
           #print(time)
           m.close(clock.fps())
           break
It's the same code as before. However, I was able to reproduce the issue... when the SD card was not mounted straight. Please check that your OpenMV Cam's SD card is fully inserted and that the pads are coming off of the SD card socket. I have a cam that's got a damaged SD card socket and I was getting all kinds of errors.
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Wed May 02, 2018 7:14 pm

Hmm.. I made sure that the SD card was firmly in place, but I still have the same problem. I now also get an EIO5 error after I reconnect the camera... I will try using maybe on older firmware?
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Wed May 02, 2018 7:29 pm

Using older firmware didn't work. Anyway I have noticed it seems like it disconnects at about 5 seconds...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Wed May 02, 2018 10:58 pm

What OS are you using? OpenMV IDE could be unstable on it. Mac?
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Thu May 03, 2018 1:12 am

As I mentioned before, I am using Windows 10.
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Thu May 03, 2018 2:09 am

Sorry, I'll have the latest release out by Sunday. There are a few IDE protocol fixes in it that may help.
Nyamekye,
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Fri May 04, 2018 12:30 am

Ibrahim is digging deeper into this problem. He doesn't quite know what it is yet. But, it will get solved. There's definitely something funny going on with using the "with open" construct related to MicroPython - we've noticed that if you stop the cam using that call it crashes. If you try to open and close a file in the regular way things might improve.
Nyamekye,
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Fri May 04, 2018 11:08 am

@iScienceLuvr

Hi, can you please test the attached firmware ?
Attachments
firmware.zip
(1.06 MiB) Downloaded 651 times
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Fri May 04, 2018 7:30 pm

nope, still not working...
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Fri May 04, 2018 8:35 pm

I'm pretty sure this fixes it, hmm can you try uploading the fw again ? Note browse for the patched firmware don't copy it to the IDE's directories.
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Fri May 04, 2018 8:56 pm

no I copied and pasted the file location into the box and pressed run to upload the firmware. I tried it again as you requested and it still doesn't work.
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat May 05, 2018 3:12 pm

Okay, Ibrahim's still working on this.
Nyamekye,
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Sat May 05, 2018 3:34 pm

Hi, I'm attaching a new image, can you give it a try and let me know if it fixes the issue ? Note I changed the fw version to 2.9.1 to avoid any confusion. Also I'd format the SD first.
Attachments
firmware.zip
(1.06 MiB) Downloaded 663 times
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sat May 05, 2018 4:38 pm

ok it looks like it works for like 10 sec but more than that it won't work...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat May 05, 2018 4:58 pm

This firmware works for me... I don't get any bugs with it and I can record for 100 seconds.

What error are you getting?
Nyamekye,
iScienceLuvr
Posts: 25
Joined: Thu Apr 26, 2018 6:55 pm

Re: saving video on SD card

Postby iScienceLuvr » Sat May 05, 2018 8:40 pm

it just stops and camera disconnects, like previously...
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Sat May 05, 2018 10:15 pm

Can you try this out:

It sounds like you may just have an IDE timeout issue and it's disconnecting because the camera didn't respond to a command in time. If the light keeps flashing then the camera is working fine and it's just the IDE disconnecting.

Code: Select all

import sensor, image, time, pyb, mjpeg

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
led0 = pyb.LED(1)
led1 = pyb.LED(2)

m = mjpeg.Mjpeg('openmv_video.mjpeg')

clock = time.clock()
start = pyb.millis()
led0.on()
with open("time_stamps.txt", "wb") as f:
   while(True):
       clock.tick()
       led1.toggle()
       f.write("%d\n" % pyb.elapsed_millis(start))
       m.add_frame(sensor.snapshot())
       print(clock.fps())
       if pyb.elapsed_millis(start) > 100000:
           print('over')
           #print(time)
           m.close(clock.fps())
           break
led0.off()
Nyamekye,
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Sun May 06, 2018 5:15 pm

Hi again, I found two more bugs in SD and fixed them. This image should be good.

Attached.
Attachments
firmware.zip
(1.06 MiB) Downloaded 684 times
askerker
Posts: 5
Joined: Tue Jun 12, 2018 10:12 am

Re: saving video on SD card

Postby askerker » Thu Jun 14, 2018 9:54 am

Hi,

I am using the code in this discussion but am unable to get images or videos (jpg or mjpeg) to save. The file name will only show up on my computer once I have disconnected the device and reconnected it but will have zero bytes in it.

Any help would be greatly appreciated!

I am using a mac with the M7.
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Sun Jun 24, 2018 8:32 am

askerker wrote:
Thu Jun 14, 2018 9:54 am
Hi,

I am using the code in this discussion but am unable to get images or videos (jpg or mjpeg) to save. The file name will only show up on my computer once I have disconnected the device and reconnected it but will have zero bytes in it.

Any help would be greatly appreciated!

I am using a mac with the M7.
Are you still having this problem ? You should use the IDE to reset the camera instead of disconnecting it to give it a chance to unmount the filesystem.
User avatar
jiapei100
Posts: 36
Joined: Fri Oct 14, 2016 7:07 pm
Contact:

Re: saving video on SD card

Postby jiapei100 » Sat Jul 13, 2019 7:50 pm

  • The total capacity on OpenMV is less than 100K, which is NOT enough to save ANY video, an SD card is a must for video recording (BTW, what is the MAXIMUM SD card capacity that OpenMV is able to support? )
  • Is there a way or the need to specify SD card location? For instance, we ALL know that

    Code: Select all

    log = open('.log.csv', 'w') 
    will write a log file log.csv just under the same folder as main.py. Is there a folder specifically for SD card?
  • How to view the contents inside the SD card when the SD card is plugged in OpenMV board? I now have my SD card formatted into EXT4. Which file format is OpenMV able to load/see on the SD card?


Thank you very much
Pei
Welcome to Longer Vision
http://www.longervision.ca
User avatar
jiapei100
Posts: 36
Joined: Fri Oct 14, 2016 7:07 pm
Contact:

Re: saving video on SD card

Postby jiapei100 » Sat Jul 13, 2019 8:41 pm

jiapei100 wrote:
Sat Jul 13, 2019 7:50 pm
  • The total capacity on OpenMV is less than 100K, which is NOT enough to save ANY video, an SD card is a must for video recording (BTW, what is the MAXIMUM SD card capacity that OpenMV is able to support? )
  • Is there a way or the need to specify SD card location? For instance, we ALL know that

    Code: Select all

    log = open('.log.csv', 'w') 
    will write a log file log.csv just under the same folder as main.py. Is there a folder specifically for SD card?
  • How to view the contents inside the SD card when the SD card is plugged in OpenMV board? I now have my SD card formatted into EXT4. Which file format is OpenMV able to load/see on the SD card?


Thank you very much
Pei
It looks I've found something here https://elmwoodelectronics.ca/blogs/new ... first-look , cited in the following:
  • OpenMV only works with FAT.
  • Max readable SDHC card value is 32G
  • If the camera saves a picture to the SD Card while mounted on a pc... the PC won't see the photo without remounting the drive as it assumes USB flash drives can't magically create files on their own (which is usually true 99% of the time).


What's more (cite):

3 ways OpenMV will mount as a USB device :
  • Only internal on board memory (no SD Card) : 2 Megs
    Will appear as a standard USB drive.
    Contents will be 'Main.py'
  • With MicroSD card inserted on board :
    To capture any images or video you have to insert a microSD card.
    You won't get a warning until the runtime file tries to save a file.
    This will now supplant the internal memory and be the default usb drive you see.
    When you plug in the OpenMV, you will ***no longer see the internal 2 Meg memory*** mounted to the desktop.
    Only the Camera SD Card will now mount to desktop.
However, according to my test, both M4 and M7 does NOT come with 2M on-board memory, but much less than 100K.
https://raw.githubusercontent.com/Longe ... erties.jpg
https://raw.githubusercontent.com/Longe ... erties.jpg

Finally, how to show up the SD card's contents when the SD card is plugged in OpenMV board, which is USB connected to the laptop?

Thanks
Welcome to Longer Vision
http://www.longervision.ca
User avatar
iabdalkader
Posts: 952
Joined: Sun May 24, 2015 3:53 pm

Re: saving video on SD card

Postby iabdalkader » Sun Jul 14, 2019 6:03 pm

The 2MBs cited is the whole flash size, a large part of the flash is used for the firmware image. The internal/flash file system is around 100KBs. I don't understand your second question, just connect the SD card and connect the camera via USB and the SD card will be mounted.
User avatar
jiapei100
Posts: 36
Joined: Fri Oct 14, 2016 7:07 pm
Contact:

Re: saving video on SD card

Postby jiapei100 » Sun Jul 14, 2019 9:31 pm

iabdalkader wrote:
Sun Jul 14, 2019 6:03 pm
The 2MBs cited is the whole flash size, a large part of the flash is used for the firmware image. The internal/flash file system is around 100KBs. I don't understand your second question, just connect the SD card and connect the camera via USB and the SD card will be mounted.
Thank you iabdalkader...
Yeah, it's quite weird that when I plugged in my SD card, nothing shows up...
  • Should I format the SD card in advance to some particular file format?
    Currently, my 32 HC Micro SD card comes with ext4 ...
  • Otherwise, should I flash something onto the SD card before inserting into OpenMV? I think it should be OK for me to just leave the SD card empty, right?
Cheers
Pei
Welcome to Longer Vision
http://www.longervision.ca
User avatar
kwagyeman
Posts: 3338
Joined: Sun May 24, 2015 2:10 pm

Re: saving video on SD card

Postby kwagyeman » Mon Jul 15, 2019 12:13 pm

We support FAT and exFAT. Most microcontrollers only support these formats.

As for files not showing up on the PC when recorded... there's no way to fix this. Modern OSes don't mount the USB flash drive like a network disk, they think they have full control and mount the disk on as a block device. It's very easy to loss data if the PC and the OpenMV Cam are accessing the flash drive at the same time when this is the case.

Anyway, unless we can change the driver code that scans USB drives on Mac/Linux/Windows you just have to reconnect the device again for the PC to rescan it.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests